ConfigurationResourceValidator { public void validateConfigurationResource(Buffer configurationResource, String resourceSchema, Handler<AsyncResult<ValidationResult>> resultHandler){ vertx.executeBlocking(future -> { if(!JsonUtil.isValidJson(configurationResource)){ String message = "Unable to parse json"; log.warn(message); future.complete(new ValidationResult(ValidationStatus.VALIDATED_NEGATIV, message)); return; } if(StringUtils.isEmpty(resourceSchema)){ log.info("validated positive since no schema was provided"); future.complete(new ValidationResult(ValidationStatus.VALIDATED_POSITIV)); return; } JsonObject schemaObject; try { schemaObject = new JsonObject(resourceSchema); } catch(DecodeException ex) { String message = "Unable to parse json schema"; log.warn(message); future.complete(new ValidationResult(ValidationStatus.VALIDATED_NEGATIV, message)); return; } if(SCHEMA_DECLARATION.equals(schemaObject.getString("$schema"))) { JsonSchema schema; try { schema = JsonSchemaFactory.getInstance().getSchema(resourceSchema); } catch (Exception e) { String message = "Cannot load schema"; log.warn(message, e); future.complete(new ValidationResult(ValidationStatus.VALIDATED_NEGATIV, message)); return; } try { JsonNode jsonNode = new ObjectMapper().readTree(configurationResource.toString()); final Set<ValidationMessage> valMsgs = schema.validate(jsonNode); if(valMsgs.isEmpty()) { log.info("validated positive"); future.complete(new ValidationResult(ValidationStatus.VALIDATED_POSITIV)); } else { JsonArray validationDetails = extractMessagesAsJson(valMsgs); future.complete(new ValidationResult(ValidationStatus.VALIDATED_NEGATIV, "Validation failed", validationDetails)); } } catch (IOException e) { String message = "Cannot read JSON"; log.warn(message, e.getMessage()); future.complete(new ValidationResult(ValidationStatus.VALIDATED_NEGATIV, message)); } } else { String message = "Invalid schema: Expected property '$schema' with content '"+SCHEMA_DECLARATION+"'"; log.warn(message); future.complete(new ValidationResult(ValidationStatus.VALIDATED_NEGATIV, message)); } }, resultHandler); } ConfigurationResourceValidator(Vertx vertx); void validateConfigurationResource(Buffer configurationResource, String resourceSchema,
                                              Handler<AsyncResult<ValidationResult>> resultHandler);  }
@Test public void testValidateInvalidJson(TestContext context) { Async async = context.async(); configurationResourceValidator = new ConfigurationResourceValidator(vertx); configurationResourceValidator.validateConfigurationResource(Buffer.buffer(INVALID_JSON_CONTENT), PERSON_SCHEMA, event -> { context.assertTrue(event.succeeded()); context.assertEquals(ValidationStatus.VALIDATED_NEGATIV, event.result().getValidationStatus()); String expectedMessage = "Unable to parse json"; context.assertEquals(expectedMessage, event.result().getMessage()); async.complete(); }); }
@Test public void testValidateValidJsonWithNoSchemaProvided(TestContext context) { Async async = context.async(); configurationResourceValidator = new ConfigurationResourceValidator(vertx); configurationResourceValidator.validateConfigurationResource(Buffer.buffer(CONTENT_MATCHING_PERSON_SCHEMA), null, event -> { context.assertTrue(event.succeeded()); context.assertEquals(ValidationStatus.VALIDATED_POSITIV, event.result().getValidationStatus()); async.complete(); }); }
@Test public void testValidateValidJsonWithEmptySchemaProvided(TestContext context) { Async async = context.async(); configurationResourceValidator = new ConfigurationResourceValidator(vertx); configurationResourceValidator.validateConfigurationResource(Buffer.buffer(CONTENT_MATCHING_PERSON_SCHEMA), "", event -> { context.assertTrue(event.succeeded()); context.assertEquals(ValidationStatus.VALIDATED_POSITIV, event.result().getValidationStatus()); async.complete(); }); }
@Test public void testValidateValidJsonWithInvalidSchemaProvided(TestContext context) { Async async = context.async(); configurationResourceValidator = new ConfigurationResourceValidator(vertx); configurationResourceValidator.validateConfigurationResource(Buffer.buffer(CONTENT_MATCHING_PERSON_SCHEMA), INVALID_SCHEMA, event -> { context.assertTrue(event.succeeded()); context.assertEquals(ValidationStatus.VALIDATED_NEGATIV, event.result().getValidationStatus()); String expectedMessage = "Unable to parse json schema"; context.assertEquals(expectedMessage, event.result().getMessage()); async.complete(); }); }
@Test public void testValidateValidJsonWithInvalidSchemaMissingDeclarationProvided(TestContext context) { Async async = context.async(); configurationResourceValidator = new ConfigurationResourceValidator(vertx); configurationResourceValidator.validateConfigurationResource(Buffer.buffer(CONTENT_MATCHING_PERSON_SCHEMA), INVALID_SCHEMA_MISSING_DECLARATION, event -> { context.assertTrue(event.succeeded()); context.assertEquals(ValidationStatus.VALIDATED_NEGATIV, event.result().getValidationStatus()); String expectedMessage = "Invalid schema: Expected property '$schema'"; context.assertTrue(event.result().getMessage().startsWith(expectedMessage)); async.complete(); }); }
@Test public void testValidateValidJsonWithContentNotMatchingSchema(TestContext context) { Async async = context.async(); configurationResourceValidator = new ConfigurationResourceValidator(vertx); configurationResourceValidator.validateConfigurationResource(Buffer.buffer(CONTENT_NOT_MATCHING_PERSON_SCHEMA), PERSON_SCHEMA, event -> { context.assertTrue(event.succeeded()); context.assertEquals(ValidationStatus.VALIDATED_NEGATIV, event.result().getValidationStatus()); String expectedMessage = "Validation failed"; context.assertEquals(expectedMessage, event.result().getMessage()); context.assertNotNull(event.result().getValidationDetails()); JsonArray validationDetails = event.result().getValidationDetails(); context.assertEquals("$.lastName: is missing but it is required", validationDetails.getJsonObject(0).getString("message")); async.complete(); }); }
@Test public void testValidateValidJsonWithContentMatchingSchema(TestContext context) { Async async = context.async(); configurationResourceValidator = new ConfigurationResourceValidator(vertx); configurationResourceValidator.validateConfigurationResource(Buffer.buffer(CONTENT_MATCHING_PERSON_SCHEMA), PERSON_SCHEMA, event -> { context.assertTrue(event.succeeded()); context.assertEquals(ValidationStatus.VALIDATED_POSITIV, event.result().getValidationStatus()); async.complete(); }); }