JsonSchemaValidation { public ProcessingReport validate(Message message, List<JsonSchemaRepository> schemaRepositories, JsonMessageValidationContext validationContext, ReferenceResolver referenceResolver) { return validate(message, jsonSchemaFilter.filter(schemaRepositories, validationContext, referenceResolver)); } JsonSchemaValidation();  JsonSchemaValidation(JsonSchemaFilter jsonSchemaFilter); ProcessingReport validate(Message message,
                                     List<JsonSchemaRepository> schemaRepositories,
                                     JsonMessageValidationContext validationContext,
                                     ReferenceResolver referenceResolver);  }
@Test public void testValidJsonMessageSuccessfullyValidated() throws Exception { JsonSchemaRepository jsonSchemaRepository = new JsonSchemaRepository(); jsonSchemaRepository.setBeanName("schemaRepository1"); Resource schemaResource = new ClassPathResource("com/consol/citrus/validation/ProductsSchema.json"); SimpleJsonSchema schema = new SimpleJsonSchema(schemaResource); schema.afterPropertiesSet(); jsonSchemaRepository.getSchemas().add(schema); List<JsonSchemaRepository> schemaRepositories = Collections.singletonList(jsonSchemaRepository); when(jsonSchemaFilterMock.filter(schemaRepositories, validationContextMock, referenceResolverMock)) .thenReturn(Collections.singletonList(schema)); Message receivedMessage = new DefaultMessage("[\n" + " {\n" + " \"id\": 2,\n" + " \"name\": \"An ice sculpture\",\n" + " \"price\": 12.50,\n" + " \"tags\": [\"cold\", \"ice\"],\n" + " \"dimensions\": {\n" + " \"length\": 7.0,\n" + " \"width\": 12.0,\n" + " \"height\": 9.5\n" + " }\n" + " }\n" + " ]"); ProcessingReport report = validator.validate( receivedMessage, schemaRepositories, validationContextMock, referenceResolverMock); Assert.assertTrue(report.isSuccess()); }
@Test public void testInvalidJsonMessageValidationIsNotSuccessful() throws Exception { JsonSchemaRepository jsonSchemaRepository = new JsonSchemaRepository(); jsonSchemaRepository.setBeanName("schemaRepository1"); Resource schemaResource = new ClassPathResource("com/consol/citrus/validation/ProductsSchema.json"); SimpleJsonSchema schema = new SimpleJsonSchema(schemaResource); schema.afterPropertiesSet(); jsonSchemaRepository.getSchemas().add(schema); List<JsonSchemaRepository> schemaRepositories = Collections.singletonList(jsonSchemaRepository); when(jsonSchemaFilterMock.filter(schemaRepositories, validationContextMock, referenceResolverMock)) .thenReturn(Collections.singletonList(schema)); Message receivedMessage = new DefaultMessage("[\n" + " {\n" + " \"name\": \"An ice sculpture\",\n" + " \"price\": 12.50,\n" + " \"tags\": [\"cold\", \"ice\"],\n" + " \"dimensions\": {\n" + " \"length\": 7.0,\n" + " \"width\": 12.0,\n" + " \"height\": 9.5\n" + " }\n" + " }\n" + " ]"); ProcessingReport report = validator.validate( receivedMessage, schemaRepositories, validationContextMock, referenceResolverMock); Assert.assertFalse(report.isSuccess()); }
@Test public void testValidationIsSuccessfulIfOneSchemaMatches() throws Exception { JsonSchemaRepository jsonSchemaRepository = new JsonSchemaRepository(); jsonSchemaRepository.setBeanName("schemaRepository1"); Resource schemaResource = new ClassPathResource("com/consol/citrus/validation/BookSchema.json"); SimpleJsonSchema schema = new SimpleJsonSchema(schemaResource); schema.afterPropertiesSet(); jsonSchemaRepository.getSchemas().add(schema); schemaResource = new ClassPathResource("com/consol/citrus/validation/ProductsSchema.json"); schema = new SimpleJsonSchema(schemaResource); schema.afterPropertiesSet(); jsonSchemaRepository.getSchemas().add(schema); List<JsonSchemaRepository> schemaRepositories = Collections.singletonList(jsonSchemaRepository); when(jsonSchemaFilterMock.filter(schemaRepositories, validationContextMock, referenceResolverMock)) .thenReturn(Collections.singletonList(schema)); Message receivedMessage = new DefaultMessage("[\n" + " {\n" + " \"id\": 2,\n" + " \"name\": \"An ice sculpture\",\n" + " \"price\": 12.50,\n" + " \"tags\": [\"cold\", \"ice\"],\n" + " \"dimensions\": {\n" + " \"length\": 7.0,\n" + " \"width\": 12.0,\n" + " \"height\": 9.5\n" + " }\n" + " }\n" + " ]"); ProcessingReport report = validator.validate( receivedMessage, schemaRepositories, validationContextMock, referenceResolverMock); Assert.assertTrue(report.isSuccess()); }
@Test public void testValidationOfJsonSchemaRepositoryList() throws Exception { List<JsonSchemaRepository> repositoryList = new LinkedList<>(); JsonSchemaRepository jsonSchemaRepository = new JsonSchemaRepository(); jsonSchemaRepository.setBeanName("schemaRepository1"); Resource schemaResource = new ClassPathResource("com/consol/citrus/validation/BookSchema.json"); SimpleJsonSchema schema = new SimpleJsonSchema(schemaResource); schema.afterPropertiesSet(); jsonSchemaRepository.getSchemas().add(schema); repositoryList.add(jsonSchemaRepository); jsonSchemaRepository = new JsonSchemaRepository(); jsonSchemaRepository.setBeanName("schemaRepository2"); schemaResource = new ClassPathResource("com/consol/citrus/validation/ProductsSchema.json"); schema = new SimpleJsonSchema(schemaResource); schema.afterPropertiesSet(); jsonSchemaRepository.getSchemas().add(schema); repositoryList.add(jsonSchemaRepository); Message receivedMessage = new DefaultMessage("[\n" + " {\n" + " \"id\": 2,\n" + " \"name\": \"An ice sculpture\",\n" + " \"price\": 12.50,\n" + " \"tags\": [\"cold\", \"ice\"],\n" + " \"dimensions\": {\n" + " \"length\": 7.0,\n" + " \"width\": 12.0,\n" + " \"height\": 9.5\n" + " }\n" + " }\n" + " ]"); ProcessingReport report = validator.validate( receivedMessage, repositoryList, validationContextMock, referenceResolverMock); Assert.assertTrue(report.isSuccess()); }
@Test public void testJsonSchemaFilterIsCalled() { List<JsonSchemaRepository> repositoryList = Collections.singletonList(mock(JsonSchemaRepository.class)); Message message = mock(Message.class); JsonMessageValidationContext jsonMessageValidationContext = mock(JsonMessageValidationContext.class); validator.validate(message, repositoryList, jsonMessageValidationContext, referenceResolverMock); verify(jsonSchemaFilterMock).filter(repositoryList, jsonMessageValidationContext, referenceResolverMock); }