Explorar el Código

Added Location to HandlingEvent

jorgen_falk hace 18 años
padre
commit
c6fe368584

+ 7
- 7
dddsample/src/main/java/se/citerus/dddsample/domain/Cargo.java Ver fichero

@@ -48,6 +48,13 @@ public class Cargo {
48 48
     return currentLocation().equals(finalDestination);
49 49
   }
50 50
 
51
+  /**
52
+   * Returns the last known Location or Location.UNKOWN if no HandlingEvent history can be found for this Cargo
53
+   * 
54
+   * TODO: Rename this to lastKnownLocation. 
55
+   * 
56
+   * @return The last known location
57
+   */
51 58
   public Location currentLocation() {
52 59
     HandlingEvent lastEvent = history.lastEvent();
53 60
     
@@ -57,13 +64,6 @@ public class Cargo {
57 64
     }
58 65
    
59 66
     Location location = lastEvent.location();
60
-    
61
-    // If the last handling event has no idea of where the cargo is due to lack of CarrierMovement (like for CLAIM or RECEIVE events)
62
-    // location must be calculated based on event type and origin or final destination
63
-    // TODO: Maybe we need to refactor HandlingEvent.
64
-    if (location == Location.UNKNOWN){
65
-      location = (lastEvent.type() == HandlingEvent.Type.CLAIM) ? finalDestination : origin;
66
-    }
67 67
       
68 68
     return location;
69 69
   }

+ 21
- 34
dddsample/src/main/java/se/citerus/dddsample/domain/HandlingEvent.java Ver fichero

@@ -3,6 +3,7 @@ package se.citerus.dddsample.domain;
3 3
 import org.apache.commons.lang.builder.EqualsBuilder;
4 4
 
5 5
 import javax.persistence.*;
6
+
6 7
 import java.util.Date;
7 8
 import java.util.HashSet;
8 9
 import java.util.Set;
