diff --git a/mapstruct-3/src/main/java/com/baeldung/dto/OrderDto.java b/mapstruct-3/src/main/java/com/baeldung/dto/OrderDto.java new file mode 100644 index 000000000000..bea03f41716c --- /dev/null +++ b/mapstruct-3/src/main/java/com/baeldung/dto/OrderDto.java @@ -0,0 +1,45 @@ +package com.baeldung.dto; + +import java.util.List; + +public class OrderDto { + + private String transactionId; + + private List orderItemIds; + + private PaymentDto payment; + + public OrderDto(String transactionId, List orderItemIds, PaymentDto payment) { + this.transactionId = transactionId; + this.orderItemIds = orderItemIds; + this.payment = payment; + } + + public OrderDto() { + } + + public String getTransactionId() { + return transactionId; + } + + public void setTransactionId(String transactionId) { + this.transactionId = transactionId; + } + + public List getOrderItemIds() { + return orderItemIds; + } + + public void setOrderItemIds(List orderItemIds) { + this.orderItemIds = orderItemIds; + } + + public PaymentDto getPayment() { + return payment; + } + + public void setPayment(PaymentDto payment) { + this.payment = payment; + } +} diff --git a/mapstruct-3/src/main/java/com/baeldung/dto/PaymentDto.java b/mapstruct-3/src/main/java/com/baeldung/dto/PaymentDto.java new file mode 100644 index 000000000000..ac0674aa7f3a --- /dev/null +++ b/mapstruct-3/src/main/java/com/baeldung/dto/PaymentDto.java @@ -0,0 +1,32 @@ +package com.baeldung.dto; + +public class PaymentDto { + + private String type; + + private Double amount; + + public PaymentDto() { + } + + public PaymentDto(String type, Double amount) { + this.type = type; + this.amount = amount; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Double getAmount() { + return amount; + } + + public void setAmount(Double amount) { + this.amount = amount; + } +} diff --git a/mapstruct-3/src/main/java/com/baeldung/entity/Order.java b/mapstruct-3/src/main/java/com/baeldung/entity/Order.java new file mode 100644 index 000000000000..f757e5a8b11c --- /dev/null +++ b/mapstruct-3/src/main/java/com/baeldung/entity/Order.java @@ -0,0 +1,45 @@ +package com.baeldung.entity; + +import java.util.List; + +public class Order { + + private String transactionId; + + private List orderItemIds; + + private Payment payment; + + public Order(String transactionId, List orderItemIds, Payment payment) { + this.transactionId = transactionId; + this.orderItemIds = orderItemIds; + this.payment = payment; + } + + public Order() { + } + + public String getTransactionId() { + return transactionId; + } + + public void setTransactionId(String transactionId) { + this.transactionId = transactionId; + } + + public List getOrderItemIds() { + return orderItemIds; + } + + public void setOrderItemIds(List orderItemIds) { + this.orderItemIds = orderItemIds; + } + + public Payment getPayment() { + return payment; + } + + public void setPayment(Payment payment) { + this.payment = payment; + } +} diff --git a/mapstruct-3/src/main/java/com/baeldung/entity/Payment.java b/mapstruct-3/src/main/java/com/baeldung/entity/Payment.java new file mode 100644 index 000000000000..d63332d12578 --- /dev/null +++ b/mapstruct-3/src/main/java/com/baeldung/entity/Payment.java @@ -0,0 +1,32 @@ +package com.baeldung.entity; + +public class Payment { + + private String type; + + private String amount; + + public Payment() { + } + + public Payment(String type, String amount) { + this.type = type; + this.amount = amount; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getAmount() { + return amount; + } + + public void setAmount(String amount) { + this.amount = amount; + } +} diff --git a/mapstruct-3/src/main/java/com/baeldung/mapper/AlwaysNullCheckPaymentMapper.java b/mapstruct-3/src/main/java/com/baeldung/mapper/AlwaysNullCheckPaymentMapper.java new file mode 100644 index 000000000000..97823d95cb72 --- /dev/null +++ b/mapstruct-3/src/main/java/com/baeldung/mapper/AlwaysNullCheckPaymentMapper.java @@ -0,0 +1,14 @@ +package com.baeldung.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.NullValueCheckStrategy; + +import com.baeldung.dto.PaymentDto; +import com.baeldung.entity.Payment; + +@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS) +public interface AlwaysNullCheckPaymentMapper { + + PaymentDto toDto(Payment payment); + +} diff --git a/mapstruct-3/src/main/java/com/baeldung/mapper/MediaMapper.java b/mapstruct-3/src/main/java/com/baeldung/mapper/MediaMapper.java index 2629a1820a3e..e240b121e8d3 100644 --- a/mapstruct-3/src/main/java/com/baeldung/mapper/MediaMapper.java +++ b/mapstruct-3/src/main/java/com/baeldung/mapper/MediaMapper.java @@ -1,9 +1,8 @@ package com.baeldung.mapper; -import org.mapstruct.Mapper; - import com.baeldung.dto.MediaDto; import com.baeldung.entity.Media; +import org.mapstruct.Mapper; @Mapper public interface MediaMapper { diff --git a/mapstruct-3/src/main/java/com/baeldung/mapper/OrderMapperWithAfterMapping.java b/mapstruct-3/src/main/java/com/baeldung/mapper/OrderMapperWithAfterMapping.java new file mode 100644 index 000000000000..75093be66b88 --- /dev/null +++ b/mapstruct-3/src/main/java/com/baeldung/mapper/OrderMapperWithAfterMapping.java @@ -0,0 +1,28 @@ +package com.baeldung.mapper; + +import java.util.ArrayList; + +import org.mapstruct.AfterMapping; +import org.mapstruct.Mapper; +import org.mapstruct.MappingTarget; + +import com.baeldung.dto.OrderDto; +import com.baeldung.entity.Order; + +@Mapper(uses = PaymentMapper.class) +public interface OrderMapperWithAfterMapping { + + OrderDto toDto(Order order); + + @AfterMapping + default OrderDto postProcessing(@MappingTarget OrderDto orderDto) { + if (orderDto.getOrderItemIds() == null) { + orderDto.setOrderItemIds(new ArrayList<>()); + } + if (orderDto.getTransactionId() == null) { + orderDto.setTransactionId("N/A"); + } + return orderDto; + } + +} diff --git a/mapstruct-3/src/main/java/com/baeldung/mapper/OrderMapperWithDefault.java b/mapstruct-3/src/main/java/com/baeldung/mapper/OrderMapperWithDefault.java new file mode 100644 index 000000000000..afff8c85cbd6 --- /dev/null +++ b/mapstruct-3/src/main/java/com/baeldung/mapper/OrderMapperWithDefault.java @@ -0,0 +1,16 @@ +package com.baeldung.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +import com.baeldung.dto.OrderDto; +import com.baeldung.entity.Order; + +@Mapper(uses = PaymentMapper.class) +public interface OrderMapperWithDefault { + + @Mapping(source = "payment", target = "payment", defaultExpression = "java(new com.baeldung.dto.PaymentDto())") + @Mapping(source = "transactionId", target = "transactionId", defaultValue = "N/A") + OrderDto toDto(Order order); + +} diff --git a/mapstruct-3/src/main/java/com/baeldung/mapper/PaymentMapper.java b/mapstruct-3/src/main/java/com/baeldung/mapper/PaymentMapper.java new file mode 100644 index 000000000000..58a40e15d8b0 --- /dev/null +++ b/mapstruct-3/src/main/java/com/baeldung/mapper/PaymentMapper.java @@ -0,0 +1,13 @@ +package com.baeldung.mapper; + +import org.mapstruct.Mapper; + +import com.baeldung.dto.PaymentDto; +import com.baeldung.entity.Payment; + +@Mapper +public interface PaymentMapper { + + PaymentDto toDto(Payment payment); + +} diff --git a/mapstruct-3/src/test/java/com/baeldung/mapper/AlwaysNullCheckPaymentMapperUnitTest.java b/mapstruct-3/src/test/java/com/baeldung/mapper/AlwaysNullCheckPaymentMapperUnitTest.java new file mode 100644 index 000000000000..b5590b43d755 --- /dev/null +++ b/mapstruct-3/src/test/java/com/baeldung/mapper/AlwaysNullCheckPaymentMapperUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.mapper; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; +import org.mapstruct.factory.Mappers; + +import com.baeldung.dto.PaymentDto; +import com.baeldung.entity.Payment; + +public class AlwaysNullCheckPaymentMapperUnitTest { + + @Test + public void whenPaymentIsNotNull_thenPaymentDtoIsCreated() { + Payment source = new Payment("Cash", "12.2"); + AlwaysNullCheckPaymentMapper mapper = Mappers.getMapper(AlwaysNullCheckPaymentMapper.class); + PaymentDto result = mapper.toDto(source); + assertEquals("Cash", result.getType()); + assertEquals(12.2d, result.getAmount(), 0.01d); + } + + @Test + public void whenPaymentIsNull_thenPaymentDtoIsNull() { + AlwaysNullCheckPaymentMapper mapper = Mappers.getMapper(AlwaysNullCheckPaymentMapper.class); + PaymentDto result = mapper.toDto(null); + assertNull(result); + } + +} diff --git a/mapstruct-3/src/test/java/com/baeldung/mapper/OrderMapperWithAfterMappingUnitTest.java b/mapstruct-3/src/test/java/com/baeldung/mapper/OrderMapperWithAfterMappingUnitTest.java new file mode 100644 index 000000000000..e6b497307afd --- /dev/null +++ b/mapstruct-3/src/test/java/com/baeldung/mapper/OrderMapperWithAfterMappingUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.mapper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.List; + +import org.junit.Test; +import org.mapstruct.factory.Mappers; + +import com.baeldung.dto.OrderDto; +import com.baeldung.entity.Order; +import com.baeldung.entity.Payment; + +public class OrderMapperWithAfterMappingUnitTest { + + @Test + public void whenOrderMapperWithAfterMappingToDtoMapsOrderWithEmptyProperties_thenDefaultValuesAreUsed() { + OrderMapperWithAfterMapping mapper = Mappers.getMapper(OrderMapperWithAfterMapping.class); + Order orderSource = new Order(); + orderSource.setPayment(new Payment("Cash", "12.0")); + OrderDto mapped = mapper.toDto(orderSource); + assertEquals("Cash", mapped.getPayment() + .getType()); + assertEquals(12.0d, mapped.getPayment() + .getAmount(), 0.01d); + assertEquals("N/A", mapped.getTransactionId()); + assertNotNull(mapped.getOrderItemIds()); + assertEquals(0, mapped.getOrderItemIds() + .size()); + } + + @Test + public void whenOrderMapperWithAfterMappingToDtoMapsOrderWithNonEmptyProperties_thenSourceValuesAreUsed() { + OrderMapperWithAfterMapping mapper = Mappers.getMapper(OrderMapperWithAfterMapping.class); + Order orderSource = new Order(); + orderSource.setPayment(new Payment("Cash", "13.1")); + orderSource.setOrderItemIds(List.of("item1", "item2")); + orderSource.setTransactionId("orderTransaction"); + OrderDto mapped = mapper.toDto(orderSource); + assertEquals("Cash", mapped.getPayment() + .getType()); + assertEquals(13.1d, mapped.getPayment() + .getAmount(), 0.01d); + assertEquals("orderTransaction", mapped.getTransactionId()); + assertNotNull(mapped.getOrderItemIds()); + assertEquals(2, mapped.getOrderItemIds() + .size()); + } + +} diff --git a/mapstruct-3/src/test/java/com/baeldung/mapper/OrderMapperWithDefaultUnitTest.java b/mapstruct-3/src/test/java/com/baeldung/mapper/OrderMapperWithDefaultUnitTest.java new file mode 100644 index 000000000000..72ad41a359b3 --- /dev/null +++ b/mapstruct-3/src/test/java/com/baeldung/mapper/OrderMapperWithDefaultUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.mapper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.List; + +import org.junit.Test; +import org.mapstruct.factory.Mappers; + +import com.baeldung.dto.OrderDto; +import com.baeldung.entity.Order; +import com.baeldung.entity.Payment; + +public class OrderMapperWithDefaultUnitTest { + + @Test + public void whenOrderMapperWithDefaultToDtoMapsOrderWithEmptyProperties_thenDefaultValuesAreUsed() { + OrderMapperWithDefault mapper = Mappers.getMapper(OrderMapperWithDefault.class); + Order orderSource = new Order(); + orderSource.setPayment(new Payment("Cash", "82.8")); + OrderDto mapped = mapper.toDto(orderSource); + assertEquals("Cash", mapped.getPayment() + .getType()); + assertEquals(82.8d, mapped.getPayment() + .getAmount(), 0.01d); + assertEquals("N/A", mapped.getTransactionId()); + assertNull(mapped.getOrderItemIds()); + } + + @Test + public void whenOrderMapperWithDefaultToDtoMapsOrderWithNonEmptyProperties_thenSourceValuesAreUsed() { + OrderMapperWithDefault mapper = Mappers.getMapper(OrderMapperWithDefault.class); + Order orderSource = new Order(); + orderSource.setPayment(new Payment("Cash", "121.32")); + orderSource.setOrderItemIds(List.of("item1", "item2")); + orderSource.setTransactionId("orderTransaction"); + OrderDto mapped = mapper.toDto(orderSource); + assertEquals("Cash", mapped.getPayment() + .getType()); + assertEquals(121.32d, mapped.getPayment() + .getAmount(), 0.001d); + assertEquals("orderTransaction", mapped.getTransactionId()); + assertNotNull(mapped.getOrderItemIds()); + assertEquals(2, mapped.getOrderItemIds() + .size()); + } + +} diff --git a/mapstruct-3/src/test/java/com/baeldung/mapper/PaymentMapperUnitTest.java b/mapstruct-3/src/test/java/com/baeldung/mapper/PaymentMapperUnitTest.java new file mode 100644 index 000000000000..c7716a64df4a --- /dev/null +++ b/mapstruct-3/src/test/java/com/baeldung/mapper/PaymentMapperUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.mapper; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; +import org.mapstruct.factory.Mappers; + +import com.baeldung.dto.PaymentDto; +import com.baeldung.entity.Payment; + +public class PaymentMapperUnitTest { + + @Test + public void whenPaymentIsNotNull_thenPaymentDtoIsCreated() { + Payment source = new Payment("Cash", "12.2"); + PaymentMapper mapper = Mappers.getMapper(PaymentMapper.class); + PaymentDto result = mapper.toDto(source); + assertEquals("Cash", result.getType()); + assertEquals(12.2d, result.getAmount(), 0.01d); + } + + @Test + public void whenPaymentIsNull_thenPaymentDtoIsNull() { + PaymentMapper mapper = Mappers.getMapper(PaymentMapper.class); + PaymentDto result = mapper.toDto(null); + assertNull(result); + } + +}