Creating TypeMap to use property mapping and converter class

This commit is contained in:
Sasa M 2020-04-29 21:55:30 +02:00 committed by Sasa M
parent 8d7d98a144
commit 9bf7546d42
4 changed files with 43 additions and 49 deletions

View File

@ -6,7 +6,7 @@ import java.util.List;
import java.util.stream.Collectors;
/**
* This is a helper class that contains method for generic mapping of the users list.
* This is a helper class that contains method for custom mapping of the users list.
* Initially, an instance of ModelMapper was created.
*
* @author Sasa Milenkovic

View File

@ -1,37 +0,0 @@
package com.baeldung.modelmapper;
import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.PropertyMap;
import java.util.List;
import java.util.stream.Collectors;
/**
* UserPropertyMap class instantiates the converter to map the data from the UserList to the UsersLisDTO.
* In the configuration method, we call a converter to do the mapping.
*
* @author Sasa Milenkovic
*/
public class UserPropertyMap extends PropertyMap<UserList, UserListDTO> {
Converter<List<User>, List<String>> converter = new AbstractConverter<List<User>, List<String>>() {
@Override
protected List<String> convert(List<User> users) {
return users
.stream()
.map(User::getUsername)
.collect(Collectors.toList());
}
};
@Override
protected void configure() {
using(converter).map(source.getUsers(), destination.getUsernames());
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.modelmapper;
import org.modelmapper.AbstractConverter;
import java.util.List;
import java.util.stream.Collectors;
/**
* UsersListConverter class map the property data from the list of users into the list of user names.
*
* @author Sasa Milenkovic
*/
public class UsersListConverter extends AbstractConverter<List<User>, List<String>> {
@Override
protected List<String> convert(List<User> users) {
return users
.stream()
.map(User::getUsername)
.collect(Collectors.toList());
}
}

View File

@ -4,6 +4,7 @@ import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import org.modelmapper.TypeToken;
import java.util.ArrayList;
@ -12,14 +13,12 @@ import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
/**
* This class has test methods of mapping Integer to Character list,
* mapping user list to DTO list using MapperUtil generic methods and Converter
* mapping users list to DTO list using MapperUtil custom type method and property mapping using converter class
*
* @author Sasa Milenkovic
*/
@ -32,7 +31,12 @@ public class UsersListMappingUnitTest {
public void init() {
modelMapper = new ModelMapper();
modelMapper.addMappings(new UserPropertyMap());
TypeMap<UserList, UserListDTO> typeMap = modelMapper.createTypeMap(UserList.class, UserListDTO.class);
typeMap.addMappings(mapper -> mapper.using(new UsersListConverter())
.map(UserList::getUsers, UserListDTO::setUsernames));
users = new ArrayList();
users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER"));
users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER"));
@ -41,7 +45,7 @@ public class UsersListMappingUnitTest {
}
@Test
public void whenMapIntegerToCharList() {
public void whenInteger_thenMapToCharacter() {
List<Integer> integers = new ArrayList<Integer>();
@ -57,9 +61,9 @@ public class UsersListMappingUnitTest {
}
@Test
public void givenUsersList_whenUseGenericType_thenMapToDto() {
public void givenUsersList_whenUseGenericType_thenMapToUserDTO() {
// Mapping lists using custom type methods
// Mapping lists using custom (generic) type mapping
List<UserDTO> userDtoList = MapperUtil.mapList(users, UserDTO.class);
@ -68,16 +72,20 @@ public class UsersListMappingUnitTest {
.and(hasProperty("email", equalTo("user1@baeldung.com")))
.and(hasProperty("username", equalTo("user1")))));
// Mapping lists using PropertyMap and Converter
}
@Test
public void givenUsersList_whenUseConverter_thenMapToUsernames() {
// Mapping lists using property mapping and converter
UserList userList = new UserList();
userList.setUsers(users);
UserListDTO dtos = new UserListDTO();
modelMapper.map(userList, dtos);
assertNotNull(dtos);
assertThat(dtos, Matchers.hasProperty("usernames"));
assertThat(dtos.getUsernames(), hasSize(3));
assertThat(dtos.getUsernames(), hasItems("user1", "user2", "user3"));
}