FileBasedJsonSchemaValidator { public void validateWithoutSchemaCatalog(final String envelopeJson, final String actionName) { final JSONObject payload = payloadExtractor.extractPayloadFrom(envelopeJson); try { schemaOf(actionName).validate(payload); } catch (final ValidationException e) { throw new JsonSchemaValidationException( schemaValidationErrorMessageGenerator.generateErrorMessage(e), e); } }  void validateWithoutSchemaCatalog(final String envelopeJson, final String actionName);  }
@Test public void shouldLoadASchemaFromTheFileSystemByItsNameAndValidate() throws Exception { final String actionName = "example.action-name"; final String envelopeJson = "{\"envelope\": \"json\"}"; final JSONObject payload = mock(JSONObject.class); final Schema schema = mock(Schema.class); when(payloadExtractor.extractPayloadFrom(envelopeJson)).thenReturn(payload); when(jsonSchemaLoader.loadSchema(actionName)).thenReturn(schema); fileBasedJsonSchemaValidator.validateWithoutSchemaCatalog(envelopeJson, actionName); verify(schema).validate(payload); }
@Test public void shouldThrowExceptionIfSchemaValidationFails() throws Exception { final String actionName = "example.action-name"; final String envelopeJson = "{\"envelope\": \"json\"}"; final String errorMessage = "error message"; final JSONObject payload = mock(JSONObject.class); final Schema schema = mock(Schema.class); final ValidationException validationException = mock(ValidationException.class); when(payloadExtractor.extractPayloadFrom(envelopeJson)).thenReturn(payload); when(jsonSchemaLoader.loadSchema(actionName)).thenReturn(schema); when(schemaValidationErrorMessageGenerator.generateErrorMessage(validationException)).thenReturn(errorMessage); doThrow(validationException).when(schema).validate(payload); try { fileBasedJsonSchemaValidator.validateWithoutSchemaCatalog(envelopeJson, actionName); fail(); } catch (final JsonSchemaValidationException e) { assertThat(e.getMessage(), is(errorMessage)); assertThat(e.getCause(), is(validationException)); } }