jueves, 26 de mayo de 2022

MapStruct walking to map between java beans 2


MapStruct walking to map between java beans 2

https://github.com/jalbertomr/mapStructDemo

Employee and Division and his respective figurative DTOs, check the divergency in employee name, the diferent type of the dates.


Convertion between dates are required and is done by DateConverter class.


@Component
public class DateConverter {
private final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private final DateTimeFormatter DATETIMEFORMATER = DateTimeFormatter.ofPattern(DATETIME_FORMAT);

public Date strDateTimeToDate(String strDateTime) throws ParseException {
return new SimpleDateFormat(DATETIME_FORMAT).parse(strDateTime);
};

public String dateToStrDateTime(Date date){ return new SimpleDateFormat( DATETIME_FORMAT).format( date);
}

public LocalDateTime stringAsLocalDateTime(String str) {
return LocalDateTime.parse( str, DATETIMEFORMATER);
}

public String localDateTimeAsString( LocalDateTime dateTime) {
return dateTime.format(DATETIMEFORMATER);
}

public Date localDateTimeToDate(LocalDateTime localDateTime){
return //java.util.Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
java.sql.Timestamp.valueOf(localDateTime);
}

public LocalDateTime dateToLocalDateTime( Date date){
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}

}

The EmployeeMapper declare the use of DateConverter and also uses Collections


@Mapper(uses= DateConverter.class)
public interface EmployeeMapper {
EmployeeMapper INSTANCE = Mappers.getMapper(EmployeeMapper.class);

@Mapping(source = "id", target = "employeeId")
@Mapping(target = "employeeName", expression = "java(employee.getFirstName() + \" \" + employee.getLastName() )")
@Mapping(source = "startDate", target = "employeeStartDate", dateFormat = "YYYY-mm-dd HH:mm:ss")
@Mapping(source = "employee.division.id", target="divisionDto.id")
EmployeeDto employeeToDto(Employee employee);

@Mapping(source = "employeeId", target = "id")
@Mapping(target = "firstName", expression = "java(employeeDto.getEmployeeName() )")
@Mapping(target = "lastName", expression = "java(employeeDto.getEmployeeName() )")
@Mapping(source = "employeeStartDate", target = "startDate", dateFormat = "YYYY-mm-dd HH:mm:ss")
@Mapping(source="employeeDto.divisionDto.id", target="division.id")
@Mapping(source="employeeDto.divisionDto.name", target="division.name")
Employee employeeDtoToEmployee(EmployeeDto employeeDto);

List<EmployeeDto> employeesListToDto(List<Employee> employees);

Set<EmployeeDto> employeesSetToDto(Set<Employee> employees);

Map<String, EmployeeDto> employeesMapToDto(Map<String, Employee> employee);
}

The Code Generated shows the details on the conversions.


@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2022-05-26T19:07:17-0500",
comments = "version: 1.4.2.Final, compiler: javac, environment: Java 11.0.8 (Oracle Corporation)"
)
public class EmployeeMapperImpl implements EmployeeMapper {

private final DateConverter dateConverter = new DateConverter();

@Override
public EmployeeDto employeeToDto(Employee employee) {
if ( employee == null ) {
return null;
}

EmployeeDto employeeDto = new EmployeeDto();

employeeDto.setDivisionDto( divisionToDivisionDto( employee.getDivision() ) );
employeeDto.setEmployeeId( employee.getId() );
employeeDto.setEmployeeStartDate( dateConverter.dateToStrDateTime( employee.getStartDate() ) );

employeeDto.setEmployeeName( employee.getFirstName() + " " + employee.getLastName() );

return employeeDto;
}

@Override
public Employee employeeDtoToEmployee(EmployeeDto employeeDto) {
if ( employeeDto == null ) {
return null;
}

Employee employee = new Employee();

employee.setDivision( divisionDtoToDivision( employeeDto.getDivisionDto() ) );
employee.setId( employeeDto.getEmployeeId() );
try {
employee.setStartDate( dateConverter.strDateTimeToDate( employeeDto.getEmployeeStartDate() ) );
}
catch ( ParseException e ) {
throw new RuntimeException( e );
}

employee.setFirstName( employeeDto.getEmployeeName() );
employee.setLastName( employeeDto.getEmployeeName() );

return employee;
}

@Override
public List<EmployeeDto> employeesListToDto(List<Employee> employees) {
if ( employees == null ) {
return null;
}

List<EmployeeDto> list = new ArrayList<EmployeeDto>( employees.size() );
for ( Employee employee : employees ) {
list.add( employeeToDto( employee ) );
}

return list;
}

@Override
public Set<EmployeeDto> employeesSetToDto(Set<Employee> employees) {
if ( employees == null ) {
return null;
}

Set<EmployeeDto> set = new HashSet<EmployeeDto>( Math.max( (int) ( employees.size() / .75f ) + 1, 16 ) );
for ( Employee employee : employees ) {
set.add( employeeToDto( employee ) );
}

return set;
}

@Override
public Map<String, EmployeeDto> employeesMapToDto(Map<String, Employee> employee) {
if ( employee == null ) {
return null;
}

Map<String, EmployeeDto> map = new HashMap<String, EmployeeDto>( Math.max( (int) ( employee.size() / .75f ) + 1, 16 ) );

for ( java.util.Map.Entry<String, Employee> entry : employee.entrySet() ) {
String key = entry.getKey();
EmployeeDto value = employeeToDto( entry.getValue() );
map.put( key, value );
}

return map;
}

protected DivisionDto divisionToDivisionDto(Division division) {
if ( division == null ) {
return null;
}

DivisionDto divisionDto = new DivisionDto();

divisionDto.setId( division.getId() );
divisionDto.setName( division.getName() );

return divisionDto;
}

protected Division divisionDtoToDivision(DivisionDto divisionDto) {
if ( divisionDto == null ) {
return null;
}

Division division = new Division();

division.setId( divisionDto.getId() );
division.setName( divisionDto.getName() );

return division;
}
}

