Skip to content

Commit 5ebb8fd

Browse files
committed
Add aggregate test cases
Add test cases to reach a 100% coverage of all message handling functions of the OrderAggregate and OrderLine member. Added, upgrade to JUnit 5 #BAEL-4767
1 parent 5635c87 commit 5ebb8fd

1 file changed

Lines changed: 109 additions & 32 deletions

File tree

Lines changed: 109 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,139 @@
11
package com.baeldung.axon.commandmodel;
22

3-
import java.util.UUID;
4-
5-
import com.baeldung.axon.coreapi.exceptions.UnconfirmedOrderException;
6-
import org.axonframework.test.aggregate.AggregateTestFixture;
7-
import org.axonframework.test.aggregate.FixtureConfiguration;
8-
import org.junit.*;
9-
3+
import com.baeldung.axon.commandmodel.order.OrderAggregate;
4+
import com.baeldung.axon.coreapi.commands.AddProductCommand;
105
import com.baeldung.axon.coreapi.commands.ConfirmOrderCommand;
6+
import com.baeldung.axon.coreapi.commands.DecrementProductCountCommand;
7+
import com.baeldung.axon.coreapi.commands.IncrementProductCountCommand;
118
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
129
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
1310
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
1411
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
1512
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
13+
import com.baeldung.axon.coreapi.events.ProductAddedEvent;
14+
import com.baeldung.axon.coreapi.events.ProductCountDecrementedEvent;
15+
import com.baeldung.axon.coreapi.events.ProductCountIncrementedEvent;
16+
import com.baeldung.axon.coreapi.events.ProductRemovedEvent;
17+
import com.baeldung.axon.coreapi.exceptions.DuplicateOrderLineException;
18+
import com.baeldung.axon.coreapi.exceptions.OrderAlreadyConfirmedException;
19+
import com.baeldung.axon.coreapi.exceptions.UnconfirmedOrderException;
20+
import org.axonframework.test.aggregate.AggregateTestFixture;
21+
import org.axonframework.test.aggregate.FixtureConfiguration;
22+
import org.axonframework.test.matchers.Matchers;
23+
import org.junit.jupiter.api.*;
1624

