Просмотр исходного кода

Moved scenario tests into separate subpackage under src/test

peter_backlund 18 лет назад
Родитель
Сommit
6e95839829

+ 192
- 0
dddsample/src/test/java/se/citerus/dddsample/scenario/CargoHandlingScenarioTest.java Просмотреть файл

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

+ 68
- 0
dddsample/src/test/java/se/citerus/dddsample/scenario/TrackingScenarioTest.java Просмотреть файл

@@ -0,0 +1,68 @@
1
+package se.citerus.dddsample.scenario;
2
+
3
+import junit.framework.TestCase;
4
+import se.citerus.dddsample.domain.model.cargo.Cargo;
5
+import se.citerus.dddsample.domain.model.cargo.CargoTestHelper;
6
+import se.citerus.dddsample.domain.model.cargo.Delivery;
7
+import se.citerus.dddsample.domain.model.cargo.TrackingId;
8
+import static se.citerus.dddsample.domain.model.carrier.SampleVoyages.CM001;
9
+import static se.citerus.dddsample.domain.model.carrier.SampleVoyages.CM002;
10
+import se.citerus.dddsample.domain.model.handling.HandlingEvent;
11
+import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.LOAD;
12
+import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.UNLOAD;
13
+import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
14
+
15
+import java.text.DateFormat;
16
+import java.text.ParseException;
17
+import java.text.SimpleDateFormat;
18
+import java.util.Arrays;
19
+import java.util.Date;
20
+import java.util.List;
21
+
22
+public class TrackingScenarioTest extends TestCase {
23
+
24
+  public void testTrackingScenarioStage1() throws Exception {
25
+
26
+    Cargo cargo = populateCargo();
27
+
28
+    Delivery delivery = cargo.delivery();
29
+
30
+    List<HandlingEvent> handlingEvents = delivery.history();
31
+
32
+    assertEquals(4, handlingEvents.size());
33
+    final HandlingEvent event = delivery.lastEvent();
34
+
35
+    assertEquals(UNLOAD, event.type());
36
+
37
+//    assertFalse(cargo.atFinalDestiation());
38
+//    assertEquals("CNHKG", cargo.currentLocation().unlocode());
39
+
40
+  }
41
+
42
+  private Cargo populateCargo() throws Exception {
43
+    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, MELBOURNE);
44
+
45
+    final List<HandlingEvent> handlingEventList = Arrays.asList(
46
+      new HandlingEvent(cargo, getDate("2007-12-01"), new Date(), LOAD, STOCKHOLM, CM001),
47
+      new HandlingEvent(cargo, getDate("2007-12-02"), new Date(), UNLOAD, HAMBURG, CM001),
48
+      new HandlingEvent(cargo, getDate("2007-12-03"), new Date(), LOAD, HAMBURG, CM002),
49
+      new HandlingEvent(cargo, getDate("2007-12-05"), new Date(), UNLOAD, HONGKONG, CM002)
50
+    );
51
+    CargoTestHelper.setDeliveryHistory(cargo, handlingEventList);
52
+
53
+    return cargo;
54
+  }
55
+
56
+  /**
57
+   * Parse an ISO 8601 (YYYY-MM-DD) String to Date
58
+   *
59
+   * @param isoFormat String to parse.
60
+   * @return Created date instance.
61
+   * @throws ParseException Thrown if parsing fails.
62
+   */
63
+  private Date getDate(String isoFormat) throws ParseException {
64
+    final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
65
+    return dateFormat.parse(isoFormat);
66
+  }
67
+
68
+}