瀏覽代碼

Split CargoService into BookingService and TrackingService.

peter_backlund 18 年之前
父節點
當前提交
7a5983f3ae

dddsample/src/main/java/se/citerus/dddsample/service/CargoService.java → dddsample/src/main/java/se/citerus/dddsample/service/BookingService.java 查看文件

@@ -4,29 +4,13 @@ import se.citerus.dddsample.domain.Cargo;
4 4
 import se.citerus.dddsample.domain.Itinerary;
5 5
 import se.citerus.dddsample.domain.TrackingId;
6 6
 import se.citerus.dddsample.domain.UnLocode;
7
-import se.citerus.dddsample.service.dto.CargoTrackingDTO;
8 7
 
9 8
 import java.util.List;
10 9
 
11 10
 /**
12
- * Cargo service.
11
+ * Cargo booking service.
13 12
  */
14
-public interface CargoService {
15
-
16
-  /**
17
-   * @param trackingId tracking id
18
-   * @return A cargo and its delivery history, or null if no cargo with given tracking id is found.
19
-   */
20
-  CargoTrackingDTO track(TrackingId trackingId);
21
-
22
-  /**
23
-   * Send relevant notifications to interested parties,
24
-   * for example if a cargo has been misdirected, or unloaded
25
-   * at the final destination.
26
-   *
27
-   * @param trackingId cargo tracking id
28
-   */
29
-  void notify(TrackingId trackingId);
13
+public interface BookingService {
30 14
 
31 15
   /**
32 16
    * @return A list of all locations where the company ships cargo to.
@@ -34,13 +18,6 @@ public interface CargoService {
34 18
   List<UnLocode> listShippingLocations();
35 19
 
36 20
   /**
37
-   * Lists all cargos.
38
-   *
39
-   * @return All cargos.
40
-   */
41
-  List<Cargo> listAllCargos();
42
-
43
-  /**
44 21
    * Registers a new cargo in the tracking system, not yet routed.
45 22
    *
46 23
    * @param origin      cargo origin
@@ -65,4 +42,11 @@ public interface CargoService {
65 42
    */
66 43
   void assignCargoToRoute(TrackingId trackingId, Itinerary itinerary);
67 44
 
45
+  /**
46
+   * Lists all cargos.
47
+   *
48
+   * @return All cargos.
49
+   */
50
+  List<Cargo> listAllCargos();
51
+
68 52
 }

dddsample/src/main/java/se/citerus/dddsample/service/CargoServiceImpl.java → dddsample/src/main/java/se/citerus/dddsample/service/BookingServiceImpl.java 查看文件

@@ -7,13 +7,11 @@ import org.springframework.transaction.annotation.Transactional;
7 7
 import se.citerus.dddsample.domain.*;
8 8
 import se.citerus.dddsample.repository.CargoRepository;
9 9
 import se.citerus.dddsample.repository.LocationRepository;
10
-import se.citerus.dddsample.service.dto.CargoTrackingDTO;
11
-import se.citerus.dddsample.service.dto.assembler.CargoTrackingDTOAssembler;
12 10
 
13 11
 import java.util.ArrayList;
14 12
 import java.util.List;
15 13
 
16
-public final class CargoServiceImpl implements CargoService {
14
+public final class BookingServiceImpl implements BookingService {
17 15
 
18 16
   private CargoRepository cargoRepository;
19 17
   private LocationRepository locationRepository;
@@ -46,40 +44,6 @@ public final class CargoServiceImpl implements CargoService {
46 44
     return unlocodes;
47 45
   }
48 46
 
49
-  // TODO: move track() and notify() to TrackingService, rename this class BookingService
50
-  @Transactional(readOnly = true)
51
-  public CargoTrackingDTO track(final TrackingId trackingId) {
52
-    Validate.notNull(trackingId);
53
-
54
-    final Cargo cargo = cargoRepository.find(trackingId);
55
-    if (cargo == null) {
56
-      return null;
57
-    }
58
-
59
-    return new CargoTrackingDTOAssembler().toDTO(cargo);
60
-  }
61
-
62
-  @Transactional(readOnly = true)
63
-  public void notify(final TrackingId trackingId) {
64
-    Validate.notNull(trackingId);
65
-
66
-    final Cargo cargo = cargoRepository.find(trackingId);
67
-    if (cargo == null) {
68
-      logger.warn("Can't notify listeners for non-existing cargo " + trackingId);
69
-      return;
70
-    }
71
-
72
-    // TODO: more elaborate notifications, such as email to affected customer
73
-    if (cargo.isMisdirected()) {
74
-      logger.info("Cargo " + trackingId + " has been misdirected. " +
75
-        "Last event was " + cargo.deliveryHistory().lastEvent());
76
-    }
77
-    if (cargo.isUnloadedAtDestination()) {
78
-      logger.info("Cargo " + trackingId + " has been unloaded " +
79
-        "at its final destination " + cargo.destination());
80
-    }
81
-  }
82
-
83 47
   @Transactional(readOnly = true)
84 48
   public List<Cargo> listAllCargos() {
85 49
     final List<Cargo> allCargos = cargoRepository.findAll();

+ 4
- 4
dddsample/src/main/java/se/citerus/dddsample/service/HandlingEventMessageDelegate.java 查看文件

@@ -18,7 +18,7 @@ import javax.jms.MessageListener;
18 18
  */
19 19
 public class HandlingEventMessageDelegate implements MessageListener {
20 20
 
21
-  private CargoService cargoService;
21
+  private TrackingService trackingService;
22 22
   private final Log logger = LogFactory.getLog(getClass());
23 23
 
24 24
   public void onMessage(final Message message) {
@@ -27,13 +27,13 @@ public class HandlingEventMessageDelegate implements MessageListener {
27 27
     }
28 28
     try {
29 29
       String tidString = message.getStringProperty(JmsEventServiceImpl.TRACKING_ID_KEY);
30
-      cargoService.notify(new TrackingId(tidString));
30
+      trackingService.notify(new TrackingId(tidString));
31 31
     } catch (JMSException e) {
32 32
       logger.error(e, e);
33 33
     }
34 34
   }
35 35
 
36
-  public void setCargoService(CargoService cargoService) {
37
-    this.cargoService = cargoService;
36
+  public void setTrackingService(TrackingService trackingService) {
37
+    this.trackingService = trackingService;
38 38
   }
39 39
 }

+ 4
- 4
dddsample/src/main/java/se/citerus/dddsample/service/ThreadBasedEventServiceImpl.java 查看文件

@@ -7,17 +7,17 @@ import se.citerus.dddsample.domain.HandlingEvent;
7 7
  */
8 8
 public class ThreadBasedEventServiceImpl implements EventService {
9 9
 
10
-  private CargoService cargoService;
10
+  private TrackingService trackingService;
11 11
 
12 12
   public void fireHandlingEventRegistered(final HandlingEvent event) {
13 13
     new Thread(new Runnable() {
14 14
       public void run() {
15
-        cargoService.notify(event.cargo().trackingId());
15
+        trackingService.notify(event.cargo().trackingId());
16 16
       }
17 17
     }).start();
18 18
   }
19 19
 
20
-  public void setCargoService(CargoService cargoService) {
21
-    this.cargoService = cargoService;
20
+  public void setTrackingService(TrackingService trackingService) {
21
+    this.trackingService = trackingService;
22 22
   }
23 23
 }

+ 27
- 0
dddsample/src/main/java/se/citerus/dddsample/service/TrackingService.java 查看文件

@@ -0,0 +1,27 @@
1
+package se.citerus.dddsample.service;
2
+
3
+import se.citerus.dddsample.domain.TrackingId;
4
+import se.citerus.dddsample.service.dto.CargoTrackingDTO;
5
+
6
+/**
7
+ * Cargo tracking service.
8
+ *
9
+ */
10
+public interface TrackingService {
11
+
12
+  /**
13
+   * @param trackingId tracking id
14
+   * @return A cargo and its delivery history, or null if no cargo with given tracking id is found.
15
+   */
16
+  CargoTrackingDTO track(TrackingId trackingId);
17
+
18
+  /**
19
+   * Send relevant notifications to interested parties,
20
+   * for example if a cargo has been misdirected, or unloaded
21
+   * at the final destination.
22
+   *
23
+   * @param trackingId cargo tracking id
24
+   */
25
+  void notify(TrackingId trackingId);
26
+
27
+}

+ 56
- 0
dddsample/src/main/java/se/citerus/dddsample/service/TrackingServiceImpl.java 查看文件

@@ -0,0 +1,56 @@
1
+package se.citerus.dddsample.service;
2
+
3
+import org.apache.commons.lang.Validate;
4
+import org.apache.commons.logging.Log;
5
+import org.apache.commons.logging.LogFactory;
6
+import org.springframework.transaction.annotation.Transactional;
7
+import se.citerus.dddsample.domain.Cargo;
8
+import se.citerus.dddsample.domain.TrackingId;
9
+import se.citerus.dddsample.repository.CargoRepository;
10
+import se.citerus.dddsample.service.dto.CargoTrackingDTO;
11
+import se.citerus.dddsample.service.dto.assembler.CargoTrackingDTOAssembler;
12
+
13
+public class TrackingServiceImpl implements TrackingService {
14
+
15
+  private CargoRepository cargoRepository;
16
+
17
+  private final Log logger = LogFactory.getLog(getClass());
18
+
19
+  @Transactional(readOnly = true)
20
+  public CargoTrackingDTO track(final TrackingId trackingId) {
21
+    Validate.notNull(trackingId);
22
+
23
+    final Cargo cargo = cargoRepository.find(trackingId);
24
+    if (cargo == null) {
25
+      return null;
26
+    }
27
+
28
+    return new CargoTrackingDTOAssembler().toDTO(cargo);
29
+  }
30
+
31
+  @Transactional(readOnly = true)
32
+  public void notify(final TrackingId trackingId) {
33
+    Validate.notNull(trackingId);
34
+
35
+    final Cargo cargo = cargoRepository.find(trackingId);
36
+    if (cargo == null) {
37
+      logger.warn("Can't notify listeners for non-existing cargo " + trackingId);
38
+      return;
39
+    }
40
+
41
+    // TODO: more elaborate notifications, such as email to affected customer
42
+    if (cargo.isMisdirected()) {
43
+      logger.info("Cargo " + trackingId + " has been misdirected. " +
44
+        "Last event was " + cargo.deliveryHistory().lastEvent());
45
+    }
46
+    if (cargo.isUnloadedAtDestination()) {
47
+      logger.info("Cargo " + trackingId + " has been unloaded " +
48
+        "at its final destination " + cargo.destination());
49
+    }
50
+  }
51
+
52
+  public void setCargoRepository(CargoRepository cargoRepository) {
53
+    this.cargoRepository = cargoRepository;
54
+  }
55
+
56
+}

+ 10
- 10
dddsample/src/main/java/se/citerus/dddsample/web/CargoAdminController.java 查看文件

@@ -4,7 +4,7 @@ import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
4 4
 import se.citerus.dddsample.domain.*;
5 5
 import se.citerus.dddsample.repository.CarrierMovementRepository;
6 6
 import se.citerus.dddsample.repository.LocationRepository;
7
-import se.citerus.dddsample.service.CargoService;
7
+import se.citerus.dddsample.service.BookingService;
8 8
 import se.citerus.dddsample.service.RoutingService;
9 9
 import se.citerus.dddsample.service.dto.CargoRoutingDTO;
10 10
 import se.citerus.dddsample.service.dto.ItineraryCandidateDTO;
@@ -23,7 +23,7 @@ import java.util.*;
23 23
  */
24 24
 public final class CargoAdminController extends MultiActionController {
25 25
 
26
-  private CargoService cargoService;
26
+  private BookingService bookingService;
27 27
   private RoutingService routingService;
28 28
   private LocationRepository locationRepository;
29 29
   private CarrierMovementRepository carrierMovementRepository;
@@ -33,7 +33,7 @@ public final class CargoAdminController extends MultiActionController {
33 33
 
34 34
   public Map registrationForm(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
35 35
     final Map<String, Object> map = new HashMap<String, Object>();
36
-    final List<UnLocode> unLocodes = cargoService.listShippingLocations();
36
+    final List<UnLocode> unLocodes = bookingService.listShippingLocations();
37 37
     final List<String> unLocodeStrings = new ArrayList<String>();
38 38
 
39 39
     for (UnLocode unLocode : unLocodes) {
@@ -47,7 +47,7 @@ public final class CargoAdminController extends MultiActionController {
47 47
   public void register(final HttpServletRequest request, final HttpServletResponse response,
48 48
                        final RegistrationCommand command) throws Exception {
49 49
 
50
-    final TrackingId trackingId = cargoService.registerNewCargo(
50
+    final TrackingId trackingId = bookingService.registerNewCargo(
51 51
       new UnLocode(command.getOriginUnlocode()),
52 52
       new UnLocode(command.getDestinationUnlocode())
53 53
     );
@@ -56,7 +56,7 @@ public final class CargoAdminController extends MultiActionController {
56 56
 
57 57
   public Map list(HttpServletRequest request, HttpServletResponse response) {
58 58
     final Map<String, Object> map = new HashMap<String, Object>();
59
-    final List<Cargo> allCargos = cargoService.listAllCargos();
59
+    final List<Cargo> allCargos = bookingService.listAllCargos();
60 60
 
61 61
     final CargoRoutingDTOAssembler assembler = new CargoRoutingDTOAssembler();
62 62
     final List<CargoRoutingDTO> dtoList = new ArrayList<CargoRoutingDTO>(allCargos.size());
@@ -72,7 +72,7 @@ public final class CargoAdminController extends MultiActionController {
72 72
   public Map show(final HttpServletRequest request, final HttpServletResponse response) {
73 73
     final Map<String, Object> map = new HashMap<String, Object>();
74 74
     final TrackingId trackingId = new TrackingId(request.getParameter("trackingId"));
75
-    final Cargo cargo = cargoService.loadCargoForRouting(trackingId);
75
+    final Cargo cargo = bookingService.loadCargoForRouting(trackingId);
76 76
     final CargoRoutingDTO dto = new CargoRoutingDTOAssembler().toDTO(cargo);
77 77
     map.put("cargo", dto);
78 78
     return map;
@@ -82,7 +82,7 @@ public final class CargoAdminController extends MultiActionController {
82 82
     final Map<String, Object> map = new HashMap<String, Object>();
83 83
     final TrackingId trackingId = new TrackingId(request.getParameter("trackingId"));
84 84
 
85
-    final Cargo cargo = cargoService.loadCargoForRouting(trackingId);
85
+    final Cargo cargo = bookingService.loadCargoForRouting(trackingId);
86 86
     final RouteSpecification routeSpecification = RouteSpecification.forCargo(cargo, new Date());
87 87
     final List<Itinerary> itineraries = routingService.requestPossibleRoutes(routeSpecification);
88 88
 
@@ -114,13 +114,13 @@ public final class CargoAdminController extends MultiActionController {
114 114
     final ItineraryCandidateDTO selectedItinerary = new ItineraryCandidateDTO(legDTOs);
115 115
     final Itinerary itinerary = new ItineraryCandidateDTOAssembler().fromDTO(selectedItinerary, carrierMovementRepository, locationRepository);
116 116
 
117
-    cargoService.assignCargoToRoute(trackingId, itinerary);
117
+    bookingService.assignCargoToRoute(trackingId, itinerary);
118 118
 
119 119
     response.sendRedirect("list.html");
120 120
   }
121 121
 
122
-  public void setCargoService(final CargoService cargoService) {
123
-    this.cargoService = cargoService;
122
+  public void setBookingService(final BookingService bookingService) {
123
+    this.bookingService = bookingService;
124 124
   }
125 125
 
126 126
   public void setRoutingService(final RoutingService routingService) {

+ 5
- 12
dddsample/src/main/java/se/citerus/dddsample/web/CargoTrackingController.java 查看文件

@@ -4,7 +4,7 @@ import org.springframework.validation.BindException;
4 4
 import org.springframework.web.servlet.ModelAndView;
5 5
 import org.springframework.web.servlet.mvc.SimpleFormController;
6 6
 import se.citerus.dddsample.domain.TrackingId;
7
-import se.citerus.dddsample.service.CargoService;
7
+import se.citerus.dddsample.service.TrackingService;
8 8
 import se.citerus.dddsample.service.dto.CargoTrackingDTO;
9 9
 import se.citerus.dddsample.web.command.TrackCommand;
10 10
 
@@ -21,7 +21,7 @@ public final class CargoTrackingController extends SimpleFormController {
21 21
   /**
22 22
    * Service instance.
23 23
    */
24
-  private CargoService cargoService;
24
+  private TrackingService trackingService;
25 25
 
26 26
   public CargoTrackingController() {
27 27
     setCommandClass(TrackCommand.class);
@@ -33,7 +33,7 @@ public final class CargoTrackingController extends SimpleFormController {
33 33
 
34 34
     final TrackCommand trackCommand = (TrackCommand) command;
35 35
     final String tidStr = trackCommand.getTrackingId();
36
-    final CargoTrackingDTO cargo = cargoService.track(new TrackingId(tidStr));
36
+    final CargoTrackingDTO cargo = trackingService.track(new TrackingId(tidStr));
37 37
 
38 38
     final Map<String, CargoTrackingDTO> model = new HashMap<String, CargoTrackingDTO>();
39 39
     if (cargo != null) {
@@ -45,14 +45,7 @@ public final class CargoTrackingController extends SimpleFormController {
45 45
     return showForm(request, response, errors, model);
46 46
   }
47 47
 
48
-
49
-  /**
50
-   * Sets the cargo service instance.
51
-   *
52
-   * @param cargoService The service.
53
-   */
54
-  public void setCargoService(final CargoService cargoService) {
55
-    this.cargoService = cargoService;
48
+  public void setTrackingService(TrackingService trackingService) {
49
+    this.trackingService = trackingService;
56 50
   }
57
-
58 51
 }

+ 1
- 1
dddsample/src/main/resources/context-messaging-jms.xml 查看文件

@@ -20,7 +20,7 @@
20 20
   <amq:topic id="handlingEventTopic" name="HandlingEventTopic" physicalName="HandlingEventTopic"/>
21 21
 
22 22
   <bean id="handlingEventMessageDelegate" class="se.citerus.dddsample.service.HandlingEventMessageDelegate">
23
-    <property name="cargoService" ref="cargoService"/>
23
+    <property name="trackingService" ref="trackingService"/>
24 24
   </bean>
25 25
 
26 26
   <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">

+ 1
- 1
dddsample/src/main/resources/context-messaging-thread.xml 查看文件

@@ -5,7 +5,7 @@
5 5
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6 6
 
7 7
   <bean id="eventService" class="se.citerus.dddsample.service.ThreadBasedEventServiceImpl">
8
-    <property name="cargoService" ref="cargoService"/>
8
+    <property name="bookingService" ref="bookingService"/>
9 9
   </bean>
10 10
 
11 11
 </beans>

+ 5
- 1
dddsample/src/main/resources/context-service.xml 查看文件

@@ -9,11 +9,15 @@
9 9
 
10 10
   <tx:annotation-driven transaction-manager="transactionManager"/>
11 11
 
12
-  <bean id="cargoService" class="se.citerus.dddsample.service.CargoServiceImpl">
12
+  <bean id="bookingService" class="se.citerus.dddsample.service.BookingServiceImpl">
13 13
     <property name="cargoRepository" ref="cargoRepository"/>
14 14
     <property name="locationRepository" ref="locationRepository"/>
15 15
   </bean>
16 16
 
17
+  <bean id="trackingService" class="se.citerus.dddsample.service.TrackingServiceImpl">
18
+    <property name="cargoRepository" ref="cargoRepository"/>
19
+  </bean>
20
+
17 21
   <bean id="handlingEventService" class="se.citerus.dddsample.service.HandlingEventServiceImpl">
18 22
     <property name="cargoRepository" ref="cargoRepository"/>
19 23
     <property name="handlingEventRepository" ref="handlingEventRepository"/>

+ 2
- 2
dddsample/src/main/webapp/WEB-INF/dispatch-servlet.xml 查看文件

@@ -16,15 +16,15 @@
16 16
     <property name="commandName" value="trackCommand"/>
17 17
     <property name="formView" value="cargo/track"/>
18 18
     <property name="successView" value="start"/>
19
-    <property name="cargoService" ref="cargoService"/>
19
+    <property name="trackingService" ref="trackingService"/>
20 20
     <property name="validator" ref="trackCommandValidator"/>
21 21
   </bean>
22 22
 
23 23
   <bean id="trackCommandValidator" class="se.citerus.dddsample.web.command.TrackCommandValidator"/>
24 24
 
25 25
   <bean name="/admin/*" class="se.citerus.dddsample.web.CargoAdminController">
26
-    <property name="cargoService" ref="cargoService"/>
27 26
     <property name="routingService" ref="routingService"/>
27
+    <property name="bookingService" ref="bookingService"/>
28 28
     <property name="carrierMovementRepository" ref="carrierMovementRepository"/>
29 29
     <property name="locationRepository" ref="locationRepository"/>
30 30
   </bean>

+ 55
- 0
dddsample/src/test/java/se/citerus/dddsample/service/BookingServiceTest.java 查看文件

@@ -0,0 +1,55 @@
1
+package se.citerus.dddsample.service;
2
+
3
+import junit.framework.TestCase;
4
+import static org.easymock.EasyMock.*;
5
+import se.citerus.dddsample.domain.Cargo;
6
+import static se.citerus.dddsample.domain.SampleLocations.CHICAGO;
7
+import static se.citerus.dddsample.domain.SampleLocations.STOCKHOLM;
8
+import se.citerus.dddsample.domain.TrackingId;
9
+import se.citerus.dddsample.domain.UnLocode;
10
+import se.citerus.dddsample.repository.CargoRepository;
11
+import se.citerus.dddsample.repository.LocationRepository;
12
+
13
+public class BookingServiceTest extends TestCase {
14
+
15
+  BookingServiceImpl cargoService;
16
+  CargoRepository cargoRepository;
17
+  LocationRepository locationRepository;
18
+
19
+  protected void setUp() throws Exception {
20
+    cargoService = new BookingServiceImpl();
21
+    cargoRepository = createMock(CargoRepository.class);
22
+    locationRepository = createMock(LocationRepository.class);
23
+    cargoService.setCargoRepository(cargoRepository);
24
+    cargoService.setLocationRepository(locationRepository);
25
+  }
26
+
27
+  public void testRegisterNew() {
28
+    TrackingId expectedTrackingId = new TrackingId("TRK1");
29
+    UnLocode fromUnlocode = new UnLocode("USCHI");
30
+    UnLocode toUnlocode = new UnLocode("SESTO");
31
+
32
+    expect(cargoRepository.nextTrackingId()).andReturn(expectedTrackingId);
33
+    expect(locationRepository.find(fromUnlocode)).andReturn(CHICAGO);
34
+    expect(locationRepository.find(toUnlocode)).andReturn(STOCKHOLM);
35
+
36
+    cargoRepository.save(isA(Cargo.class));
37
+
38
+    replay(cargoRepository, locationRepository);
39
+
40
+    TrackingId trackingId = cargoService.registerNewCargo(fromUnlocode, toUnlocode);
41
+    assertEquals(expectedTrackingId, trackingId);
42
+  }
43
+
44
+  public void testRegisterNewNullArguments() {
45
+    replay(cargoRepository, locationRepository);
46
+    try {
47
+      cargoService.registerNewCargo(null, null);
48
+      fail("Null arguments should not be allowed");
49
+    } catch (IllegalArgumentException expected) {}
50
+  }
51
+
52
+  protected void tearDown() throws Exception {
53
+    verify(cargoRepository, locationRepository);
54
+  }
55
+}

dddsample/src/test/java/se/citerus/dddsample/service/CargoServiceTest.java → dddsample/src/test/java/se/citerus/dddsample/service/TrackingServiceTest.java 查看文件

@@ -6,7 +6,6 @@ import se.citerus.dddsample.domain.*;
6 6
 import static se.citerus.dddsample.domain.SampleLocations.CHICAGO;
7 7
 import static se.citerus.dddsample.domain.SampleLocations.STOCKHOLM;
8 8
 import se.citerus.dddsample.repository.CargoRepository;
9
-import se.citerus.dddsample.repository.LocationRepository;
10 9
 import se.citerus.dddsample.service.dto.CargoTrackingDTO;
11 10
 import se.citerus.dddsample.service.dto.HandlingEventDTO;
12 11
 
@@ -15,19 +14,16 @@ import java.util.Date;
15 14
 import java.util.List;
16 15
 
17 16
 
18
-public class CargoServiceTest extends TestCase {
17
+public class TrackingServiceTest extends TestCase {
19 18
 
20
-  CargoServiceImpl cargoService;
19
+  TrackingServiceImpl cargoService;
21 20
   CargoRepository cargoRepository;
22
-  LocationRepository locationRepository;
23 21
 
24 22
   protected void setUp() throws Exception {
25
-    cargoService = new CargoServiceImpl();
23
+    cargoService = new TrackingServiceImpl();
26 24
     cargoRepository = createMock(CargoRepository.class);
27
-    locationRepository = createMock(LocationRepository.class);
28 25
 
29 26
     cargoService.setCargoRepository(cargoRepository);
30
-    cargoService.setLocationRepository(locationRepository);
31 27
   }
32 28
 
33 29
   public void testTrackingScenario() {
@@ -90,32 +86,8 @@ public class CargoServiceTest extends TestCase {
90 86
     assertNull(cargoDTO);
91 87
   }
92 88
 
93
-  public void testRegisterNew() {
94
-    TrackingId expectedTrackingId = new TrackingId("TRK1");
95
-    UnLocode fromUnlocode = new UnLocode("USCHI");
96
-    UnLocode toUnlocode = new UnLocode("SESTO");
97
-
98
-    expect(cargoRepository.nextTrackingId()).andReturn(expectedTrackingId);
99
-    expect(locationRepository.find(fromUnlocode)).andReturn(CHICAGO);
100
-    expect(locationRepository.find(toUnlocode)).andReturn(STOCKHOLM);
101
-
102
-    cargoRepository.save(isA(Cargo.class));
103
-
104
-    replay(cargoRepository, locationRepository);
105
-    
106
-    TrackingId trackingId = cargoService.registerNewCargo(fromUnlocode, toUnlocode);
107
-    assertEquals(expectedTrackingId, trackingId);
108
-  }
109
-
110
-  public void testRegisterNewNullArguments() {
111
-    try {
112
-      cargoService.registerNewCargo(null, null);
113
-      fail("Null arguments should not be allowed");
114
-    } catch (IllegalArgumentException expected) {}
115
-  }
116
-
117 89
   protected void onTearDown() throws Exception {
118
-    verify(cargoRepository, locationRepository);
90
+    verify(cargoRepository);
119 91
   }
120 92
 
121 93
 }

+ 13
- 32
dddsample/src/test/java/se/citerus/dddsample/web/CargoTrackingControllerTest.java 查看文件

@@ -9,15 +9,17 @@ import org.springframework.validation.BindingResult;
9 9
 import org.springframework.validation.Errors;
10 10
 import org.springframework.validation.FieldError;
11 11
 import org.springframework.web.servlet.ModelAndView;
12
-import se.citerus.dddsample.domain.*;
12
+import se.citerus.dddsample.domain.Cargo;
13
+import se.citerus.dddsample.domain.HandlingEvent;
13 14
 import static se.citerus.dddsample.domain.SampleLocations.HONGKONG;
14 15
 import static se.citerus.dddsample.domain.SampleLocations.TOKYO;
15
-import se.citerus.dddsample.service.CargoService;
16
+import se.citerus.dddsample.domain.StatusCode;
17
+import se.citerus.dddsample.domain.TrackingId;
18
+import se.citerus.dddsample.service.TrackingService;
16 19
 import se.citerus.dddsample.service.dto.CargoTrackingDTO;
17 20
 import se.citerus.dddsample.web.command.TrackCommand;
18 21
 
19 22
 import java.util.Date;
20
-import java.util.List;
21 23
 
22 24
 public class CargoTrackingControllerTest extends TestCase {
23 25
   CargoTrackingController controller;
@@ -39,8 +41,8 @@ public class CargoTrackingControllerTest extends TestCase {
39 41
     controller.setCommandName("test-command-name");
40 42
   }
41 43
 
42
-  private CargoService getCargoServiceMock() {
43
-    return new EmptyStubCargoService() {
44
+  private TrackingService getCargoServiceMock() {
45
+    return new EmptyStubTrackingService() {
44 46
 
45 47
       public CargoTrackingDTO track(TrackingId trackingId) {
46 48
         final Cargo cargo = new Cargo(trackingId, HONGKONG, TOKYO);
@@ -64,12 +66,12 @@ public class CargoTrackingControllerTest extends TestCase {
64 66
     };
65 67
   }
66 68
 
67
-  private CargoService getCargoServiceNullMock() {
68
-    return new EmptyStubCargoService();
69
+  private TrackingService getTrackingServiceNullMock() {
70
+    return new EmptyStubTrackingService();
69 71
   }
70 72
 
71 73
   public void testHandleGet() throws Exception {
72
-    controller.setCargoService(getCargoServiceMock());
74
+    controller.setTrackingService(getCargoServiceMock());
73 75
     request.setMethod("GET");
74 76
 
75 77
     ModelAndView mav = controller.handleRequest(request, response);
@@ -80,7 +82,7 @@ public class CargoTrackingControllerTest extends TestCase {
80 82
   }
81 83
 
82 84
   public void testHandlePost() throws Exception {
83
-    controller.setCargoService(getCargoServiceMock());
85
+    controller.setTrackingService(getCargoServiceMock());
84 86
     request.addParameter("trackingId", "JKL456");
85 87
     request.setMethod("POST");
86 88
 
@@ -94,7 +96,7 @@ public class CargoTrackingControllerTest extends TestCase {
94 96
   }
95 97
 
96 98
   public void testUnknownCargo() throws Exception {
97
-    controller.setCargoService(getCargoServiceNullMock());
99
+    controller.setTrackingService(getTrackingServiceNullMock());
98 100
     request.setMethod("POST");
99 101
     request.setParameter("trackingId", "unknown-id");
100 102
 
@@ -113,32 +115,11 @@ public class CargoTrackingControllerTest extends TestCase {
113 115
     assertEquals(command.getTrackingId(), fe.getArguments()[0]);
114 116
   }
115 117
 
116
-  private class EmptyStubCargoService implements CargoService {
117
-    public TrackingId registerNewCargo(UnLocode origin, UnLocode destination) {
118
-      return null;
119
-    }
120
-
121
-    public List<UnLocode> listShippingLocations() {
122
-      return null;
123
-    }
124
-
118
+  private class EmptyStubTrackingService implements TrackingService {
125 119
     public CargoTrackingDTO track(TrackingId trackingId) {
126 120
       return null;
127 121
     }
128
-
129 122
     public void notify(TrackingId trackingId) {
130 123
     }
131
-
132
-    public Cargo loadCargoForRouting(TrackingId trackingId) {
133
-      return null;
134
-    }
135
-
136
-    public List<Cargo> listAllCargos() {
137
-      return null;
138
-    }
139
-
140
-    public void assignCargoToRoute(TrackingId trackingId, Itinerary itinerary) {
141
-    }
142
-
143 124
   }
144 125
 }