EnvelopeValidator { public void validate(final JsonEnvelope jsonEnvelope, final String actionName, final Optional<MediaType> mediaType) { try { final JsonValue payload = jsonEnvelope.payload(); if (!NULL.equals(payload)) { jsonSchemaValidator.validate( objectMapper.writeValueAsString(payload), actionName, mediaType); } } catch (final JsonProcessingException e) { envelopeValidationExceptionHandler.handle(new EnvelopeValidationException("Error serialising json.", e)); } catch (final SchemaLoadingException e) { envelopeValidationExceptionHandler.handle(new EnvelopeValidationException( format("Could not load json schema that matches message type %s.", actionName), e)); } catch (final JsonSchemaValidationException e) { envelopeValidationExceptionHandler.handle(new EnvelopeValidationException( format("Message not valid against schema: %n%s : validation error: %s", jsonEnvelope.toObfuscatedDebugString(), new DefaultJsonValidationLoggerHelper().toValidationTrace(e)), e)); } catch (final EnvelopeValidationException e) { envelopeValidationExceptionHandler.handle(e); } } EnvelopeValidator(
            final JsonSchemaValidator jsonSchemaValidator,
            final ObjectMapper objectMapper,
            final EnvelopeValidationExceptionHandler envelopeValidationExceptionHandler); void validate(final JsonEnvelope jsonEnvelope, final String actionName, final Optional<MediaType> mediaType);  }
@Test public void shouldValidateThePayloadOfAJsonEnvelope() throws Exception { final String actionName = "example.action-name"; final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json")); final String payloadJson = "{\"some\": \"json\"}"; final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class); final JsonValue payload = mock(JsonValue.class); when(jsonEnvelope.payload()).thenReturn(payload); when(objectMapper.writeValueAsString(payload)).thenReturn(payloadJson); envelopeValidator.validate(jsonEnvelope, actionName, mediaType); verify(jsonSchemaValidator).validate( payloadJson, actionName, mediaType); }
@Test public void shouldDoNothingIfTheEnvelopePayloadIsNull() throws Exception { final String actionName = "example.action-name"; final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json")); final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class); when(jsonEnvelope.payload()).thenReturn(NULL); envelopeValidator.validate(jsonEnvelope, actionName, mediaType); verifyZeroInteractions(objectMapper); verifyZeroInteractions(jsonSchemaValidator); }
@Test public void shouldHandleAJsonProcessingException() throws Exception { final JsonProcessingException jsonProcessingException = new JsonGenerationException("Ooops"); final String actionName = "example.action-name"; final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json")); final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class); final JsonValue payload = mock(JsonValue.class); when(jsonEnvelope.payload()).thenReturn(payload); when(objectMapper.writeValueAsString(payload)).thenThrow(jsonProcessingException); envelopeValidator.validate(jsonEnvelope, actionName, mediaType); verify(envelopeValidationExceptionHandler).handle(exceptionArgumentCaptor.capture()); final EnvelopeValidationException envelopeValidationException = exceptionArgumentCaptor.getValue(); assertThat(envelopeValidationException.getMessage(), is("Error serialising json.")); assertThat(envelopeValidationException.getCause(), is(jsonProcessingException)); verifyZeroInteractions(jsonSchemaValidator); }
@Test public void shouldHandleASchemaLoadingException() throws Exception { final SchemaLoadingException schemaLoadingException = new SchemaLoadingException("Ooops"); final String actionName = "exaple.action-name"; final String payloadJson = "{\"some\": \"json\"}"; final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json")); final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class); final JsonValue payload = mock(JsonValue.class); when(jsonEnvelope.payload()).thenReturn(payload); when(objectMapper.writeValueAsString(payload)).thenReturn(payloadJson); doThrow(schemaLoadingException).when(jsonSchemaValidator).validate( payloadJson, actionName, mediaType); envelopeValidator.validate(jsonEnvelope, actionName, mediaType); verify(envelopeValidationExceptionHandler).handle(exceptionArgumentCaptor.capture()); final EnvelopeValidationException envelopeValidationException = exceptionArgumentCaptor.getValue(); assertThat(envelopeValidationException.getMessage(), is("Could not load json schema that matches message type exaple.action-name.")); assertThat(envelopeValidationException.getCause(), is(schemaLoadingException)); }
@SuppressWarnings("deprecation") @Test public void shouldHandleAValidationException() throws Exception { final JsonSchemaValidationException validationException = new JsonSchemaValidationException("Ooops", new ValidationException("Opps")); final String actionName = "exaple.action-name"; final String payloadJson = "{\"some\": \"json\"}"; final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json")); final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class); final JsonValue payload = mock(JsonValue.class); when(jsonEnvelope.payload()).thenReturn(payload); when(objectMapper.writeValueAsString(payload)).thenReturn(payloadJson); when(jsonEnvelope.toObfuscatedDebugString()).thenReturn("debug-json"); doThrow(validationException).when(jsonSchemaValidator).validate( payloadJson, actionName, mediaType); envelopeValidator.validate(jsonEnvelope, actionName, mediaType); verify(envelopeValidationExceptionHandler).handle(exceptionArgumentCaptor.capture()); final EnvelopeValidationException envelopeValidationException = exceptionArgumentCaptor.getValue(); final String exceptionMessage = "Message not valid against schema: \ndebug-json : validation error:" + " {\"message\":\"#: Opps\",\"violation\":\"#\",\"causingExceptions\":[]}"; assertThat(envelopeValidationException.getMessage(), is(exceptionMessage)); assertThat(envelopeValidationException.getCause(), is(validationException)); }
@Test public void shouldHandleAEnvelopeValidationException() throws Exception { final EnvelopeValidationException envelopeValidationException = new EnvelopeValidationException("Ooops"); final String actionName = "exaple.action-name"; final String payloadJson = "{\"some\": \"json\"}"; final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json")); final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class); final JsonValue payload = mock(JsonValue.class); when(jsonEnvelope.payload()).thenReturn(payload); when(objectMapper.writeValueAsString(payload)).thenReturn(payloadJson); when(jsonEnvelope.toObfuscatedDebugString()).thenReturn("debug-json"); doThrow(envelopeValidationException).when(jsonSchemaValidator).validate( payloadJson, actionName, mediaType); envelopeValidator.validate(jsonEnvelope, actionName, mediaType); verify(envelopeValidationExceptionHandler).handle(exceptionArgumentCaptor.capture()); assertThat(exceptionArgumentCaptor.getValue(), is(envelopeValidationException)); }