@Test public void validateShouldReturnANonEmptyResultWhenParsingThrows() throws IOException { ObjectNode emptyRootNode = new ObjectMapper().createObjectNode(); ObjectMapper mapper = mock(ObjectMapper.class); JsonGenerator jsonGenerator = null; when(mapper.readValue(anyString(), eq(UpdateMessagePatch.class))) .thenThrow(JsonMappingException.from(jsonGenerator, "Exception when parsing")); when(objectMapperFactory.forParsing()) .thenReturn(mapper); UpdateMessagePatchValidator sut = new UpdateMessagePatchValidator(objectMapperFactory); Set<ValidationResult> result = sut.validate(emptyRootNode); assertThat(result).isNotEmpty(); assertThat(result).extracting(ValidationResult::getProperty).contains(ValidationResult.UNDEFINED_PROPERTY); }
@Override public Set<ValidationResult> validate(ObjectNode json) { try { parser.readValue(json.toString(), UpdateMessagePatch.class); } catch (JsonMappingException e) { return ImmutableSet.of(ValidationResult.builder() .property(firstFieldFrom(e.getPath()).orElse(ValidationResult.UNDEFINED_PROPERTY)) .message(e.getMessage()) .build()); } catch (IOException e) { return ImmutableSet.of(ValidationResult.builder() .message(e.getMessage()) .build()); } return ImmutableSet.of(); }
UpdateMessagePatchValidator implements Validator<ObjectNode> { @Override public Set<ValidationResult> validate(ObjectNode json) { try { parser.readValue(json.toString(), UpdateMessagePatch.class); } catch (JsonMappingException e) { return ImmutableSet.of(ValidationResult.builder() .property(firstFieldFrom(e.getPath()).orElse(ValidationResult.UNDEFINED_PROPERTY)) .message(e.getMessage()) .build()); } catch (IOException e) { return ImmutableSet.of(ValidationResult.builder() .message(e.getMessage()) .build()); } return ImmutableSet.of(); } }
UpdateMessagePatchValidator implements Validator<ObjectNode> { @Override public Set<ValidationResult> validate(ObjectNode json) { try { parser.readValue(json.toString(), UpdateMessagePatch.class); } catch (JsonMappingException e) { return ImmutableSet.of(ValidationResult.builder() .property(firstFieldFrom(e.getPath()).orElse(ValidationResult.UNDEFINED_PROPERTY)) .message(e.getMessage()) .build()); } catch (IOException e) { return ImmutableSet.of(ValidationResult.builder() .message(e.getMessage()) .build()); } return ImmutableSet.of(); } @Inject @VisibleForTesting  UpdateMessagePatchValidator(ObjectMapperFactory parserFactory); }
UpdateMessagePatchValidator implements Validator<ObjectNode> { @Override public Set<ValidationResult> validate(ObjectNode json) { try { parser.readValue(json.toString(), UpdateMessagePatch.class); } catch (JsonMappingException e) { return ImmutableSet.of(ValidationResult.builder() .property(firstFieldFrom(e.getPath()).orElse(ValidationResult.UNDEFINED_PROPERTY)) .message(e.getMessage()) .build()); } catch (IOException e) { return ImmutableSet.of(ValidationResult.builder() .message(e.getMessage()) .build()); } return ImmutableSet.of(); } @Inject @VisibleForTesting  UpdateMessagePatchValidator(ObjectMapperFactory parserFactory); @Override boolean isValid(ObjectNode patch); @Override Set<ValidationResult> validate(ObjectNode json); }
UpdateMessagePatchValidator implements Validator<ObjectNode> { @Override public Set<ValidationResult> validate(ObjectNode json) { try { parser.readValue(json.toString(), UpdateMessagePatch.class); } catch (JsonMappingException e) { return ImmutableSet.of(ValidationResult.builder() .property(firstFieldFrom(e.getPath()).orElse(ValidationResult.UNDEFINED_PROPERTY)) .message(e.getMessage()) .build()); } catch (IOException e) { return ImmutableSet.of(ValidationResult.builder() .message(e.getMessage()) .build()); } return ImmutableSet.of(); } @Inject @VisibleForTesting  UpdateMessagePatchValidator(ObjectMapperFactory parserFactory); @Override boolean isValid(ObjectNode patch); @Override Set<ValidationResult> validate(ObjectNode json);  }