@@ -32,6 +33,9 @@ public class HandlingEvent {
32 33
   private Date timeOccurred;
33 34
 
34 35
   private Date timeRegistered;
36
+  
37
+  @ManyToOne
38
+  private Location location;
35 39
 
36 40
   @Transient // TODO: cargo-event relation should not be bidirectional
37 41
   private Set<Cargo> cargos;
@@ -40,17 +44,18 @@ public class HandlingEvent {
40 44
     LOAD, UNLOAD, RECEIVE, CLAIM
41 45
   }
42 46
 
43
-  public HandlingEvent(Date timeOccurred, Date timeRegistered, Type type) {
44
-    this(timeOccurred, timeRegistered, type, null);
47
+  public HandlingEvent(Date timeOccurred, Date timeRegistered, Type type, Location location) {
48
+    this(timeOccurred, timeRegistered, type, location, null);
45 49
   }
46 50
 
47
-  public HandlingEvent(Date timeOccurred, Date timeRegistered, Type type, CarrierMovement carrierMovement) {
51
+  public HandlingEvent(Date timeOccurred, Date timeRegistered, Type type, Location location, CarrierMovement carrierMovement) {
48 52
     this.id = UUID.randomUUID();
49 53
     this.timeRegistered = timeRegistered;
50 54
     this.timeOccurred = timeOccurred;
51 55
     this.type = type;
52 56
     this.carrierMovement = carrierMovement;
53 57
     this.cargos = new HashSet<Cargo>();
58
+    this.location = location;
54 59
   }
55 60
 
56 61
   /**
@@ -87,52 +92,34 @@ public class HandlingEvent {
87 92
   }
88 93
 
89 94
   /**
90
-   * Returns the Location of the Cargo. The location is calculated based on the following rules:
91
-   * <br>For
92
-   * <ul>
93
-   * <li> RECEIVE events: Location.UNKNOWN is returned. This basically means that the cargo is at its origin but not yet loaded on a CarrierMovment
94
-   * <li> CLAIM events: Location.UNKNOWN is returned. This means that the cargo is at its final destination and has been unloaded and claimed by the customer.
95
-   * <li> LOAD events: The from Location is returned.
96
-   * <li> UNLOAD events: The to Location is returned.
97
-   * </ul> 
95
+   * Returns the Location of the HandlingEvent
98 96
    * 
99 97
    * @return The Location
100 98
    */
101
-  public Location location() {
102
-    Location location = Location.UNKNOWN;
103
-    
104
-    //My gosh! A switch statement....
105
-    switch (type) {
106
-      case LOAD:
107
-        location = carrierMovement.from();
108
-      break;
109
-
110
-      case UNLOAD:
111
-        location = carrierMovement.to();
112
-      break;
113
-      
114
-      default: 
115
-        // for others (RECEIVE, CLAIM) Location.NULL is fine...
116
-      break;
117
-    }
118
-    
99
+  public Location location() {    
119 100
     return location;
120 101
   }
121 102
 
122 103
 
123 104
   /**
124
-   * Register a set of Cargos
105
+   * Add a Cargo to this HandlingEvent
125 106
    * 
126
-   * @param cargosToRegister
107
+   * @param cargo
127 108
    */
128
-  public void register(Set<Cargo> cargosToRegister) {
129
-    this.cargos.addAll(cargosToRegister);
109
+  public void add(Cargo cargo) {
110
+    this.cargos.add(cargo);
130 111
   }
131 112
   
132
-  public Set<Cargo> registerdCargos(){
113
+  /**
114
+   * Returns a Set of Cargos associated with this HandlingEvent
115
+   * 
116
+   * @return The associated Cargos
117
+   */
118
+  public Set<Cargo> cargos(){
133 119
     return cargos;
134 120
   }
135 121
 
122
+
136 123
   @Override
137 124
   public boolean equals(Object obj) {
138 125
     if (!(obj instanceof HandlingEvent)) {

+ 19
- 29
dddsample/src/main/java/se/citerus/dddsample/repository/HandlingEventRepositoryInMem.java Ver fichero

@@ -34,63 +34,53 @@ public class HandlingEventRepositoryInMem implements HandlingEventRepository{
34 34
     
35 35
     // CargoXYZ
36 36
     final Cargo cargoXYZ = new Cargo(new TrackingId("XYZ"), new Location("SESTO"), new Location("AUMEL"));
37
-    registerEvent(cargoXYZ, "2007-11-30", HandlingEvent.Type.RECEIVE, null);
37
+    registerEvent(cargoXYZ, "2007-11-30", HandlingEvent.Type.RECEIVE, new Location("SESTO"), null);
38 38
 
39 39
     final CarrierMovement stockholmToHamburg = carrierMovementRepository.find(new CarrierId("SESTO_DEHAM"));
40
-    registerEvent(cargoXYZ, "2007-12-01", HandlingEvent.Type.LOAD, stockholmToHamburg);
41
-    registerEvent(cargoXYZ, "2007-12-02", HandlingEvent.Type.UNLOAD, stockholmToHamburg);
40
+    registerEvent(cargoXYZ, "2007-12-01", HandlingEvent.Type.LOAD, new Location("SESTO"), stockholmToHamburg);
41
+    registerEvent(cargoXYZ, "2007-12-02", HandlingEvent.Type.UNLOAD, new Location("DEHAM"), stockholmToHamburg);
42 42
     
43 43
     final CarrierMovement hamburgToHongKong = carrierMovementRepository.find(new CarrierId("DEHAM_CNHKG"));
44
-    registerEvent(cargoXYZ, "2007-12-03", HandlingEvent.Type.LOAD, hamburgToHongKong);
45
-    registerEvent(cargoXYZ, "2007-12-05", HandlingEvent.Type.UNLOAD, hamburgToHongKong);
44
+    registerEvent(cargoXYZ, "2007-12-03", HandlingEvent.Type.LOAD, new Location("DEHAM"), hamburgToHongKong);
45
+    registerEvent(cargoXYZ, "2007-12-05", HandlingEvent.Type.UNLOAD, new Location("CNHKG"), hamburgToHongKong);
46 46
     
47 47
     //CargoZYX
48 48
     final Cargo cargoZYX = new Cargo(new TrackingId("ZYX"), new Location("AUMEL"), new Location("SESTO"));
49
-    registerEvent(cargoZYX, "2007-12-09", HandlingEvent.Type.RECEIVE, null);
49
+    registerEvent(cargoZYX, "2007-12-09", HandlingEvent.Type.RECEIVE, new Location("AUMEL"), null);
50 50
     
51 51
     final CarrierMovement melbourneToTokyo = carrierMovementRepository.find(new CarrierId("AUMEL_JPTOK"));
52
-    registerEvent(cargoZYX, "2007-12-10", HandlingEvent.Type.LOAD, melbourneToTokyo);
53
-    registerEvent(cargoZYX, "2007-12-12", HandlingEvent.Type.UNLOAD, melbourneToTokyo);
52
+    registerEvent(cargoZYX, "2007-12-10", HandlingEvent.Type.LOAD, new Location("AUMEL"), melbourneToTokyo);
53
+    registerEvent(cargoZYX, "2007-12-12", HandlingEvent.Type.UNLOAD, new Location("JPTOK"), melbourneToTokyo);
54 54
     
55 55
     final CarrierMovement tokyoToLosAngeles = carrierMovementRepository.find(new CarrierId("JPTOK_USLA"));
56
-    registerEvent(cargoZYX, "2007-12-13", HandlingEvent.Type.LOAD, tokyoToLosAngeles);
56
+    registerEvent(cargoZYX, "2007-12-13", HandlingEvent.Type.LOAD, new Location("JPTOK"), tokyoToLosAngeles);
57 57
  
58 58
     //CargoABC
59 59
     final Cargo cargoABC = new Cargo(new TrackingId("ABC"), new Location("SESTO"), new Location("FIHEL"));
60
-    registerEvent(cargoABC, "2008-01-01", HandlingEvent.Type.RECEIVE, null);
60
+    registerEvent(cargoABC, "2008-01-01", HandlingEvent.Type.RECEIVE, new Location("SESTO"), null);
61 61
     
62 62
     final CarrierMovement stockholmToHelsinki = new CarrierMovement(
63 63
             new CarrierId("CAR_001"), new Location("SESTO"), new Location("FIHEL"));
64 64
 
65
-    registerEvent(cargoABC, "2008-01-02", HandlingEvent.Type.LOAD, stockholmToHelsinki);
66
-    registerEvent(cargoABC, "2008-01-03", HandlingEvent.Type.UNLOAD, stockholmToHelsinki);
67
-    registerEvent(cargoABC, "2008-01-05", HandlingEvent.Type.CLAIM, null);
65
+    registerEvent(cargoABC, "2008-01-02", HandlingEvent.Type.LOAD, new Location("SESTO"), stockholmToHelsinki);
66
+    registerEvent(cargoABC, "2008-01-03", HandlingEvent.Type.UNLOAD, new Location("FIHEL"), stockholmToHelsinki);
67
+    registerEvent(cargoABC, "2008-01-05", HandlingEvent.Type.CLAIM, new Location("FIHEL"), null);
68 68
 
69 69
     //CargoCBA
70 70
     final Cargo cargoCBA = new Cargo(new TrackingId("CBA"), new Location("FIHEL"), new Location("SESTO"));
71
-    registerEvent(cargoCBA, "2008-01-10", HandlingEvent.Type.RECEIVE, null);
71
+    registerEvent(cargoCBA, "2008-01-10", HandlingEvent.Type.RECEIVE, new Location("FIHEL"), null);
72 72
   }
73 73
 
74 74
   
75
-  private void registerEvent(Cargo cargo, String date, Type type, CarrierMovement carrierMovement) throws ParseException{
76
-    HandlingEvent ev= new HandlingEvent(getDate(date), new Date(), type, carrierMovement);
77
-    ev.register(toSet(cargo));
75
+  private void registerEvent(Cargo cargo, String date, Type type, Location location, CarrierMovement carrierMovement) throws ParseException{
76
+    HandlingEvent ev= new HandlingEvent(getDate(date), new Date(), type, location, carrierMovement);
77
+    ev.add(cargo);
78 78
     String id = cargo.trackingId() + "_" + type + "_" + date;
79 79
     
80 80
     logger.debug("Adding event " + id + "(" + ev + ")");
81 81
     eventDB.put(id, ev);
82 82
   }
83 83
   
84
-  
85
-  private Set<Cargo> toSet(Cargo... cargoArgs) {
86
-    Set<Cargo> cargos = new HashSet<Cargo>();
87
-    for (Cargo cargo : cargoArgs) {
88
-      cargos.add(cargo);
89
-    }
90
-    
91
-    return cargos;
92
-  }
93
-
94 84
   /**
95 85
    * Parse an ISO 8601 (YYYY-MM-DD) String to Date
96 86
    * 
@@ -111,7 +101,7 @@ public class HandlingEventRepositoryInMem implements HandlingEventRepository{
111 101
 
112 102
   public void save(HandlingEvent event) {
113 103
     // Mimmick saving to database
114
-    for (Cargo cargo : event.registerdCargos()) {
104
+    for (Cargo cargo : event.cargos()) {
115 105
       cargo.handle(event);
116 106
     }
117 107
   }
@@ -120,7 +110,7 @@ public class HandlingEventRepositoryInMem implements HandlingEventRepository{
120 110
   public Set<HandlingEvent> findByTrackingId(final TrackingId trackingId) {
121 111
     Set<HandlingEvent> events = new HashSet<HandlingEvent>();
122 112
     for (HandlingEvent event : eventDB.values()) {
123
-      for (Cargo cargo : event.registerdCargos()) {
113
+      for (Cargo cargo : event.cargos()) {
124 114
         if (cargo.trackingId().equals(trackingId)) {
125 115
           events.add(event);
126 116
           break;

+ 5
- 2
dddsample/src/main/java/se/citerus/dddsample/service/HandlingEventService.java Ver fichero

@@ -2,6 +2,8 @@ package se.citerus.dddsample.service;
2 2
 
3 3
 import java.util.Date;
4 4
 
5
+import se.citerus.dddsample.domain.Location;
6
+
5 7
 
6 8
 public interface HandlingEventService {
7 9
 
@@ -12,9 +14,10 @@ public interface HandlingEventService {
12 14
    * 
13 15
    * @param date
14 16
    * @param type
17
+   * @param location TODO
15 18
    * @param carrierId
16
-   * @param trackIds
19
+   * @param trackId
17 20
    */
18
-  public abstract void register(Date date, String type, String carrierId, String[] trackIds);
21
+  public abstract void register(Date date, String type, Location location, String carrierId, String trackId);
19 22
 
20 23
 }

+ 13
- 21
dddsample/src/main/java/se/citerus/dddsample/service/HandlingEventServiceImpl.java Ver fichero

@@ -1,16 +1,20 @@
1 1
 package se.citerus.dddsample.service;
2 2
 
3
+import java.util.Date;
4
+
3 5
 import org.apache.commons.lang.Validate;
4 6
 import org.springframework.transaction.annotation.Transactional;
5
-import se.citerus.dddsample.domain.*;
7
+
8
+import se.citerus.dddsample.domain.Cargo;
9
+import se.citerus.dddsample.domain.CarrierId;
10
+import se.citerus.dddsample.domain.CarrierMovement;
11
+import se.citerus.dddsample.domain.HandlingEvent;
12
+import se.citerus.dddsample.domain.Location;
13
+import se.citerus.dddsample.domain.TrackingId;
6 14
 import se.citerus.dddsample.repository.CargoRepository;
7 15
 import se.citerus.dddsample.repository.CarrierMovementRepository;
8 16
 import se.citerus.dddsample.repository.HandlingEventRepository;
9 17
 
10
-import java.util.Date;
11
-import java.util.HashSet;
12
-import java.util.Set;
13
-
14 18
 public class HandlingEventServiceImpl implements HandlingEventService {
15 19
   private CargoRepository cargoRepository;
16 20
   private CarrierMovementRepository carrierMovementRepository;
@@ -18,27 +22,15 @@ public class HandlingEventServiceImpl implements HandlingEventService {
18 22
 
19 23
 
20 24
   @Transactional(readOnly = false)
21
-  public void register(Date date, String type, String carrierId, String[] trackingIds) {
25
+  public void register(Date date, String type, Location location, String carrierId, String trackingId) {
22 26
     CarrierMovement cm = findCarrier(new CarrierId(carrierId));
23
-    HandlingEvent event = new HandlingEvent(date, new Date(), HandlingEvent.parseType(type), cm);
24
-    Set<Cargo> cargos = findCargos(trackingIds);
25
-    event.register(cargos);
27
+    HandlingEvent event = new HandlingEvent(date, new Date(), HandlingEvent.parseType(type), location, cm);
28
+    Cargo cargo = findCargo(trackingId);
29
+    event.add(cargo);
26 30
     
27 31
     handlingEventRepository.save(event);
28 32
   }
29 33
 
30
-
31
-  private Set<Cargo> findCargos(String[] trackingIds) {
32
-    Set<Cargo> cargos = new HashSet<Cargo>();
33
-    
34
-    for (String trackingId : trackingIds) {
35
-      cargos.add(findCargo(trackingId));
36
-    }
37
-    
38
-    return cargos;
39
-  }
40
-
41
-
42 34
   private Cargo findCargo(String trackingId) {
43 35
     Cargo cargo = cargoRepository.find(new TrackingId(trackingId));
44 36
     Validate.notNull(cargo, "Cargo is not found. Tracking ID=" + trackingId);

+ 20
- 20
dddsample/src/test/java/se/citerus/dddsample/domain/CargoTest.java Ver fichero

@@ -68,7 +68,7 @@ public class CargoTest extends TestCase {
68 68
   private Cargo populateCargoReceivedStockholm() throws Exception {
69 69
     final Cargo cargo = new Cargo(new TrackingId("XYZ"), new Location("SESTO"), new Location("AUMEL"));
70 70
 
71
-    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.RECEIVE, null));
71
+    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.RECEIVE, new Location("SESTO")));
72 72
 
73 73
     return cargo;
74 74
   }
@@ -76,7 +76,7 @@ public class CargoTest extends TestCase {
76 76
   private Cargo populateCargoClaimedMelbourne() throws Exception {
77 77
     final Cargo cargo = populateCargoOffMelbourne();
78 78
 
79
-    cargo.handle(new HandlingEvent(getDate("2007-12-09"), new Date(), HandlingEvent.Type.CLAIM, null));
79
+    cargo.handle(new HandlingEvent(getDate("2007-12-09"), new Date(), HandlingEvent.Type.CLAIM, new Location("AUMEL")));
80 80
     
81 81
     return cargo;
82 82
   }
@@ -88,14 +88,14 @@ public class CargoTest extends TestCase {
88 88
     final CarrierMovement stockholmToHamburg = new CarrierMovement(
89 89
             new CarrierId("CAR_001"), new Location("SESTO"), new Location("DEHAM"));
90 90
 
91
-    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, stockholmToHamburg));
92
-    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, stockholmToHamburg));
91
+    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, new Location("SESTO"), stockholmToHamburg));
92
+    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, new Location("DEHAM"), stockholmToHamburg));
93 93
 
94 94
     final CarrierMovement hamburgToHongKong = new CarrierMovement(
95 95
             new CarrierId("CAR_001"), new Location("DEHAM"), new Location("CNHGK"));
96 96
 
97
-    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, hamburgToHongKong));
98
-    cargo.handle(new HandlingEvent(getDate("2007-12-04"), new Date(), HandlingEvent.Type.UNLOAD, hamburgToHongKong));
97
+    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, new Location("DEHAM"), hamburgToHongKong));
98
+    cargo.handle(new HandlingEvent(getDate("2007-12-04"), new Date(), HandlingEvent.Type.UNLOAD, new Location("CNHGK"), hamburgToHongKong));
99 99
 
