Skip to content

Commit 67982c7

Browse files
Sasa Msasam0320
authored andcommitted
Creating TypeMap to use property mapping and converter class
1 parent 7ba327b commit 67982c7

4 files changed

Lines changed: 43 additions & 49 deletions

File tree

java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.stream.Collectors;
77

88
/**
9-
* This is a helper class that contains method for generic mapping of the users list.
9+
* This is a helper class that contains method for custom mapping of the users list.
1010
* Initially, an instance of ModelMapper was created.
1111
*
1212
* @author Sasa Milenkovic

java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.modelmapper;
2+
3+
import org.modelmapper.AbstractConverter;
4+
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* UsersListConverter class map the property data from the list of users into the list of user names.
10+
*
11+
* @author Sasa Milenkovic
12+
*/
13+
public class UsersListConverter extends AbstractConverter<List<User>, List<String>> {
14+
15+
@Override
16+
protected List<String> convert(List<User> users) {
17+
18+
return users
19+
.stream()
20+
.map(User::getUsername)
21+
.collect(Collectors.toList());
22+
}
23+
}

java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.Before;
55
import org.junit.Test;
66
import org.modelmapper.ModelMapper;
7+
import org.modelmapper.TypeMap;
78
import org.modelmapper.TypeToken;
89

910
import java.util.ArrayList;
@@ -12,14 +13,12 @@
1213
import static org.hamcrest.Matchers.equalTo;
1314
import static org.hamcrest.Matchers.hasItems;
1415
import static org.hamcrest.Matchers.hasProperty;
15-
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
16-
import static org.junit.Assert.assertNotNull;
1716
import static org.junit.Assert.assertThat;
1817

1918

2019
/**
2120
* This class has test methods of mapping Integer to Character list,
22-
* mapping user list to DTO list using MapperUtil generic methods and Converter
21+
* mapping users list to DTO list using MapperUtil custom type method and property mapping using converter class
2322
*
2423
* @author Sasa Milenkovic
2524
*/
@@ -32,7 +31,12 @@ public class UsersListMappingUnitTest {
3231
public void init() {
3332

3433
modelMapper = new ModelMapper();
35-
modelMapper.addMappings(new UserPropertyMap());
34+
35+
TypeMap<UserList, UserListDTO> typeMap = modelMapper.createTypeMap(UserList.class, UserListDTO.class);
36+
37+
typeMap.addMappings(mapper -> mapper.using(new UsersListConverter())
38+
.map(UserList::getUsers, UserListDTO::setUsernames));
39+
3640
users = new ArrayList();
3741
users.add(new User("b100", "user1", "[email protected]", "111-222", "USER"));
3842
users.add(new User("b101", "user2", "[email protected]", "111-333", "USER"));
@@ -41,7 +45,7 @@ public void init() {
4145
}
4246

4347
@Test
44-
public void whenMapIntegerToCharList() {
48+
public void whenInteger_thenMapToCharacter() {
4549

4650
List<Integer> integers = new ArrayList<Integer>();
4751

@@ -57,9 +61,9 @@ public void whenMapIntegerToCharList() {
5761
}
5862

5963
@Test
60-
public void givenUsersList_whenUseGenericType_thenMapToDto() {
64+
public void givenUsersList_whenUseGenericType_thenMapToUserDTO() {
6165

62-
// Mapping lists using custom type methods
66+
// Mapping lists using custom (generic) type mapping
6367

6468
List<UserDTO> userDtoList = MapperUtil.mapList(users, UserDTO.class);
6569

@@ -68,16 +72,20 @@ public void givenUsersList_whenUseGenericType_thenMapToDto() {
6872
.and(hasProperty("email", equalTo("[email protected]")))
6973
.and(hasProperty("username", equalTo("user1")))));
7074

71-
// Mapping lists using PropertyMap and Converter
75+
76+
}
77+
78+
@Test
79+
public void givenUsersList_whenUseConverter_thenMapToUsernames() {
80+
81+
// Mapping lists using property mapping and converter
7282

7383
UserList userList = new UserList();
7484
userList.setUsers(users);
7585
UserListDTO dtos = new UserListDTO();
7686
modelMapper.map(userList, dtos);
7787

78-
assertNotNull(dtos);
79-
assertThat(dtos, Matchers.hasProperty("usernames"));
80-
assertThat(dtos.getUsernames(), hasSize(3));
88+
assertThat(dtos.getUsernames(), hasItems("user1", "user2", "user3"));
8189

8290
}
8391

0 commit comments

Comments
 (0)