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