100 100
     return cargo;
101 101
   }
@@ -106,13 +106,13 @@ public class CargoTest extends TestCase {
106 106
     final CarrierMovement stockholmToHamburg = new CarrierMovement(
107 107
             new CarrierId("CAR_001"), new Location("SESTO"), new Location("DEHAM"));
108 108
 
109
-    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, stockholmToHamburg));
110
-    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, stockholmToHamburg));
109
+    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, new Location("SESTO"), stockholmToHamburg));
110
+    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, new Location("DEHAM"), stockholmToHamburg));
111 111
 
112 112
     final CarrierMovement hamburgToHongKong = new CarrierMovement(
113 113
             new CarrierId("CAR_001"), new Location("DEHAM"), new Location("CNHGK"));
114 114
 
115
-    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, hamburgToHongKong));
115
+    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, new Location("DEHAM"), hamburgToHongKong));
116 116
 
117 117
     return cargo;
118 118
   }
@@ -123,20 +123,20 @@ public class CargoTest extends TestCase {
123 123
     final CarrierMovement stockholmToHamburg = new CarrierMovement(
124 124
             new CarrierId("CAR_001"), new Location("SESTO"), new Location("DEHAM"));
125 125
 
126
-    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, stockholmToHamburg));
127
-    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, stockholmToHamburg));
126
+    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, new Location("SESTO"), stockholmToHamburg));
127
+    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, new Location("DEHAM"), stockholmToHamburg));
128 128
 
