added code for mapstruct tutorial

This commit is contained in:
jesus-dayo 2016-08-19 10:25:27 +08:00
parent 55ea01d4bd
commit f0947ae8fb
8 changed files with 108 additions and 76 deletions

View File

@ -5,6 +5,7 @@ public class EmployeeDTO {
private int employeeId;
private String employeeName;
private DivisionDTO division;
private String employeeStartDt;
public int getEmployeeId() {
return employeeId;
@ -30,4 +31,12 @@ public class EmployeeDTO {
this.division = division;
}
public String getEmployeeStartDt() {
return employeeStartDt;
}
public void setEmployeeStartDt(String employeeStartDt) {
this.employeeStartDt = employeeStartDt;
}
}

View File

@ -1,10 +1,13 @@
package org.baeldung.entity;
import java.util.Date;
public class Employee {
private int id;
private String name;
private Division division;
private Date startDt;
public int getId() {
return id;
@ -30,4 +33,12 @@ public class Employee {
this.division = division;
}
public Date getStartDt() {
return startDt;
}
public void setStartDt(Date startDt) {
this.startDt = startDt;
}
}

View File

@ -1,5 +1,7 @@
package org.baeldung.mapper;
import java.util.List;
import org.baeldung.dto.DivisionDTO;
import org.baeldung.dto.EmployeeDTO;
import org.baeldung.entity.Division;
@ -11,14 +13,22 @@ import org.mapstruct.Mappings;
@Mapper
public interface EmployeeMapper {
@Mappings({ @Mapping(target = "employeeId", source = "entity.id"), @Mapping(target = "employeeName", source = "entity.name") })
@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") })
@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<Employee> convertEmployeeDTOListToEmployeeList(List<EmployeeDTO> list);
List<EmployeeDTO> convertEmployeeListToEmployeeDTOList(List<Employee> list);
}

View File

@ -4,7 +4,7 @@ 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);

View File

@ -1,14 +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);
}

View File

@ -2,6 +2,12 @@ package org.baeldung.mapper;
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;
@ -11,10 +17,12 @@ import org.mapstruct.factory.Mappers;
public class EmployeeMapperTest {
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
private static final String DATE_FORMAT = "dd-MM-yyyy HH:mm:ss";
@Test
public void givenEmployeeDTOwithDiffNametoEmployee_whenMaps_thenCorrect() {
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
EmployeeDTO dto = new EmployeeDTO();
dto.setEmployeeId(1);
dto.setEmployeeName("John");
@ -27,8 +35,6 @@ public class EmployeeMapperTest {
@Test
public void givenEmployeewithDiffNametoEmployeeDTO_whenMaps_thenCorrect() {
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
Employee entity = new Employee();
entity.setId(1);
entity.setName("John");
@ -41,8 +47,6 @@ public class EmployeeMapperTest {
@Test
public void givenEmployeeDTOwithNestedMappingToEmployee_whenMaps_thenCorrect() {
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
EmployeeDTO dto = new EmployeeDTO();
dto.setDivision(new DivisionDTO(1, "Division1"));
@ -54,8 +58,6 @@ public class EmployeeMapperTest {
@Test
public void givenEmployeeWithNestedMappingToEmployeeDTO_whenMaps_thenCorrect() {
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
Employee entity = new Employee();
entity.setDivision(new Division(1, "Division1"));
@ -65,4 +67,59 @@ public class EmployeeMapperTest {
assertEquals(dto.getDivision().getName(), entity.getDivision().getName());
}
@Test
public void givenEmployeeListToEmployeeDTOList_whenMaps_thenCorrect() {
List<Employee> employeeList = new ArrayList<>();
Employee emp = new Employee();
emp.setId(1);
emp.setName("EmpName");
emp.setDivision(new Division(1, "Division1"));
employeeList.add(emp);
List<EmployeeDTO> 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<EmployeeDTO> employeeDTOList = new ArrayList<>();
EmployeeDTO empDTO = new EmployeeDTO();
empDTO.setEmployeeId(1);
empDTO.setEmployeeName("EmpName");
empDTO.setDivision(new DivisionDTO(1, "Division1"));
employeeDTOList.add(empDTO);
List<Employee> 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());
}
}

View File

@ -1,18 +1,24 @@
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 {
@Autowired
SimpleSourceDestinationMapper simpleSourceDestinationMapper;
@Test
public void givenSimpleSourceToSimpleDestination_whenMaps_thenCorrect() {
SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers.getMapper(SimpleSourceDestinationMapper.class);
SimpleSource simpleSource = new SimpleSource();
simpleSource.setName("SourceName");
simpleSource.setDescription("SourceDescription");
@ -25,8 +31,6 @@ public class SimpleSourceDestinationMapperTest {
@Test
public void givenSimpleDestinationToSourceDestination_whenMaps_thenCorrect() {
SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers.getMapper(SimpleSourceDestinationMapper.class);
SimpleDestination destination = new SimpleDestination();
destination.setName("DestinationName");
destination.setDescription("DestinationDescription");

View File

@ -1,45 +0,0 @@
package org.baeldung.mapper;
import static org.junit.Assert.assertEquals;
import org.baeldung.dto.SimpleSource;
import org.baeldung.entity.SimpleDestination;
import org.junit.Test;
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 SimpleSourceDestinationSpringMapperTest {
@Autowired
private SimpleSourceDestinationSpringMapper simpleSourceDestinationSpringMapper;
@Test
public void givenSimpleSourceToSimpleDestination_whenMaps_thenCorrect() {
SimpleSource simpleSource = new SimpleSource();
simpleSource.setName("SourceName");
simpleSource.setDescription("SourceDescription");
SimpleDestination destination = simpleSourceDestinationSpringMapper.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 = simpleSourceDestinationSpringMapper.destinationToSource(destination);
assertEquals(destination.getName(), source.getName());
assertEquals(destination.getDescription(), source.getDescription());
}
}