Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions mapstruct-3/src/main/java/com/baeldung/dto/AncestorDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.baeldung.dto;

public class AncestorDto {

private String firstName;

private Double weight;

private String vatNumber;

public AncestorDto() {
}

public AncestorDto(String firstName, Double weight, String vatNumber) {
this.firstName = firstName;
this.weight = weight;
this.vatNumber = vatNumber;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public Double getWeight() {
return weight;
}

public void setWeight(Double weight) {
this.weight = weight;
}

public String getVatNumber() {
return vatNumber;
}

public void setVatNumber(String vatNumber) {
this.vatNumber = vatNumber;
}
}
45 changes: 45 additions & 0 deletions mapstruct-3/src/main/java/com/baeldung/dto/OrderDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.baeldung.dto;

import java.util.List;

public class OrderDto {

private String transactionId;

private List<String> orderItemIds;

private PaymentDto paymentDto;

public OrderDto(String transactionId, List<String> orderItemIds, PaymentDto paymentDto) {
this.transactionId = transactionId;
this.orderItemIds = orderItemIds;
this.paymentDto = paymentDto;
}

public OrderDto() {
}

public String getTransactionId() {
return transactionId;
}

public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}

public List<String> getOrderItemIds() {
return orderItemIds;
}

public void setOrderItemIds(List<String> orderItemIds) {
this.orderItemIds = orderItemIds;
}

public PaymentDto getPaymentDto() {
return paymentDto;
}

public void setPaymentDto(PaymentDto paymentDto) {
this.paymentDto = paymentDto;
}
}
21 changes: 21 additions & 0 deletions mapstruct-3/src/main/java/com/baeldung/dto/PaymentDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.dto;

public class PaymentDto {

private String type;

public PaymentDto() {
}

public PaymentDto(String type) {
this.type = type;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}
43 changes: 43 additions & 0 deletions mapstruct-3/src/main/java/com/baeldung/entity/GrandDad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.baeldung.entity;

public class GrandDad {

private Identity identity;

private String weight;

private String vatNumber;

public GrandDad() {
}

public GrandDad(Identity identity, String weight, String vatNumber) {
this.identity = identity;
this.weight = weight;
this.vatNumber = vatNumber;
}

public Identity getIdentity() {
return identity;
}

public void setIdentity(Identity identity) {
this.identity = identity;
}

public String getWeight() {
return weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public String getVatNumber() {
return vatNumber;
}

public void setVatNumber(String vatNumber) {
this.vatNumber = vatNumber;
}
}
32 changes: 32 additions & 0 deletions mapstruct-3/src/main/java/com/baeldung/entity/Identity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baeldung.entity;

public class Identity {

private String name;
private String surname;

public Identity() {
}

public Identity(String name, String surname) {
this.name = name;
this.surname = surname;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

}
45 changes: 45 additions & 0 deletions mapstruct-3/src/main/java/com/baeldung/entity/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.baeldung.entity;

import java.util.List;

public class Order {

private String transactionId;

private List<String> orderItemIds;

private Payment payment;

public Order(String transactionId, List<String> 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<String> getOrderItemIds() {
return orderItemIds;
}

public void setOrderItemIds(List<String> orderItemIds) {
this.orderItemIds = orderItemIds;
}

public Payment getPayment() {
return payment;
}

public void setPayment(Payment payment) {
this.payment = payment;
}
}
21 changes: 21 additions & 0 deletions mapstruct-3/src/main/java/com/baeldung/entity/Payment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.entity;

public class Payment {

private String type;

public Payment() {
}

public Payment(String type) {
this.type = type;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}
15 changes: 15 additions & 0 deletions mapstruct-3/src/main/java/com/baeldung/mapper/AncestorMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.mapper;

import com.baeldung.dto.AncestorDto;
import com.baeldung.entity.GrandDad;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper
public interface AncestorMapper {

@Mapping(source = "identity.name", target = "firstName")
@Mapping(source = "weight", target = "weight", numberFormat = "###.#")
AncestorDto toDto(GrandDad grandDad);

}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.baeldung.mapper;

import com.baeldung.dto.OrderDto;
import com.baeldung.entity.Order;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

import java.util.ArrayList;

@Mapper(uses = PaymentMapper.class)
public interface OrderMapperWithAfterMapping {

@Mapping(source = "payment", target = "paymentDto")
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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.mapper;

import com.baeldung.dto.OrderDto;
import com.baeldung.entity.Order;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(uses = PaymentMapper.class)
public interface OrderMapperWithDefault {

@Mapping(
source = "payment",
target = "paymentDto",
defaultExpression = "java(new com.baeldung.dto.PaymentDto())"
)
@Mapping(
source = "transactionId",
target = "transactionId",
defaultValue = "N/A"
)
OrderDto toDto(Order order);

}
12 changes: 12 additions & 0 deletions mapstruct-3/src/main/java/com/baeldung/mapper/PaymentMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.mapper;

import com.baeldung.dto.PaymentDto;
import com.baeldung.entity.Payment;
import org.mapstruct.Mapper;

@Mapper
public interface PaymentMapper {

PaymentDto toDto(Payment payment);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.mapper;

import com.baeldung.dto.AncestorDto;
import com.baeldung.entity.GrandDad;
import com.baeldung.entity.Identity;
import org.junit.Test;
import org.mapstruct.factory.Mappers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class AncestorMapperUnitTest {

@Test
public void whenAncestorMapperToDtoMapsGrandad_thenSourceValuesAreUsed() {
AncestorMapper mapper = Mappers.getMapper(AncestorMapper.class);
GrandDad source = new GrandDad(
new Identity("Thomas", "Tom"),
"80.00",
"1234556"
);
AncestorDto mapped = mapper.toDto(source);
assertEquals("Thomas", mapped.getFirstName());
assertEquals(80.0d, mapped.getWeight(), 0.01d);
assertEquals("1234556", mapped.getVatNumber());
}

@Test
public void whenAncestorMapperToDtoMapsGrandadWithEmptyValues_thenNullValuesAreMapped() {
AncestorMapper mapper = Mappers.getMapper(AncestorMapper.class);
GrandDad source = new GrandDad(
null,
null,
"1234556"
);
AncestorDto mapped = mapper.toDto(source);
assertNull(mapped.getFirstName());
assertNull(mapped.getWeight());
assertEquals("1234556", mapped.getVatNumber());
}

}
Loading