Explorar el Código

Added unit test for event registration web service.

Allowed null carrier movement id to be passed into HandlingEventService.register().
peter_backlund hace 18 años
padre
commit
a5b9399b19

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

@@ -1,8 +1,8 @@
1 1
 package se.citerus.dddsample.service;
2 2
 
3
-import se.citerus.dddsample.domain.TrackingId;
4 3
 import se.citerus.dddsample.domain.CarrierMovementId;
5 4
 import se.citerus.dddsample.domain.HandlingEvent;
5
+import se.citerus.dddsample.domain.TrackingId;
6 6
 
7 7
 import java.util.Date;
8 8
 
@@ -16,11 +16,11 @@ public interface HandlingEventService {
16 16
   /**
17 17
    * @param completionTime when the event was completed, for example finished loading
18 18
    * @param trackingId tracking id
19
-   * @param carrierMovementId carrier movement
19
+   * @param carrierMovementId carrier movement id, if applicable (may be null)
20 20
    * @param unlocode United Nations Location Code for the location of the event
21 21
    * @param type type of event
22 22
    * @throws UnknownCarrierMovementIdException if there's not carrier movement with this id
23
-   * @throws se.citerus.dddsample.service.UnknownTrackingIdException if there's no cargo with this tracking id
23
+   * @throws UnknownTrackingIdException if there's no cargo with this tracking id
24 24
    */
25 25
   void register(Date completionTime, TrackingId trackingId, CarrierMovementId carrierMovementId, String unlocode, HandlingEvent.Type type)
26 26
   throws UnknownCarrierMovementIdException, UnknownTrackingIdException;

+ 3
- 1
dddsample/src/main/java/se/citerus/dddsample/service/HandlingEventServiceImpl.java Ver fichero

@@ -40,7 +40,9 @@ public class HandlingEventServiceImpl implements HandlingEventService {
40 40
   }
41 41
 
42 42
   private CarrierMovement findCarrierMovement(CarrierMovementId carrierMovementId) throws UnknownCarrierMovementIdException {
43
-    Validate.notNull(carrierMovementId, "Carrier ID is required");
43
+    if (carrierMovementId == null) {
44
+      return null;
45
+    }
44 46
     CarrierMovement carrierMovement = carrierMovementRepository.find(carrierMovementId);
45 47
     if (carrierMovement == null) {
46 48
       throw new UnknownCarrierMovementIdException(carrierMovementId);

+ 9
- 0
dddsample/src/main/java/se/citerus/dddsample/ws/HandlingEventServiceEndpoint.java Ver fichero

@@ -9,6 +9,15 @@ import javax.jws.WebService;
9 9
 @WebService
10 10
 public interface HandlingEventServiceEndpoint {
11 11
 
12
+  /**
13
+   * Register an cargo handling event.
14
+   *
15
+   * @param completionTime time when event occured, for example a the loading of cargo was completed
16
+   * @param trackingId tracking id of the cargo
17
+   * @param carrierMovementId carrier movement id, if applicable
18
+   * @param unlocode United Nations Location Code for the location where the event occured
19
+   * @param eventType type of event
20
+   */
12 21
   void register(String completionTime, String trackingId, String carrierMovementId, String unlocode, String eventType);
13 22
 
14 23
 }

+ 9
- 5
dddsample/src/main/java/se/citerus/dddsample/ws/HandlingEventServiceEndpointImpl.java Ver fichero

@@ -6,9 +6,9 @@ import se.citerus.dddsample.domain.CarrierMovementId;
6 6
 import se.citerus.dddsample.domain.HandlingEvent;
7 7
 import se.citerus.dddsample.domain.TrackingId;
8 8
 import se.citerus.dddsample.service.HandlingEventService;
9
+import se.citerus.dddsample.service.InvalidEventTypeException;
9 10
 import se.citerus.dddsample.service.UnknownCarrierMovementIdException;
10 11
 import se.citerus.dddsample.service.UnknownTrackingIdException;
11
-import se.citerus.dddsample.service.InvalidEventTypeException;
12 12
 
13 13
 import javax.jws.WebService;
14 14
 import java.text.ParseException;
@@ -20,20 +20,24 @@ public class HandlingEventServiceEndpointImpl implements HandlingEventServiceEnd
20 20
 
21 21
   private HandlingEventService handlingEventService;
22 22
   private static final Log logger = LogFactory.getLog(HandlingEventServiceEndpointImpl.class);
23
-  private static final String ISO_8601_FORMAT = "yyyy-mm-dd HH:MM:SS.SSS";
23
+  protected static final String ISO_8601_FORMAT = "yyyy-mm-dd HH:MM:SS.SSS";
24 24
 
25 25
   public void register(String completionTime, String trackingId, String carrierMovementId,
26 26
                        String unlocode, String eventType) {
27 27
     try {
28 28
       Date date = parseIso8601Date(completionTime);
29 29
       TrackingId tid = new TrackingId(trackingId);
30
-      CarrierMovementId cid = new CarrierMovementId(carrierMovementId);
30
+      CarrierMovementId cid;
31
+      if (carrierMovementId != null) {
32
+        cid = new CarrierMovementId(carrierMovementId);
33
+      } else {
34
+        cid = null;
35
+      }
31 36
       HandlingEvent.Type type = parseEventType(eventType);
32 37
 
33 38
       handlingEventService.register(date, tid, cid, unlocode, type);
34
-      
35 39
     } catch (ParseException pe) {
36
-      logger.error("Invalid date format: " + completionTime);
40
+      logger.error("Invalid date format: " + completionTime + ", must be on ISO 8601 format: " + ISO_8601_FORMAT);
37 41
     } catch (UnknownTrackingIdException utid) {
38 42
       handleRetry(utid);
39 43
     } catch (UnknownCarrierMovementIdException ucmi) {

+ 18
- 4
dddsample/src/test/java/se/citerus/dddsample/service/HandlingEventServiceTest.java Ver fichero

@@ -15,10 +15,10 @@ public class HandlingEventServiceTest extends TestCase {
15 15
   private CarrierMovementRepository carrierMovementRepository;
16 16
   private HandlingEventRepository handlingEventRepository;
17 17
   
18
-  private final Cargo cargoABC = new Cargo(new TrackingId("ABC"), new Location("ABCFROM"), new Location("ABCTO"));
19
-  private final Cargo cargoXYZ = new Cargo(new TrackingId("XYZ"), new Location("XYZFROM"), new Location("XYZTO"));
18
+  private final Cargo cargoABC = new Cargo(new TrackingId("ABC"), new Location("AFROM"), new Location("ABCTO"));
19
+  private final Cargo cargoXYZ = new Cargo(new TrackingId("XYZ"), new Location("XFROM"), new Location("XYZTO"));
20 20
   private final CarrierMovement cmAAA_BBB = new CarrierMovement(
21
-          new CarrierMovementId("CAR_001"), new Location("AAA"), new Location("BBB"));
21
+          new CarrierMovementId("CAR_001"), new Location("AAAAA"), new Location("BBBBB"));
22 22
 
23 23
   protected void setUp() throws Exception{
24 24
     service = new HandlingEventServiceImpl();
@@ -51,7 +51,21 @@ public class HandlingEventServiceTest extends TestCase {
51 51
     
52 52
     service.register(date, trackingId, carrierMovementId, "SESTO", HandlingEvent.Type.LOAD);
53 53
   }
54
+
55
+  public void testRegisterEventWithoutCarrierMovement() throws Exception {
56
+    final Date date = new Date();
57
+
58
+    final TrackingId trackingId = new TrackingId("ABC");
59
+    expect(cargoRepository.find(trackingId)).andReturn(cargoABC);
60
+
61
+    handlingEventRepository.save(isA(HandlingEvent.class));
62
+
63
+    replay(cargoRepository, carrierMovementRepository, handlingEventRepository);
64
+
65
+    service.register(date, trackingId, null, "SESTO", HandlingEvent.Type.CLAIM);
66
+  }
54 67
   
68
+
55 69
   public void testRegisterEventInvalidCarrier() throws Exception {
56 70
     final Date date = new Date();
57 71
 
@@ -59,7 +73,7 @@ public class HandlingEventServiceTest extends TestCase {
59 73
     expect(carrierMovementRepository.find(carrierMovementId)).andReturn(null);
60 74
 
61 75
     final TrackingId trackingId = new TrackingId("XYZ");
62
-    expect(cargoRepository.find(trackingId)).andReturn(new Cargo(trackingId, new Location("FROM"), new Location("TO")));
76
+    expect(cargoRepository.find(trackingId)).andReturn(new Cargo(trackingId, new Location("FROMX"), new Location("TOYYY")));
63 77
 
64 78
     replay(cargoRepository, carrierMovementRepository, handlingEventRepository);
65 79
     

+ 90
- 0
dddsample/src/test/java/se/citerus/dddsample/ws/HandlinEventServiceEndpointTest.java Ver fichero

@@ -0,0 +1,90 @@
1
+package se.citerus.dddsample.ws;
2
+
3
+import junit.framework.TestCase;
4
+import static org.easymock.EasyMock.*;
5
+import se.citerus.dddsample.domain.CarrierMovementId;
6
+import se.citerus.dddsample.domain.HandlingEvent;
7
+import se.citerus.dddsample.domain.TrackingId;
8
+import se.citerus.dddsample.service.HandlingEventService;
9
+import se.citerus.dddsample.service.UnknownCarrierMovementIdException;
10
+import se.citerus.dddsample.service.UnknownTrackingIdException;
11
+
12
+import java.text.SimpleDateFormat;
13
+import java.util.Date;
14
+
15
+public class HandlinEventServiceEndpointTest extends TestCase {
16
+
17
+  HandlingEventServiceEndpointImpl endpoint;
18
+  HandlingEventService handlingEventService;
19
+  SimpleDateFormat sdf;
20
+
21
+  protected void setUp() throws Exception {
22
+    endpoint = new HandlingEventServiceEndpointImpl();
23
+    handlingEventService = createMock(HandlingEventService.class);
24
+    endpoint.setHandlingEventService(handlingEventService);
25
+    sdf = new SimpleDateFormat(HandlingEventServiceEndpointImpl.ISO_8601_FORMAT);
26
+  }
27
+
28
+  public void testRegisterValidEvent() throws Exception {
29
+    Date date = new Date(100);
30
+
31
+    handlingEventService.register(date, new TrackingId("FOO"), new CarrierMovementId("CAR_456"), "CNHKG", HandlingEvent.Type.LOAD);
32
+    replay(handlingEventService);
33
+
34
+    // Tested call
35
+    endpoint.register(sdf.format(date), "FOO", "CAR_456", "CNHKG", "LOAD");
36
+  }
37
+
38
+
39
+  public void testRegisterUnknownTrackingId() throws Exception {
40
+    Date date = new Date(100);
41
+
42
+    TrackingId trackingId = new TrackingId("NOTFOUND");
43
+
44
+      handlingEventService.register(date, trackingId, null, "SESTO", HandlingEvent.Type.CLAIM);
45
+    expectLastCall().andThrow(new UnknownTrackingIdException(trackingId));
46
+    replay(handlingEventService);
47
+
48
+    // Tested call
49
+    endpoint.register(sdf.format(date), "NOTFOUND", null, "SESTO", "CLAIM");
50
+  }
51
+
52
+  public void testRegisterUnknownCarrierMovementId() throws Exception {
53
+      Date date = new Date(100);
54
+
55
+      TrackingId trackingId = new TrackingId("XYZ");
56
+      CarrierMovementId carrierMovementId = new CarrierMovementId("NOTFOUND");
57
+
58
+      handlingEventService.register(date, trackingId, carrierMovementId, "AUMEL", HandlingEvent.Type.UNLOAD);
59
+      expectLastCall().andThrow(new UnknownCarrierMovementIdException(carrierMovementId));
60
+      replay(handlingEventService);
61
+
62
+      // Tested call
63
+      endpoint.register(sdf.format(date), "XYZ", "NOTFOUND", "AUMEL", "UNLOAD");
64
+  }
65
+
66
+  public void testRegisterInvalidEventType() throws Exception {
67
+      Date date = new Date(100);
68
+      
69
+      replay(handlingEventService);
70
+
71
+      // Tested call
72
+      // Note: currently, every error is silently swallowed.
73
+      endpoint.register(sdf.format(date), "XYZ", "CAR_333", "AUMEL", "NO_SUCH_EVENT_TYPE");
74
+  }
75
+
76
+    public void testRegisterInvalidDateFormat() throws Exception {
77
+        Date date = new Date(100);
78
+
79
+        replay(handlingEventService);
80
+
81
+        // Tested call
82
+        // Note: currently, every error is silently swallowed.
83
+        endpoint.register("1 2 3 4", "XYZ", "CAR_333", "AUMEL", "LOAD");
84
+    }
85
+
86
+
87
+  protected void tearDown() throws Exception {
88
+    verify(handlingEventService);
89
+  }
90
+}