OpenAPISchemaValidator { public static boolean isJsonValid(String schemaText, String jsonText) throws IOException { List<String> errors = validateJson(schemaText, jsonText); if (!errors.isEmpty()) { log.debug("Get validation errors, returning false"); return false; } return true; }  static boolean isJsonValid(String schemaText, String jsonText); static List<String> validateJson(String schemaText, String jsonText); static List<String> validateJson(JsonNode schemaNode, JsonNode jsonNode); static List<String> validateJsonMessage(JsonNode specificationNode, JsonNode jsonNode, String messagePathPointer, String contentType); static JsonNode getJsonNode(String jsonText); static JsonNode getJsonNodeForSchema(String schemaText);  }
@Test public void testValidateJsonSuccess() { boolean valid = false; String schemaText = null; String jsonText = "{\"name\": \"307\", \"model\": \"Peugeot 307\", \"year\": 2003}"; try { schemaText = FileUtils.readFileToString( new File("target/test-classes/io/github/microcks/util/openapi/car-schema.json")); valid = OpenAPISchemaValidator.isJsonValid(schemaText, jsonText); } catch (Exception e) { fail("Exception should not be thrown"); } assertTrue(valid); }
@Test public void testValidateJsonSuccessWithNull() { boolean valid = false; String schemaText = null; String jsonText = "{\"name\": \"307\", \"model\": null, \"year\": 2003}"; try { schemaText = FileUtils.readFileToString( new File("target/test-classes/io/github/microcks/util/openapi/car-schema.json")); valid = OpenAPISchemaValidator.isJsonValid(schemaText, jsonText); } catch (Exception e) { fail("Exception should not be thrown"); } assertTrue(valid); }
@Test public void testValidateJsonSchemaWithReferenceSuccess() { boolean valid = true; String schemaText = null; String jsonText = "[{\"name\": \"307\", \"model\": \"Peugeot 307\", \"year\": 2003}," + "{\"name\": \"jean-pierre\", \"model\": \"Peugeot Traveler\", \"year\": 2017}]"; try { schemaText = FileUtils.readFileToString( new File("target/test-classes/io/github/microcks/util/openapi/cars-schema.json")); valid = OpenAPISchemaValidator.isJsonValid(schemaText, jsonText); } catch (Exception e) { e.printStackTrace(); fail("Exception should not be thrown"); } assertTrue(valid); }