From ae9bc9eef113faec5583d120f34fda8f3f0b7ad6 Mon Sep 17 00:00:00 2001 From: Feraru Bogdan Date: Tue, 30 Jun 2020 00:12:40 +0300 Subject: [PATCH] [BAEL-4093] Mapping Collections with MapStruct (#9597) Co-authored-by: Bogdan Feraru --- mapstruct/pom.xml | 6 +-- .../mappingCollections/dto/CompanyDTO.java | 25 +++++++++ .../mappingCollections/dto/EmployeeDTO.java | 23 +++++++++ .../dto/EmployeeFullNameDTO.java | 14 +++++ .../mapper/CompanyMapper.java | 11 ++++ .../mapper/CompanyMapperAdderPreferred.java | 12 +++++ .../mapper/EmployeeFullNameMapper.java | 20 ++++++++ .../mapper/EmployeeMapper.java | 19 +++++++ .../mappingCollections/model/Company.java | 16 ++++++ .../mappingCollections/model/Employee.java | 28 ++++++++++ .../CompanyMapperAdderPreferredUnitTest.java | 32 ++++++++++++ .../mapper/CompanyMapperUnitTest.java | 32 ++++++++++++ .../EmployeeFullNameMapperUnitTest.java | 26 ++++++++++ .../mapper/EmployeeMapperUnitTest.java | 51 +++++++++++++++++++ 14 files changed, 312 insertions(+), 3 deletions(-) create mode 100644 mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/CompanyDTO.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/EmployeeDTO.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/EmployeeFullNameDTO.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapper.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperAdderPreferred.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeFullNameMapper.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeMapper.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/model/Company.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/model/Employee.java create mode 100644 mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperAdderPreferredUnitTest.java create mode 100644 mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperUnitTest.java create mode 100644 mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeFullNameMapperUnitTest.java create mode 100644 mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeMapperUnitTest.java diff --git a/mapstruct/pom.xml b/mapstruct/pom.xml index 11c8d9b41e..9fe6ab6485 100644 --- a/mapstruct/pom.xml +++ b/mapstruct/pom.xml @@ -16,7 +16,7 @@ org.mapstruct - mapstruct-jdk8 + mapstruct ${org.mapstruct.version} @@ -71,12 +71,12 @@ - 1.3.0.Final + 1.4.0.Beta1 4.3.4.RELEASE 1.8 1.8 1.18.4 - 3.11.1 + 3.16.1 diff --git a/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/CompanyDTO.java b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/CompanyDTO.java new file mode 100644 index 0000000000..de3571e676 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/CompanyDTO.java @@ -0,0 +1,25 @@ +package com.baeldung.mapstruct.mappingCollections.dto; + +import java.util.ArrayList; +import java.util.List; + +public class CompanyDTO { + + private List employees; + + public List getEmployees() { + return employees; + } + + public void setEmployees(List employees) { + this.employees = employees; + } + + public void addEmployee(EmployeeDTO employeeDTO) { + if (employees == null) { + employees = new ArrayList<>(); + } + + employees.add(employeeDTO); + } +} diff --git a/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/EmployeeDTO.java b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/EmployeeDTO.java new file mode 100644 index 0000000000..04e5a1f180 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/EmployeeDTO.java @@ -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; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/EmployeeFullNameDTO.java b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/EmployeeFullNameDTO.java new file mode 100644 index 0000000000..10b97bfce4 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/dto/EmployeeFullNameDTO.java @@ -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; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapper.java b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapper.java new file mode 100644 index 0000000000..5310c0454e --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapper.java @@ -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); +} diff --git a/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperAdderPreferred.java b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperAdderPreferred.java new file mode 100644 index 0000000000..e5cc43074e --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperAdderPreferred.java @@ -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); +} diff --git a/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeFullNameMapper.java b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeFullNameMapper.java new file mode 100644 index 0000000000..44d3961f2b --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeFullNameMapper.java @@ -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 map(List employees); + + default EmployeeFullNameDTO map(Employee employee) { + EmployeeFullNameDTO employeeInfoDTO = new EmployeeFullNameDTO(); + employeeInfoDTO.setFullName(employee.getFirstName() + " " + employee.getLastName()); + + return employeeInfoDTO; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeMapper.java b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeMapper.java new file mode 100644 index 0000000000..45bf76c5a4 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeMapper.java @@ -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 map(List employees); + + Set map(Set employees); + + Map map(Map idEmployeeMap); +} diff --git a/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/model/Company.java b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/model/Company.java new file mode 100644 index 0000000000..f96e0b7aea --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/model/Company.java @@ -0,0 +1,16 @@ +package com.baeldung.mapstruct.mappingCollections.model; + +import java.util.List; + +public class Company { + + private List employees; + + public List getEmployees() { + return employees; + } + + public void setEmployees(List employees) { + this.employees = employees; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/model/Employee.java b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/model/Employee.java new file mode 100644 index 0000000000..db6809d53f --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapstruct/mappingCollections/model/Employee.java @@ -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; + } +} diff --git a/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperAdderPreferredUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperAdderPreferredUnitTest.java new file mode 100644 index 0000000000..ad0096f189 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperAdderPreferredUnitTest.java @@ -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 employees = result.getEmployees(); + assertThat(employees).hasSize(1); + assertThat(employees.get(0).getFirstName()).isEqualTo("John"); + assertThat(employees.get(0).getLastName()).isEqualTo("Doe"); + } +} \ No newline at end of file diff --git a/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperUnitTest.java new file mode 100644 index 0000000000..a556c88ce5 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/CompanyMapperUnitTest.java @@ -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 employees = result.getEmployees(); + assertThat(employees).hasSize(1); + assertThat(employees.get(0).getFirstName()).isEqualTo("John"); + assertThat(employees.get(0).getLastName()).isEqualTo("Doe"); + } +} \ No newline at end of file diff --git a/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeFullNameMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeFullNameMapperUnitTest.java new file mode 100644 index 0000000000..ba010a2bca --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeFullNameMapperUnitTest.java @@ -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 result = employeeMapper.map(Collections.singletonList(employee)); + + assertThat(result).hasSize(1); + assertThat(result.get(0).getFullName()).isEqualTo("John Doe"); + } +} \ No newline at end of file diff --git a/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeMapperUnitTest.java new file mode 100644 index 0000000000..dfb58252dc --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapstruct/mappingCollections/mapper/EmployeeMapperUnitTest.java @@ -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 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 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 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"); + } +} \ No newline at end of file