|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+package se.citerus.dddsample.scenario;
|
|
|
2
|
+
|
|
|
3
|
+import junit.framework.TestCase;
|
|
|
4
|
+import se.citerus.dddsample.application.*;
|
|
|
5
|
+import se.citerus.dddsample.application.impl.BookingServiceImpl;
|
|
|
6
|
+import se.citerus.dddsample.application.impl.HandlingEventServiceImpl;
|
|
|
7
|
+import se.citerus.dddsample.application.impl.TrackingServiceImpl;
|
|
|
8
|
+import se.citerus.dddsample.domain.model.cargo.*;
|
|
|
9
|
+import static se.citerus.dddsample.domain.model.carrier.SampleVoyages.*;
|
|
|
10
|
+import se.citerus.dddsample.domain.model.carrier.Voyage;
|
|
|
11
|
+import se.citerus.dddsample.domain.model.carrier.VoyageNumber;
|
|
|
12
|
+import se.citerus.dddsample.domain.model.carrier.VoyageRepository;
|
|
|
13
|
+import se.citerus.dddsample.domain.model.handling.CannotCreateHandlingEventException;
|
|
|
14
|
+import se.citerus.dddsample.domain.model.handling.HandlingEvent;
|
|
|
15
|
+import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.*;
|
|
|
16
|
+import se.citerus.dddsample.domain.model.handling.HandlingEventFactory;
|
|
|
17
|
+import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
|
|
|
18
|
+import se.citerus.dddsample.domain.model.location.Location;
|
|
|
19
|
+import se.citerus.dddsample.domain.model.location.LocationRepository;
|
|
|
20
|
+import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
|
|
|
21
|
+import se.citerus.dddsample.domain.model.location.UnLocode;
|
|
|
22
|
+import se.citerus.dddsample.domain.service.RoutingService;
|
|
|
23
|
+import se.citerus.dddsample.infrastructure.persistence.inmemory.CargoRepositoryInMem;
|
|
|
24
|
+import se.citerus.dddsample.infrastructure.persistence.inmemory.LocationRepositoryInMem;
|
|
|
25
|
+import se.citerus.dddsample.infrastructure.persistence.inmemory.VoyageRepositoryInMem;
|
|
|
26
|
+
|
|
|
27
|
+import java.util.*;
|
|
|
28
|
+
|
|
|
29
|
+/**
|
|
|
30
|
+ * Cargo scenarios.
|
|
|
31
|
+ */
|
|
|
32
|
+public class CargoLifecycleScenarioTest extends TestCase {
|
|
|
33
|
+
|
|
|
34
|
+ HandlingEventFactory handlingEventFactory;
|
|
|
35
|
+
|
|
|
36
|
+ ApplicationEvents applicationEvents;
|
|
|
37
|
+
|
|
|
38
|
+ BookingService bookingService;
|
|
|
39
|
+ HandlingEventService handlingEventService;
|
|
|
40
|
+ TrackingService trackingService;
|
|
|
41
|
+ RoutingService routingService;
|
|
|
42
|
+
|
|
|
43
|
+ HandlingEventRepository handlingEventRepository;
|
|
|
44
|
+ CargoRepository cargoRepository;
|
|
|
45
|
+ LocationRepository locationRepository;
|
|
|
46
|
+ VoyageRepository voyageRepository;
|
|
|
47
|
+
|
|
|
48
|
+ public void testCargoFromHongkongToStockholm() throws Exception {
|
|
|
49
|
+ /* Test setup: A cargo should be shipped from Hongkong to Stockholm,
|
|
|
50
|
+ and it should arrive in no more than two weeks. */
|
|
|
51
|
+ Location origin = HONGKONG;
|
|
|
52
|
+ Location destination = STOCKHOLM;
|
|
|
53
|
+ Date arrivalDeadline = inTwoWeeks();
|
|
|
54
|
+
|
|
|
55
|
+ /* Use case 1: booking
|
|
|
56
|
+
|
|
|
57
|
+ A new cargo is booked, and the unique tracking id is returned. */
|
|
|
58
|
+ TrackingId trackingId = bookingService.bookNewCargo(
|
|
|
59
|
+ origin.unLocode(), destination.unLocode(), arrivalDeadline
|
|
|
60
|
+ );
|
|
|
61
|
+
|
|
|
62
|
+ /* The tracking id can be used to lookup the cargo in the repository.
|
|
|
63
|
+
|
|
|
64
|
+ Important: The cargo, and thus the domain model, is responsible for determining
|
|
|
65
|
+ the status of the cargo, whether it is on the right track or not and so on.
|
|
|
66
|
+ This is core domain logic.
|
|
|
67
|
+
|
|
|
68
|
+ Tracking the cargo basically amounts to presenting information extracted from
|
|
|
69
|
+ the cargo aggregate in a suitable way. */
|
|
|
70
|
+ Cargo cargo = cargoRepository.find(trackingId);
|
|
|
71
|
+ assertNotNull(cargo);
|
|
|
72
|
+ assertEquals(TransportStatus.NOT_RECEIVED, cargo.delivery().transportStatus());
|
|
|
73
|
+ assertEquals(RoutingStatus.NOT_ROUTED, cargo.routingStatus());
|
|
|
74
|
+ assertFalse(cargo.isMisdirected());
|
|
|
75
|
+
|
|
|
76
|
+
|
|
|
77
|
+ /* Use case 2: routing
|
|
|
78
|
+
|
|
|
79
|
+ A number of possible routes for this cargo is requested and may be
|
|
|
80
|
+ presented to the customer in some way for him/her to choose from.
|
|
|
81
|
+ Selection could be affected by things like price and time of delivery,
|
|
|
82
|
+ but this test simply uses an arbitrary selection to mimic that process.
|
|
|
83
|
+
|
|
|
84
|
+ The cargo is then assigned to the selected route, described by an itinerary. */
|
|
|
85
|
+ List<Itinerary> itineraries = bookingService.requestPossibleRoutesForCargo(trackingId);
|
|
|
86
|
+ Itinerary itinerary = selectPreferedItinerary(itineraries);
|
|
|
87
|
+ cargo.assignToRoute(itinerary);
|
|
|
88
|
+
|
|
|
89
|
+ assertEquals(RoutingStatus.ROUTED, cargo.routingStatus());
|
|
|
90
|
+
|
|
|
91
|
+ /*
|
|
|
92
|
+ Use case 3: handling
|
|
|
93
|
+
|
|
|
94
|
+ A handling event registration attempt will be formed from parsing
|
|
|
95
|
+ the data coming in as a handling report either via
|
|
|
96
|
+ the web service interface or as an uploaded CSV file.
|
|
|
97
|
+
|
|
|
98
|
+ The handling event factory tries to create a HandlingEvent from the attempt,
|
|
|
99
|
+ and if the factory decides that this is a plausible handling event, it is stored.
|
|
|
100
|
+ If the attempt is invalid, for example if no cargo exists for the specfied tracking id,
|
|
|
101
|
+ the attempt is rejected.
|
|
|
102
|
+
|
|
|
103
|
+ Handling begins: cargo is received in Hongkong.
|
|
|
104
|
+ */
|
|
|
105
|
+ HandlingEventRegistrationAttempt attempt1 = new HandlingEventRegistrationAttempt(
|
|
|
106
|
+ new Date(), new Date(), trackingId, null, RECEIVE, HONGKONG.unLocode()
|
|
|
107
|
+ );
|
|
|
108
|
+ handlingEventService.registerHandlingEvent(attempt1);
|
|
|
109
|
+
|
|
|
110
|
+ // Next event: Load onto voyage CM003 in Hongkong
|
|
|
111
|
+ handlingEventService.registerHandlingEvent(new HandlingEventRegistrationAttempt(
|
|
|
112
|
+ new Date(), new Date(), trackingId, CM003.voyageNumber(), LOAD, HONGKONG.unLocode()
|
|
|
113
|
+ ));
|
|
|
114
|
+
|
|
|
115
|
+ // Check current state - should be ok
|
|
|
116
|
+ assertEquals(CM003, cargo.delivery().currentVoyage());
|
|
|
117
|
+ assertEquals(HONGKONG, cargo.delivery().lastKnownLocation());
|
|
|
118
|
+ assertEquals(TransportStatus.ONBOARD_CARRIER, cargo.delivery().transportStatus());
|
|
|
119
|
+ assertFalse(cargo.isMisdirected());
|
|
|
120
|
+
|
|
|
121
|
+ /*
|
|
|
122
|
+ Here's an attempt to register a handling event that's not valid
|
|
|
123
|
+ because there is no voyage with the specified voyage number,
|
|
|
124
|
+ and there's no location with the specified UN Locode either.
|
|
|
125
|
+
|
|
|
126
|
+ This attempt will be rejected and will not affet the cargo delivery in any way.
|
|
|
127
|
+ */
|
|
|
128
|
+ VoyageNumber noSuchVoyageNumber = new VoyageNumber("XX000");
|
|
|
129
|
+ UnLocode noSuchUnLocode = new UnLocode("ZZZZZ");
|
|
|
130
|
+ HandlingEventRegistrationAttempt failedAttempt = new HandlingEventRegistrationAttempt(
|
|
|
131
|
+ new Date(), new Date(), trackingId, noSuchVoyageNumber, LOAD, noSuchUnLocode
|
|
|
132
|
+ );
|
|
|
133
|
+ handlingEventService.registerHandlingEvent(failedAttempt);
|
|
|
134
|
+
|
|
|
135
|
+
|
|
|
136
|
+ // Cargo is now (incorrectly) unloaded in Tokyo
|
|
|
137
|
+ handlingEventService.registerHandlingEvent(new HandlingEventRegistrationAttempt(
|
|
|
138
|
+ new Date(), new Date(), trackingId, CM003.voyageNumber(), UNLOAD, TOKYO.unLocode()
|
|
|
139
|
+ ));
|
|
|
140
|
+
|
|
|
141
|
+ // Check current state - cargo is misdirected!
|
|
|
142
|
+ assertEquals(Voyage.NONE, cargo.delivery().currentVoyage());
|
|
|
143
|
+ assertEquals(TOKYO, cargo.delivery().lastKnownLocation());
|
|
|
144
|
+ assertEquals(TransportStatus.IN_PORT, cargo.delivery().transportStatus());
|
|
|
145
|
+ assertTrue(cargo.isMisdirected());
|
|
|
146
|
+
|
|
|
147
|
+ // Specify a new route, this time from Tokyo (where it was incorrectly unloaded) to Stockholm
|
|
|
148
|
+ RouteSpecification fromTokyo = new RouteSpecification(TOKYO, STOCKHOLM, arrivalDeadline);
|
|
|
149
|
+ cargo.specifyRoute(fromTokyo);
|
|
|
150
|
+
|
|
|
151
|
+ // The old itinerary does not satisfy the new specification
|
|
|
152
|
+ // TODO won't work until .isSatisfied() is implemented
|
|
|
153
|
+ //assertEquals(RoutingStatus.MISROUTED, cargo.routingStatus());
|
|
|
154
|
+
|
|
|
155
|
+ // Repeat procedure of selecting one out of a number of possible routes satisfying the route spec
|
|
|
156
|
+ List<Itinerary> newItineraries = bookingService.requestPossibleRoutesForCargo(cargo.trackingId());
|
|
|
157
|
+ Itinerary newItinerary = selectPreferedItinerary(newItineraries);
|
|
|
158
|
+ cargo.assignToRoute(newItinerary);
|
|
|
159
|
+
|
|
|
160
|
+ // New itinerary should satisfy new route
|
|
|
161
|
+ assertEquals(RoutingStatus.ROUTED, cargo.routingStatus());
|
|
|
162
|
+
|
|
|
163
|
+
|
|
|
164
|
+ // TODO handling from Tokyo to Stockholm
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+ /*
|
|
|
168
|
+ * Utility stubs below.
|
|
|
169
|
+ */
|
|
|
170
|
+
|
|
|
171
|
+ private Date inTwoWeeks() {
|
|
|
172
|
+ return new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 14);
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ private Itinerary selectPreferedItinerary(List<Itinerary> itineraries) {
|
|
|
176
|
+ return itineraries.get(0);
|
|
|
177
|
+ }
|
|
|
178
|
+
|
|
|
179
|
+ protected void setUp() throws Exception {
|
|
|
180
|
+ // Stub
|
|
|
181
|
+ // TODO new voyages
|
|
|
182
|
+ routingService = new RoutingService() {
|
|
|
183
|
+ public List<Itinerary> fetchRoutesForSpecification(RouteSpecification routeSpecification) {
|
|
|
184
|
+ if (routeSpecification.origin().equals(HONGKONG)) {
|
|
|
185
|
+ // Hongkong - NYC - Chicago - Stockholm, initial routing
|
|
|
186
|
+ return Arrays.asList(
|
|
|
187
|
+ new Itinerary(Arrays.asList(
|
|
|
188
|
+ new Leg(CM003, HONGKONG, NEWYORK, new Date(), new Date()),
|
|
|
189
|
+ new Leg(CM004, NEWYORK, CHICAGO, new Date(), new Date()),
|
|
|
190
|
+ new Leg(CM005, CHICAGO, STOCKHOLM, new Date(), new Date())
|
|
|
191
|
+ ))
|
|
|
192
|
+ );
|
|
|
193
|
+ } else {
|
|
|
194
|
+ // Tokyo - Hamburg - Stockholm, rerouting
|
|
|
195
|
+ return Arrays.asList(
|
|
|
196
|
+ new Itinerary(Arrays.asList(
|
|
|
197
|
+ new Leg(CM003, TOKYO, HAMBURG, new Date(), new Date()),
|
|
|
198
|
+ new Leg(CM005, HAMBURG, STOCKHOLM, new Date(), new Date())
|
|
|
199
|
+ ))
|
|
|
200
|
+ );
|
|
|
201
|
+ }
|
|
|
202
|
+ }
|
|
|
203
|
+ };
|
|
|
204
|
+
|
|
|
205
|
+
|
|
|
206
|
+ applicationEvents = new SynchronousApplicationEventsStub();
|
|
|
207
|
+
|
|
|
208
|
+ // Stub
|
|
|
209
|
+ // TODO move functionality to in-mem impl
|
|
|
210
|
+ handlingEventRepository = new HandlingEventRepository() {
|
|
|
211
|
+ public void save(HandlingEvent event) {
|
|
|
212
|
+ Cargo cargo = event.cargo();
|
|
|
213
|
+ Set<HandlingEvent> events = new HashSet<HandlingEvent>(cargo.delivery().history());
|
|
|
214
|
+ events.add(event);
|
|
|
215
|
+ CargoTestHelper.setDeliveryHistory(cargo, events);
|
|
|
216
|
+ }
|
|
|
217
|
+
|
|
|
218
|
+ public List<HandlingEvent> findEventsForCargo(TrackingId trackingId) {
|
|
|
219
|
+ return null;
|
|
|
220
|
+ }
|
|
|
221
|
+ };
|
|
|
222
|
+
|
|
|
223
|
+ // In-memory implementations
|
|
|
224
|
+ cargoRepository = new CargoRepositoryInMem();
|
|
|
225
|
+ locationRepository = new LocationRepositoryInMem();
|
|
|
226
|
+ voyageRepository = new VoyageRepositoryInMem();
|
|
|
227
|
+
|
|
|
228
|
+ // Actual factories and application services, wired with stubbed or in-memory infrastructure
|
|
|
229
|
+ trackingService = new TrackingServiceImpl(applicationEvents, cargoRepository);
|
|
|
230
|
+ handlingEventFactory = new HandlingEventFactory(cargoRepository, voyageRepository, locationRepository);
|
|
|
231
|
+ handlingEventService = new HandlingEventServiceImpl(handlingEventRepository, applicationEvents, handlingEventFactory);
|
|
|
232
|
+ bookingService = new BookingServiceImpl(cargoRepository, locationRepository, routingService);
|
|
|
233
|
+ }
|
|
|
234
|
+
|
|
|
235
|
+ private class SynchronousApplicationEventsStub implements ApplicationEvents {
|
|
|
236
|
+ @Override
|
|
|
237
|
+ public void cargoWasHandled(HandlingEvent event) {
|
|
|
238
|
+ System.out.println("EVENT: cargo was handled: " + event);
|
|
|
239
|
+ trackingService.inspectCargo(event.cargo().trackingId());
|
|
|
240
|
+ }
|
|
|
241
|
+
|
|
|
242
|
+ @Override
|
|
|
243
|
+ public void cargoWasMisdirected(Cargo cargo) {
|
|
|
244
|
+ System.out.println("EVENT: cargo was misdirected");
|
|
|
245
|
+ }
|
|
|
246
|
+
|
|
|
247
|
+ @Override
|
|
|
248
|
+ public void cargoHasArrived(Cargo cargo) {
|
|
|
249
|
+ System.out.println("EVENT: cargo has arrived: " + cargo.trackingId().idString());
|
|
|
250
|
+ }
|
|
|
251
|
+
|
|
|
252
|
+ @Override
|
|
|
253
|
+ public void rejectedHandlingEventRegistrationAttempt(HandlingEventRegistrationAttempt attempt, CannotCreateHandlingEventException problem) {
|
|
|
254
|
+ System.out.println("EVENT: rejected handling event registration attempt: " + problem);
|
|
|
255
|
+ }
|
|
|
256
|
+
|
|
|
257
|
+ @Override
|
|
|
258
|
+ public void receivedHandlingEventRegistrationAttempt(HandlingEventRegistrationAttempt attempt) {
|
|
|
259
|
+ System.out.println("EVENT: received handling event registration attempt");
|
|
|
260
|
+ }
|
|
|
261
|
+ }
|
|
|
262
|
+}
|