peter_backlund 17 lat temu
rodzic
commit
7159fb2902

+ 0
- 200
dddsample/src/test/java/se/citerus/dddsample/scenario/CargoHandlingScenarioTest.java Wyświetl plik

1
-package se.citerus.dddsample.scenario;
2
-
3
-import junit.framework.TestCase;
4
-import se.citerus.dddsample.application.HandlingEventRegistrationAttempt;
5
-import se.citerus.dddsample.application.HandlingEventService;
6
-import se.citerus.dddsample.application.SystemEvents;
7
-import se.citerus.dddsample.application.TrackingService;
8
-import se.citerus.dddsample.application.impl.HandlingEventServiceImpl;
9
-import se.citerus.dddsample.application.impl.TrackingServiceImpl;
10
-import se.citerus.dddsample.domain.model.cargo.*;
11
-import static se.citerus.dddsample.domain.model.carrier.SampleVoyages.*;
12
-import se.citerus.dddsample.domain.model.carrier.Voyage;
13
-import se.citerus.dddsample.domain.model.carrier.VoyageRepository;
14
-import se.citerus.dddsample.domain.model.handling.CannotCreateHandlingEventException;
15
-import se.citerus.dddsample.domain.model.handling.HandlingEvent;
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.service.RoutingService;
22
-import se.citerus.dddsample.infrastructure.persistence.inmemory.LocationRepositoryInMem;
23
-import se.citerus.dddsample.infrastructure.persistence.inmemory.VoyageRepositoryInMem;
24
-
25
-import java.util.*;
26
-
27
-/**
28
- * Cargo scenarios.
29
- */
30
-public class CargoHandlingScenarioTest extends TestCase {
31
-
32
-  HandlingEventFactory handlingEventFactory;
33
-
34
-  SystemEvents systemEvents;
35
-
36
-  HandlingEventService handlingEventService;
37
-  TrackingService trackingService;
38
-  RoutingService routingService;
39
-
40
-  HandlingEventRepository handlingEventRepository;
41
-  CargoRepository cargoRepository;
42
-  LocationRepository locationRepository;
43
-  VoyageRepository voyageRepository;
44
-
45
-  Cargo cargo;
46
-
47
-
48
-  public void testCargoFromHongkongToStockholm() throws Exception {
49
-
50
-    // Cargo from Hongkong to Stockholm (skipping the booking procedure here)
51
-    TrackingId trackingId = generateTrackingId();
52
-
53
-    Location origin = HONGKONG;
54
-    Location destination = STOCKHOLM;
55
-    Date arrivalDeadline = inTwoWeeks();
56
-    RouteSpecification routeSpecification = new RouteSpecification(origin, destination, arrivalDeadline);
57
-
58
-    cargo = new Cargo(trackingId, routeSpecification);
59
-
60
-    assertEquals(RoutingStatus.NOT_ROUTED, cargo.routingStatus());
61
-
62
-    // Route cargo
63
-    List<Itinerary> itineraries = routingService.fetchRoutesForSpecification(routeSpecification);
64
-    Itinerary itinerary = selectPreferedItinerary(itineraries);
65
-    cargo.assignToRoute(itinerary);
66
-
67
-    // Handling begins: cargo is received in Hongkong
68
-    handlingEventService.register(new HandlingEventRegistrationAttempt(
69
-      new Date(), new Date(), trackingId, null, HandlingEvent.Type.RECEIVE, HONGKONG.unLocode()
70
-    ));
71
-
72
-
73
-    // Loaded onto carrier movement CM003 in Hongkong
74
-    handlingEventService.register(new HandlingEventRegistrationAttempt(
75
-      new Date(), new Date(), trackingId, CM003.voyageNumber(), HandlingEvent.Type.LOAD, HONGKONG.unLocode()
76
-    ));
77
-
78
-    // Check current state - should be ok
79
-    assertEquals(CM003, cargo.delivery().currentVoyage());
80
-    assertEquals(HONGKONG, cargo.delivery().lastKnownLocation());
81
-    assertEquals(TransportStatus.ONBOARD_CARRIER, cargo.delivery().transportStatus());
82
-    assertFalse(cargo.isMisdirected());
83
-
84
-
85
-    // Cargo is now (incorrectly) unloaded in Tokyo
86
-    handlingEventService.register(new HandlingEventRegistrationAttempt(
87
-      new Date(), new Date(), trackingId, CM003.voyageNumber(), HandlingEvent.Type.UNLOAD, TOKYO.unLocode()
88
-    ));
89
-
90
-    // Check current state - cargo is misdirected!
91
-    assertEquals(Voyage.NONE, cargo.delivery().currentVoyage());
92
-    assertEquals(TOKYO, cargo.delivery().lastKnownLocation());
93
-    assertEquals(TransportStatus.IN_PORT, cargo.delivery().transportStatus());
94
-    assertTrue(cargo.isMisdirected());
95
-
96
-    // Then: Reroute!
97
-  }
98
-
99
-  
100
-
101
-  /*
102
-   * Utility stubs below.
103
-   */
104
-
105
-  private Date dateReceived() {
106
-    return new Date(100);
107
-  }
108
-
109
-  private Date dateLoadingCompleted() {
110
-    return new Date(200);
111
-  }
112
-
113
-  private Date dateUnloadingCompleted() {
114
-    return new Date(300);
115
-  }
116
-
117
-  private TrackingId generateTrackingId() {
118
-    return new TrackingId("TI1");
119
-  }
120
-
121
-  private Date inTwoWeeks() {
122
-    return new Date(new Date().getTime() + 1000*60*60*24*14);
123
-  }
124
-
125
-  private Itinerary selectPreferedItinerary(List<Itinerary> itineraries) {
126
-    return itineraries.get(0);
127
-  }
128
-
129
-  protected void setUp() throws Exception {
130
-
131
-    // Stub
132
-    routingService = new RoutingService() {
133
-      public List<Itinerary> fetchRoutesForSpecification(RouteSpecification routeSpecification) {
134
-        return Arrays.asList(
135
-          new Itinerary(Arrays.asList(
136
-                new Leg(CM003, HONGKONG, NEWYORK, new Date(), new Date()),
137
-                new Leg(CM004, NEWYORK, CHICAGO, new Date(), new Date()),
138
-                new Leg(CM005, CHICAGO, STOCKHOLM, new Date(), new Date())
139
-          ))
140
-        );
141
-      }
142
-    };
143
-
144
-    // Synchronous stub
145
-    systemEvents = new SystemEvents() {
146
-      @Override
147
-      public void cargoWasHandled(HandlingEvent event) {
148
-        trackingService.onCargoHandled(event.cargo().trackingId());
149
-      }
150
-      @Override
151
-      public void cargoWasMisdirected(Cargo cargo) {}
152
-      @Override
153
-      public void cargoHasArrived(Cargo cargo) {}
154
-      @Override
155
-      public void rejectHandlingEventRegistrationAttempt(HandlingEventRegistrationAttempt attempt, CannotCreateHandlingEventException problem) {}
156
-      @Override
157
-      public void receivedHandlingEventRegistrationAttempt(HandlingEventRegistrationAttempt attempt) {}
158
-    };
159
-
160
-    // Stub
161
-    cargoRepository = new CargoRepository() {
162
-      public Cargo find(TrackingId trackingId) {
163
-        return cargo;
164
-      }
165
-
166
-      public List<Cargo> findAll() {
167
-        return null;
168
-      }
169
-
170
-      public void save(Cargo cargo) {
171
-      }
172
-
173
-      public TrackingId nextTrackingId() {
174
-        return null;
175
-      }
176
-    };
177
-
178
-    // Stub
179
-    handlingEventRepository = new HandlingEventRepository() {
180
-      public void save(HandlingEvent event) {
181
-        Set<HandlingEvent> events = new HashSet<HandlingEvent>(cargo.delivery().history());
182
-        events.add(event);
183
-        CargoTestHelper.setDeliveryHistory(cargo, events);
184
-      }
185
-
186
-      public List<HandlingEvent> findEventsForCargo(TrackingId trackingId) {
187
-        return null;
188
-      }
189
-    };
190
-
191
-    // In-memory implementations
192
-    locationRepository = new LocationRepositoryInMem();
193
-    voyageRepository = new VoyageRepositoryInMem();
194
-
195
-    // Domain services and factories that are implemented in the domain layer - not stubbed
196
-    trackingService = new TrackingServiceImpl(systemEvents, cargoRepository);
197
-    handlingEventFactory = new HandlingEventFactory(cargoRepository, this.voyageRepository, this.locationRepository);
198
-    handlingEventService = new HandlingEventServiceImpl(handlingEventRepository, systemEvents, handlingEventFactory);
199
-  }
200
-}