modified nested mapping section
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
package org.baeldung.mapper;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.baeldung.dto.DivisionDTO;
|
||||
import org.baeldung.dto.EmployeeDTO;
|
||||
import org.baeldung.entity.Division;
|
||||
import org.baeldung.entity.Employee;
|
||||
@@ -9,40 +10,59 @@ 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());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenEmployeeDTOwithDiffNametoEmployee_whenMaps_thenCorrect() {
|
||||
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
|
||||
|
||||
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() {
|
||||
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
|
||||
|
||||
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() {
|
||||
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
|
||||
|
||||
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() {
|
||||
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,36 +9,32 @@ import org.mapstruct.factory.Mappers;
|
||||
|
||||
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());
|
||||
}
|
||||
@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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user