129 129
     final CarrierMovement hamburgToHongKong = new CarrierMovement(
130 130
             new CarrierId("CAR_001"), new Location("DEHAM"), new Location("CNHGK"));
131 131
 
132
-    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, hamburgToHongKong));
133
-    cargo.handle(new HandlingEvent(getDate("2007-12-04"), new Date(), HandlingEvent.Type.UNLOAD, hamburgToHongKong));
132
+    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, new Location("DEHAM"), hamburgToHongKong));
133
+    cargo.handle(new HandlingEvent(getDate("2007-12-04"), new Date(), HandlingEvent.Type.UNLOAD, new Location("CNHGK"), hamburgToHongKong));
134 134
 
135 135
     final CarrierMovement hongKongToMelbourne = new CarrierMovement(
136 136
             new CarrierId("CAR_001"), new Location("CNHGK"), new Location("AUMEL"));
137 137
 
138
-    cargo.handle(new HandlingEvent(getDate("2007-12-05"), new Date(), HandlingEvent.Type.LOAD, hongKongToMelbourne));
139
-    cargo.handle(new HandlingEvent(getDate("2007-12-07"), new Date(), HandlingEvent.Type.UNLOAD, hongKongToMelbourne));
138
+    cargo.handle(new HandlingEvent(getDate("2007-12-05"), new Date(), HandlingEvent.Type.LOAD, new Location("CNHGK"), hongKongToMelbourne));
139
+    cargo.handle(new HandlingEvent(getDate("2007-12-07"), new Date(), HandlingEvent.Type.UNLOAD, new Location("AUMEL"), hongKongToMelbourne));
140 140
 
