-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
DeliveryServiceInProcessComponentTest.java
127 lines (97 loc) · 4.11 KB
/
DeliveryServiceInProcessComponentTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package net.chrisrichardson.ftgo.deliveryservice;
import io.eventuate.tram.events.publisher.DomainEventPublisher;
import io.eventuate.tram.spring.events.publisher.TramEventsPublisherConfiguration;
import io.eventuate.tram.spring.inmemory.TramInMemoryConfiguration;
import net.chrisrichardson.ftgo.deliveryservice.domain.DeliveryRepository;
import net.chrisrichardson.ftgo.deliveryservice.domain.DeliveryServiceTestData;
import net.chrisrichardson.ftgo.deliveryservice.domain.RestaurantRepository;
import net.chrisrichardson.ftgo.deliveryservice.messaging.DeliveryServiceMessagingConfiguration;
import net.chrisrichardson.ftgo.deliveryservice.web.DeliveryServiceWebConfiguration;
import net.chrisrichardson.ftgo.orderservice.api.OrderServiceChannels;
import net.chrisrichardson.ftgo.orderservice.api.events.OrderCreatedEvent;
import net.chrisrichardson.ftgo.orderservice.api.events.OrderDetails;
import net.chrisrichardson.ftgo.restaurantservice.RestaurantServiceChannels;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Collections;
import static com.jayway.restassured.RestAssured.given;
import static io.eventuate.util.test.async.Eventually.eventually;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DeliveryServiceInProcessComponentTest.Config.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DeliveryServiceInProcessComponentTest {
private long restaurantId;
private long orderId;
@Configuration
@EnableAutoConfiguration
@Import({DeliveryServiceMessagingConfiguration.class,
DeliveryServiceWebConfiguration.class,
TramInMemoryConfiguration.class,
TramEventsPublisherConfiguration.class
})
public static class Config {
}
@LocalServerPort
private int port;
private String host = "localhost";
@Autowired
private DomainEventPublisher domainEventPublisher;
@Autowired
private RestaurantRepository restaurantRepository;
@Autowired
private DeliveryRepository deliveryRepository;
@Test
public void shouldScheduleDelivery() {
createRestaurant();
createOrder();
assertDeliveryCreated();
// createCourier
// acceptTicket
// TicketCancelled
}
private String baseUrl(int port, String path, String... pathElements) {
assertNotNull("host", host);
StringBuilder sb = new StringBuilder("http://");
sb.append(host);
sb.append(":");
sb.append(port);
sb.append("/");
sb.append(path);
for (String pe : pathElements) {
sb.append("/");
sb.append(pe);
}
String s = sb.toString();
System.out.println("url=" + s);
return s;
}
private void assertDeliveryCreated() {
String state = given().
when().
get(baseUrl(port, "deliveries", Long.toString(orderId))).
then().
statusCode(200).extract().path("deliveryInfo.state");
assertEquals("PENDING", state);
}
private void createOrder() {
orderId = System.currentTimeMillis();
domainEventPublisher.publish(OrderServiceChannels.ORDER_EVENT_CHANNEL, orderId, Collections.singletonList(
new OrderCreatedEvent(new OrderDetails(0L, restaurantId, null, null),
DeliveryServiceTestData.DELIVERY_ADDRESS, null)));
eventually(() -> assertTrue(deliveryRepository.findById(orderId).isPresent()));
}
private void createRestaurant() {
restaurantId = System.currentTimeMillis();
domainEventPublisher.publish(RestaurantServiceChannels.RESTAURANT_EVENT_CHANNEL, restaurantId,
Collections.singletonList(RestaurantEventMother.makeRestaurantCreated()));
eventually(() -> assertTrue(restaurantRepository.findById(restaurantId).isPresent()));
}
}