diff --git a/mapstruct/bin/pom.xml b/mapstruct/bin/pom.xml new file mode 100644 index 0000000000..8a28ae9511 --- /dev/null +++ b/mapstruct/bin/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + mapstruct + mapstruct + com.baeldung + 1.0 + jar + + + 1.0.0.Final + + + + org.mapstruct + mapstruct-jdk8 + ${org.mapstruct.version} + + + junit + junit + 4.12 + test + + + + mapstruct + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + org.mapstruct + mapstruct-processor + ${org.mapstruct.version} + + + + + + + diff --git a/mapstruct/pom.xml b/mapstruct/pom.xml index 8a28ae9511..4159ef35c9 100644 --- a/mapstruct/pom.xml +++ b/mapstruct/pom.xml @@ -24,6 +24,17 @@ 4.12 test + + org.springframework + spring-context + 4.3.2.RELEASE + + + org.springframework + spring-test + 4.3.2.RELEASE + test + mapstruct diff --git a/mapstruct/src/main/java/org/baeldung/dto/DivisionDTO.java b/mapstruct/src/main/java/org/baeldung/dto/DivisionDTO.java new file mode 100644 index 0000000000..01a5792176 --- /dev/null +++ b/mapstruct/src/main/java/org/baeldung/dto/DivisionDTO.java @@ -0,0 +1,33 @@ +package org.baeldung.dto; + +public class DivisionDTO { + + public DivisionDTO() { + } + + public DivisionDTO(int id, String name) { + super(); + this.id = id; + this.name = name; + } + + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/mapstruct/src/main/java/org/baeldung/dto/EmployeeDTO.java b/mapstruct/src/main/java/org/baeldung/dto/EmployeeDTO.java index 67b9ffc024..24d6950cab 100644 --- a/mapstruct/src/main/java/org/baeldung/dto/EmployeeDTO.java +++ b/mapstruct/src/main/java/org/baeldung/dto/EmployeeDTO.java @@ -2,41 +2,41 @@ package org.baeldung.dto; public class EmployeeDTO { - private int employeeId; - private String employeeName; - private int divisionId; - private String divisionName; + private int employeeId; + private String employeeName; + private DivisionDTO division; + private String employeeStartDt; - public int getEmployeeId() { - return employeeId; - } + public int getEmployeeId() { + return employeeId; + } - public void setEmployeeId(int employeeId) { - this.employeeId = employeeId; - } + public void setEmployeeId(int employeeId) { + this.employeeId = employeeId; + } - public String getEmployeeName() { - return employeeName; - } + public String getEmployeeName() { + return employeeName; + } - public void setEmployeeName(String employeeName) { - this.employeeName = employeeName; - } + public void setEmployeeName(String employeeName) { + this.employeeName = employeeName; + } - public int getDivisionId() { - return divisionId; - } + public DivisionDTO getDivision() { + return division; + } - public void setDivisionId(int divisionId) { - this.divisionId = divisionId; - } + public void setDivision(DivisionDTO division) { + this.division = division; + } - public String getDivisionName() { - return divisionName; - } + public String getEmployeeStartDt() { + return employeeStartDt; + } - public void setDivisionName(String divisionName) { - this.divisionName = divisionName; - } + public void setEmployeeStartDt(String employeeStartDt) { + this.employeeStartDt = employeeStartDt; + } } diff --git a/mapstruct/src/main/java/org/baeldung/dto/SimpleSource.java b/mapstruct/src/main/java/org/baeldung/dto/SimpleSource.java index fc0c75dea3..4c169461c8 100644 --- a/mapstruct/src/main/java/org/baeldung/dto/SimpleSource.java +++ b/mapstruct/src/main/java/org/baeldung/dto/SimpleSource.java @@ -2,23 +2,23 @@ package org.baeldung.dto; public class SimpleSource { - private String name; - private String description; - - public String getName() { - return name; - } + private String name; + private String description; - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } - public String getDescription() { - return description; - } + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } - public void setDescription(String description) { - this.description = description; - } - } diff --git a/mapstruct/src/main/java/org/baeldung/entity/Division.java b/mapstruct/src/main/java/org/baeldung/entity/Division.java index 04f1ab2c23..83b0916eb4 100644 --- a/mapstruct/src/main/java/org/baeldung/entity/Division.java +++ b/mapstruct/src/main/java/org/baeldung/entity/Division.java @@ -2,32 +2,32 @@ package org.baeldung.entity; public class Division { - public Division() { - } + public Division() { + } - public Division(int id, String name) { - super(); - this.id = id; - this.name = name; - } + public Division(int id, String name) { + super(); + this.id = id; + this.name = name; + } - private int id; - private String name; + private int id; + private String name; - public int getId() { - return id; - } + public int getId() { + return id; + } - public void setId(int id) { - this.id = id; - } + public void setId(int id) { + this.id = id; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } } diff --git a/mapstruct/src/main/java/org/baeldung/entity/Employee.java b/mapstruct/src/main/java/org/baeldung/entity/Employee.java index 55599a17f9..8c441813b6 100644 --- a/mapstruct/src/main/java/org/baeldung/entity/Employee.java +++ b/mapstruct/src/main/java/org/baeldung/entity/Employee.java @@ -1,33 +1,44 @@ package org.baeldung.entity; +import java.util.Date; + public class Employee { - private int id; - private String name; - private Division division; + private int id; + private String name; + private Division division; + private Date startDt; - public int getId() { - return id; - } + public int getId() { + return id; + } - public void setId(int id) { - this.id = id; - } + public void setId(int id) { + this.id = id; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } - public Division getDivision() { - return division; - } + public Division getDivision() { + return division; + } - public void setDivision(Division division) { - this.division = division; - } + public void setDivision(Division division) { + this.division = division; + } + + public Date getStartDt() { + return startDt; + } + + public void setStartDt(Date startDt) { + this.startDt = startDt; + } } diff --git a/mapstruct/src/main/java/org/baeldung/entity/SimpleDestination.java b/mapstruct/src/main/java/org/baeldung/entity/SimpleDestination.java index 901257c11b..9fdbd7f054 100644 --- a/mapstruct/src/main/java/org/baeldung/entity/SimpleDestination.java +++ b/mapstruct/src/main/java/org/baeldung/entity/SimpleDestination.java @@ -2,23 +2,23 @@ package org.baeldung.entity; public class SimpleDestination { - private String name; - private String description; - - public String getName() { - return name; - } + private String name; + private String description; - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } - public String getDescription() { - return description; - } + public void setName(String name) { + this.name = name; + } - public void setDescription(String description) { - this.description = description; - } + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } } diff --git a/mapstruct/src/main/java/org/baeldung/mapper/EmployeeMapper.java b/mapstruct/src/main/java/org/baeldung/mapper/EmployeeMapper.java index 28faffec45..013c332e6e 100644 --- a/mapstruct/src/main/java/org/baeldung/mapper/EmployeeMapper.java +++ b/mapstruct/src/main/java/org/baeldung/mapper/EmployeeMapper.java @@ -1,6 +1,10 @@ package org.baeldung.mapper; +import java.util.List; + +import org.baeldung.dto.DivisionDTO; import org.baeldung.dto.EmployeeDTO; +import org.baeldung.entity.Division; import org.baeldung.entity.Employee; import org.mapstruct.Mapper; import org.mapstruct.Mapping; @@ -9,19 +13,18 @@ import org.mapstruct.Mappings; @Mapper public interface EmployeeMapper { - @Mappings({ - @Mapping(target="divisionId",source="entity.division.id"), - @Mapping(target="divisionName",source="entity.division.name"), - @Mapping(target="employeeId",source="entity.id"), - @Mapping(target="employeeName",source="entity.name") - }) - EmployeeDTO employeeToEmployeeDTO(Employee entity); - - @Mappings({ - @Mapping(target="id",source="dto.employeeId"), - @Mapping(target="name",source="dto.employeeName"), - @Mapping(target="division",expression="java(new org.baeldung.entity.Division(dto.getDivisionId(),dto.getDivisionName()))") - }) - Employee employeeDTOtoEmployee(EmployeeDTO dto); - + @Mappings({ @Mapping(target = "employeeId", source = "entity.id"), @Mapping(target = "employeeName", source = "entity.name"), @Mapping(target = "employeeStartDt", source = "entity.startDt", dateFormat = "dd-MM-yyyy HH:mm:ss") }) + EmployeeDTO employeeToEmployeeDTO(Employee entity); + + @Mappings({ @Mapping(target = "id", source = "dto.employeeId"), @Mapping(target = "name", source = "dto.employeeName"), @Mapping(target = "startDt", source = "dto.employeeStartDt", dateFormat = "dd-MM-yyyy HH:mm:ss") }) + Employee employeeDTOtoEmployee(EmployeeDTO dto); + + DivisionDTO divisionToDivisionDTO(Division entity); + + Division divisionDTOtoDivision(DivisionDTO dto); + + List convertEmployeeDTOListToEmployeeList(List list); + + List convertEmployeeListToEmployeeDTOList(List list); + } diff --git a/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationMapper.java b/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationMapper.java index f3210288d2..3e872e68a3 100644 --- a/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationMapper.java +++ b/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationMapper.java @@ -4,10 +4,11 @@ import org.baeldung.dto.SimpleSource; import org.baeldung.entity.SimpleDestination; import org.mapstruct.Mapper; -@Mapper +@Mapper(componentModel = "spring") public interface SimpleSourceDestinationMapper { - SimpleDestination sourceToDestination(SimpleSource source); + SimpleDestination sourceToDestination(SimpleSource source); + SimpleSource destinationToSource(SimpleDestination destination); - + } diff --git a/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationSpringMapper.java b/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationSpringMapper.java deleted file mode 100644 index 2077eabf51..0000000000 --- a/mapstruct/src/main/java/org/baeldung/mapper/SimpleSourceDestinationSpringMapper.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.baeldung.mapper; - -import org.baeldung.dto.SimpleSource; -import org.baeldung.entity.SimpleDestination; -import org.mapstruct.Mapper; - -@Mapper(componentModel="spring") -public interface SimpleSourceDestinationSpringMapper { - - SimpleDestination sourceToDestination(SimpleSource source); - SimpleSource destinationToSource(SimpleDestination destination); - -} diff --git a/mapstruct/src/main/resources/applicationContext.xml b/mapstruct/src/main/resources/applicationContext.xml new file mode 100644 index 0000000000..1e6649139c --- /dev/null +++ b/mapstruct/src/main/resources/applicationContext.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/mapstruct/src/test/java/org/baeldung/mapper/EmployeeMapperTest.java b/mapstruct/src/test/java/org/baeldung/mapper/EmployeeMapperTest.java index 0433013f2e..c5998f89ef 100644 --- a/mapstruct/src/test/java/org/baeldung/mapper/EmployeeMapperTest.java +++ b/mapstruct/src/test/java/org/baeldung/mapper/EmployeeMapperTest.java @@ -1,7 +1,14 @@ package org.baeldung.mapper; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.baeldung.dto.DivisionDTO; import org.baeldung.dto.EmployeeDTO; import org.baeldung.entity.Division; import org.baeldung.entity.Employee; @@ -9,40 +16,108 @@ import org.junit.Test; import org.mapstruct.factory.Mappers; public class EmployeeMapperTest { - - @Test - public void givenEmployeeDTOtoEmployee_whenMaps_thenCorrect(){ - EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class); - - EmployeeDTO dto = new EmployeeDTO(); - dto.setDivisionId(1); - dto.setDivisionName("IT Division"); - dto.setEmployeeId(1); - dto.setEmployeeName("John"); - - Employee entity = mapper.employeeDTOtoEmployee(dto); - - assertEquals(dto.getDivisionId(), entity.getDivision().getId()); - assertEquals(dto.getDivisionName(), entity.getDivision().getName()); - assertEquals(dto.getEmployeeId(),entity.getId()); - assertEquals(dto.getEmployeeName(),entity.getName()); - } - - @Test - public void givenEmployeetoEmployeeDTO_whenMaps_thenCorrect(){ - EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class); - - Employee entity = new Employee(); - entity.setDivision(new Division(1,"IT Division")); - entity.setId(1); - entity.setName("John"); - - EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity); - - assertEquals(dto.getDivisionId(), entity.getDivision().getId()); - assertEquals(dto.getDivisionName(), entity.getDivision().getName()); - assertEquals(dto.getEmployeeId(),entity.getId()); - assertEquals(dto.getEmployeeName(),entity.getName()); - } - + + EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class); + + private static final String DATE_FORMAT = "dd-MM-yyyy HH:mm:ss"; + + @Test + public void givenEmployeeDTOwithDiffNametoEmployee_whenMaps_thenCorrect() { + EmployeeDTO dto = new EmployeeDTO(); + dto.setEmployeeId(1); + dto.setEmployeeName("John"); + + Employee entity = mapper.employeeDTOtoEmployee(dto); + + assertEquals(dto.getEmployeeId(), entity.getId()); + assertEquals(dto.getEmployeeName(), entity.getName()); + } + + @Test + public void givenEmployeewithDiffNametoEmployeeDTO_whenMaps_thenCorrect() { + Employee entity = new Employee(); + entity.setId(1); + entity.setName("John"); + + EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity); + + assertEquals(dto.getEmployeeId(), entity.getId()); + assertEquals(dto.getEmployeeName(), entity.getName()); + } + + @Test + public void givenEmployeeDTOwithNestedMappingToEmployee_whenMaps_thenCorrect() { + EmployeeDTO dto = new EmployeeDTO(); + dto.setDivision(new DivisionDTO(1, "Division1")); + + Employee entity = mapper.employeeDTOtoEmployee(dto); + + assertEquals(dto.getDivision().getId(), entity.getDivision().getId()); + assertEquals(dto.getDivision().getName(), entity.getDivision().getName()); + } + + @Test + public void givenEmployeeWithNestedMappingToEmployeeDTO_whenMaps_thenCorrect() { + Employee entity = new Employee(); + entity.setDivision(new Division(1, "Division1")); + + EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity); + + assertEquals(dto.getDivision().getId(), entity.getDivision().getId()); + assertEquals(dto.getDivision().getName(), entity.getDivision().getName()); + } + + @Test + public void givenEmployeeListToEmployeeDTOList_whenMaps_thenCorrect() { + List employeeList = new ArrayList<>(); + Employee emp = new Employee(); + emp.setId(1); + emp.setName("EmpName"); + emp.setDivision(new Division(1, "Division1")); + employeeList.add(emp); + + List employeeDtoList = mapper.convertEmployeeListToEmployeeDTOList(employeeList); + EmployeeDTO employeeDTO = employeeDtoList.get(0); + assertEquals(employeeDTO.getEmployeeId(), emp.getId()); + assertEquals(employeeDTO.getEmployeeName(), emp.getName()); + assertEquals(employeeDTO.getDivision().getId(), emp.getDivision().getId()); + assertEquals(employeeDTO.getDivision().getName(), emp.getDivision().getName()); + } + + @Test + public void givenEmployeeDTOListToEmployeeList_whenMaps_thenCorrect() { + List employeeDTOList = new ArrayList<>(); + EmployeeDTO empDTO = new EmployeeDTO(); + empDTO.setEmployeeId(1); + empDTO.setEmployeeName("EmpName"); + empDTO.setDivision(new DivisionDTO(1, "Division1")); + employeeDTOList.add(empDTO); + + List employeeList = mapper.convertEmployeeDTOListToEmployeeList(employeeDTOList); + Employee employee = employeeList.get(0); + assertEquals(employee.getId(), empDTO.getEmployeeId()); + assertEquals(employee.getName(), empDTO.getEmployeeName()); + assertEquals(employee.getDivision().getId(), empDTO.getDivision().getId()); + assertEquals(employee.getDivision().getName(), empDTO.getDivision().getName()); + } + + @Test + public void givenEmployeeWithStartDateMappingToEmployeeDTO_whenMaps_thenCorrect() throws ParseException { + Employee entity = new Employee(); + entity.setStartDt(new Date()); + + EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity); + SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT); + assertEquals(format.parse(dto.getEmployeeStartDt()).toString(), entity.getStartDt().toString()); + } + + @Test + public void givenEmployeeDTOWithStartDateMappingToEmployee_whenMaps_thenCorrect() throws ParseException { + EmployeeDTO dto = new EmployeeDTO(); + dto.setEmployeeStartDt("01-04-2016 01:00:00"); + + Employee entity = mapper.employeeDTOtoEmployee(dto); + SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT); + assertEquals(format.parse(dto.getEmployeeStartDt()).toString(), entity.getStartDt().toString()); + } } diff --git a/mapstruct/src/test/java/org/baeldung/mapper/SimpleSourceDestinationMapperTest.java b/mapstruct/src/test/java/org/baeldung/mapper/SimpleSourceDestinationMapperTest.java index a7e3b05dc6..226603b16a 100644 --- a/mapstruct/src/test/java/org/baeldung/mapper/SimpleSourceDestinationMapperTest.java +++ b/mapstruct/src/test/java/org/baeldung/mapper/SimpleSourceDestinationMapperTest.java @@ -1,44 +1,44 @@ package org.baeldung.mapper; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import org.baeldung.dto.SimpleSource; import org.baeldung.entity.SimpleDestination; import org.junit.Test; -import org.mapstruct.factory.Mappers; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:applicationContext.xml") public class SimpleSourceDestinationMapperTest { - @Test - public void givenSimpleSourceToSimpleDestination_whenMaps_thenCorrect() { - SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers - .getMapper(SimpleSourceDestinationMapper.class); - - SimpleSource simpleSource = new SimpleSource(); - simpleSource.setName("SourceName"); - simpleSource.setDescription("SourceDescription"); - - SimpleDestination destination = - simpleSourceDestinationMapper.sourceToDestination(simpleSource); - - assertEquals(simpleSource.getName(), destination.getName()); - assertEquals(simpleSource.getDescription(), destination.getDescription()); - } - - @Test - public void givenSimpleDestinationToSourceDestination_whenMaps_thenCorrect() { - SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers - .getMapper(SimpleSourceDestinationMapper.class); - - SimpleDestination destination = new SimpleDestination(); - destination.setName("DestinationName"); - destination.setDescription("DestinationDescription"); - - SimpleSource source = - simpleSourceDestinationMapper.destinationToSource(destination); - - assertEquals(destination.getName(), source.getName()); - assertEquals(destination.getDescription(), source.getDescription()); - } + @Autowired + SimpleSourceDestinationMapper simpleSourceDestinationMapper; + + @Test + public void givenSimpleSourceToSimpleDestination_whenMaps_thenCorrect() { + SimpleSource simpleSource = new SimpleSource(); + simpleSource.setName("SourceName"); + simpleSource.setDescription("SourceDescription"); + + SimpleDestination destination = simpleSourceDestinationMapper.sourceToDestination(simpleSource); + + assertEquals(simpleSource.getName(), destination.getName()); + assertEquals(simpleSource.getDescription(), destination.getDescription()); + } + + @Test + public void givenSimpleDestinationToSourceDestination_whenMaps_thenCorrect() { + SimpleDestination destination = new SimpleDestination(); + destination.setName("DestinationName"); + destination.setDescription("DestinationDescription"); + + SimpleSource source = simpleSourceDestinationMapper.destinationToSource(destination); + + assertEquals(destination.getName(), source.getName()); + assertEquals(destination.getDescription(), source.getDescription()); + } }