141 141
     return cargo;
142 142
   }
@@ -147,19 +147,19 @@ public class CargoTest extends TestCase {
147 147
     final CarrierMovement stockholmToHamburg = new CarrierMovement(
148 148
             new CarrierId("CAR_001"), new Location("SESTO"), new Location("DEHAM"));
149 149
 
150
-    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, stockholmToHamburg));
151
-    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, stockholmToHamburg));
150
+    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, new Location("SESTO"), stockholmToHamburg));
151
+    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, new Location("DEHAM"), stockholmToHamburg));
152 152
 
153 153
     final CarrierMovement hamburgToHongKong = new CarrierMovement(
154 154
             new CarrierId("CAR_001"), new Location("DEHAM"), new Location("CNHGK"));
155 155
 
156
-    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, hamburgToHongKong));
157
-    cargo.handle(new HandlingEvent(getDate("2007-12-04"), new Date(), HandlingEvent.Type.UNLOAD, hamburgToHongKong));
156
+    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, new Location("DEHAM"), hamburgToHongKong));
157
+    cargo.handle(new HandlingEvent(getDate("2007-12-04"), new Date(), HandlingEvent.Type.UNLOAD, new Location("CNHGK"), hamburgToHongKong));
158 158
 
159 159
     final CarrierMovement hongKongToMelbourne = new CarrierMovement(
160 160
             new CarrierId("CAR_001"), new Location("CNHGK"), new Location("AUMEL"));
161 161
 
162
-    cargo.handle(new HandlingEvent(getDate("2007-12-05"), new Date(), HandlingEvent.Type.LOAD, hongKongToMelbourne));
162
+    cargo.handle(new HandlingEvent(getDate("2007-12-05"), new Date(), HandlingEvent.Type.LOAD, new Location("CNHGK"), hongKongToMelbourne));
163 163
 
