/java/

Modelmapper examples

2014-02-18 23:55:39

Example 1 - String to String

package pl.btbw.mapper;

import org.junit.Test;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.CustomerEntity;

import static org.fest.assertions.Assertions.assertThat;

public class EntityToDtoMapperTest {

    private EntityToDtoMapper mapper = new EntityToDtoMapper();

    @Test
    public void mappEntityToDto() {
        // arrange
        CustomerEntity entity = getCustomerEntity();

        // act
        CustomerDto dto = mapper.asDto(entity);

        // assert
        assertThat(dto).isNotNull();
        assertThat(dto.getFirstName()).isEqualTo(entity.getFirstName());
        assertThat(dto.getLastName()).isEqualTo(entity.getLastName());
        assertThat(dto.getLogin()).isEqualTo(entity.getLogin());
        assertThat(dto.getPassword()).isEqualTo(entity.getPassword());
    }

    private CustomerEntity getCustomerEntity() {
        CustomerEntity entity = new CustomerEntity();
        entity.setFirstName("first name");
        entity.setLastName("last name");
        entity.setLogin("login");
        entity.setPassword("123");
        return entity;
    }
}

package pl.btbw.mapper;

import org.modelmapper.ModelMapper;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.CustomerEntity;

public class EntityToDtoMapper {

    public CustomerDto asDto(CustomerEntity entity) {
        return new ModelMapper().map(entity, CustomerDto.class);
    }

// without modelmapper
//    public CustomerDto asDto(CustomerEntity entity) {
//        CustomerDto dto = new CustomerDto();
//        dto.setFirstName(entity.getFirstName());
//        dto.setLastName(entity.getLastName());
//        dto.setLogin(entity.getLogin());
//        dto.setPassword(entity.getPassword());
//        return dto;
//    }
}

public class CustomerDto {

    private String firstName;
    private String lastName;
    private String login;
    private String password;

   // get & set
}

public class CustomerEntity {

    private String firstName;
    private String lastName;
    private String login;
    private String password;

    // get & set
}

Example 2 - int to Integer, Integer to int

package pl.btbw.mapper;

import org.junit.Test;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.CustomerEntity;

import static org.fest.assertions.Assertions.assertThat;

public class EntityToDtoMapperTest {

    private EntityToDtoMapper mapper = new EntityToDtoMapper();

    @Test
    public void mappEntityToDto() {
        // arrange
        CustomerEntity entity = getCustomerEntity();

        // act
        CustomerDto dto = mapper.asDto(entity);

        // assert
        assertThat(dto.getId()).isEqualTo(entity.getId());
        assertThat(dto.getAge()).isEqualTo(entity.getAge());
    }

    private CustomerEntity getCustomerEntity() {
        CustomerEntity entity = new CustomerEntity();
        entity.setId(1);
        entity.setAge(16);
        return entity;
    }
}
package pl.btbw.mapper;

import org.modelmapper.ModelMapper;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.CustomerEntity;

public class EntityToDtoMapper {

    public CustomerDto asDto(CustomerEntity entity) {
        return new ModelMapper().map(entity, CustomerDto.class);
    }

// without modelmapper
//    public CustomerDto asDto(CustomerEntity entity) {
//        CustomerDto dto = new CustomerDto();
//        dto.setId(entity.getId());
//        dto.setAge(entity.getAge());
//        return dto;
//    }
}
public class CustomerDto {

    private int id;
    private Integer age;

   // get & set
}

public class CustomerEntity {

    private Integer id;
    private int age;

    // get & set
}

Example 3 - String to Enum

package pl.btbw.mapper;

import org.junit.Test;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.CustomerEntity;
import pl.btbw.pl.btbw.util.CustomerEnum;

import static org.fest.assertions.Assertions.assertThat;

public class DtoToEntityMapperTest {

    private DtoToEntityMapper mapper = new DtoToEntityMapper();

    @Test
    public void dtoToEntity() {
        // arrange
        CustomerDto dto = new CustomerDto();
        dto.setCustomerType(CustomerEnum.TYPE_A.name());

        // act
        CustomerEntity entity = mapper.asEntity(dto);

        // assert
        assertThat(entity.getCustomerType().toString()).isEqualTo(dto.getCustomerType());
    }
}
package pl.btbw.mapper;

import org.modelmapper.ModelMapper;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.CustomerEntity;

public class DtoToEntityMapper {

