@Test public void validateShouldReturnPropertyNameWhenPropertyHasInvalidType() throws IOException { ObjectMapper mapper = new ObjectMapper(); when(objectMapperFactory.forParsing()) .thenReturn(mapper); String jsonContent = "{ \"isUnread\" : \"123\" }"; ObjectNode rootNode = mapper.readValue(jsonContent, ObjectNode.class); UpdateMessagePatchValidator sut = new UpdateMessagePatchValidator(objectMapperFactory); Set<ValidationResult> result = sut.validate(rootNode); assertThat(result).extracting(ValidationResult::getProperty).contains("isUnread"); }
@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);  }