Creating TypeMap to use property mapping and converter class
This commit is contained in:
@@ -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"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user