Sfoglia il codice sorgente

Fixed bug when trying to find a nonexisting cargo.

Patrik Fredriksson 18 anni fa
parent
commit
d8847bd9b2

+ 13
- 7
dddsample/src/main/java/se/citerus/dddsample/application/persistence/CargoRepositoryHibernate.java Vedi File

@@ -22,11 +22,17 @@ public class CargoRepositoryHibernate extends HibernateRepository implements Car
22 22
     // Query for id and then perform a standard load()
23 23
     // to use metadata-defined query and lazy proxy access
24 24
     Long id = (Long) getSession().
25
-            createQuery("select id from Cargo where trackingId = :tid").
26
-            setParameter("tid", tid).
27
-            uniqueResult();
25
+       createQuery("select id from Cargo where trackingId = :tid").
26
+       setParameter("tid", tid).
27
+       uniqueResult();
28
+
29
+    if (id != null) {
30
+      return (Cargo) getSession().get(Cargo.class, id);
31
+    } else {
32
+      return null;
33
+    }
34
+
28 35
 
29
-    return (Cargo) getSession().load(Cargo.class, id);
30 36
   }
31 37
 
32 38
   public void save(Cargo cargo) {
@@ -35,8 +41,8 @@ public class CargoRepositoryHibernate extends HibernateRepository implements Car
35 41
     // Delete orphaned itineraries - conceptually the responsibility
36 42
     // of the Cargo aggregate
37 43
     final List<Itinerary> orphans = getSession().
38
-      createQuery("from Itinerary where cargo = null").
39
-      list();
44
+       createQuery("from Itinerary where cargo = null").
45
+       list();
40 46
     for (Itinerary orphan : orphans) {
41 47
       getSession().delete(orphan);
42 48
     }
@@ -45,7 +51,7 @@ public class CargoRepositoryHibernate extends HibernateRepository implements Car
45 51
   public TrackingId nextTrackingId() {
46 52
     final String random = UUID.randomUUID().toString().toUpperCase();
47 53
     return new TrackingId(
48
-      random.substring(0, random.indexOf("-"))
54
+       random.substring(0, random.indexOf("-"))
49 55
     );
50 56
   }
51 57
 

+ 1
- 1
dddsample/src/main/java/se/citerus/dddsample/domain/model/cargo/CargoRepository.java Vedi File

@@ -8,7 +8,7 @@ public interface CargoRepository {
8 8
    * Finds a cargo using given id.
9 9
    *
10 10
    * @param trackingId Id
11
-   * @return Cargo
11
+   * @return Cargo if found, else {@code null}
12 12
    */
13 13
   Cargo find(TrackingId trackingId);
14 14
 

+ 4
- 0
dddsample/src/test/java/se/citerus/dddsample/application/persistence/CargoRepositoryTest.java Vedi File

@@ -85,6 +85,10 @@ public class CargoRepositoryTest extends AbstractRepositoryTest {
85 85
     assertEquals(cargo, event.cargo());
86 86
   }
87 87
 
88
+  public void testFindByCargoIdUnknownId() {
89
+    assertNull(cargoRepository.find(new TrackingId("UNKNOWN")));
90
+  }
91
+
88 92
   private void assertLeg(Leg firstLeg, String cmId, Location expectedFrom, Location expectedTo) {
89 93
     assertEquals(new CarrierMovementId(cmId), firstLeg.carrierMovement().carrierMovementId());
90 94
     assertEquals(expectedFrom, firstLeg.from());