17-
public class OrderAggregateUnitTest {
25+
import java.util.UUID;
26+
27+
class OrderAggregateUnitTest {
28+
29+
private static final String ORDER_ID = UUID.randomUUID().toString();
30+
private static final String PRODUCT_ID = UUID.randomUUID().toString();
1831

1932
private FixtureConfiguration<OrderAggregate> fixture;
2033

21-
@Before
22-
public void setUp() {
34+
@BeforeEach
35+
void setUp() {
2336
fixture = new AggregateTestFixture<>(OrderAggregate.class);
2437
}
2538

2639
@Test
27-
public void giveNoPriorActivity_whenPlaceOrderCommand_thenShouldPublishOrderPlacedEvent() {
28-
String orderId = UUID.randomUUID().toString();
29-
String product = "Deluxe Chair";
40+
void giveNoPriorActivity_whenPlaceOrderCommand_thenShouldPublishOrderPlacedEvent() {
3041
fixture.givenNoPriorActivity()
31-
.when(new PlaceOrderCommand(orderId, product))
32-
.expectEvents(new OrderPlacedEvent(orderId, product));
42+
.when(new PlaceOrderCommand(ORDER_ID))
43+
.expectEvents(new OrderPlacedEvent(ORDER_ID));
44+
}
45+
46+
@Test
47+
void givenOrderPlacedEvent_whenAddProductCommand_thenShouldPublishProductAddedEvent() {
48+
fixture.given(new OrderPlacedEvent(ORDER_ID))
49+
.when(new AddProductCommand(ORDER_ID, PRODUCT_ID))
50+
.expectEvents(new ProductAddedEvent(ORDER_ID, PRODUCT_ID));
51+
}
52+
53+
@Test
54+
void givenOrderPlacedEventAndProductAddedEvent_whenAddProductCommandForSameProductId_thenShouldThrowDuplicateOrderLineException() {
55+
fixture.given(new OrderPlacedEvent(ORDER_ID), new ProductAddedEvent(ORDER_ID, PRODUCT_ID))
56+
.when(new AddProductCommand(ORDER_ID, PRODUCT_ID))
57+
.expectException(DuplicateOrderLineException.class)
58+
.expectExceptionMessage(Matchers.predicate(message -> ((String) message).contains(PRODUCT_ID)));
59+
}
60+
61+
@Test
62+
void givenOrderPlacedEventAndProductAddedEvent_whenIncrementProductCountCommand_thenShouldPublishProductCountIncrementedEvent() {
63+
fixture.given(new OrderPlacedEvent(ORDER_ID), new ProductAddedEvent(ORDER_ID, PRODUCT_ID))
64+
.when(new IncrementProductCountCommand(ORDER_ID, PRODUCT_ID))
65+
.expectEvents(new ProductCountIncrementedEvent(ORDER_ID, PRODUCT_ID));
66+
}
67+
68+
@Test
69+
void givenOrderPlacedEventProductAddedEventAndProductCountIncrementedEvent_whenDecrementProductCountCommand_thenShouldPublishProductCountDecrementedEvent() {
70+
fixture.given(new OrderPlacedEvent(ORDER_ID),
71+
new ProductAddedEvent(ORDER_ID, PRODUCT_ID),
72+
new ProductCountIncrementedEvent(ORDER_ID, PRODUCT_ID))
73+
.when(new DecrementProductCountCommand(ORDER_ID, PRODUCT_ID))
74+
.expectEvents(new ProductCountDecrementedEvent(ORDER_ID, PRODUCT_ID));
3375
}
3476

3577
@Test
36-
public void givenOrderPlacedEvent_whenConfirmOrderCommand_thenShouldPublishOrderConfirmedEvent() {
37-
String orderId = UUID.randomUUID().toString();
38-
String product = "Deluxe Chair";
39-
fixture.given(new OrderPlacedEvent(orderId, product))
40-
.when(new ConfirmOrderCommand(orderId))
41-
.expectEvents(new OrderConfirmedEvent(orderId));
78+
void givenOrderPlacedEventAndProductAddedEvent_whenDecrementProductCountCommand_thenShouldPublishProductRemovedEvent() {
79+
fixture.given(new OrderPlacedEvent(ORDER_ID), new ProductAddedEvent(ORDER_ID, PRODUCT_ID))
80+
.when(new DecrementProductCountCommand(ORDER_ID, PRODUCT_ID))
81+
.expectEvents(new ProductRemovedEvent(ORDER_ID, PRODUCT_ID));
4282
}
4383

4484
@Test
45-
public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowUnconfirmedOrderException() {
46-
String orderId = UUID.randomUUID().toString();
47-
String product = "Deluxe Chair";
48-
fixture.given(new OrderPlacedEvent(orderId, product))
49-
.when(new ShipOrderCommand(orderId))
85+
void givenOrderPlacedEvent_whenConfirmOrderCommand_thenShouldPublishOrderConfirmedEvent() {
86+
fixture.given(new OrderPlacedEvent(ORDER_ID))
87+
.when(new ConfirmOrderCommand(ORDER_ID))
88+
.expectEvents(new OrderConfirmedEvent(ORDER_ID));
89+
}
90+
91+
@Test
92+
void givenOrderPlacedEventAndOrderConfirmedEvent_whenConfirmOrderCommand_thenExpectNoEvents() {
93+
fixture.given(new OrderPlacedEvent(ORDER_ID), new OrderConfirmedEvent(ORDER_ID))
94+
.when(new ConfirmOrderCommand(ORDER_ID))
95+
.expectNoEvents();
96+
}
97+
98+
@Test
99+
void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowUnconfirmedOrderException() {
100+
fixture.given(new OrderPlacedEvent(ORDER_ID))
101+
.when(new ShipOrderCommand(ORDER_ID))
50102
.expectException(UnconfirmedOrderException.class);
51103
}
52104

53105
@Test
54-
public void givenOrderPlacedEventAndOrderConfirmedEvent_whenShipOrderCommand_thenShouldPublishOrderShippedEvent() {
55-
String orderId = UUID.randomUUID().toString();
56-
String product = "Deluxe Chair";
57-
fixture.given(new OrderPlacedEvent(orderId, product), new OrderConfirmedEvent(orderId))
58-
.when(new ShipOrderCommand(orderId))
59-
.expectEvents(new OrderShippedEvent(orderId));
106+
void givenOrderPlacedEventAndOrderConfirmedEvent_whenShipOrderCommand_thenShouldPublishOrderShippedEvent() {
107+
fixture.given(new OrderPlacedEvent(ORDER_ID), new OrderConfirmedEvent(ORDER_ID))
108+
.when(new ShipOrderCommand(ORDER_ID))
109+
.expectEvents(new OrderShippedEvent(ORDER_ID));
60110
}
61111

112+
@Test
113+
void givenOrderPlacedEventProductAndOrderConfirmedEvent_whenAddProductCommand_thenShouldThrowOrderAlreadyConfirmedException() {
114+
fixture.given(new OrderPlacedEvent(ORDER_ID), new OrderConfirmedEvent(ORDER_ID))
115+
.when(new AddProductCommand(ORDER_ID, PRODUCT_ID))
116+
.expectException(OrderAlreadyConfirmedException.class)
117+
.expectExceptionMessage(Matchers.predicate(message -> ((String) message).contains(ORDER_ID)));
118+
}
119+
120+
@Test
121+
void givenOrderPlacedEventProductAddedEventAndOrderConfirmedEvent_whenIncrementProductCountCommand_thenShouldThrowOrderAlreadyConfirmedException() {
122+
fixture.given(new OrderPlacedEvent(ORDER_ID),
123+
new ProductAddedEvent(ORDER_ID, PRODUCT_ID),
124+
new OrderConfirmedEvent(ORDER_ID))
125+
.when(new IncrementProductCountCommand(ORDER_ID, PRODUCT_ID))
126+
.expectException(OrderAlreadyConfirmedException.class)
127+
.expectExceptionMessage(Matchers.predicate(message -> ((String) message).contains(ORDER_ID)));
128+
}
129+
130+
@Test
131+
void givenOrderPlacedEventProductAddedEventAndOrderConfirmedEvent_whenDecrementProductCountCommand_thenShouldThrowOrderAlreadyConfirmedException() {
132+
fixture.given(new OrderPlacedEvent(ORDER_ID),
133+
new ProductAddedEvent(ORDER_ID, PRODUCT_ID),
134+
new OrderConfirmedEvent(ORDER_ID))
135+
.when(new DecrementProductCountCommand(ORDER_ID, PRODUCT_ID))
136+
.expectException(OrderAlreadyConfirmedException.class)
137+
.expectExceptionMessage(Matchers.predicate(message -> ((String) message).contains(ORDER_ID)));
138+
}
62139
}

0 commit comments

Comments
 (0)