164 164
     return cargo;
165 165
   }

+ 4
- 4
dddsample/src/test/java/se/citerus/dddsample/domain/DeliveryHistoryTest.java Ver fichero

@@ -14,10 +14,10 @@ public class DeliveryHistoryTest extends TestCase {
14 14
     assertTrue(dh.eventsOrderedByTime().isEmpty());
15 15
 
16 16
     DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
17
-    HandlingEvent he1 = new HandlingEvent(df.parse("2010-01-03"), new Date(), HandlingEvent.Type.RECEIVE);
18
-    HandlingEvent he2 = new HandlingEvent(df.parse("2010-01-01"), new Date(), HandlingEvent.Type.LOAD);
19
-    HandlingEvent he3 = new HandlingEvent(df.parse("2010-01-04"), new Date(), HandlingEvent.Type.CLAIM);
20
-    HandlingEvent he4 = new HandlingEvent(df.parse("2010-01-02"), new Date(), HandlingEvent.Type.UNLOAD);
17
+    HandlingEvent he1 = new HandlingEvent(df.parse("2010-01-03"), new Date(), HandlingEvent.Type.RECEIVE, new Location("a"));
18
+    HandlingEvent he2 = new HandlingEvent(df.parse("2010-01-01"), new Date(), HandlingEvent.Type.LOAD, new Location("b"));
19
+    HandlingEvent he3 = new HandlingEvent(df.parse("2010-01-04"), new Date(), HandlingEvent.Type.CLAIM, new Location("c"));
20
+    HandlingEvent he4 = new HandlingEvent(df.parse("2010-01-02"), new Date(), HandlingEvent.Type.UNLOAD, new Location("d"));
21 21
     dh.addEvent(he1, he2, he3, he4);
22 22
 
23 23
     List<HandlingEvent> orderEvents = dh.eventsOrderedByTime();

+ 10
- 17
dddsample/src/test/java/se/citerus/dddsample/domain/HandlingEventTest.java Ver fichero

@@ -6,38 +6,31 @@ import se.citerus.dddsample.domain.HandlingEvent.Type;
6 6
 import java.util.Date;
7 7
 
8 8
 public class HandlingEventTest extends TestCase {
9
-  public void testCurrentLocationLoadEvent() throws Exception {
9
+  public void testCurrentLocation() throws Exception {
10 10
     Location locationAAA = new Location("AAA");
11 11
     Location locationBBB = new Location("BBB");
12 12
     CarrierId carrierId = new CarrierId("CAR_001");
13 13
     CarrierMovement cm = new CarrierMovement(carrierId, locationAAA, locationBBB);
14 14
     
15
-    HandlingEvent ev = new HandlingEvent(new Date(), new Date(), HandlingEvent.Type.LOAD, cm);
15
+    Date timeOccured = new Date();
16
+    Date timeRegistrated = new Date();
17
+    HandlingEvent ev = new HandlingEvent(timeOccured, timeRegistrated, HandlingEvent.Type.LOAD, locationAAA, cm);
16 18
     
17 19
     assertEquals(locationAAA, ev.location());
18 20
   }
19 21
   
20
-  public void testCurrentLocationUnloadEvent() throws Exception {
22
+  public void testCurrentLocationMisdirectedCargo() throws Exception {
21 23
     Location locationAAA = new Location("AAA");
22 24
     Location locationBBB = new Location("BBB");
25
+    Location locationCCC = new Location("CCC");
23 26
     CarrierId carrierId = new CarrierId("CAR_001");
24 27
     CarrierMovement cm = new CarrierMovement(carrierId, locationAAA, locationBBB);
25 28
     
26
-    HandlingEvent ev = new HandlingEvent(new Date(), new Date(), HandlingEvent.Type.UNLOAD, cm);
29
+    HandlingEvent ev = new HandlingEvent(new Date(), new Date(), HandlingEvent.Type.UNLOAD, locationCCC, cm);
27 30
     
28
-    assertEquals(locationBBB, ev.location());
31
+    assertEquals(locationCCC, ev.location());
29 32
   }
30 33
   
31
-  public void testCurrentLocationReceivedEvent() throws Exception {
32
-    HandlingEvent ev = new HandlingEvent(new Date(), new Date(), HandlingEvent.Type.RECEIVE, null);
33
-
34
-    assertEquals(Location.UNKNOWN, ev.location());
35
-  }
36
-  public void testCurrentLocationClaimedEvent() throws Exception {
37
-    HandlingEvent ev = new HandlingEvent(new Date(), new Date(), HandlingEvent.Type.CLAIM, null);
38
-
39
-    assertEquals(Location.UNKNOWN, ev.location());
40
-  }
41 34
   
42 35
   public void testParseType() throws Exception {
43 36
     assertEquals(Type.CLAIM, HandlingEvent.parseType("CLAIM"));
@@ -63,8 +56,8 @@ public class HandlingEventTest extends TestCase {
63 56
     CarrierId carrierId = new CarrierId("CAR_001");
64 57
     CarrierMovement cm = new CarrierMovement(carrierId, locationAAA, locationBBB);
65 58
 
66
-    HandlingEvent ev1 = new HandlingEvent(timeOccured, timeRegistered, HandlingEvent.Type.LOAD, cm);
67
-    HandlingEvent ev2 = new HandlingEvent(timeOccured, timeRegistered, HandlingEvent.Type.LOAD, cm);
59
+    HandlingEvent ev1 = new HandlingEvent(timeOccured, timeRegistered, HandlingEvent.Type.LOAD, locationAAA, cm);
60
+    HandlingEvent ev2 = new HandlingEvent(timeOccured, timeRegistered, HandlingEvent.Type.LOAD, locationAAA, cm);
68 61
 
69 62
     // They are the same real-world event
70 63
     assertTrue(ev1.sameAs(ev2));

+ 4
- 4
dddsample/src/test/java/se/citerus/dddsample/domain/TrackingScenarioTest.java Ver fichero

@@ -33,14 +33,14 @@ public class TrackingScenarioTest extends TestCase {
33 33
     final CarrierMovement stockholmToHamburg = new CarrierMovement(
34 34
             new CarrierId("CAR_001"), new Location("SESTO"), new Location("DEHAM"));
35 35
 
36
-    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, stockholmToHamburg));
37
-    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, stockholmToHamburg));
36
+    cargo.handle(new HandlingEvent(getDate("2007-12-01"), new Date(), HandlingEvent.Type.LOAD, new Location("SESTO"), stockholmToHamburg));
37
+    cargo.handle(new HandlingEvent(getDate("2007-12-02"), new Date(), HandlingEvent.Type.UNLOAD, new Location("DEHAM"), stockholmToHamburg));
38 38
 
39 39
     final CarrierMovement hamburgToHongKong = new CarrierMovement(
40 40
             new CarrierId("CAR_002"), new Location("DEHAM"), new Location("CNHKG"));
41 41
 
42
-    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, hamburgToHongKong));
43
-    cargo.handle(new HandlingEvent(getDate("2007-12-05"), new Date(), HandlingEvent.Type.UNLOAD, hamburgToHongKong));
42
+    cargo.handle(new HandlingEvent(getDate("2007-12-03"), new Date(), HandlingEvent.Type.LOAD, new Location("DEHAM"), hamburgToHongKong));
43
+    cargo.handle(new HandlingEvent(getDate("2007-12-05"), new Date(), HandlingEvent.Type.UNLOAD, new Location("CNHKG"), hamburgToHongKong));
44 44
 
45 45
     return cargo;
46 46
   }

