JsonImpl implements Json { public String parse(Map<String, Object> body) { try { String json = mapper().writeValueAsString(body); return json; } catch (JsonProcessingException e) { log.error(e.getMessage(), e); return null; } }  String parse(Map<String, Object> body); String parse(String string); String parse(T object); T parse(String json, Class<?> classType); @Override T parse(String json, TypeReference<T> type); T parse(String json, Class<?> parametrized, Class<?>... parameterClasses); Map<String, Object> parseToMap(T object); boolean isJson(String string);  }
@Test(expected = BeanValidationException.class) public void notBlankAnnotationShouldWork() { String json = "{\"name\":\"\"}"; try { subject.parse(json , PersonJsonTest.class); } catch (BeanValidationException e) { List<BeanValidationErrorDTO> errors = subject.parse(e.getViolations(), new TypeReference<List<BeanValidationErrorDTO>>() {}); assertThat(errors, contains(hasProperty("attribute", is("name")))); throw e; } }
@Test(expected = BeanValidationException.class) public void minAnnotationShouldWork() { String json = "{\"name\":\"Getu\", \"age\": 9}"; try { subject.parse(json , PersonJsonTest.class); } catch (BeanValidationException e) { List<BeanValidationErrorDTO> errors = subject.parse(e.getViolations(), new TypeReference<List<BeanValidationErrorDTO>>() {}); assertThat(errors, contains(hasProperty("message", is("You have to be older than 18 to drive in Brasil")))); throw e; } }
@Test(expected = BeanValidationException.class) public void interpolationWithMessageExpressionsShouldWork() { String json = "{\"name\":\"Getu\", \"age\": 20, \"socialMedia\": 0}"; try { subject.parse(json , PersonJsonTest.class); } catch (BeanValidationException e) { List<BeanValidationErrorDTO> errors = subject.parse(e.getViolations(), new TypeReference<List<BeanValidationErrorDTO>>() {}); assertThat(errors, contains(hasProperty("message", is("You have to have at least 2 social medias")))); throw e; } }
@Test(expected = BeanValidationException.class) public void pastAnnotationShouldWork() { LocalDate tomorrow = LocalDate.now().plusDays(1); String json = String.format("{\"name\":\"Getu\", \"age\": 20, \"socialMedia\": 3, \"birth\": \"%s\"}", tomorrow.toString()); try { subject.parse(json , PersonJsonTest.class); } catch (BeanValidationException e) { List<BeanValidationErrorDTO> errors = subject.parse(e.getViolations(), new TypeReference<List<BeanValidationErrorDTO>>() {}); assertThat(errors, contains(hasProperty("attribute", is("birth")))); throw e; } }
@Test(expected = BeanValidationException.class) public void emailAnnotationShouldWork() { LocalDate yesterday = LocalDate.now().minusDays(1); String json = String.format("{\"name\":\"Getu\", \"age\": 20, \"socialMedia\": 3, \"birth\": \"%s\", \"email\": \"heimdall_at_heimdall.com\"}", yesterday.toString()); try { subject.parse(json , PersonJsonTest.class); } catch (BeanValidationException e) { List<BeanValidationErrorDTO> errors = subject.parse(e.getViolations(), new TypeReference<List<BeanValidationErrorDTO>>() {}); assertThat(errors, contains(hasProperty("attribute", is("email")))); throw e; } }
@Test public void noBeanValidationShouldRaise() { LocalDate yesterday = LocalDate.now().minusDays(1); String json = String.format("{\"name\":\"Getu\", \"age\": 20, \"socialMedia\": 3, \"birth\": \"%s\", \"email\": \"heimdall@heimdall.com\"}", yesterday.toString()); PersonJsonTest person = subject.parse(json , PersonJsonTest.class); assertThat(person, hasProperty("name", is("Getu"))); assertThat(person, hasProperty("age", is(20))); assertThat(person, hasProperty("socialMedia", is(3))); assertThat(person, hasProperty("birth", is(yesterday))); assertThat(person, hasProperty("email", is("heimdall@heimdall.com"))); }