jorgen_falk пре 18 година
родитељ
комит
02b9f2998c

+ 22
- 1
dddsample/pom.xml Прегледај датотеку

@@ -119,10 +119,31 @@
119 119
         </executions>
120 120
         <configuration>
121 121
           <sei>se.citerus.dddsample.ws.HandlingEventServiceEndpointImpl</sei>
122
+          <genWsdl>true</genWsdl>
122 123
           <verbose>true</verbose>
123 124
         </configuration>
124 125
       </plugin>
125
-
126
+      
127
+      <!-- Jar plugin for generation of WS Client classes -->
128
+      <plugin>
129
+        <groupId>org.apache.maven.plugins</groupId>
130
+        <artifactId>maven-jar-plugin</artifactId>
131
+        <executions>
132
+          <execution>
133
+            <phase>package</phase>
134
+            <goals>
135
+              <goal>jar</goal>
136
+            </goals>
137
+            <configuration>
138
+              <classifier>ws-client</classifier>
139
+              <includes>
140
+                <include>**/se/citerus/dddsample/ws/*</include>
141
+              </includes>
142
+            </configuration>
143
+          </execution>
144
+        </executions>
145
+      </plugin>
146
+      
126 147
     </plugins>
127 148
   </build>
128 149
   <dependencies>

+ 9
- 0
dddsample/src/main/java/se/citerus/dddsample/repository/LocationRepository.java Прегледај датотеку

@@ -0,0 +1,9 @@
1
+package se.citerus.dddsample.repository;
2
+
3
+import se.citerus.dddsample.domain.Location;
4
+
5
+public interface LocationRepository {
6
+
7
+  Location find(String string);
8
+
9
+}

+ 13
- 0
dddsample/src/main/java/se/citerus/dddsample/repository/LocationRepositoryHibernate.java Прегледај датотеку

@@ -0,0 +1,13 @@
1
+package se.citerus.dddsample.repository;
2
+
3
+import se.citerus.dddsample.domain.Location;
4
+
5
+public class LocationRepositoryHibernate extends HibernateRepository implements LocationRepository {
6
+
7
+  public Location find(String unlcode) {
8
+    return (Location) getSession().
9
+          createQuery("from Location where unlocode = ?").
10
+          setParameter(0, unlcode).
11
+          uniqueResult();
12
+  }
13
+}

+ 1
- 1
dddsample/src/main/java/se/citerus/dddsample/service/HandlingEventService.java Прегледај датотеку

@@ -23,6 +23,6 @@ public interface HandlingEventService {
23 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
-  throws UnknownCarrierMovementIdException, UnknownTrackingIdException;
26
+  throws UnknownCarrierMovementIdException, UnknownTrackingIdException, UnknownLocationException;
27 27
 
28 28
 }

+ 18
- 4
dddsample/src/main/java/se/citerus/dddsample/service/HandlingEventServiceImpl.java Прегледај датотеку

@@ -6,6 +6,7 @@ import se.citerus.dddsample.domain.*;
6 6
 import se.citerus.dddsample.repository.CargoRepository;
7 7
 import se.citerus.dddsample.repository.CarrierMovementRepository;
8 8
 import se.citerus.dddsample.repository.HandlingEventRepository;
9
+import se.citerus.dddsample.repository.LocationRepository;
9 10
 
10 11
 import java.util.Date;
11 12
 
@@ -13,9 +14,10 @@ public class HandlingEventServiceImpl implements HandlingEventService {
13 14
   private CargoRepository cargoRepository;
14 15
   private CarrierMovementRepository carrierMovementRepository;
15 16
   private HandlingEventRepository handlingEventRepository;
17
+  private LocationRepository locationRepository;
16 18
 
17 19
   @Transactional(readOnly = false)
18
-  public void register(Date completionTime, TrackingId trackingId, CarrierMovementId carrierMovementId, String unlocode, HandlingEvent.Type type) throws UnknownCarrierMovementIdException, UnknownTrackingIdException {
20
+  public void register(Date completionTime, TrackingId trackingId, CarrierMovementId carrierMovementId, String unlocode, HandlingEvent.Type type) throws UnknownCarrierMovementIdException, UnknownTrackingIdException, UnknownLocationException {
19 21
     Cargo cargo = findCargo(trackingId);
20 22
     CarrierMovement carrierMovement = findCarrierMovement(carrierMovementId);
21 23
     Location location = findLocation(unlocode);
@@ -51,9 +53,17 @@ public class HandlingEventServiceImpl implements HandlingEventService {
51 53
     return carrierMovement;
52 54
   }
53 55
 
54
-  private Location findLocation(String unlocode) {
55
-    // TODO: introdcue Location repository, lookup and add new Location when not found
56
-    return new Location(unlocode);
56
+  private Location findLocation(String unlocode) throws UnknownLocationException {
57
+    if (unlocode == null){
58
+      return Location.UNKNOWN;
59
+    }
60
+    
61
+    Location location = locationRepository.find(unlocode);
62
+    if (location == null){
63
+      throw new UnknownLocationException(unlocode);
64
+    }
65
+    
66
+    return location;
57 67
   }
58 68
 
59 69
   public void setCargoRepository(CargoRepository cargoRepository) {
@@ -67,4 +77,8 @@ public class HandlingEventServiceImpl implements HandlingEventService {
67 77
   public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
68 78
     this.handlingEventRepository = handlingEventRepository;
69 79
   }
80
+
81
+  public void setLocationRepository(LocationRepository locationRepository) {
82
+    this.locationRepository = locationRepository;
83
+  }
70 84
 }

+ 16
- 0
dddsample/src/main/java/se/citerus/dddsample/service/UnknownLocationException.java Прегледај датотеку

@@ -0,0 +1,16 @@
1
+package se.citerus.dddsample.service;
2
+
3
+public class UnknownLocationException extends Exception {
4
+
5
+  private String unlocode;
6
+
7
+  public UnknownLocationException(String unlocode) {
8
+    this.unlocode = unlocode;
9
+  }
10
+
11
+
12
+  @Override
13
+  public String getMessage() {
14
+    return "No location with UN location code " + unlocode + " exists in the system";
15
+  }
16
+}

+ 3
- 0
dddsample/src/main/resources/context-persistence.xml Прегледај датотеку

@@ -48,4 +48,7 @@
48 48
     <property name="sessionFactory" ref="sessionFactory"/>
49 49
   </bean>
50 50
 
51
+  <bean id="locationRepository" class="se.citerus.dddsample.repository.LocationRepositoryHibernate">
52
+    <property name="sessionFactory" ref="sessionFactory"/>
53
+  </bean>
51 54
 </beans>

+ 1
- 0
dddsample/src/main/resources/context-service.xml Прегледај датотеку

@@ -21,6 +21,7 @@
21 21
     <property name="cargoRepository" ref="cargoRepository"/>
22 22
     <property name="handlingEventRepository" ref="handlingEventRepository"/>
23 23
     <property name="carrierRepository" ref="carrierMovementRepository"/>
24
+    <property name="locationRepository" ref="locationRepository"/>
24 25
   </bean>
25 26
 
26 27
 </beans>

+ 17
- 0
dddsample/src/test/java/se/citerus/dddsample/repository/LocationRepositoryTest.java Прегледај датотеку

@@ -0,0 +1,17 @@
1
+package se.citerus.dddsample.repository;
2
+
3
+import se.citerus.dddsample.domain.Location;
4
+
5
+public class LocationRepositoryTest extends AbstractRepositoryTest {
6
+  private LocationRepository locationRepository;
7
+  
8
+  public void testFind() throws Exception {
9
+    Location location = locationRepository.find("AUMEL");
10
+    assertNotNull(location);
11
+    assertEquals("AUMEL", location.unlocode());
12
+  }
13
+
14
+  public void setLocationRepository(LocationRepository locationRepository) {
15
+    this.locationRepository = locationRepository;
16
+  }
17
+}

+ 35
- 4
dddsample/src/test/java/se/citerus/dddsample/service/HandlingEventServiceTest.java Прегледај датотеку

@@ -6,6 +6,7 @@ import se.citerus.dddsample.domain.*;
6 6
 import se.citerus.dddsample.repository.CargoRepository;
7 7
 import se.citerus.dddsample.repository.CarrierMovementRepository;
8 8
 import se.citerus.dddsample.repository.HandlingEventRepository;
9
+import se.citerus.dddsample.repository.LocationRepository;
9 10
 
10 11
 import java.util.Date;
11 12
 
@@ -14,21 +15,28 @@ public class HandlingEventServiceTest extends TestCase {
14 15
   private CargoRepository cargoRepository;
15 16
   private CarrierMovementRepository carrierMovementRepository;
16 17
   private HandlingEventRepository handlingEventRepository;
18
+  private LocationRepository locationRepository;
17 19
   
18 20
   private final Cargo cargoABC = new Cargo(new TrackingId("ABC"), new Location("AFROM"), new Location("ABCTO"));
19 21
   private final Cargo cargoXYZ = new Cargo(new TrackingId("XYZ"), new Location("XFROM"), new Location("XYZTO"));
20 22
   private final CarrierMovement cmAAA_BBB = new CarrierMovement(
21 23
           new CarrierMovementId("CAR_001"), new Location("AAAAA"), new Location("BBBBB"));
24
+  
25
+  private final Location locationSESTO = new Location("SESTO");
26
+  private final Location locationAUMEL = new Location("AUMEL");
27
+  private final Location locationCNHKG = new Location("CNHKG");
22 28
 
23 29
   protected void setUp() throws Exception{
24 30
     service = new HandlingEventServiceImpl();
25 31
     cargoRepository = createMock(CargoRepository.class);
26 32
     carrierMovementRepository = createMock(CarrierMovementRepository.class);
27 33
     handlingEventRepository = createMock(HandlingEventRepository.class);
34
+    locationRepository = createMock(LocationRepository.class);
28 35
     
29 36
     service.setCargoRepository(cargoRepository);
30 37
     service.setCarrierRepository(carrierMovementRepository);
31 38
     service.setHandlingEventRepository(handlingEventRepository);
39
+    service.setLocationRepository(locationRepository);
32 40
   }
33 41
 
34 42
   protected void tearDown() throws Exception {
@@ -43,11 +51,13 @@ public class HandlingEventServiceTest extends TestCase {
43 51
 
44 52
     final CarrierMovementId carrierMovementId = new CarrierMovementId("AAA_BBB");
45 53
     expect(carrierMovementRepository.find(carrierMovementId)).andReturn(cmAAA_BBB);
54
+    
55
+    expect(locationRepository.find("SESTO")).andReturn(locationSESTO);
46 56
 
47 57
     // TODO: does not inspect the handling event instance in a sufficient way
48 58
     handlingEventRepository.save(isA(HandlingEvent.class));
49 59
 
50
-    replay(cargoRepository, carrierMovementRepository, handlingEventRepository);
60
+    replay(cargoRepository, carrierMovementRepository, handlingEventRepository, locationRepository);
51 61
     
52 62
     service.register(date, trackingId, carrierMovementId, "SESTO", HandlingEvent.Type.LOAD);
53 63
   }
@@ -59,8 +69,10 @@ public class HandlingEventServiceTest extends TestCase {
59 69
     expect(cargoRepository.find(trackingId)).andReturn(cargoABC);
60 70
 
61 71
     handlingEventRepository.save(isA(HandlingEvent.class));
72
+    
73
+    expect(locationRepository.find("SESTO")).andReturn(locationSESTO);
62 74
 
63
-    replay(cargoRepository, carrierMovementRepository, handlingEventRepository);
75
+    replay(cargoRepository, carrierMovementRepository, handlingEventRepository, locationRepository);
64 76
 
65 77
     service.register(date, trackingId, null, "SESTO", HandlingEvent.Type.CLAIM);
66 78
   }
@@ -75,7 +87,9 @@ public class HandlingEventServiceTest extends TestCase {
75 87
     final TrackingId trackingId = new TrackingId("XYZ");
76 88
     expect(cargoRepository.find(trackingId)).andReturn(new Cargo(trackingId, new Location("FROMX"), new Location("TOYYY")));
77 89
 
78
-    replay(cargoRepository, carrierMovementRepository, handlingEventRepository);
90
+    expect(locationRepository.find("AUMEL")).andReturn(locationAUMEL);
91
+    
92
+    replay(cargoRepository, carrierMovementRepository, handlingEventRepository, locationRepository);
79 93
     
80 94
     try {
81 95
       service.register(date, trackingId, carrierMovementId, "AUMEL", HandlingEvent.Type.UNLOAD);
@@ -89,11 +103,28 @@ public class HandlingEventServiceTest extends TestCase {
89 103
     final TrackingId trackingId = new TrackingId("XYZ");
90 104
     expect(cargoRepository.find(trackingId)).andReturn(null);
91 105
 
92
-    replay(cargoRepository, carrierMovementRepository, handlingEventRepository);
106
+    expect(locationRepository.find("CNHKG")).andReturn(locationCNHKG);
107
+    
108
+    replay(cargoRepository, carrierMovementRepository, handlingEventRepository, locationRepository);
93 109
     
94 110
     try {
95 111
       service.register(date, trackingId, null, "CNHKG", HandlingEvent.Type.CLAIM);
96 112
       fail("Should not be able to register an event with non-existing cargo");
97 113
     } catch (UnknownTrackingIdException expected) {}
98 114
   }
115
+  
116
+  public void testRegisterEventInvalidLocation() throws Exception {
117
+    final Date date = new Date();
118
+
119
+    final TrackingId trackingId = new TrackingId("XYZ");
120
+    expect(cargoRepository.find(trackingId)).andReturn(cargoXYZ);
121
+    expect(locationRepository.find("WAY_OFF")).andReturn(null);
122
+    
123
+    replay(cargoRepository, carrierMovementRepository, handlingEventRepository, locationRepository);
124
+    
125
+    try {
126
+      service.register(date, trackingId, null, "WAY_OFF", HandlingEvent.Type.CLAIM);
127
+      fail("Should not be able to register an event with non-existing Location");
128
+    } catch (UnknownLocationException expected) {}
129
+  }
99 130
 }