Skip to content

Commit 912d17c

Browse files
committed
Moving com.baeldung.modelmmaper package to java-collections-conversations module
1 parent 013a2cf commit 912d17c

7 files changed

Lines changed: 90 additions & 252 deletions

File tree

model-mapper/src/com/baeldung/util/UserPropertyMap.java renamed to java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
package com.baeldung.util;
1+
package com.baeldung.modelmapper;
22

3-
import com.baeldung.model.User;
4-
import com.baeldung.model.UserDTO;
5-
import com.baeldung.model.UserList;
63
import org.modelmapper.AbstractConverter;
74
import org.modelmapper.Converter;
85
import org.modelmapper.PropertyMap;
@@ -11,10 +8,11 @@
118

129
/**
1310
* @author sasam0320
14-
* @date 4/18/2020
11+
* @description
12+
* UserPropertyMap class instantiates the converter to map the data from the user list to the user name list.
13+
* In the configuration method, we call a converter to do the mapping.
1514
*/
16-
17-
public class UserPropertyMap extends PropertyMap<UserList, UserDTO> {
15+
public class UserPropertyMap extends PropertyMap<UserList, UserListDTO> {
1816

1917

2018
Converter<List<User>, List<String>> converter = new AbstractConverter<List<User>, List<String>>() {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.modelmapper;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.modelmapper.ModelMapper;
7+
import org.modelmapper.TypeToken;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import static org.hamcrest.Matchers.equalTo;
13+
import static org.hamcrest.Matchers.hasItems;
14+
import static org.hamcrest.Matchers.hasProperty;
15+
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
16+
17+
import static org.junit.Assert.assertNotNull;
18+
import static org.junit.Assert.assertThat;
19+
20+
21+
/**
22+
* @sasam0320
23+
* @description
24+
* This class has test methods of mapping Integer to Character list,
25+
* mapping user list to DTO list using MapperUtil generic methods and Converter
26+
*/
27+
public class UserMappingTest {
28+
29+
private ModelMapper mapper;
30+
private List<User> users;
31+
32+
@Before
33+
public void init() {
34+
35+
mapper = new ModelMapper();
36+
mapper.addMappings(new UserPropertyMap());
37+
users = new ArrayList();
38+
users.add(new User("b100", "user1", "[email protected]", "111-222", "USER"));
39+
users.add(new User("b101", "user2", "[email protected]", "111-333", "USER"));
40+
users.add(new User("b102", "user3", "[email protected]", "111-444", "ADMIN"));
41+
42+
}
43+
44+
@Test
45+
public void testMapIntegerList() {
46+
47+
List<Integer> integers = new ArrayList<Integer>();
48+
49+
integers.add(1);
50+
integers.add(2);
51+
integers.add(3);
52+
53+
List<Character> characters = mapper.map(integers, new TypeToken<List<Character>>() {
54+
}.getType());
55+
56+
assertThat(characters, hasItems('1','2','3'));
57+
58+
}
59+
60+
@Test
61+
public void testMapGenericTypeLists() {
62+
63+
// Mapping lists using generic type methods
64+
65+
List<UserDTO> userDtoList = MapperUtil.mapList(users, UserDTO.class);
66+
67+
assertThat(userDtoList, Matchers.<UserDTO>hasItem(
68+
Matchers.both(hasProperty("userId", equalTo("b100")))
69+
.and(hasProperty("email", equalTo("[email protected]")))
70+
.and(hasProperty("userName", equalTo("user1")))));
71+
72+
// Mapping lists using PropertyMap and Converter
73+
74+
UserList userList = new UserList();
75+
userList.setUsers(users);
76+
UserListDTO dto = new UserListDTO();
77+
mapper.map(userList, dto);
78+
79+
assertNotNull(dto);
80+
assertThat(dto, Matchers.hasProperty("usernames"));
81+
assertThat(dto.getUsernames(), hasSize(3));
82+
83+
}
84+
85+
}

model-mapper/src/Main.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

model-mapper/src/com/baeldung/model/User.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

model-mapper/src/com/baeldung/model/UserDTO.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

model-mapper/src/com/baeldung/model/UserList.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

model-mapper/src/com/baeldung/util/MapperUtil.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)