+ 3
- 3
dddsample/src/test/java/se/citerus/dddsample/service/CargoServiceTest.java Ver fichero

@@ -70,7 +70,7 @@ public class CargoServiceTest extends AbstractDependencyInjectionSpringContextTe
70 70
         Cargo cargo = new Cargo(new TrackingId("XYZ"), new Location("ORIG"), new Location("DEST"));
71 71
         CarrierMovement cm = new CarrierMovement(new CarrierId("CAR_001"), new Location("FROM"), new Location("TO"));
72 72
         cargo.deliveryHistory().addEvent(
73
-                new HandlingEvent(new Date(10L), new Date(20L), HandlingEvent.Type.CLAIM, cm)
73
+                new HandlingEvent(new Date(10L), new Date(20L), HandlingEvent.Type.CLAIM, new Location("TO"), cm)
74 74
         );
75 75
         return cargo;
76 76
       }
@@ -87,12 +87,12 @@ public class CargoServiceTest extends AbstractDependencyInjectionSpringContextTe
87 87
     assertEquals("XYZ", cargoDTO.getTrackingId());
88 88
     assertEquals("ORIG", cargoDTO.getOrigin());
89 89
     assertEquals("DEST", cargoDTO.getFinalDestination());
90
-    assertEquals("DEST", cargoDTO.getCurrentLocation());
90
+    assertEquals("TO", cargoDTO.getCurrentLocation());
91 91
 
92 92
     List<HandlingEventDTO> events = cargoDTO.getEvents();
93 93
     assertEquals(1, events.size());
94 94
     HandlingEventDTO eventDTO = events.get(0);
95
-    assertEquals(Location.UNKNOWN.unlocode(), eventDTO.getLocation());
95
+    assertEquals("TO", eventDTO.getLocation());
96 96
     assertEquals("CLAIM", eventDTO.getType());
97 97
     assertEquals("CAR_001", eventDTO.getCarrier());    
98 98
     assertEquals(new Date(10), eventDTO.getTime());

+ 6
- 10
dddsample/src/test/java/se/citerus/dddsample/service/HandlingEventServiceTest.java Ver fichero

