[BAEL-4093] Mapping Collections with MapStruct (#9597)

Co-authored-by: Bogdan Feraru <bogdan.feraru@iquestgroup.com>
This commit is contained in:
Feraru Bogdan 2020-06-30 00:12:40 +03:00 committed by GitHub
parent db0342d3b2
commit ae9bc9eef1
14 changed files with 312 additions and 3 deletions

View File

@ -16,7 +16,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.mapstruct</groupId> <groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId> <artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version> <version>${org.mapstruct.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@ -71,12 +71,12 @@
</build> </build>
<properties> <properties>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version> <org.mapstruct.version>1.4.0.Beta1</org.mapstruct.version>
<springframework.version>4.3.4.RELEASE</springframework.version> <springframework.version>4.3.4.RELEASE</springframework.version>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<org.projectlombok.version>1.18.4</org.projectlombok.version> <org.projectlombok.version>1.18.4</org.projectlombok.version>
<assertj.version>3.11.1</assertj.version> <assertj.version>3.16.1</assertj.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,25 @@
package com.baeldung.mapstruct.mappingCollections.dto;
import java.util.ArrayList;
import java.util.List;
public class CompanyDTO {
private List<EmployeeDTO> employees;
public List<EmployeeDTO> getEmployees() {
return employees;
}
public void setEmployees(List<EmployeeDTO> employees) {
this.employees = employees;
}
public void addEmployee(EmployeeDTO employeeDTO) {
if (employees == null) {
employees = new ArrayList<>();
}
employees.add(employeeDTO);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.mapstruct.mappingCollections.dto;
public class EmployeeDTO {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.mapstruct.mappingCollections.dto;
public class EmployeeFullNameDTO {
private String fullName;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.mapstruct.mappingCollections.mapper;
import com.baeldung.mapstruct.mappingCollections.dto.CompanyDTO;
import com.baeldung.mapstruct.mappingCollections.model.Company;
import org.mapstruct.Mapper;
@Mapper(uses = EmployeeMapper.class)
public interface CompanyMapper {
CompanyDTO map(Company company);
}

View File

@ -0,0 +1,12 @@
package com.baeldung.mapstruct.mappingCollections.mapper;
import com.baeldung.mapstruct.mappingCollections.dto.CompanyDTO;
import com.baeldung.mapstruct.mappingCollections.model.Company;
import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface CompanyMapperAdderPreferred {
CompanyDTO map(Company company);
}

View File

@ -0,0 +1,20 @@
package com.baeldung.mapstruct.mappingCollections.mapper;
import com.baeldung.mapstruct.mappingCollections.dto.EmployeeFullNameDTO;
import com.baeldung.mapstruct.mappingCollections.model.Employee;
import org.mapstruct.Mapper;
import java.util.List;
@Mapper
public interface EmployeeFullNameMapper {
List<EmployeeFullNameDTO> map(List<Employee> employees);
default EmployeeFullNameDTO map(Employee employee) {
EmployeeFullNameDTO employeeInfoDTO = new EmployeeFullNameDTO();
employeeInfoDTO.setFullName(employee.getFirstName() + " " + employee.getLastName());
return employeeInfoDTO;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.mapstruct.mappingCollections.mapper;
import com.baeldung.mapstruct.mappingCollections.dto.EmployeeDTO;
import com.baeldung.mapstruct.mappingCollections.model.Employee;
import org.mapstruct.Mapper;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Mapper
public interface EmployeeMapper {
List<EmployeeDTO> map(List<Employee> employees);
Set<EmployeeDTO> map(Set<Employee> employees);
Map<String, EmployeeDTO> map(Map<String, Employee> idEmployeeMap);
}

View File

@ -0,0 +1,16 @@
package com.baeldung.mapstruct.mappingCollections.model;
import java.util.List;
public class Company {
private List<Employee> employees;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.mapstruct.mappingCollections.model;
public class Employee {
private String firstName;
private String lastName;
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.mapstruct.mappingCollections.mapper;
import com.baeldung.mapstruct.mappingCollections.dto.CompanyDTO;
import com.baeldung.mapstruct.mappingCollections.dto.EmployeeDTO;
import com.baeldung.mapstruct.mappingCollections.model.Company;
import com.baeldung.mapstruct.mappingCollections.model.Employee;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class CompanyMapperAdderPreferredUnitTest {
private CompanyMapperAdderPreferred companyMapper = Mappers.getMapper(CompanyMapperAdderPreferred.class);
@Test
void whenMappingToCompanyDTO_thenExpectCorrectMappingResult() {
Employee employee = new Employee("John", "Doe");
Company company = new Company();
company.setEmployees(Collections.singletonList(employee));
CompanyDTO result = companyMapper.map(company);
List<EmployeeDTO> employees = result.getEmployees();
assertThat(employees).hasSize(1);
assertThat(employees.get(0).getFirstName()).isEqualTo("John");
assertThat(employees.get(0).getLastName()).isEqualTo("Doe");
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.mapstruct.mappingCollections.mapper;
import com.baeldung.mapstruct.mappingCollections.dto.CompanyDTO;
import com.baeldung.mapstruct.mappingCollections.dto.EmployeeDTO;
import com.baeldung.mapstruct.mappingCollections.model.Company;
import com.baeldung.mapstruct.mappingCollections.model.Employee;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class CompanyMapperUnitTest {
private CompanyMapper companyMapper = Mappers.getMapper(CompanyMapper.class);
@Test
void whenMappingToCompanyDTO_thenExpectCorrectMappingResult() {
Employee employee = new Employee("John", "Doe");
Company company = new Company();
company.setEmployees(Collections.singletonList(employee));
CompanyDTO result = companyMapper.map(company);
List<EmployeeDTO> employees = result.getEmployees();
assertThat(employees).hasSize(1);
assertThat(employees.get(0).getFirstName()).isEqualTo("John");
assertThat(employees.get(0).getLastName()).isEqualTo("Doe");
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.mapstruct.mappingCollections.mapper;
import com.baeldung.mapstruct.mappingCollections.dto.EmployeeFullNameDTO;
import com.baeldung.mapstruct.mappingCollections.model.Employee;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class EmployeeFullNameMapperUnitTest {
private EmployeeFullNameMapper employeeMapper = Mappers.getMapper(EmployeeFullNameMapper.class);
@Test
void whenMappingToEmployeeFullNameDTOList_thenExpectCorrectMappingResult() {
Employee employee = new Employee("John", "Doe");
List<EmployeeFullNameDTO> result = employeeMapper.map(Collections.singletonList(employee));
assertThat(result).hasSize(1);
assertThat(result.get(0).getFullName()).isEqualTo("John Doe");
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.mapstruct.mappingCollections.mapper;
import com.baeldung.mapstruct.mappingCollections.dto.EmployeeDTO;
import com.baeldung.mapstruct.mappingCollections.model.Employee;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
class EmployeeMapperUnitTest {
private EmployeeMapper employeeMapper = Mappers.getMapper(EmployeeMapper.class);
@Test
void whenMappingToEmployeeDTOList_thenExpectCorrectMappingResult() {
Employee employee = new Employee("John", "Doe");
List<EmployeeDTO> result = employeeMapper.map(Collections.singletonList(employee));
assertThat(result).hasSize(1);
assertThat(result.get(0).getFirstName()).isEqualTo("John");
assertThat(result.get(0).getLastName()).isEqualTo("Doe");
}
@Test
void whenMappingToEmployeeDTOSet_thenExpectCorrectMappingResult() {
Employee employee = new Employee("John", "Doe");
Set<EmployeeDTO> result = employeeMapper.map(Collections.singleton(employee));
assertThat(result).hasSize(1);
assertThat(result.iterator().next().getFirstName()).isEqualTo("John");
assertThat(result.iterator().next().getLastName()).isEqualTo("Doe");
}
@Test
void whenMappingToEmployeeDTOMap_thenExpectCorrectMappingResult() {
Employee employee = new Employee("John", "Doe");
Map<String, EmployeeDTO> result = employeeMapper.map(Collections.singletonMap("1", employee));
assertThat(result).hasSize(1);
assertThat(result.get("1").getFirstName()).isEqualTo("John");
assertThat(result.get("1").getLastName()).isEqualTo("Doe");
}
}