[BAEL-3077] Add examples for mapstruct with multiple source objects (#7724)

* [BAEL-3077] Add examples for mapstruct with multiple source objects

* [BAEL-3077] Add unit tests for mappers added by examples
This commit is contained in:
Michael Sievers 2019-09-06 21:03:49 +02:00 committed by maibin
parent 0ca78b440d
commit 0e904f738a
8 changed files with 272 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package com.baeldung.dto;
public class CustomerDto {
private String forename;
private String surname;
public String getForename() {
return forename;
}
public CustomerDto setForename(String forename) {
this.forename = forename;
return this;
}
public String getSurname() {
return surname;
}
public CustomerDto setSurname(String surname) {
this.surname = surname;
return this;
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.entity;
public class Address {
private String street;
private String postalcode;
private String county;
public String getStreet() {
return street;
}
public Address setStreet(String street) {
this.street = street;
return this;
}
public String getPostalcode() {
return postalcode;
}
public Address setPostalcode(String postalcode) {
this.postalcode = postalcode;
return this;
}
public String getCounty() {
return county;
}
public Address setCounty(String county) {
this.county = county;
return this;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.entity;
public class Customer {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public Customer setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public String getLastName() {
return lastName;
}
public Customer setLastName(String lastName) {
this.lastName = lastName;
return this;
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.entity;
public class DeliveryAddress {
private String forename;
private String surname;
private String street;
private String postalcode;
private String county;
public String getForename() {
return forename;
}
public DeliveryAddress setForename(String forename) {
this.forename = forename;
return this;
}
public String getSurname() {
return surname;
}
public DeliveryAddress setSurname(String surname) {
this.surname = surname;
return this;
}
public String getStreet() {
return street;
}
public DeliveryAddress setStreet(String street) {
this.street = street;
return this;
}
public String getPostalcode() {
return postalcode;
}
public DeliveryAddress setPostalcode(String postalcode) {
this.postalcode = postalcode;
return this;
}
public String getCounty() {
return county;
}
public DeliveryAddress setCounty(String county) {
this.county = county;
return this;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.baeldung.dto.CustomerDto;
import com.baeldung.entity.Customer;
@Mapper
public interface CustomerDtoMapper {
@Mapping(source = "firstName", target = "forename")
@Mapping(source = "lastName", target = "surname")
CustomerDto from(Customer customer);
}

View File

@ -0,0 +1,24 @@
package com.baeldung.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import com.baeldung.entity.Address;
import com.baeldung.entity.Customer;
import com.baeldung.entity.DeliveryAddress;
@Mapper
public interface DeliveryAddressMapper {
@Mapping(source = "customer.firstName", target = "forename")
@Mapping(source = "customer.lastName", target = "surname")
@Mapping(source = "address.street", target = "street")
@Mapping(source = "address.postalcode", target = "postalcode")
@Mapping(source = "address.county", target = "county")
DeliveryAddress from(Customer customer, Address address);
@Mapping(source = "address.postalcode", target = "postalcode")
@Mapping(source = "address.county", target = "county")
DeliveryAddress updateAddress(@MappingTarget DeliveryAddress deliveryAddress, Address address);
}

View File

@ -0,0 +1,29 @@
package com.baeldung.mapper;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.baeldung.dto.CustomerDto;
import com.baeldung.entity.Customer;
public class CustomerDtoMapperUnitTest {
private CustomerDtoMapper customerDtoMapper = Mappers.getMapper(CustomerDtoMapper.class);
@Test
void testGivenCustomer_mapsToCustomerDto() {
// given
Customer customer = new Customer().setFirstName("Max")
.setLastName("Powers");
// when
CustomerDto customerDto = customerDtoMapper.from(customer);
// then
assertEquals(customerDto.getForename(), customer.getFirstName());
assertEquals(customerDto.getSurname(), customer.getLastName());
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.mapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import org.junit.Test;
import org.mapstruct.factory.Mappers;
import com.baeldung.entity.Address;
import com.baeldung.entity.Customer;
import com.baeldung.entity.DeliveryAddress;
public class DeliveryAddressMapperUnitTest {
private DeliveryAddressMapper deliveryAddressMapper = Mappers.getMapper(DeliveryAddressMapper.class);
@Test
public void testGivenCustomerAndAddress_mapsToDeliveryAddress() {
// given
Customer customer = new Customer().setFirstName("Max")
.setLastName("Powers");
Address homeAddress = new Address().setStreet("123 Some Street")
.setCounty("Nevada")
.setPostalcode("89123");
// when
DeliveryAddress deliveryAddress = deliveryAddressMapper.from(customer, homeAddress);
// then
assertEquals(deliveryAddress.getForename(), customer.getFirstName());
assertEquals(deliveryAddress.getSurname(), customer.getLastName());
assertEquals(deliveryAddress.getStreet(), homeAddress.getStreet());
assertEquals(deliveryAddress.getCounty(), homeAddress.getCounty());
assertEquals(deliveryAddress.getPostalcode(), homeAddress.getPostalcode());
}
@Test
public void testGivenDeliveryAddressAndSomeOtherAddress_updatesDeliveryAddress() {
// given
Customer customer = new Customer().setFirstName("Max")
.setLastName("Powers");
DeliveryAddress deliveryAddress = new DeliveryAddress().setStreet("123 Some Street")
.setCounty("Nevada")
.setPostalcode("89123");
Address otherAddress = new Address().setStreet("456 Some other street")
.setCounty("Arizona")
.setPostalcode("12345");
// when
DeliveryAddress updatedDeliveryAddress = deliveryAddressMapper.updateAddress(deliveryAddress, otherAddress);
// then
assertSame(deliveryAddress, updatedDeliveryAddress);
assertEquals(deliveryAddress.getStreet(), otherAddress.getStreet());
assertEquals(deliveryAddress.getCounty(), otherAddress.getCounty());
assertEquals(deliveryAddress.getPostalcode(), otherAddress.getPostalcode());
}
}