|
26 | 26 | @Slf4j |
27 | 27 | public class YamlDocCheckModel { |
28 | 28 |
|
29 | | - public static final String ARG_CHECK_TYPE = "check-type"; |
30 | | - |
31 | | - public static final String ARG_CHECK_SCHEMA = "check-schema"; |
32 | | - |
33 | | - public static final String ARG_PRINT_ONLY_KO = "print-only-ko"; |
34 | | - |
35 | | - public static final String ARG_CHECK_ONCE = "check-once"; |
36 | | - |
37 | | - private YamlDocCheckModel() {} |
38 | | - |
39 | | - @SuppressWarnings("unchecked") |
40 | | - private static void checkCurrent( CheckContext context, String path, Class<?> typeModel, Map<String, Object> currentPropertyMap ) { |
41 | | - String childSchema = null; |
42 | | - if ( !BooleanUtils.isTrue( context.getParams().getProperty( ARG_PRINT_ONLY_KO ) ) ) { |
43 | | - context.getReport().print( path ); |
44 | | - context.getReport().println( " ok!" ); |
45 | | - } |
46 | | - if ( !currentPropertyMap.containsKey( "type" ) ) { |
47 | | - if ( !currentPropertyMap.containsKey( "properties" ) ) { |
48 | | - Object ref = currentPropertyMap.get( "$ref" ); |
49 | | - if ( ref != null ) { |
50 | | - childSchema = ((String)ref).substring( "#/components/schemas/".length() ); |
51 | | - currentPropertyMap = (Map<String, Object>)context.getSchemas().get( childSchema ); |
52 | | - } else { |
53 | | - log.info( "cannot find : {}", currentPropertyMap ); |
54 | | - } |
55 | | - } |
56 | | - check( context, path, typeModel, childSchema, currentPropertyMap ); |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - @SuppressWarnings("unchecked") |
61 | | - private static void check( CheckContext context, String path, Class<?> typeModel, String schemaName, Map<String, Object> schemaModel ) { |
62 | | - log.debug( "path {}, class : {}, schema : {}", path, typeModel, schemaModel ); |
63 | | - Map<String, Object> properties = (Map<String, Object>)schemaModel.get( "properties" ); |
64 | | - boolean doCheck = true; |
65 | | - if ( BooleanUtils.isTrue( context.getParams().getProperty( ARG_CHECK_ONCE ) ) ) { |
66 | | - log.trace( "check? : {}, current checked : {}", schemaName, context.getChecked() ); |
67 | | - doCheck = !context.getChecked().contains( schemaName ); |
68 | | - context.getChecked().add( schemaName ); |
69 | | - } |
70 | | - if ( properties != null && doCheck ) { |
71 | | - for ( Map.Entry<String, Object> entry : properties.entrySet() ) { |
72 | | - String key = entry.getKey(); |
73 | | - Object value = entry.getValue(); |
74 | | - String newPath = path+"."+key; |
75 | | - log.debug( "key : {} -> {} ({})", key, value, value.getClass().getName() ); |
76 | | - try { |
77 | | - String getterName = MethodHelper.getGetterNameForProperty( key ); |
78 | | - log.debug( "try {} on {}", getterName, typeModel ); |
79 | | - Method getter = typeModel.getMethod( getterName , MethodHelper.NO_PARAM_TYPES ); |
80 | | - checkCurrent(context, newPath, getter.getReturnType(), (Map<String, Object>)value ); |
81 | | - } catch (Exception e) { |
82 | | - context.increaseKoCount(); |
83 | | - log.info( "no getter for '{}' -> '{}'", path, newPath ); |
84 | | - context.getReport().print( newPath ); |
85 | | - context.getReport().print( " not found in implementation!!!!! " ); |
86 | | - context.getReport().print( typeModel.getName() ); |
87 | | - context.getReport().print( " - " ); |
88 | | - context.getReport().println( schemaName ); |
89 | | - } |
90 | | - } |
91 | | - } |
92 | | - } |
93 | | - |
94 | | - private static final String MISSING_REQUIRED = "Missing required parameters : "; |
95 | | - |
96 | | - @SuppressWarnings("unchecked") |
97 | | - public static int handleModelCheck( Properties props ) throws ConfigException { |
98 | | - SimpleValue<Integer> res = new SimpleValue<>( Result.RESULT_CODE_OK ); |
99 | | - String inputYaml = props.getProperty( YamlDocMain.ARG_INPUT_YAML ); |
100 | | - String checkType = props.getProperty( ARG_CHECK_TYPE ); |
101 | | - String checkSchema = props.getProperty( ARG_CHECK_SCHEMA ); |
102 | | - String outputFile = props.getProperty( YamlDocMain.ARG_OUTPUT_FILE ); |
103 | | - if ( StringUtils.isEmpty( inputYaml ) ) { |
104 | | - throw new ConfigException( MISSING_REQUIRED+YamlDocMain.ARG_INPUT_YAML ); |
105 | | - } |
106 | | - if ( StringUtils.isEmpty( checkType ) ) { |
107 | | - throw new ConfigException( MISSING_REQUIRED+ARG_CHECK_TYPE ); |
108 | | - } |
109 | | - if ( StringUtils.isEmpty( checkSchema ) ) { |
110 | | - throw new ConfigException( MISSING_REQUIRED+ARG_CHECK_SCHEMA ); |
111 | | - } |
112 | | - ConfigException.apply( () -> { |
113 | | - try ( Reader reader = new FileReader( new File( inputYaml ) ); |
114 | | - StringWriter buffer = new StringWriter(); |
115 | | - PrintWriter report = new PrintWriter( buffer ) ) { |
116 | | - report.println( "# comparison " ); |
117 | | - report.println(); |
118 | | - report.print( "*schema* : " ); |
119 | | - report.println( checkSchema ); |
120 | | - report.println(); |
121 | | - report.print( "*type* : " ); |
122 | | - report.println( checkType ); |
123 | | - report.println(); |
124 | | - Yaml yaml = new Yaml(); |
125 | | - Map<String, Object> fullYaml = yaml.load( reader ); |
126 | | - Map<String, Object> components = (Map<String, Object>)fullYaml.get( "components" ); |
127 | | - Map<String, Object> schemas = (Map<String, Object>)components.get( "schemas" ); |
128 | | - Map<String, Object> schemaModel = (Map<String, Object>)schemas.get( checkSchema ); |
129 | | - if ( schemaModel == null ) { |
130 | | - throw new ConfigException( "schema not found : "+checkSchema ); |
131 | | - } |
132 | | - Class<?> typeModel = null; |
133 | | - try { |
134 | | - typeModel = Class.forName( checkType ); |
135 | | - } catch (Exception e) { |
136 | | - throw new ConfigException( "Failed to create type to check : "+checkType ); |
137 | | - } |
138 | | - CheckContext context = new CheckContext(props, schemas, report, new LinkedHashSet<>()); |
139 | | - check( context, "root", typeModel, checkSchema, schemaModel ); |
140 | | - res.setValue( context.getCountKo() ); |
141 | | - log.info( "report {} -> \n{}", res.getValue(), buffer.toString() ); |
142 | | - if ( StringUtils.isNotEmpty( outputFile ) ) { |
143 | | - FileIO.writeString( buffer.toString() , outputFile ); |
144 | | - } |
145 | | - } |
146 | | - } ); |
147 | | - return res.getValue(); |
148 | | - } |
149 | | - |
| 29 | + public static final String ARG_CHECK_TYPE = "check-type"; |
| 30 | + |
| 31 | + public static final String ARG_CHECK_SCHEMA = "check-schema"; |
| 32 | + |
| 33 | + public static final String ARG_PRINT_ONLY_KO = "print-only-ko"; |
| 34 | + |
| 35 | + public static final String ARG_CHECK_ONCE = "check-once"; |
| 36 | + |
| 37 | + private YamlDocCheckModel() { |
| 38 | + } |
| 39 | + |
| 40 | + @SuppressWarnings("unchecked") |
| 41 | + private static void checkCurrent(CheckContext context, String path, Class<?> typeModel, |
| 42 | + Map<String, Object> currentPropertyMap) { |
| 43 | + String childSchema = null; |
| 44 | + if (!BooleanUtils.isTrue(context.getParams().getProperty(ARG_PRINT_ONLY_KO))) { |
| 45 | + context.getReport().print(path); |
| 46 | + context.getReport().println(" ok!"); |
| 47 | + } |
| 48 | + if (!currentPropertyMap.containsKey("type")) { |
| 49 | + if (!currentPropertyMap.containsKey("properties")) { |
| 50 | + Object ref = currentPropertyMap.get("$ref"); |
| 51 | + if (ref != null) { |
| 52 | + childSchema = ((String) ref).substring("#/components/schemas/".length()); |
| 53 | + currentPropertyMap = (Map<String, Object>) context.getSchemas().get(childSchema); |
| 54 | + } else { |
| 55 | + log.info("cannot find : {}", currentPropertyMap); |
| 56 | + } |
| 57 | + } |
| 58 | + check(context, path, typeModel, childSchema, currentPropertyMap); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + private static void check(CheckContext context, String path, Class<?> typeModel, String schemaName, |
| 64 | + Map<String, Object> schemaModel) { |
| 65 | + log.debug("path {}, class : {}, schema : {}", path, typeModel, schemaModel); |
| 66 | + Map<String, Object> properties = (Map<String, Object>) schemaModel.get("properties"); |
| 67 | + boolean doCheck = true; |
| 68 | + if (BooleanUtils.isTrue(context.getParams().getProperty(ARG_CHECK_ONCE))) { |
| 69 | + log.trace("check? : {}, current checked : {}", schemaName, context.getChecked()); |
| 70 | + doCheck = !context.getChecked().contains(schemaName); |
| 71 | + context.getChecked().add(schemaName); |
| 72 | + } |
| 73 | + if (properties != null && doCheck) { |
| 74 | + for (Map.Entry<String, Object> entry : properties.entrySet()) { |
| 75 | + String key = entry.getKey(); |
| 76 | + Object value = entry.getValue(); |
| 77 | + String newPath = path + "." + key; |
| 78 | + log.debug("key : {} -> {} ({})", key, value, value.getClass().getName()); |
| 79 | + try { |
| 80 | + String getterName = MethodHelper.getGetterNameForProperty(key); |
| 81 | + log.debug("try {} on {}", getterName, typeModel); |
| 82 | + Method getter = typeModel.getMethod(getterName, MethodHelper.NO_PARAM_TYPES); |
| 83 | + checkCurrent(context, newPath, getter.getReturnType(), (Map<String, Object>) value); |
| 84 | + } catch (Exception e) { |
| 85 | + context.increaseKoCount(); |
| 86 | + log.info("no getter for '{}' -> '{}'", path, newPath); |
| 87 | + context.getReport().print(newPath); |
| 88 | + context.getReport().print(" not found in implementation!!!!! "); |
| 89 | + context.getReport().print(typeModel.getName()); |
| 90 | + context.getReport().print(" - "); |
| 91 | + context.getReport().println(schemaName); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static final String MISSING_REQUIRED = "Missing required parameters : "; |
| 98 | + |
| 99 | + @SuppressWarnings("unchecked") |
| 100 | + public static int handleModelCheck(Properties props) throws ConfigException { |
| 101 | + SimpleValue<Integer> res = new SimpleValue<>(Result.RESULT_CODE_OK); |
| 102 | + String inputYaml = props.getProperty(YamlDocMain.ARG_INPUT_YAML); |
| 103 | + String checkType = props.getProperty(ARG_CHECK_TYPE); |
| 104 | + String checkSchema = props.getProperty(ARG_CHECK_SCHEMA); |
| 105 | + String outputFile = props.getProperty(YamlDocMain.ARG_OUTPUT_FILE); |
| 106 | + if (StringUtils.isEmpty(inputYaml)) { |
| 107 | + throw new ConfigException(MISSING_REQUIRED + YamlDocMain.ARG_INPUT_YAML); |
| 108 | + } |
| 109 | + if (StringUtils.isEmpty(checkType)) { |
| 110 | + throw new ConfigException(MISSING_REQUIRED + ARG_CHECK_TYPE); |
| 111 | + } |
| 112 | + if (StringUtils.isEmpty(checkSchema)) { |
| 113 | + throw new ConfigException(MISSING_REQUIRED + ARG_CHECK_SCHEMA); |
| 114 | + } |
| 115 | + ConfigException.apply(() -> { |
| 116 | + try (Reader reader = new FileReader(new File(inputYaml)); |
| 117 | + StringWriter buffer = new StringWriter(); |
| 118 | + PrintWriter report = new PrintWriter(buffer)) { |
| 119 | + report.println("# comparison "); |
| 120 | + report.println(); |
| 121 | + report.print("*schema* : "); |
| 122 | + report.println(checkSchema); |
| 123 | + report.println(); |
| 124 | + report.print("*type* : "); |
| 125 | + report.println(checkType); |
| 126 | + report.println(); |
| 127 | + Yaml yaml = new Yaml(); |
| 128 | + Map<String, Object> fullYaml = yaml.load(reader); |
| 129 | + Map<String, Object> components = (Map<String, Object>) fullYaml.get("components"); |
| 130 | + Map<String, Object> schemas = (Map<String, Object>) components.get("schemas"); |
| 131 | + Map<String, Object> schemaModel = (Map<String, Object>) schemas.get(checkSchema); |
| 132 | + if (schemaModel == null) { |
| 133 | + throw new ConfigException("schema not found : " + checkSchema); |
| 134 | + } |
| 135 | + Class<?> typeModel = null; |
| 136 | + try { |
| 137 | + typeModel = Class.forName(checkType); |
| 138 | + } catch (Exception e) { |
| 139 | + throw new ConfigException("Failed to create type to check : " + checkType); |
| 140 | + } |
| 141 | + CheckContext context = new CheckContext(props, schemas, report, new LinkedHashSet<>()); |
| 142 | + check(context, "root", typeModel, checkSchema, schemaModel); |
| 143 | + res.setValue(context.getCountKo()); |
| 144 | + log.info("report {} -> \n{}", res.getValue(), buffer.toString()); |
| 145 | + if (StringUtils.isNotEmpty(outputFile)) { |
| 146 | + FileIO.writeString(buffer.toString(), outputFile); |
| 147 | + } |
| 148 | + } |
| 149 | + }); |
| 150 | + return res.getValue(); |
| 151 | + } |
| 152 | + |
150 | 153 | } |
151 | 154 |
|
152 | 155 | class CheckContext { |
153 | | - @Getter private Properties params; |
154 | | - @Getter private Map<String, Object> schemas; |
155 | | - @Getter private PrintWriter report; |
156 | | - @Getter private Set<String> checked; |
157 | | - |
158 | | - public CheckContext(Properties params, Map<String, Object> schemas, PrintWriter report, |
159 | | - Set<String> checked) { |
160 | | - super(); |
161 | | - this.params = params; |
162 | | - this.schemas = schemas; |
163 | | - this.report = report; |
164 | | - this.checked = checked; |
165 | | - this.countKo = 0; |
166 | | - } |
167 | | - @Getter int countKo; |
168 | | - public void increaseKoCount() { |
169 | | - this.countKo++; |
170 | | - } |
| 156 | + @Getter |
| 157 | + private Properties params; |
| 158 | + @Getter |
| 159 | + private Map<String, Object> schemas; |
| 160 | + @Getter |
| 161 | + private PrintWriter report; |
| 162 | + @Getter |
| 163 | + private Set<String> checked; |
| 164 | + |
| 165 | + public CheckContext(Properties params, Map<String, Object> schemas, PrintWriter report, |
| 166 | + Set<String> checked) { |
| 167 | + super(); |
| 168 | + this.params = params; |
| 169 | + this.schemas = schemas; |
| 170 | + this.report = report; |
| 171 | + this.checked = checked; |
| 172 | + this.countKo = 0; |
| 173 | + } |
| 174 | + |
| 175 | + @Getter |
| 176 | + int countKo; |
| 177 | + |
| 178 | + public void increaseKoCount() { |
| 179 | + this.countKo++; |
| 180 | + } |
171 | 181 | } |
0 commit comments