|
|
@@ -1,171 +1,231 @@
|
|
1
|
1
|
package se.citerus.dddsample.infrastructure.persistence.hibernate;
|
|
2
|
2
|
|
|
|
3
|
+import org.hibernate.SessionFactory;
|
|
|
4
|
+import org.hibernate.classic.Session;
|
|
|
5
|
+import org.junit.Before;
|
|
|
6
|
+import org.junit.Test;
|
|
|
7
|
+import org.junit.runner.RunWith;
|
|
|
8
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
9
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
10
|
+import org.springframework.orm.hibernate3.HibernateTransactionManager;
|
|
|
11
|
+import org.springframework.test.context.ContextConfiguration;
|
|
|
12
|
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
|
13
|
+import org.springframework.test.context.transaction.TransactionConfiguration;
|
|
|
14
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
15
|
+import org.springframework.transaction.support.TransactionTemplate;
|
|
3
|
16
|
import se.citerus.dddsample.application.util.SampleDataGenerator;
|
|
4
|
17
|
import se.citerus.dddsample.domain.model.cargo.*;
|
|
5
|
18
|
import se.citerus.dddsample.domain.model.handling.HandlingEvent;
|
|
6
|
|
-import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.LOAD;
|
|
7
|
|
-import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.RECEIVE;
|
|
|
19
|
+import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
|
|
8
|
20
|
import se.citerus.dddsample.domain.model.location.Location;
|
|
9
|
21
|
import se.citerus.dddsample.domain.model.location.LocationRepository;
|
|
10
|
|
-import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
|
|
11
|
22
|
import se.citerus.dddsample.domain.model.location.UnLocode;
|
|
12
|
|
-import static se.citerus.dddsample.domain.model.voyage.SampleVoyages.CM004;
|
|
13
|
23
|
import se.citerus.dddsample.domain.model.voyage.Voyage;
|
|
14
|
24
|
import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
|
|
15
|
25
|
import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
|
|
|
26
|
+import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
|
|
|
27
|
+import static se.citerus.dddsample.domain.model.voyage.SampleVoyages.*;
|
|
|
28
|
+import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.*;
|
|
|
29
|
+
|
|
|
30
|
+import javax.sql.DataSource;
|
|
16
|
31
|
|
|
|
32
|
+import java.lang.reflect.Field;
|
|
17
|
33
|
import java.util.Arrays;
|
|
18
|
34
|
import java.util.Date;
|
|
19
|
35
|
import java.util.List;
|
|
20
|
36
|
import java.util.Map;
|
|
21
|
37
|
|
|
22
|
|
-/*public class CargoRepositoryTest extends AbstractRepositoryTest {
|
|
|
38
|
+import static org.junit.Assert.*;
|
|
23
|
39
|
|
|
24
|
|
- CargoRepository cargoRepository;
|
|
25
|
|
- LocationRepository locationRepository;
|
|
26
|
|
- VoyageRepository voyageRepository;
|
|
|
40
|
+@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
41
|
+@ContextConfiguration(value = {"/context-infrastructure-persistence.xml", "/context-domain.xml"})
|
|
|
42
|
+@TransactionConfiguration(transactionManager = "transactionManager")
|
|
|
43
|
+@Transactional
|
|
|
44
|
+public class CargoRepositoryTest {
|
|
27
|
45
|
|
|
28
|
|
- public void setCargoRepository(CargoRepository cargoRepository) {
|
|
29
|
|
- this.cargoRepository = cargoRepository;
|
|
30
|
|
- }
|
|
|
46
|
+ @Autowired
|
|
|
47
|
+ CargoRepository cargoRepository;
|
|
31
|
48
|
|
|
32
|
|
- public void setLocationRepository(LocationRepository locationRepository) {
|
|
33
|
|
- this.locationRepository = locationRepository;
|
|
34
|
|
- }
|
|
|
49
|
+ @Autowired
|
|
|
50
|
+ LocationRepository locationRepository;
|
|
35
|
51
|
|
|
36
|
|
- public void setVoyageRepository(VoyageRepository voyageRepository) {
|
|
37
|
|
- this.voyageRepository = voyageRepository;
|
|
38
|
|
- }
|
|
|
52
|
+ @Autowired
|
|
|
53
|
+ VoyageRepository voyageRepository;
|
|
39
|
54
|
|
|
40
|
|
- public void testFindByCargoId() {
|
|
41
|
|
- final TrackingId trackingId = new TrackingId("FGH");
|
|
42
|
|
- final Cargo cargo = cargoRepository.find(trackingId);
|
|
43
|
|
- assertEquals(STOCKHOLM, cargo.origin());
|
|
44
|
|
- assertEquals(HONGKONG, cargo.routeSpecification().origin());
|
|
45
|
|
- assertEquals(HELSINKI, cargo.routeSpecification().destination());
|
|
|
55
|
+ @Autowired
|
|
|
56
|
+ HandlingEventRepository handlingEventRepository;
|
|
46
|
57
|
|
|
47
|
|
- assertNotNull(cargo.delivery());
|
|
|
58
|
+ @Autowired
|
|
|
59
|
+ SessionFactory sessionFactory;
|
|
48
|
60
|
|
|
49
|
|
- final List<HandlingEvent> events = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
|
|
50
|
|
- assertEquals(2, events.size());
|
|
|
61
|
+ @Autowired
|
|
|
62
|
+ private DataSource dataSource;
|
|
51
|
63
|
|
|
52
|
|
- HandlingEvent firstEvent = events.get(0);
|
|
53
|
|
- assertHandlingEvent(cargo, firstEvent, RECEIVE, HONGKONG, 100, 160, Voyage.NONE);
|
|
|
64
|
+ @Autowired
|
|
|
65
|
+ private HibernateTransactionManager transactionManager;
|
|
54
|
66
|
|
|
55
|
|
- HandlingEvent secondEvent = events.get(1);
|
|
|
67
|
+ private JdbcTemplate jdbcTemplate;
|
|
56
|
68
|
|
|
57
|
|
- Voyage hongkongMelbourneTokyoAndBack = new Voyage.Builder(
|
|
58
|
|
- new VoyageNumber("0303"), HONGKONG).
|
|
59
|
|
- addMovement(MELBOURNE, new Date(), new Date()).
|
|
60
|
|
- addMovement(TOKYO, new Date(), new Date()).
|
|
61
|
|
- addMovement(HONGKONG, new Date(), new Date()).
|
|
62
|
|
- build();
|
|
63
|
|
-
|
|
64
|
|
- assertHandlingEvent(cargo, secondEvent, LOAD, HONGKONG, 150, 110, hongkongMelbourneTokyoAndBack);
|
|
|
69
|
+ @Before
|
|
|
70
|
+ public void setup() {
|
|
|
71
|
+ jdbcTemplate = new JdbcTemplate(dataSource);
|
|
|
72
|
+ SampleDataGenerator.loadSampleData(jdbcTemplate, new TransactionTemplate(transactionManager));
|
|
|
73
|
+ }
|
|
65
|
74
|
|
|
66
|
|
- List<Leg> legs = cargo.itinerary().legs();
|
|
67
|
|
- assertEquals(3, legs.size());
|
|
|
75
|
+ @Test
|
|
|
76
|
+ public void testFindByCargoId() {
|
|
|
77
|
+ final TrackingId trackingId = new TrackingId("FGH");
|
|
|
78
|
+ final Cargo cargo = cargoRepository.find(trackingId);
|
|
|
79
|
+ assertEquals(STOCKHOLM, cargo.origin());
|
|
|
80
|
+ assertEquals(HONGKONG, cargo.routeSpecification().origin());
|
|
|
81
|
+ assertEquals(HELSINKI, cargo.routeSpecification().destination());
|
|
68
|
82
|
|
|
69
|
|
- Leg firstLeg = legs.get(0);
|
|
70
|
|
- assertLeg(firstLeg, "0101", HONGKONG, MELBOURNE);
|
|
|
83
|
+ assertNotNull(cargo.delivery());
|
|
71
|
84
|
|
|
72
|
|
- Leg secondLeg = legs.get(1);
|
|
73
|
|
- assertLeg(secondLeg, "0101", MELBOURNE, STOCKHOLM);
|
|
|
85
|
+ final List<HandlingEvent> events = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
|
|
|
86
|
+ assertEquals(2, events.size());
|
|
74
|
87
|
|
|
75
|
|
- Leg thirdLeg = legs.get(2);
|
|
76
|
|
- assertLeg(thirdLeg, "0101", STOCKHOLM, HELSINKI);
|
|
77
|
|
- }
|
|
|
88
|
+ HandlingEvent firstEvent = events.get(0);
|
|
|
89
|
+ assertHandlingEvent(cargo, firstEvent, RECEIVE, HONGKONG, 100, 160, Voyage.NONE);
|
|
78
|
90
|
|
|
79
|
|
- private void assertHandlingEvent(Cargo cargo, HandlingEvent event, HandlingEvent.Type expectedEventType, Location expectedLocation, int completionTimeMs, int registrationTimeMs, Voyage voyage) {
|
|
80
|
|
- assertEquals(expectedEventType, event.type());
|
|
81
|
|
- assertEquals(expectedLocation, event.location());
|
|
|
91
|
+ HandlingEvent secondEvent = events.get(1);
|
|
82
|
92
|
|
|
83
|
|
- Date expectedCompletionTime = SampleDataGenerator.offset(completionTimeMs);
|
|
84
|
|
- assertEquals(expectedCompletionTime, event.completionTime());
|
|
|
93
|
+ Voyage hongkongMelbourneTokyoAndBack = new Voyage.Builder(
|
|
|
94
|
+ new VoyageNumber("0303"), HONGKONG).
|
|
|
95
|
+ addMovement(MELBOURNE, new Date(), new Date()).
|
|
|
96
|
+ addMovement(TOKYO, new Date(), new Date()).
|
|
|
97
|
+ addMovement(HONGKONG, new Date(), new Date()).
|
|
|
98
|
+ build();
|
|
85
|
99
|
|
|
86
|
|
- Date expectedRegistrationTime = SampleDataGenerator.offset(registrationTimeMs);
|
|
87
|
|
- assertEquals(expectedRegistrationTime, event.registrationTime());
|
|
|
100
|
+ assertHandlingEvent(cargo, secondEvent, LOAD, HONGKONG, 150, 110, hongkongMelbourneTokyoAndBack);
|
|
88
|
101
|
|
|
89
|
|
- assertEquals(voyage, event.voyage());
|
|
90
|
|
- assertEquals(cargo, event.cargo());
|
|
91
|
|
- }
|
|
|
102
|
+ List<Leg> legs = cargo.itinerary().legs();
|
|
|
103
|
+ assertEquals(3, legs.size());
|
|
92
|
104
|
|
|
93
|
|
- public void testFindByCargoIdUnknownId() {
|
|
94
|
|
- assertNull(cargoRepository.find(new TrackingId("UNKNOWN")));
|
|
95
|
|
- }
|
|
|
105
|
+ Leg firstLeg = legs.get(0);
|
|
|
106
|
+ assertLeg(firstLeg, "0101", HONGKONG, MELBOURNE);
|
|
96
|
107
|
|
|
97
|
|
- private void assertLeg(Leg firstLeg, String vn, Location expectedFrom, Location expectedTo) {
|
|
98
|
|
- assertEquals(new VoyageNumber(vn), firstLeg.voyage().voyageNumber());
|
|
99
|
|
- assertEquals(expectedFrom, firstLeg.loadLocation());
|
|
100
|
|
- assertEquals(expectedTo, firstLeg.unloadLocation());
|
|
101
|
|
- }
|
|
|
108
|
+ Leg secondLeg = legs.get(1);
|
|
|
109
|
+ assertLeg(secondLeg, "0101", MELBOURNE, STOCKHOLM);
|
|
102
|
110
|
|
|
103
|
|
- public void testSave() {
|
|
104
|
|
- TrackingId trackingId = new TrackingId("AAA");
|
|
105
|
|
- Location origin = locationRepository.find(STOCKHOLM.unLocode());
|
|
106
|
|
- Location destination = locationRepository.find(MELBOURNE.unLocode());
|
|
|
111
|
+ Leg thirdLeg = legs.get(2);
|
|
|
112
|
+ assertLeg(thirdLeg, "0101", STOCKHOLM, HELSINKI);
|
|
|
113
|
+ }
|
|
107
|
114
|
|
|
108
|
|
- Cargo cargo = new Cargo(trackingId, new RouteSpecification(origin, destination, new Date()));
|
|
109
|
|
- cargoRepository.store(cargo);
|
|
|
115
|
+ private void assertHandlingEvent(Cargo cargo, HandlingEvent event, HandlingEvent.Type expectedEventType, Location expectedLocation, int completionTimeMs, int registrationTimeMs, Voyage voyage) {
|
|
|
116
|
+ assertEquals(expectedEventType, event.type());
|
|
|
117
|
+ assertEquals(expectedLocation, event.location());
|
|
110
|
118
|
|
|
111
|
|
- cargo.assignToRoute(new Itinerary(Arrays.asList(
|
|
112
|
|
- new Leg(
|
|
113
|
|
- voyageRepository.find(new VoyageNumber("0101")),
|
|
114
|
|
- locationRepository.find(STOCKHOLM.unLocode()),
|
|
115
|
|
- locationRepository.find(MELBOURNE.unLocode()),
|
|
116
|
|
- new Date(), new Date())
|
|
117
|
|
- )));
|
|
118
|
|
-
|
|
119
|
|
- flush();
|
|
|
119
|
+ Date expectedCompletionTime = SampleDataGenerator.offset(completionTimeMs);
|
|
|
120
|
+ assertEquals(expectedCompletionTime, event.completionTime());
|
|
120
|
121
|
|
|
121
|
|
- Map<String, Object> map = sjt.queryForMap(
|
|
122
|
|
- "select * from Cargo where tracking_id = ?", trackingId.idString());
|
|
|
122
|
+ Date expectedRegistrationTime = SampleDataGenerator.offset(registrationTimeMs);
|
|
|
123
|
+ assertEquals(expectedRegistrationTime, event.registrationTime());
|
|
123
|
124
|
|
|
124
|
|
- assertEquals("AAA", map.get("TRACKING_ID"));
|
|
|
125
|
+ assertEquals(voyage, event.voyage());
|
|
|
126
|
+ assertEquals(cargo, event.cargo());
|
|
|
127
|
+ }
|
|
125
|
128
|
|
|
126
|
|
- Long originId = getLongId(origin);
|
|
127
|
|
- assertEquals(originId, map.get("SPEC_ORIGIN_ID"));
|
|
|
129
|
+ @Test
|
|
|
130
|
+ public void testFindByCargoIdUnknownId() {
|
|
|
131
|
+ assertNull(cargoRepository.find(new TrackingId("UNKNOWN")));
|
|
|
132
|
+ }
|
|
128
|
133
|
|
|
129
|
|
- Long destinationId = getLongId(destination);
|
|
130
|
|
- assertEquals(destinationId, map.get("SPEC_DESTINATION_ID"));
|
|
|
134
|
+ private void assertLeg(Leg firstLeg, String vn, Location expectedFrom, Location expectedTo) {
|
|
|
135
|
+ assertEquals(new VoyageNumber(vn), firstLeg.voyage().voyageNumber());
|
|
|
136
|
+ assertEquals(expectedFrom, firstLeg.loadLocation());
|
|
|
137
|
+ assertEquals(expectedTo, firstLeg.unloadLocation());
|
|
|
138
|
+ }
|
|
131
|
139
|
|
|
132
|
|
- getSession().clear();
|
|
|
140
|
+ @Test
|
|
|
141
|
+ public void testSave() {
|
|
|
142
|
+ TrackingId trackingId = new TrackingId("AAA");
|
|
|
143
|
+ Location origin = locationRepository.find(STOCKHOLM.unLocode());
|
|
|
144
|
+ Location destination = locationRepository.find(MELBOURNE.unLocode());
|
|
133
|
145
|
|
|
134
|
|
- final Cargo loadedCargo = cargoRepository.find(trackingId);
|
|
135
|
|
- assertEquals(1, loadedCargo.itinerary().legs().size());
|
|
136
|
|
- }
|
|
|
146
|
+ Cargo cargo = new Cargo(trackingId, new RouteSpecification(origin, destination, new Date()));
|
|
|
147
|
+ cargoRepository.store(cargo);
|
|
137
|
148
|
|
|
138
|
|
- public void testReplaceItinerary() {
|
|
139
|
|
- Cargo cargo = cargoRepository.find(new TrackingId("FGH"));
|
|
140
|
|
- Long cargoId = getLongId(cargo);
|
|
141
|
|
- assertEquals(3, sjt.queryForInt("select count(*) from Leg where cargo_id = ?", cargoId));
|
|
|
149
|
+ cargo.assignToRoute(new Itinerary(Arrays.asList(
|
|
|
150
|
+ new Leg(
|
|
|
151
|
+ voyageRepository.find(new VoyageNumber("0101")),
|
|
|
152
|
+ locationRepository.find(STOCKHOLM.unLocode()),
|
|
|
153
|
+ locationRepository.find(MELBOURNE.unLocode()),
|
|
|
154
|
+ new Date(), new Date())
|
|
|
155
|
+ )));
|
|
142
|
156
|
|
|
143
|
|
- Location legFrom = locationRepository.find(new UnLocode("FIHEL"));
|
|
144
|
|
- Location legTo = locationRepository.find(new UnLocode("DEHAM"));
|
|
145
|
|
- Itinerary newItinerary = new Itinerary(Arrays.asList(new Leg(CM004, legFrom, legTo, new Date(), new Date())));
|
|
|
157
|
+ flush();
|
|
146
|
158
|
|
|
147
|
|
- cargo.assignToRoute(newItinerary);
|
|
|
159
|
+ Map<String, Object> map = jdbcTemplate.queryForMap(
|
|
|
160
|
+ "select * from Cargo where tracking_id = ?", trackingId.idString());
|
|
148
|
161
|
|
|
149
|
|
- cargoRepository.store(cargo);
|
|
150
|
|
- flush();
|
|
|
162
|
+ assertEquals("AAA", map.get("TRACKING_ID"));
|
|
151
|
163
|
|
|
152
|
|
- assertEquals(1, sjt.queryForInt("select count(*) from Leg where cargo_id = ?", cargoId));
|
|
153
|
|
- }
|
|
|
164
|
+ Long originId = getLongId(origin);
|
|
|
165
|
+ assertEquals(originId, map.get("SPEC_ORIGIN_ID"));
|
|
154
|
166
|
|
|
|
167
|
+ Long destinationId = getLongId(destination);
|
|
|
168
|
+ assertEquals(destinationId, map.get("SPEC_DESTINATION_ID"));
|
|
|
169
|
+
|
|
|
170
|
+ sessionFactory.getCurrentSession().clear();
|
|
|
171
|
+
|
|
|
172
|
+ final Cargo loadedCargo = cargoRepository.find(trackingId);
|
|
|
173
|
+ assertEquals(1, loadedCargo.itinerary().legs().size());
|
|
|
174
|
+ }
|
|
|
175
|
+
|
|
|
176
|
+ @Test
|
|
|
177
|
+ public void testReplaceItinerary() {
|
|
|
178
|
+ Cargo cargo = cargoRepository.find(new TrackingId("FGH"));
|
|
|
179
|
+ Long cargoId = getLongId(cargo);
|
|
|
180
|
+ assertEquals(3, jdbcTemplate.queryForInt("select count(*) from Leg where cargo_id = ?", cargoId));
|
|
155
|
181
|
|
|
156
|
|
- public void testFindAll() {
|
|
157
|
|
- List<Cargo> all = cargoRepository.findAll();
|
|
158
|
|
- assertNotNull(all);
|
|
159
|
|
- assertEquals(6, all.size());
|
|
160
|
|
- }
|
|
|
182
|
+ Location legFrom = locationRepository.find(new UnLocode("FIHEL"));
|
|
|
183
|
+ Location legTo = locationRepository.find(new UnLocode("DEHAM"));
|
|
|
184
|
+ Itinerary newItinerary = new Itinerary(Arrays.asList(new Leg(CM004, legFrom, legTo, new Date(), new Date())));
|
|
161
|
185
|
|
|
162
|
|
- public void testNextTrackingId() {
|
|
163
|
|
- TrackingId trackingId = cargoRepository.nextTrackingId();
|
|
164
|
|
- assertNotNull(trackingId);
|
|
|
186
|
+ cargo.assignToRoute(newItinerary);
|
|
165
|
187
|
|
|
166
|
|
- TrackingId trackingId2 = cargoRepository.nextTrackingId();
|
|
167
|
|
- assertNotNull(trackingId2);
|
|
168
|
|
- assertFalse(trackingId.equals(trackingId2));
|
|
169
|
|
- }
|
|
|
188
|
+ cargoRepository.store(cargo);
|
|
|
189
|
+ flush();
|
|
170
|
190
|
|
|
171
|
|
-}*/
|
|
|
191
|
+ assertEquals(1, jdbcTemplate.queryForInt("select count(*) from Leg where cargo_id = ?", cargoId));
|
|
|
192
|
+ }
|
|
|
193
|
+
|
|
|
194
|
+ @Test
|
|
|
195
|
+ public void testFindAll() {
|
|
|
196
|
+ List<Cargo> all = cargoRepository.findAll();
|
|
|
197
|
+ assertNotNull(all);
|
|
|
198
|
+ assertEquals(6, all.size());
|
|
|
199
|
+ }
|
|
|
200
|
+
|
|
|
201
|
+ @Test
|
|
|
202
|
+ public void testNextTrackingId() {
|
|
|
203
|
+ TrackingId trackingId = cargoRepository.nextTrackingId();
|
|
|
204
|
+ assertNotNull(trackingId);
|
|
|
205
|
+
|
|
|
206
|
+ TrackingId trackingId2 = cargoRepository.nextTrackingId();
|
|
|
207
|
+ assertNotNull(trackingId2);
|
|
|
208
|
+ assertFalse(trackingId.equals(trackingId2));
|
|
|
209
|
+ }
|
|
|
210
|
+
|
|
|
211
|
+
|
|
|
212
|
+ private void flush() {
|
|
|
213
|
+ sessionFactory.getCurrentSession().flush();
|
|
|
214
|
+ }
|
|
|
215
|
+
|
|
|
216
|
+ private Long getLongId(Object o) {
|
|
|
217
|
+ final Session session = sessionFactory.getCurrentSession();
|
|
|
218
|
+ if (session.contains(o)) {
|
|
|
219
|
+ return (Long) session.getIdentifier(o);
|
|
|
220
|
+ } else {
|
|
|
221
|
+ try {
|
|
|
222
|
+ Field id = o.getClass().getDeclaredField("id");
|
|
|
223
|
+ id.setAccessible(true);
|
|
|
224
|
+ return (Long) id.get(o);
|
|
|
225
|
+ } catch (Exception e) {
|
|
|
226
|
+ throw new RuntimeException();
|
|
|
227
|
+ }
|
|
|
228
|
+ }
|
|
|
229
|
+ }
|
|
|
230
|
+
|
|
|
231
|
+}
|