Merge pull request #9147 from sasam0320/BAEL-3986
BAEL 3986 - mapping lists using ModelMapper
This commit is contained in:
		
						commit
						f4f6c2fe88
					
				| @ -20,6 +20,23 @@ | |||||||
|             <artifactId>commons-lang3</artifactId> |             <artifactId>commons-lang3</artifactId> | ||||||
|             <version>${commons-lang3.version}</version> |             <version>${commons-lang3.version}</version> | ||||||
|         </dependency> |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.modelmapper</groupId> | ||||||
|  |             <artifactId>modelmapper</artifactId> | ||||||
|  |             <version>${modelmapper.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>junit</groupId> | ||||||
|  |             <artifactId>junit</artifactId> | ||||||
|  |             <version>${junit.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.hamcrest</groupId> | ||||||
|  |             <artifactId>hamcrest</artifactId> | ||||||
|  |             <version>${hamcrest.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|     </dependencies> |     </dependencies> | ||||||
| 
 | 
 | ||||||
|     <build> |     <build> | ||||||
|  | |||||||
| @ -0,0 +1,32 @@ | |||||||
|  | package com.baeldung.modelmapper; | ||||||
|  | 
 | ||||||
|  | import org.modelmapper.ModelMapper; | ||||||
|  | 
 | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.stream.Collectors; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 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 | ||||||
|  |  */ | ||||||
|  | public class MapperUtil { | ||||||
|  | 
 | ||||||
|  |     private static ModelMapper modelMapper = new ModelMapper(); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     private MapperUtil() { | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static <S, T> List<T> mapList(List<S> source, Class<T> targetClass) { | ||||||
|  | 
 | ||||||
|  |         return source | ||||||
|  |                 .stream() | ||||||
|  |                 .map(element -> modelMapper.map(element, targetClass)) | ||||||
|  |                 .collect(Collectors.toList()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,70 @@ | |||||||
|  | package com.baeldung.modelmapper; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * User model entity class | ||||||
|  |  * | ||||||
|  |  * @author Sasa Milenkovic | ||||||
|  |  */ | ||||||
|  | public class User { | ||||||
|  | 
 | ||||||
|  |     private String userId; | ||||||
|  |     private String username; | ||||||
|  |     private String email; | ||||||
|  |     private String contactNumber; | ||||||
|  |     private String userType; | ||||||
|  | 
 | ||||||
|  |     // Standard constructors, getters and setters | ||||||
|  | 
 | ||||||
|  |     public User() { | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public User(String userId, String username, String email, String contactNumber, String userType) { | ||||||
|  |         this.userId = userId; | ||||||
|  |         this.username = username; | ||||||
|  |         this.email = email; | ||||||
|  |         this.contactNumber = contactNumber; | ||||||
|  |         this.userType = userType; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getUserId() { | ||||||
|  |         return userId; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setUserId(String userId) { | ||||||
|  |         this.userId = userId; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getUsername() { | ||||||
|  |         return username; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setUsername(String userName) { | ||||||
|  |         this.username = userName; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getEmail() { | ||||||
|  |         return email; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setEmail(String email) { | ||||||
|  |         this.email = email; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getContactNumber() { | ||||||
|  |         return contactNumber; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setContactNumber(String contactNumber) { | ||||||
|  |         this.contactNumber = contactNumber; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getUserType() { | ||||||
|  |         return userType; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setUserType(String userType) { | ||||||
|  |         this.userType = userType; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,41 @@ | |||||||
|  | package com.baeldung.modelmapper; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * UserDTO model class | ||||||
|  |  * | ||||||
|  |  * @author Sasa Milenkovic | ||||||
|  |  */ | ||||||
|  | public class UserDTO { | ||||||
|  | 
 | ||||||
|  |     private String userId; | ||||||
|  |     private String username; | ||||||
|  |     private String email; | ||||||
|  | 
 | ||||||
|  |     // getters and setters | ||||||
|  | 
 | ||||||
|  |     public String getUserId() { | ||||||
|  |         return userId; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setUserId(String userId) { | ||||||
|  |         this.userId = userId; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getUsername() { | ||||||
|  |         return username; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setUsername(String username) { | ||||||
|  |         this.username = username; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getEmail() { | ||||||
|  |         return email; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setEmail(String email) { | ||||||
|  |         this.email = email; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,21 @@ | |||||||
|  | package com.baeldung.modelmapper; | ||||||
|  | 
 | ||||||
|  | import java.util.Collection; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * UserList class that contain collection of users | ||||||
|  |  * | ||||||
|  |  * @author Sasa Milenkovic | ||||||
|  |  */ | ||||||
|  | public class UserList { | ||||||
|  | 
 | ||||||
|  |     private Collection<User> users; | ||||||
|  | 
 | ||||||
|  |     public Collection<User> getUsers() { | ||||||
|  |         return users; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setUsers(Collection<User> users) { | ||||||
|  |         this.users = users; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,21 @@ | |||||||
|  | package com.baeldung.modelmapper; | ||||||
|  | 
 | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * UserListDTO class that contain list of username properties | ||||||
|  |  * | ||||||
|  |  * @author Sasa Milenkovic | ||||||
|  |  */ | ||||||
|  | public class UserListDTO { | ||||||
|  | 
 | ||||||
|  |     private List<String> usernames; | ||||||
|  | 
 | ||||||
|  |     public List<String> getUsernames() { | ||||||
|  |         return usernames; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setUsernames(List<String> usernames) { | ||||||
|  |         this.usernames = usernames; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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()); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,92 @@ | |||||||
|  | package com.baeldung.modelmapper; | ||||||
|  | 
 | ||||||
|  | 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; | ||||||
|  | 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.junit.Assert.assertThat; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * This class has test methods of mapping Integer to Character list, | ||||||
|  |  * mapping users list to DTO list using MapperUtil custom type method and property mapping using converter class | ||||||
|  |  * | ||||||
|  |  * @author Sasa Milenkovic | ||||||
|  |  */ | ||||||
|  | public class UsersListMappingUnitTest { | ||||||
|  | 
 | ||||||
|  |     private ModelMapper modelMapper; | ||||||
|  |     private List<User> users; | ||||||
|  | 
 | ||||||
|  |     @Before | ||||||
|  |     public void init() { | ||||||
|  | 
 | ||||||
|  |         modelMapper = new ModelMapper(); | ||||||
|  | 
 | ||||||
|  |         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")); | ||||||
|  |         users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN")); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenInteger_thenMapToCharacter() { | ||||||
|  | 
 | ||||||
|  |         List<Integer> integers = new ArrayList<Integer>(); | ||||||
|  | 
 | ||||||
|  |         integers.add(1); | ||||||
|  |         integers.add(2); | ||||||
|  |         integers.add(3); | ||||||
|  | 
 | ||||||
|  |         List<Character> characters = modelMapper.map(integers, new TypeToken<List<Character>>() { | ||||||
|  |         }.getType()); | ||||||
|  | 
 | ||||||
|  |         assertThat(characters, hasItems('1', '2', '3')); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenUsersList_whenUseGenericType_thenMapToUserDTO() { | ||||||
|  | 
 | ||||||
|  |         // Mapping lists using custom (generic) type mapping | ||||||
|  | 
 | ||||||
|  |         List<UserDTO> userDtoList = MapperUtil.mapList(users, UserDTO.class); | ||||||
|  | 
 | ||||||
|  |         assertThat(userDtoList, Matchers.<UserDTO>hasItem( | ||||||
|  |                 Matchers.both(hasProperty("userId", equalTo("b100"))) | ||||||
|  |                         .and(hasProperty("email", equalTo("user1@baeldung.com"))) | ||||||
|  |                         .and(hasProperty("username", equalTo("user1"))))); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @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); | ||||||
|  | 
 | ||||||
|  |         assertThat(dtos.getUsernames(), hasItems("user1", "user2", "user3")); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -1,8 +1,8 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <project | <project | ||||||
|     xmlns="http://maven.apache.org/POM/4.0.0" |         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||
|     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |         xmlns="http://maven.apache.org/POM/4.0.0" | ||||||
|     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||||
|     <modelVersion>4.0.0</modelVersion> |     <modelVersion>4.0.0</modelVersion> | ||||||
|     <artifactId>parent-java</artifactId> |     <artifactId>parent-java</artifactId> | ||||||
|     <version>0.0.1-SNAPSHOT</version> |     <version>0.0.1-SNAPSHOT</version> | ||||||
| @ -45,6 +45,9 @@ | |||||||
|         <guava.version>23.0</guava.version> |         <guava.version>23.0</guava.version> | ||||||
|         <commons.io.version>2.6</commons.io.version> |         <commons.io.version>2.6</commons.io.version> | ||||||
|         <jmh.version>1.19</jmh.version> |         <jmh.version>1.19</jmh.version> | ||||||
|  |         <modelmapper.version>2.3.7</modelmapper.version> | ||||||
|  |         <junit.version>4.12</junit.version> | ||||||
|  |         <hamcrest.version>2.2</hamcrest.version> | ||||||
|     </properties> |     </properties> | ||||||
| 
 | 
 | ||||||
| </project> | </project> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user