Skip to content

Commit 23c4f82

Browse files
committed
BAEL-9511 - Review comments
1 parent 683b4bf commit 23c4f82

File tree

6 files changed

+127
-41
lines changed

6 files changed

+127
-41
lines changed

mapstruct-3/src/main/java/com/baeldung/mapper/OrderMapperWithAfterMapping.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import com.baeldung.entity.Order;
55
import org.mapstruct.AfterMapping;
66
import org.mapstruct.Mapper;
7+
import org.mapstruct.Mapping;
78
import org.mapstruct.MappingTarget;
89

910
import java.util.ArrayList;
1011

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

15+
@Mapping(source = "payment", target = "paymentDto")
1416
OrderDto toDto(Order order);
1517

1618
@AfterMapping

mapstruct-3/src/main/java/com/baeldung/mapper/OrderMapperWithBeanMappingStrategy.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

mapstruct-3/src/main/java/com/baeldung/mapper/OrderMapperWithStrategy.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.mapper;
2+
3+
import com.baeldung.dto.AncestorDto;
4+
import com.baeldung.entity.GrandDad;
5+
import com.baeldung.entity.Identity;
6+
import org.junit.Test;
7+
import org.mapstruct.factory.Mappers;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertNull;
11+
12+
public class AncestorMapperTest {
13+
14+
@Test
15+
public void whenAncestorMapperToDtoMapsGrandad_thenSourceValuesAreUsed() {
16+
AncestorMapper mapper = Mappers.getMapper(AncestorMapper.class);
17+
GrandDad source = new GrandDad(
18+
new Identity("Thomas", "Tom"),
19+
"80.00",
20+
"1234556"
21+
);
22+
AncestorDto mapped = mapper.toDto(source);
23+
assertEquals("Thomas", mapped.getFirstName());
24+
assertEquals( 80.0d, mapped.getWeight(), 0.01d);
25+
assertEquals("1234556", mapped.getVatNumber());
26+
}
27+
28+
@Test
29+
public void whenAncestorMapperToDtoMapsGrandadWithEmptyValues_thenNullValuesAreMapped() {
30+
AncestorMapper mapper = Mappers.getMapper(AncestorMapper.class);
31+
GrandDad source = new GrandDad(
32+
null,
33+
null,
34+
"1234556"
35+
);
36+
AncestorDto mapped = mapper.toDto(source);
37+
assertNull(mapped.getFirstName());
38+
assertNull(mapped.getWeight());
39+
assertEquals("1234556", mapped.getVatNumber());
40+
}
41+
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.mapper;
2+
3+
4+
import com.baeldung.dto.OrderDto;
5+
import com.baeldung.entity.Order;
6+
import com.baeldung.entity.Payment;
7+
import org.junit.Test;
8+
import org.mapstruct.factory.Mappers;
9+
10+
import java.util.List;
11+
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertNotNull;
14+
15+
public class OrderMapperWithAfterMappingTest {
16+
17+
@Test
18+
public void whenOrderMapperWithAfterMappingToDtoMapsOrderWithEmptyProperties_thenDefaultValuesAreUsed() {
19+
OrderMapperWithAfterMapping mapper = Mappers.getMapper(OrderMapperWithAfterMapping.class);
20+
Order orderSource = new Order();
21+
orderSource.setPayment(new Payment("Cash"));
22+
OrderDto mapped = mapper.toDto(orderSource);
23+
assertEquals("Cash", mapped.getPaymentDto().getType());
24+
assertEquals("N/A", mapped.getTransactionId());
25+
assertNotNull(mapped.getOrderItemIds());
26+
assertEquals(0, mapped.getOrderItemIds().size());
27+
}
28+
29+
@Test
30+
public void whenOrderMapperWithAfterMappingToDtoMapsOrderWithNonEmptyProperties_thenSourceValuesAreUsed() {
31+
OrderMapperWithAfterMapping mapper = Mappers.getMapper(OrderMapperWithAfterMapping.class);
32+
Order orderSource = new Order();
33+
orderSource.setPayment(new Payment("Cash"));
34+
orderSource.setOrderItemIds(List.of("item1", "item2"));
35+
orderSource.setTransactionId("orderTransaction");
36+
OrderDto mapped = mapper.toDto(orderSource);
37+
assertEquals("Cash", mapped.getPaymentDto().getType());
38+
assertEquals("orderTransaction", mapped.getTransactionId());
39+
assertNotNull(mapped.getOrderItemIds());
40+
assertEquals(2, mapped.getOrderItemIds().size());
41+
}
42+
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.mapper;
2+
3+
import com.baeldung.dto.OrderDto;
4+
import com.baeldung.entity.Order;
5+
import com.baeldung.entity.Payment;
6+
import org.junit.Test;
7+
import org.mapstruct.factory.Mappers;
8+
9+
import java.util.List;
10+
11+
import static org.junit.Assert.*;
12+
13+
public class OrderMapperWithDefaultTest {
14+
15+
@Test
16+
public void whenOrderMapperWithDefaultToDtoMapsOrderWithEmptyProperties_thenDefaultValuesAreUsed() {
17+
OrderMapperWithDefault mapper = Mappers.getMapper(OrderMapperWithDefault.class);
18+
Order orderSource = new Order();
19+
orderSource.setPayment(new Payment("Cash"));
20+
OrderDto mapped = mapper.toDto(orderSource);
21+
assertEquals("Cash", mapped.getPaymentDto().getType());
22+
assertEquals("N/A", mapped.getTransactionId());
23+
assertNull(mapped.getOrderItemIds());
24+
}
25+
26+
@Test
27+
public void whenOrderMapperWithDefaultToDtoMapsOrderWithNonEmptyProperties_thenSourceValuesAreUsed() {
28+
OrderMapperWithDefault mapper = Mappers.getMapper(OrderMapperWithDefault.class);
29+
Order orderSource = new Order();
30+
orderSource.setPayment(new Payment("Cash"));
31+
orderSource.setOrderItemIds(List.of("item1", "item2"));
32+
orderSource.setTransactionId("orderTransaction");
33+
OrderDto mapped = mapper.toDto(orderSource);
34+
assertEquals("Cash", mapped.getPaymentDto().getType());
35+
assertEquals("orderTransaction", mapped.getTransactionId());
36+
assertNotNull(mapped.getOrderItemIds());
37+
assertEquals(2, mapped.getOrderItemIds().size());
38+
}
39+
40+
}

0 commit comments

Comments
 (0)