    public CustomerEntity asEntity(CustomerDto dto) {
        return new ModelMapper().map(dto, CustomerEntity.class);
    }

// without modelmapper
//    public CustomerEntity asEntity(CustomerDto dto) {
//        CustomerEntity entity = new CustomerEntity();
//        entity.setCustomerType(CustomerEnum.valueOf(dto.getCustomerType()));
//        return entity;
//    }

}

public class CustomerDto {

    private String customerType;
    // get & set
}

public class CustomerEntity {

    private CustomerEnum customerType;
    // get & set
}

public enum CustomerEnum {
    TYPE_A,
    TYPE_B,
    TYPE_C
}

Example 4 - String to Integer

package pl.btbw.mapper;

import org.junit.Test;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.CustomerEntity;

import static org.fest.assertions.Assertions.assertThat;

public class DtoToEntityMapperTest {

    private DtoToEntityMapper mapper = new DtoToEntityMapper();

    @Test
    public void dtoToEntity() {
        // arrange
        CustomerDto dto = new CustomerDto();
        dto.setSomeSecretNumber("123");

        // act
        CustomerEntity entity = mapper.asEntity(dto);
        // assert
        assertThat(entity.getSomeSecretNumber()).isEqualTo(Integer.valueOf(dto.getSomeSecretNumber()));
    }
}
package pl.btbw.mapper;

import org.modelmapper.ModelMapper;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.CustomerEntity;

public class DtoToEntityMapper {

    public CustomerEntity asEntity(CustomerDto dto) {
        return new ModelMapper().map(dto, CustomerEntity.class);
    }

// without modelmapper
//    public CustomerEntity asEntity(CustomerDto dto) {
//        CustomerEntity entity = new CustomerEntity();
//        entity.setSomeSecretNumber(Integer.valueOf(dto.getSomeSecretNumber()));
//        return entity;
//    }

}
public class CustomerDto {

    private String someSecretNumber;
    // get & set
}

public class CustomerEntity {

    private Integer someSecretNumber;
    // get & set
}

Example 5 - Object to Object

package pl.btbw.mapper;

import org.junit.Test;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.AddressEntity;
import pl.btbw.entity.CustomerEntity;

import static org.fest.assertions.Assertions.assertThat;

public class DtoToEntityMapperTest {

    private DtoToEntityMapper mapper = new DtoToEntityMapper();

    @Test
    public void entityToDto() {
        // arrange
        CustomerEntity entity = new CustomerEntity();
        entity.setName("name");
        entity.setAddress(getAddress());

        // act
        CustomerDto dto = mapper.asDto(entity);
        // assert
        assertThat(entity.getName()).isEqualTo(dto.getName());
        assertThat(entity.getAddress().getCityName()).isEqualTo(dto.getAddress().getCityName());
        assertThat(entity.getAddress().getStreetName()).isEqualTo(dto.getAddress().getStreetName());
    }

    public AddressEntity getAddress() {
        AddressEntity address = new AddressEntity();
        address.setCityName("city name");
        address.setStreetName("street name");
        return address;
    }
}
package pl.btbw.mapper;

import org.modelmapper.ModelMapper;
import pl.btbw.dto.AddressDto;
import pl.btbw.dto.CustomerDto;
import pl.btbw.entity.AddressEntity;
import pl.btbw.entity.CustomerEntity;

public class DtoToEntityMapper {
    private AddressEntity address;

    public CustomerDto asDto(CustomerEntity entity) {
        return new ModelMapper().map(entity, CustomerDto.class);
    }

    // without modelmapper
//    public CustomerDto asDto(CustomerEntity entity) {
//        CustomerDto dto = new CustomerDto();
//        dto.setName(entity.getName());
//        mappAddress(entity, dto);
//        return dto;
//    }
//
//    public void mappAddress(CustomerEntity entity, CustomerDto dto) {
//        AddressDto addressDto = new AddressDto();
//        addressDto.setCityName(entity.getAddress().getCityName());
//        addressDto.setStreetName(entity.getAddress().getStreetName());
//        dto.setAddress(addressDto);
//    }

}
public class AddressDto {

    private String cityName;

    private String streetName;
    // get & set
}

public class CustomerDto {

    private String name;

    private AddressDto address;
    // get & set
}

public class AddressEntity {

    private String cityName;

    private String streetName;
    // get & set
}

public class CustomerEntity {

    private String name;

    private AddressEntity address;
    // get & set
}