@@ -17,7 +17,6 @@ public class HandlingEventServiceTest extends TestCase {
17 17
   private HandlingEventRepository handlingEventRepository;
18 18
   
19 19
   private final Cargo cargoABC = new Cargo(new TrackingId("ABC"), new Location("ABCFROM"), new Location("ABCTO"));
20
-  private final Cargo cargoXYZ = new Cargo(new TrackingId("XYZ"), new Location("XYZFROM"), new Location("XYZTO"));
21 20
   private final CarrierMovement cmAAA_BBB = new CarrierMovement(
22 21
           new CarrierId("CAR_001"), new Location("AAA"), new Location("BBB"));
23 22
 
@@ -34,12 +33,11 @@ public class HandlingEventServiceTest extends TestCase {
34 33
 
35 34
   public void testRegisterEvent() throws Exception {
36 35
     String carrierId = "AAA_BBB";
37
-    final String[] trackIds = { "ABC", "XYZ" };
36
+    final String trackId = "ABC";
38 37
     Date date = Calendar.getInstance().getTime();
39 38
     String type = "UNLOAD";
40 39
     
41 40
     expect(cargoRepository.find(new TrackingId("ABC"))).andReturn(cargoABC);
42
-    expect(cargoRepository.find(new TrackingId("XYZ"))).andReturn(cargoXYZ);
43 41
     expect(carrierMovementRepository.find(new CarrierId("AAA_BBB"))).andReturn(cmAAA_BBB);
44 42
 
45 43
     // TODO: does not inspect the handling event instance in a sufficient way
@@ -47,24 +45,23 @@ public class HandlingEventServiceTest extends TestCase {
47 45
     
48 46
     replay(cargoRepository, carrierMovementRepository, handlingEventRepository);
49 47
     
50
-    service.register(date, type, carrierId, trackIds);
48
+    service.register(date, type, new Location("ABCTO"), carrierId, trackId);
51 49
     
52 50
     verify(cargoRepository, carrierMovementRepository, handlingEventRepository);
53 51
   }
54 52
   
55 53
   public void testRegisterEventInvalidCarrier() throws Exception {
56
-    final String[] trackIds = { "ABC", "XYZ" };
54
+    final String trackId = "ABC";
57 55
     Date date = Calendar.getInstance().getTime();
58 56
     String type = "UNLOAD";
59 57
     
60 58
     expect(cargoRepository.find(new TrackingId("ABC"))).andReturn(cargoABC);
61
-    expect(cargoRepository.find(new TrackingId("XYZ"))).andReturn(cargoXYZ);
62 59
     expect(carrierMovementRepository.find(new CarrierId("AAA_BBB"))).andReturn(null);
63 60
     
64 61
     replay(cargoRepository, carrierMovementRepository, handlingEventRepository);
65 62
     
66 63
     try {
67
-      service.register(date, type, "AAA_BBB", trackIds);
64
+      service.register(date, type, new Location("BBB"), "AAA_BBB", trackId);
68 65
       assertFalse(true);
69 66
     } catch (IllegalArgumentException e) {
70 67
       // Expected IllegalArgumentExecption
@@ -72,18 +69,17 @@ public class HandlingEventServiceTest extends TestCase {
72 69
   }
73 70
   
74 71
   public void testRegisterEventInvalidCargo() throws Exception {
75
-    final String[] trackIds = { "ABC", "XYZ" };
72
+    final String trackId =  "XYZ";
76 73
     Date date = Calendar.getInstance().getTime();
77 74
     String type = "UNLOAD";
78 75
     
79
-    expect(cargoRepository.find(new TrackingId("ABC"))).andReturn(cargoABC);
80 76
     expect(cargoRepository.find(new TrackingId("XYZ"))).andReturn(null);
81 77
     expect(carrierMovementRepository.find(new CarrierId("AAA_BBB"))).andReturn(cmAAA_BBB);
82 78
     
83 79
     replay(cargoRepository, carrierMovementRepository, handlingEventRepository);
84 80
     
85 81
     try {
86
-      service.register(date, type, "AAA_BBB", trackIds);
82
+      service.register(date, type,  new Location("BBB"), "AAA_BBB", trackId);
87 83
       assertFalse(true);
88 84
     } catch (IllegalArgumentException e) {
89 85
       // Expected IllegalArgumentExecption

+ 1
- 1
dddsample/src/test/java/se/citerus/dddsample/web/CargoTrackingControllerTest.java Ver fichero

@@ -44,7 +44,7 @@ public class CargoTrackingControllerTest extends TestCase {
44 44
     return new CargoService() {
45 45
       public CargoWithHistoryDTO find(String trackingId) {
46 46
         Cargo cargo = new Cargo(new TrackingId(trackingId), new Location("AAA"), new Location("BBB"));
47
-        HandlingEvent event = new HandlingEvent(new Date(10L), new Date(20L), HandlingEvent.Type.RECEIVE);
47
+        HandlingEvent event = new HandlingEvent(new Date(10L), new Date(20L), HandlingEvent.Type.RECEIVE, new Location("AAA"));
48 48
         cargo.deliveryHistory().addEvent(event);
49 49
 
50 50
         // TODO: use DTO assemblers