The @Tests

@Slf4j
class EmployeeMapperTest {

@Test //Employee -> EmployeeDto
void mapEmployeeTest() throws ParseException {
//given
Division division = new Division(1, "North");
Date startDate = new SimpleDateFormat("YYYY-mm-dd HH:mm:ss").parse("2022-06-01 09:00:05");
Employee employee = new Employee( 23, "Jose Alberto", "Martinez", division, startDate);
//when
EmployeeDto employeeDto = EmployeeMapper.INSTANCE.employeeToDto(employee);
log.info("employee: {}", employee);
log.info("employeeDto: {}", employeeDto);
//then
Assertions.assertEquals(23, employeeDto.getEmployeeId());
Assertions.assertEquals( employee.getFirstName() + " " + employee.getLastName(), employeeDto.getEmployeeName());
Assertions.assertEquals( division.getId(), employeeDto.getDivisionDto().getId());
Assertions.assertEquals( division.getName(), employeeDto.getDivisionDto().getName());
}

@Test
void employeeDtoToEmployeeTest() {
//given
DivisionDto divisionDto = new DivisionDto(3,"PACIFIC","WESTERN");
EmployeeDto employeeDto = new EmployeeDto(102, "Jose Alberto Martinez", divisionDto, "2022-06-01 09:10:05");
Employee employee = EmployeeMapper.INSTANCE.employeeDtoToEmployee(employeeDto);
//then
log.info("employeeDto: {}", employeeDto);
log.info("employee: [{}]", employee);
Assertions.assertEquals(employee.getId(), employeeDto.getEmployeeId());
Assertions.assertEquals(employee.getFirstName(), employeeDto.getEmployeeName());
Assertions.assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(employee.getStartDate()), employeeDto.getEmployeeStartDate());
Assertions.assertEquals(employee.getDivision().getId(),employeeDto.getDivisionDto().getId());
Assertions.assertEquals(employee.getDivision().getName(),employeeDto.getDivisionDto().getName());
}

@Test
void employeesListToDto() throws ParseException {
//given
Division division = new Division(1, "North");
Date startDate = new SimpleDateFormat("YYYY-mm-dd HH:mm:ss").parse("2022-06-01 09:00:05");
Employee employee = new Employee(33L, "Jose Alberto", "Martinez", division, startDate);
List<Employee> employees = new ArrayList<>();
employees.add(employee);
//when
List<EmployeeDto> employeesDto = EmployeeMapper.INSTANCE.employeesListToDto(employees);
//then
System.out.println(employees);
System.out.println(employeesDto);

Assertions.assertEquals(employees.get(0).getId(), employeesDto.get(0).getEmployeeId());
Assertions.assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(employees.get(0).getStartDate()),employeesDto.get(0).getEmployeeStartDate());
Assertions.assertEquals(employees.get(0).getDivision().getName(),employeesDto.get(0).getDivisionDto().getName());
//...
}

@Test
void employeesSetToDto() throws ParseException {
//given
Division division = new Division(1, "North");
Date startDate = new SimpleDateFormat("YYYY-mm-dd HH:mm:ss").parse("2022-06-01 09:00:05");
Employee employee = new Employee(33L, "Jose Alberto", "Martinez", division, startDate);
Set<Employee> employeesSet = new HashSet<>();
employeesSet.add( employee);
//when
Set<EmployeeDto> employeesDtoSet = EmployeeMapper.INSTANCE.employeesSetToDto(employeesSet);
log.info("employeesSet: {} ", employeesSet);
log.info("employeesDtoSet: {} ", employeesDtoSet);
//then
Assertions.assertEquals(employeesSet.iterator().next().getDivision().getName(),employeesDtoSet.iterator().next().getDivisionDto().getName());
//...
}

@Test
void employeesMaoToDto() throws ParseException {
//given
Division division = new Division(1, "North");
Date startDate = new SimpleDateFormat("YYYY-mm-dd HH:mm:ss").parse("2022-06-01 09:00:05");
Employee employee = new Employee(33L, "Jose Alberto", "Martinez", division, startDate);
Map<String,Employee> employeesMap = new HashMap<>();
employeesMap.put("emp1" ,employee);
//when
Map<String,EmployeeDto> employeesDtoMap = EmployeeMapper.INSTANCE.employeesMapToDto(employeesMap);
log.info("employeesMap: {} ", employeesMap);
log.info("employeesDtoMap: {} ", employeesDtoMap);
//then
Assertions.assertEquals(employeesMap.get("emp1").getDivision().getName(),employeesDtoMap.get("emp1").getDivisionDto().getName());
}
}


eot

No hay comentarios:

Publicar un comentario