diff --git a/mapstruct/src/main/java/com/baeldung/dto/CustomerDto.java b/mapstruct/src/main/java/com/baeldung/dto/CustomerDto.java new file mode 100644 index 0000000000..617f2c6e0c --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/dto/CustomerDto.java @@ -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; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/entity/Address.java b/mapstruct/src/main/java/com/baeldung/entity/Address.java new file mode 100644 index 0000000000..4a6edbd75d --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/entity/Address.java @@ -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; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/entity/Customer.java b/mapstruct/src/main/java/com/baeldung/entity/Customer.java new file mode 100644 index 0000000000..cdde6b542a --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/entity/Customer.java @@ -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; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/entity/DeliveryAddress.java b/mapstruct/src/main/java/com/baeldung/entity/DeliveryAddress.java new file mode 100644 index 0000000000..084c5f86ef --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/entity/DeliveryAddress.java @@ -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; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/mapper/CustomerDtoMapper.java b/mapstruct/src/main/java/com/baeldung/mapper/CustomerDtoMapper.java new file mode 100644 index 0000000000..2c84f80167 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapper/CustomerDtoMapper.java @@ -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); +} diff --git a/mapstruct/src/main/java/com/baeldung/mapper/DeliveryAddressMapper.java b/mapstruct/src/main/java/com/baeldung/mapper/DeliveryAddressMapper.java new file mode 100644 index 0000000000..6a337fbb9e --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapper/DeliveryAddressMapper.java @@ -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); +} diff --git a/mapstruct/src/test/java/com/baeldung/mapper/CustomerDtoMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/CustomerDtoMapperUnitTest.java new file mode 100644 index 0000000000..cded90138b --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/CustomerDtoMapperUnitTest.java @@ -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()); + } +} diff --git a/mapstruct/src/test/java/com/baeldung/mapper/DeliveryAddressMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/DeliveryAddressMapperUnitTest.java new file mode 100644 index 0000000000..e2c12fcba3 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/DeliveryAddressMapperUnitTest.java @@ -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()); + } +}