Kaynağa Gözat

Created test for handling event factory

peter_backlund 18 yıl önce
ebeveyn
işleme
141f4abc43

+ 129
- 0
dddsample/src/test/java/se/citerus/dddsample/domain/model/handling/HandlingEventFactoryTest.java Dosyayı Görüntüle

@@ -0,0 +1,129 @@
1
+package se.citerus.dddsample.domain.model.handling;
2
+
3
+import junit.framework.TestCase;
4
+import static org.easymock.EasyMock.*;
5
+import se.citerus.dddsample.application.persistence.CarrierMovementRepositoryInMem;
6
+import se.citerus.dddsample.application.persistence.LocationRepositoryInMem;
7
+import se.citerus.dddsample.domain.model.cargo.Cargo;
8
+import se.citerus.dddsample.domain.model.cargo.CargoRepository;
9
+import se.citerus.dddsample.domain.model.cargo.TrackingId;
10
+import se.citerus.dddsample.domain.model.carrier.CarrierMovement;
11
+import se.citerus.dddsample.domain.model.carrier.CarrierMovementId;
12
+import se.citerus.dddsample.domain.model.carrier.CarrierMovementRepository;
13
+import static se.citerus.dddsample.domain.model.carrier.SampleCarrierMovements.CM001;
14
+import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type;
15
+import se.citerus.dddsample.domain.model.location.LocationRepository;
16
+import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
17
+import se.citerus.dddsample.domain.model.location.UnLocode;
18
+import se.citerus.dddsample.domain.service.UnknownCarrierMovementIdException;
19
+import se.citerus.dddsample.domain.service.UnknownLocationException;
20
+import se.citerus.dddsample.domain.service.UnknownTrackingIdException;
21
+
22
+import java.util.Date;
23
+
24
+public class HandlingEventFactoryTest extends TestCase {
25
+
26
+  HandlingEventFactory factory;
27
+  CargoRepository cargoRepository;
28
+  CarrierMovementRepository carrierMovementRepository;
29
+  LocationRepository locationRepository;
30
+  TrackingId trackingId;
31
+  Cargo cargo;
32
+
33
+  protected void setUp() throws Exception {
34
+    factory = new HandlingEventFactory();
35
+
36
+    cargoRepository = createMock(CargoRepository.class);
37
+    factory.setCargoRepository(cargoRepository);
38
+
39
+    carrierMovementRepository = new CarrierMovementRepositoryInMem();
40
+    factory.setCarrierMovementRepository(carrierMovementRepository);
41
+
42
+    locationRepository = new LocationRepositoryInMem();
43
+    factory.setLocationRepository(locationRepository);
44
+    trackingId = new TrackingId("ABC");
45
+    cargo = new Cargo(trackingId, TOKYO, HELSINKI);
46
+  }
47
+
48
+  public void testCreateHandlingEventWithCarrierMovement() throws Exception {
49
+    expect(cargoRepository.find(trackingId)).andReturn(cargo);
50
+
51
+    replay(cargoRepository);
52
+
53
+    CarrierMovementId carrierMovementId = CM001.carrierMovementId();
54
+    UnLocode unLocode = STOCKHOLM.unLocode();
55
+    HandlingEvent handlingEvent = factory.createHandlingEvent(
56
+      new Date(100), trackingId, carrierMovementId, unLocode, Type.LOAD
57
+    );
58
+
59
+    assertNotNull(handlingEvent);
60
+    assertEquals(STOCKHOLM, handlingEvent.location());
61
+    assertEquals(CM001, handlingEvent.carrierMovement());
62
+    assertEquals(cargo, handlingEvent.cargo());
63
+    assertEquals(new Date(100), handlingEvent.completionTime());
64
+    assertTrue(handlingEvent.registrationTime().before(new Date(System.currentTimeMillis() + 1)));
65
+  }
66
+
67
+  public void testCreateHandlingEventWithoutCarrierMovement() throws Exception {
68
+    expect(cargoRepository.find(trackingId)).andReturn(cargo);
69
+
70
+    replay(cargoRepository);
71
+
72
+    UnLocode unLocode = STOCKHOLM.unLocode();
73
+    HandlingEvent handlingEvent = factory.createHandlingEvent(
74
+      new Date(100), trackingId, null, unLocode, Type.CLAIM
75
+    );
76
+
77
+    assertNotNull(handlingEvent);
78
+    assertEquals(STOCKHOLM, handlingEvent.location());
79
+    assertEquals(CarrierMovement.NONE, handlingEvent.carrierMovement());
80
+    assertEquals(cargo, handlingEvent.cargo());
81
+    assertEquals(new Date(100), handlingEvent.completionTime());
82
+    assertTrue(handlingEvent.registrationTime().before(new Date(System.currentTimeMillis() + 1)));
83
+  }
84
+
85
+  public void testCreateHandlingEventUnknownLocation() throws Exception {
86
+    expect(cargoRepository.find(trackingId)).andReturn(cargo);
87
+
88
+    replay(cargoRepository);
89
+
90
+    UnLocode invalid = new UnLocode("NOEXT");
91
+    try {
92
+      factory.createHandlingEvent(
93
+        new Date(100), trackingId, CM001.carrierMovementId(), invalid, Type.LOAD
94
+      );
95
+      fail("Expected UnknownLocationException");
96
+    } catch (UnknownLocationException expected) {}
97
+  }
98
+
99
+  public void testCreateHandlingEventUnknownCarrierMovement() throws Exception {
100
+    expect(cargoRepository.find(trackingId)).andReturn(cargo);
101
+
102
+    replay(cargoRepository);
103
+
104
+    try {
105
+      CarrierMovementId invalid = new CarrierMovementId("XXX");
106
+      factory.createHandlingEvent(
107
+        new Date(100), trackingId, invalid, STOCKHOLM.unLocode(), Type.LOAD
108
+      );
109
+      fail("Expected UnknownCarrierMovementIdException");
110
+    } catch (UnknownCarrierMovementIdException expected) {}
111
+  }
112
+
113
+  public void testCreateHandlingEventUnknownTrackingId() throws Exception {
114
+    expect(cargoRepository.find(trackingId)).andReturn(null);
115
+
116
+    replay(cargoRepository);
117
+
118
+    try {
119
+      factory.createHandlingEvent(
120
+        new Date(100), trackingId, CM001.carrierMovementId(), STOCKHOLM.unLocode(), Type.LOAD
121
+      );
122
+      fail("Expected UnknownTrackingIdException");
123
+    } catch (UnknownTrackingIdException expected) {}
124
+  }
125
+
126
+  protected void tearDown() throws Exception {
127
+    verify(cargoRepository);
128
+  }
129
+}