Pārlūkot izejas kodu

Cargo now requires a route specification on creation.

peter_backlund 17 gadus atpakaļ
vecāks
revīzija
2fb2a8b87b

+ 8
- 8
dddsample/src/main/java/se/citerus/dddsample/application/util/SampleDataGenerator.java Parādīt failu

@@ -120,16 +120,16 @@ public class SampleDataGenerator implements ServletContextListener {
120 120
 
121 121
   private static void loadCargoData(JdbcTemplate jdbcTemplate) {
122 122
     String cargoSql =
123
-      "insert into Cargo (id, tracking_id, origin_id, destination_id, arrival_deadline) " +
124
-      "values (?, ?, ?, ?, ?)";
123
+      "insert into Cargo (id, tracking_id, origin_id, spec_origin_id, spec_destination_id, spec_arrival_deadline) " +
124
+      "values (?, ?, ?, ?, ?, ?)";
125 125
 
126 126
     Object[][] cargoArgs = {
127
-      {1, "XYZ", 1, 2, ts(10)},
128
-      {2, "ABC", 1, 5, ts(20)},
129
-      {3, "ZYX", 2, 1, ts(30)},
130
-      {4, "CBA", 5, 1, ts(40)},
131
-      {5, "FGH", 3, 5, ts(50)},
132
-      {6, "JKL", 6, 4, ts(60)}
127
+      {1, "XYZ", 1, 1, 2, ts(10)},
128
+      {2, "ABC", 1, 1, 5, ts(20)},
129
+      {3, "ZYX", 2, 2, 1, ts(30)},
130
+      {4, "CBA", 5, 5, 1, ts(40)},
131
+      {5, "FGH", 1, 3, 5, ts(50)},  // Cargo origin differs from spec origin
132
+      {6, "JKL", 6, 6, 4, ts(60)}
133 133
     };
134 134
     executeUpdate(jdbcTemplate, cargoSql, cargoArgs);
135 135
   }

+ 23
- 24
dddsample/src/main/java/se/citerus/dddsample/domain/model/cargo/Cargo.java Parādīt failu

@@ -7,8 +7,6 @@ import se.citerus.dddsample.domain.model.handling.HandlingEvent;
7 7
 import se.citerus.dddsample.domain.model.location.Location;
8 8
 import se.citerus.dddsample.domain.shared.DomainObjectUtils;
9 9
 
10
-import java.util.Date;
11
-
12 10
 /**
13 11
  * A Cargo. This is the central class in the domain model,
14 12
  * and it is the root of the Cargo-Itinerary-Leg-DeliveryHistory aggregate.
@@ -40,27 +38,22 @@ import java.util.Date;
40 38
 public class Cargo implements Entity<Cargo> {
41 39
 
42 40
   private TrackingId trackingId;
41
+  private Location origin;
43 42
   private Itinerary itinerary;
44 43
   private Delivery delivery;
45 44
   private RouteSpecification routeSpecification;
46 45
 
46
+  // TODO origin can be taken from route spec on creation, even if the origin never changes
47
+  public Cargo(final TrackingId trackingId, final Location origin, final RouteSpecification routeSpecification) {
48
+    Validate.notNull(trackingId, "Tracking id is required");
49
+    Validate.notNull(origin, "Origin location is required");
50
+    Validate.notNull(routeSpecification, "Route specification is required");
47 51
 
48
-  /**
49
-   * @param trackingId tracking id
50
-   * @param routeSpecification route specification
51
-   */
52
-  public Cargo(TrackingId trackingId, RouteSpecification routeSpecification) {
53
-    Validate.notNull(trackingId);
54
-    Validate.notNull(routeSpecification);
55 52
     this.trackingId = trackingId;
53
+    this.origin = origin;
56 54
     this.routeSpecification = routeSpecification;
57 55
   }
58 56
 
59
-  // TODO remove this, only present during migration to other ctor
60
-  public Cargo(TrackingId trackingId, Location origin, Location destination) {
61
-    this(trackingId, new RouteSpecification(origin, destination, new Date()));
62
-  }
63
-
64 57
 
65 58
   /**
66 59
    * The tracking id is the identity of this entity, and is unique.
@@ -75,14 +68,7 @@ public class Cargo implements Entity<Cargo> {
75 68
    * @return Origin location.
76 69
    */
77 70
   public Location origin() {
78
-    return routeSpecification.origin();
79
-  }
80
-
81
-  /**
82
-   * @return Destination of the cargo.
83
-   */
84
-  public Location destination() {
85
-    return routeSpecification.destination();
71
+    return origin;
86 72
   }
87 73
 
88 74
   /**
@@ -100,10 +86,17 @@ public class Cargo implements Entity<Cargo> {
100 86
   }
101 87
 
102 88
   /**
89
+   * @return The route specification.
90
+   */
91
+  public RouteSpecification routeSpecification() {
92
+    return routeSpecification;
93
+  }
94
+  
95
+  /**
103 96
    * @return True if the cargo has arrived at its destination.
104 97
    */
105 98
   public boolean hasArrived() {
106
-    return destination().equals(delivery.lastKnownLocation());
99
+    return routeSpecification.destination().equals(delivery.lastKnownLocation());
107 100
   }
108 101
 
109 102
   /**
@@ -180,13 +173,14 @@ public class Cargo implements Entity<Cargo> {
180 173
   public boolean isUnloadedAtDestination() {
181 174
     for (HandlingEvent event : delivery().history()) {
182 175
       if (HandlingEvent.Type.UNLOAD.equals(event.type())
183
-        && destination().equals(event.location())) {
176
+        && routeSpecification.destination().equals(event.location())) {
184 177
         return true;
185 178
       }
186 179
     }
187 180
     return false;
188 181
   }
189 182
 
183
+  @Override
190 184
   public boolean sameIdentityAs(final Cargo other) {
191 185
     return other != null && trackingId.sameValueAs(other.trackingId);
192 186
   }
@@ -213,6 +207,11 @@ public class Cargo implements Entity<Cargo> {
213 207
     return trackingId.hashCode();
214 208
   }
215 209
 
210
+  @Override
211
+  public String toString() {
212
+    return trackingId.toString();
213
+  }
214
+
216 215
   Cargo() {
217 216
     // Needed by Hibernate
218 217
   }

+ 5
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/model/cargo/TrackingId.java Parādīt failu

@@ -53,6 +53,11 @@ public final class TrackingId implements ValueObject<TrackingId> {
53 53
     return new TrackingId(id);
54 54
   }
55 55
 
56
+  @Override
57
+  public String toString() {
58
+    return id;
59
+  }
60
+
56 61
   TrackingId() {
57 62
     // Needed by Hibernate
58 63
   }

+ 5
- 3
dddsample/src/main/resources/se/citerus/dddsample/infrastructure/persistence/hibernate/Cargo.hbm.xml Parādīt failu

@@ -11,6 +11,8 @@
11 11
       <generator class="org.hibernate.id.IdentityGenerator"/>
12 12
     </id>
13 13
 
14
+    <many-to-one name="origin" column="origin_id" not-null="false" cascade="none" update="false" foreign-key="origin_fk"/>
15
+
14 16
     <component name="trackingId" unique="true" update="false">
15 17
       <property name="id" column="tracking_id"/>
16 18
     </component>
@@ -23,9 +25,9 @@
23 25
     </component>
24 26
 
25 27
     <component name="routeSpecification">
26
-      <many-to-one name="origin" column="origin_id" cascade="none" update="false" foreign-key="origin_fk"/>
27
-      <many-to-one name="destination" column="destination_id" cascade="none" foreign-key="destination_fk"/>
28
-      <property name="arrivalDeadline" column="arrival_deadline" not-null="true"/>
28
+      <many-to-one name="origin" column="spec_origin_id" cascade="none" update="false" foreign-key="spec_origin_fk"/>
29
+      <many-to-one name="destination" column="spec_destination_id" cascade="none" foreign-key="spec_destination_fk"/>
30
+      <property name="arrivalDeadline" column="spec_arrival_deadline" not-null="true"/>
29 31
     </component>
30 32
 
31 33
     <component name="itinerary">

+ 16
- 20
dddsample/src/test/java/se/citerus/dddsample/domain/model/cargo/CargoTest.java Parādīt failu

@@ -29,10 +29,10 @@ public class CargoTest extends TestCase {
29 29
   }
30 30
 
31 31
   public void testRoutingStatus() throws Exception {
32
-    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, MELBOURNE);
32
+    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, new RouteSpecification(STOCKHOLM, MELBOURNE, new Date()));
33 33
     final Itinerary good = new Itinerary();
34 34
     final Itinerary bad = new Itinerary();
35
-    final RouteSpecification acceptOnlyGood = new RouteSpecification(cargo.origin(), cargo.destination(), new Date()) {
35
+    final RouteSpecification acceptOnlyGood = new RouteSpecification(cargo.origin(), cargo.routeSpecification().destination(), new Date()) {
36 36
       @Override
37 37
       public boolean isSatisfiedBy(Itinerary itinerary) {
38 38
         return itinerary == good;
@@ -51,7 +51,7 @@ public class CargoTest extends TestCase {
51 51
   }
52 52
 
53 53
   public void testlastKnownLocationUnknownWhenNoEvents() throws Exception {
54
-    Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, MELBOURNE);
54
+    Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, new RouteSpecification(STOCKHOLM, MELBOURNE, new Date()));
55 55
 
56 56
     assertEquals(Location.UNKNOWN, cargo.delivery().lastKnownLocation());
57 57
   }
@@ -93,10 +93,12 @@ public class CargoTest extends TestCase {
93 93
   }
94 94
 
95 95
   public void testEquality() throws Exception {
96
-    Cargo c1 = new Cargo(new TrackingId("ABC"), STOCKHOLM, HONGKONG);
97
-    Cargo c2 = new Cargo(new TrackingId("CBA"), STOCKHOLM, HONGKONG);
98
-    Cargo c3 = new Cargo(new TrackingId("ABC"), STOCKHOLM, MELBOURNE);
99
-    Cargo c4 = new Cargo(new TrackingId("ABC"), STOCKHOLM, HONGKONG);
96
+    RouteSpecification spec1 = new RouteSpecification(STOCKHOLM, HONGKONG, new Date());
97
+    RouteSpecification spec2 = new RouteSpecification(STOCKHOLM, MELBOURNE, new Date());
98
+    Cargo c1 = new Cargo(new TrackingId("ABC"), STOCKHOLM, spec1);
99
+    Cargo c2 = new Cargo(new TrackingId("CBA"), STOCKHOLM, spec1);
100
+    Cargo c3 = new Cargo(new TrackingId("ABC"), STOCKHOLM, spec2);
101
+    Cargo c4 = new Cargo(new TrackingId("ABC"), STOCKHOLM, spec1);
100 102
 
101 103
     assertTrue("Cargos should be equal when TrackingIDs are equal", c1.equals(c4));
102 104
     assertTrue("Cargos should be equal when TrackingIDs are equal", c1.equals(c3));
@@ -163,7 +165,7 @@ public class CargoTest extends TestCase {
163 165
 
164 166
   // TODO: Generate test data some better way
165 167
   private Cargo populateCargoReceivedStockholm() throws Exception {
166
-    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, MELBOURNE);
168
+    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, new RouteSpecification(STOCKHOLM, MELBOURNE, new Date()));
167 169
 
168 170
     HandlingEvent he = new HandlingEvent(cargo, getDate("2007-12-01"), new Date(), HandlingEvent.Type.RECEIVE, STOCKHOLM);
169 171
     events.add(he);
@@ -182,7 +184,7 @@ public class CargoTest extends TestCase {
182 184
   }
183 185
 
184 186
   private Cargo populateCargoOffHongKong() throws Exception {
185
-    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, MELBOURNE);
187
+    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, new RouteSpecification(STOCKHOLM, MELBOURNE, new Date()));
186 188
 
187 189
 
188 190
     events.add(new HandlingEvent(cargo, getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, STOCKHOLM, voyage));
@@ -196,7 +198,7 @@ public class CargoTest extends TestCase {
196 198
   }
197 199
 
198 200
   private Cargo populateCargoOnHamburg() throws Exception {
199
-    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, MELBOURNE);
201
+    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, new RouteSpecification(STOCKHOLM, MELBOURNE, new Date()));
200 202
 
201 203
     events.add(new HandlingEvent(cargo, getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, STOCKHOLM, voyage));
202 204
     events.add(new HandlingEvent(cargo, getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, HAMBURG, voyage));
@@ -207,7 +209,7 @@ public class CargoTest extends TestCase {
207 209
   }
208 210
 
209 211
   private Cargo populateCargoOffMelbourne() throws Exception {
210
-    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, MELBOURNE);
212
+    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, new RouteSpecification(STOCKHOLM, MELBOURNE, new Date()));
211 213
 
212 214
     events.add(new HandlingEvent(cargo, getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, STOCKHOLM, voyage));
213 215
     events.add(new HandlingEvent(cargo, getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, HAMBURG, voyage));
@@ -223,7 +225,7 @@ public class CargoTest extends TestCase {
223 225
   }
224 226
 
225 227
   private Cargo populateCargoOnHongKong() throws Exception {
226
-    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, MELBOURNE);
228
+    final Cargo cargo = new Cargo(new TrackingId("XYZ"), STOCKHOLM, new RouteSpecification(STOCKHOLM, MELBOURNE, new Date()));
227 229
 
228 230
     events.add(new HandlingEvent(cargo, getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, STOCKHOLM, voyage));
229 231
     events.add(new HandlingEvent(cargo, getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, HAMBURG, voyage));
@@ -239,7 +241,7 @@ public class CargoTest extends TestCase {
239 241
 
240 242
   public void testIsMisdirected() throws Exception {
241 243
     //A cargo with no itinerary is not misdirected
242
-    Cargo cargo = new Cargo(new TrackingId("TRKID"), SHANGHAI, GOTHENBURG);
244
+    Cargo cargo = new Cargo(new TrackingId("TRKID"), SHANGHAI, new RouteSpecification(SHANGHAI, GOTHENBURG, new Date()));
243 245
     assertFalse(cargo.isMisdirected());
244 246
 
245 247
     cargo = setUpCargoWithItinerary(SHANGHAI, ROTTERDAM, GOTHENBURG);
@@ -249,12 +251,6 @@ public class CargoTest extends TestCase {
249 251
 
250 252
     Collection<HandlingEvent> handlingEvents = new ArrayList<HandlingEvent>();
251 253
 
252
-    /*
253
-    CarrierMovement abc = new CarrierMovement(new CarrierMovementId("ABC"), SHANGHAI, ROTTERDAM, new Date(), new Date());
254
-    CarrierMovement def = new CarrierMovement(new CarrierMovementId("DEF"), ROTTERDAM, GOTHENBURG, new Date(), new Date());
255
-    CarrierMovement ghi = new CarrierMovement(new CarrierMovementId("GHI"), ROTTERDAM, NEWYORK, new Date(), new Date());
256
-    */
257
-    
258 254
     //Happy path
259 255
     handlingEvents.add(new HandlingEvent(cargo, new Date(10), new Date(20), HandlingEvent.Type.RECEIVE, SHANGHAI));
260 256
     handlingEvents.add(new HandlingEvent(cargo, new Date(30), new Date(40), HandlingEvent.Type.LOAD, SHANGHAI, voyage));
@@ -309,7 +305,7 @@ public class CargoTest extends TestCase {
309 305
   }
310 306
 
311 307
   private Cargo setUpCargoWithItinerary(Location origin, Location midpoint, Location destination) {
312
-    Cargo cargo = new Cargo(new TrackingId("CARGO1"), origin, destination);
308
+    Cargo cargo = new Cargo(new TrackingId("CARGO1"), origin, new RouteSpecification(origin, destination, new Date()));
313 309
 
314 310
     Itinerary itinerary = new Itinerary(
315 311
       Arrays.asList(

+ 7
- 4
dddsample/src/test/java/se/citerus/dddsample/domain/model/cargo/CargoTestHelper.java Parādīt failu

@@ -4,6 +4,7 @@ import se.citerus.dddsample.domain.model.handling.HandlingEvent;
4 4
 import se.citerus.dddsample.domain.model.location.Location;
5 5
 
6 6
 import java.util.Collection;
7
+import java.util.Date;
7 8
 
8 9
 /**
9 10
  * For easy testdata creation.
@@ -11,11 +12,13 @@ import java.util.Collection;
11 12
  */
12 13
 public class CargoTestHelper {
13 14
 
14
-  public static Cargo createCargoWithDeliveryHistory(
15
-    TrackingId trackingId, Location origin, Location destination,
16
-    Collection<HandlingEvent> events) {
15
+  public static Cargo createCargoWithDeliveryHistory(TrackingId trackingId,
16
+                                                     Location origin,
17
+                                                     Location destination,
18
+                                                     Collection<HandlingEvent> events) {
17 19
 
18
-    final Cargo cargo = new Cargo(trackingId, origin, destination);
20
+    final RouteSpecification routeSpecification = new RouteSpecification(origin, destination, new Date());
21
+    final Cargo cargo = new Cargo(trackingId, origin, routeSpecification);
19 22
     setDeliveryHistory(cargo, events);
20 23
 
21 24
     return cargo;

+ 1
- 1
dddsample/src/test/java/se/citerus/dddsample/domain/model/cargo/DeliveryTest.java Parādīt failu

@@ -12,7 +12,7 @@ import java.util.*;
12 12
 
13 13
 public class DeliveryTest extends TestCase {
14 14
 
15
-  private Cargo cargo = new Cargo(new TrackingId("XYZ"), HONGKONG, NEWYORK);
15
+  private Cargo cargo = new Cargo(new TrackingId("XYZ"), HONGKONG, new RouteSpecification(HONGKONG, NEWYORK, new Date()));
16 16
 
17 17
   public void testEvensOrderedByTimeOccured() throws Exception {
18 18
     DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

+ 1
- 1
dddsample/src/test/java/se/citerus/dddsample/domain/model/cargo/ItineraryTest.java Parādīt failu

@@ -36,7 +36,7 @@ public class ItineraryTest extends TestCase {
36 36
 
37 37
     TrackingId trackingId = new TrackingId("CARGO1");
38 38
     RouteSpecification routeSpecification = new RouteSpecification(SHANGHAI, GOTHENBURG, new Date());
39
-    Cargo cargo = new Cargo(trackingId, routeSpecification);
39
+    Cargo cargo = new Cargo(trackingId, SHANGHAI, routeSpecification);
40 40
 
41 41
     Itinerary itinerary = new Itinerary(
42 42
       Arrays.asList(