ObjectMapper { public T mapObject(final Map<String, String> values) throws Exception { T result = createInstance(); for (Map.Entry<String, String> entry : values.entrySet()) { String field = entry.getKey(); String value = values.get(field); Method setter = setters.get(field); if (setter == null) { LOGGER.warn("No public setter found for field {}, this field will be set to null (if object type) or default value (if primitive type)", field); continue; } Class<?> type = setter.getParameterTypes()[0]; TypeConverter<String, ?> typeConverter = typeConverters.get(type); if (typeConverter == null) { LOGGER.warn( "Type conversion not supported for type {}, field {} will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value == null) { LOGGER.warn("Attempting to convert null to type {} for field {}, this field will be set to null (if object type) or default value (if primitive type)", type, field); continue; } if (value.isEmpty()) { LOGGER.debug("Attempting to convert an empty string to type {} for field {}, this field will be ignored", type, field); continue; } convertValue(result, field, value, setter, type, typeConverter); } return result; } ObjectMapper(final Class<T> objectType); T mapObject(final Map<String, String> values); void registerTypeConverter(final TypeConverter<String, ?> typeConverter);  }
@Test public void whenValuesAreValid_ThenTheMappedObjectShouldBeCorrectlyPopulated() throws Exception { ObjectMapper<Person> mapper = new ObjectMapper<>(Person.class); Map<String, String> values = new HashMap<>(); values.put("firstName", "foo"); values.put("lastName", "bar"); values.put("age", "30"); values.put("birthDate", "1990-12-12"); values.put("married", "true"); values.put("marriageDate", "2010-12-12"); Person person = mapper.mapObject(values); assertThat(person).isNotNull(); assertThat(person.getFirstName()).isEqualTo("foo"); assertThat(person.getLastName()).isEqualTo("bar"); assertThat(person.getAge()).isEqualTo(30); assertThat(new SimpleDateFormat("yyyy-MM-dd").format(person.getBirthDate())).isEqualTo("1990-12-12"); assertThat(person.isMarried()).isTrue(); assertThat(person.getMarriageDate()).isEqualTo(LocalDate.of(2010, 12, 12)); }
@Test public void whenValuesAreValid_ThenInheritedFieldsOfTheMappedObjectShouldBeCorrectlyPopulated() throws Exception { ObjectMapper<ExtendedPerson> mapper = new ObjectMapper<>(ExtendedPerson.class); Map<String, String> values = new HashMap<>(); values.put("firstName", "foo"); values.put("lastName", "bar"); values.put("age", "30"); values.put("birthDate", "1990-12-12"); values.put("married", "true"); values.put("nickName", "FB"); values.put("marriageDate", "2010-12-12"); ExtendedPerson extendedPerson = mapper.mapObject(values); assertThat(extendedPerson).isNotNull(); assertThat(extendedPerson.getFirstName()).isEqualTo("foo"); assertThat(extendedPerson.getLastName()).isEqualTo("bar"); assertThat(extendedPerson.getAge()).isEqualTo(30); assertThat(new SimpleDateFormat("yyyy-MM-dd").format(extendedPerson.getBirthDate())).isEqualTo("1990-12-12"); assertThat(extendedPerson.isMarried()).isTrue(); assertThat(extendedPerson.getMarriageDate()).isEqualTo(LocalDate.of(2010, 12, 12)); assertThat(extendedPerson.getNickName()).isEqualTo("FB"); }
@Test public void whenASetterDoesNotExist_ThenShouldLogAWarning() { ObjectMapper<Person> mapper = new ObjectMapper<>(Person.class); Map<String, String> values = new HashMap<>(); values.put("nickName", "foo"); try { Person person = mapper.mapObject(values); assertThat(person).isNotNull(); } catch (Exception e) { fail("Should not throw an exception even if the setter does not exist"); } }
@Test public void whenAttemptingToSetANullValue_ThenShouldNotCallTheSetter() throws Exception { ObjectMapper<Person> mapper = new ObjectMapper<>(Person.class); Map<String, String> values = new HashMap<>(); values.put("age", null); Person person = mapper.mapObject(values); assertThat(person).isNotNull(); assertThat(person.getAge()).isEqualTo(0); }