|
|
@@ -1,11 +1,13 @@
|
|
1
|
1
|
package se.citerus.dddsample.web;
|
|
2
|
2
|
|
|
3
|
3
|
import junit.framework.TestCase;
|
|
|
4
|
+
|
|
4
|
5
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
5
|
6
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
6
|
7
|
import org.springframework.mock.web.MockHttpSession;
|
|
7
|
8
|
import org.springframework.mock.web.MockServletContext;
|
|
8
|
9
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
10
|
+
|
|
9
|
11
|
import se.citerus.dddsample.domain.Cargo;
|
|
10
|
12
|
import se.citerus.dddsample.domain.Location;
|
|
11
|
13
|
import se.citerus.dddsample.domain.TrackingId;
|
|
|
@@ -13,7 +15,6 @@ import se.citerus.dddsample.service.CargoService;
|
|
13
|
15
|
import se.citerus.dddsample.web.command.TrackCommand;
|
|
14
|
16
|
|
|
15
|
17
|
public class CargoTrackingControllerTest extends TestCase {
|
|
16
|
|
-
|
|
17
|
18
|
CargoTrackingController controller;
|
|
18
|
19
|
MockHttpServletRequest request;
|
|
19
|
20
|
MockHttpServletResponse response;
|
|
|
@@ -26,11 +27,17 @@ public class CargoTrackingControllerTest extends TestCase {
|
|
26
|
27
|
response = new MockHttpServletResponse();
|
|
27
|
28
|
session = new MockHttpSession(servletContext);
|
|
28
|
29
|
request.setSession(session);
|
|
|
30
|
+
|
|
29
|
31
|
controller = new CargoTrackingController();
|
|
30
|
32
|
controller.setFormView("test-form");
|
|
31
|
33
|
controller.setSuccessView("test-success");
|
|
|
34
|
+ controller.setUnknownCargoView("test-unkownCargo");
|
|
32
|
35
|
controller.setCommandName("test-command-name");
|
|
33
|
|
- controller.setCargoService(new CargoService() {
|
|
|
36
|
+ controller.setCargoService(getCargoServiceMock());
|
|
|
37
|
+ }
|
|
|
38
|
+
|
|
|
39
|
+ private CargoService getCargoServiceMock() {
|
|
|
40
|
+ return new CargoService() {
|
|
34
|
41
|
public Cargo find(String trackingId) {
|
|
35
|
42
|
Cargo cargo = new Cargo(
|
|
36
|
43
|
new TrackingId(trackingId),
|
|
|
@@ -38,7 +45,15 @@ public class CargoTrackingControllerTest extends TestCase {
|
|
38
|
45
|
new Location("BBB"));
|
|
39
|
46
|
return cargo;
|
|
40
|
47
|
}
|
|
41
|
|
- });
|
|
|
48
|
+ };
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ private CargoService getCargoServiceNullMock() {
|
|
|
52
|
+ return new CargoService() {
|
|
|
53
|
+ public Cargo find(String trackingId) {
|
|
|
54
|
+ return null;
|
|
|
55
|
+ }
|
|
|
56
|
+ };
|
|
42
|
57
|
}
|
|
43
|
58
|
|
|
44
|
59
|
public void testHandleGet() throws Exception {
|
|
|
@@ -57,9 +72,35 @@ public class CargoTrackingControllerTest extends TestCase {
|
|
57
|
72
|
ModelAndView mav = controller.handleRequest(request, response);
|
|
58
|
73
|
|
|
59
|
74
|
assertEquals("test-success", mav.getViewName());
|
|
60
|
|
- assertEquals(1, mav.getModel().size());
|
|
|
75
|
+ assertEquals(3, mav.getModel().size());
|
|
61
|
76
|
Location location = (Location) mav.getModel().get("location");
|
|
62
|
77
|
assertEquals("AAA", location.unlocode());
|
|
63
|
78
|
}
|
|
64
|
79
|
|
|
|
80
|
+ public void testUnknownCargo() throws Exception {
|
|
|
81
|
+ controller.setCargoService(getCargoServiceNullMock());
|
|
|
82
|
+ controller.setCommandClass(TrackCommandMock.class);
|
|
|
83
|
+ request.setMethod("POST");
|
|
|
84
|
+
|
|
|
85
|
+ ModelAndView mav = controller.handleRequest(request, response);
|
|
|
86
|
+
|
|
|
87
|
+ assertEquals("test-unkownCargo", mav.getViewName());
|
|
|
88
|
+ assertEquals(1, mav.getModel().size());
|
|
|
89
|
+ String trackId = (String) mav.getModel().get("trackingId");
|
|
|
90
|
+ assertEquals("MOCK", trackId);
|
|
|
91
|
+ }
|
|
|
92
|
+
|
|
|
93
|
+
|
|
|
94
|
+
|
|
|
95
|
+
|
|
|
96
|
+ /**
|
|
|
97
|
+ * Mock track command.
|
|
|
98
|
+ *
|
|
|
99
|
+ * Sets a default track id when constructed.
|
|
|
100
|
+ */
|
|
|
101
|
+ private static class TrackCommandMock extends TrackCommand {
|
|
|
102
|
+ public TrackCommandMock() {
|
|
|
103
|
+ setTrackingId("MOCK");
|
|
|
104
|
+ }
|
|
|
105
|
+ }
|
|
65
|
106
|
}
|