|
|
@@ -1,96 +0,0 @@
|
|
1
|
|
-package se.citerus.dddsample.tracking.core.interfaces.tracking;
|
|
2
|
|
-
|
|
3
|
|
-import junit.framework.TestCase;
|
|
4
|
|
-import org.easymock.EasyMock;
|
|
5
|
|
-import org.springframework.context.support.StaticApplicationContext;
|
|
6
|
|
-import org.springframework.mock.web.MockHttpServletRequest;
|
|
7
|
|
-import org.springframework.mock.web.MockHttpServletResponse;
|
|
8
|
|
-import org.springframework.mock.web.MockHttpSession;
|
|
9
|
|
-import org.springframework.mock.web.MockServletContext;
|
|
10
|
|
-import org.springframework.validation.BindingResult;
|
|
11
|
|
-import org.springframework.validation.Errors;
|
|
12
|
|
-import org.springframework.validation.FieldError;
|
|
13
|
|
-import org.springframework.web.servlet.ModelAndView;
|
|
14
|
|
-import se.citerus.dddsample.tracking.core.domain.model.cargo.CargoRepository;
|
|
15
|
|
-import se.citerus.dddsample.tracking.core.domain.model.handling.HandlingEventRepository;
|
|
16
|
|
-import se.citerus.dddsample.tracking.core.infrastructure.persistence.inmemory.CargoRepositoryInMem;
|
|
17
|
|
-import se.citerus.dddsample.tracking.core.infrastructure.persistence.inmemory.HandlingEventRepositoryInMem;
|
|
18
|
|
-
|
|
19
|
|
-public class CargoTrackingControllerTest extends TestCase {
|
|
20
|
|
- CargoTrackingController controller;
|
|
21
|
|
- MockHttpServletRequest request;
|
|
22
|
|
- MockHttpServletResponse response;
|
|
23
|
|
- MockHttpSession session;
|
|
24
|
|
- MockServletContext servletContext;
|
|
25
|
|
- CargoRepositoryInMem cargoRepository;
|
|
26
|
|
- HandlingEventRepository handlingEventRepository;
|
|
27
|
|
-
|
|
28
|
|
- protected void setUp() throws Exception {
|
|
29
|
|
- servletContext = new MockServletContext("test");
|
|
30
|
|
- request = new MockHttpServletRequest(servletContext);
|
|
31
|
|
- response = new MockHttpServletResponse();
|
|
32
|
|
- session = new MockHttpSession(servletContext);
|
|
33
|
|
- request.setSession(session);
|
|
34
|
|
-
|
|
35
|
|
- controller = new CargoTrackingController();
|
|
36
|
|
- StaticApplicationContext applicationContext = new StaticApplicationContext();
|
|
37
|
|
- controller.setApplicationContext(applicationContext);
|
|
38
|
|
- controller.setFormView("test-form");
|
|
39
|
|
- controller.setSuccessView("test-success");
|
|
40
|
|
- controller.setCommandName("test-command-name");
|
|
41
|
|
- cargoRepository = new CargoRepositoryInMem();
|
|
42
|
|
- cargoRepository.init();
|
|
43
|
|
-
|
|
44
|
|
- handlingEventRepository = new HandlingEventRepositoryInMem();
|
|
45
|
|
- controller.setHandlingEventRepository(handlingEventRepository);
|
|
46
|
|
- }
|
|
47
|
|
-
|
|
48
|
|
- public void testHandleGet() throws Exception {
|
|
49
|
|
- controller.setCargoRepository(new CargoRepositoryInMem());
|
|
50
|
|
- request.setMethod("GET");
|
|
51
|
|
-
|
|
52
|
|
- ModelAndView mav = controller.handleRequest(request, response);
|
|
53
|
|
-
|
|
54
|
|
- assertEquals("test-form", mav.getViewName());
|
|
55
|
|
- assertEquals(2, mav.getModel().size());
|
|
56
|
|
- assertTrue(mav.getModel().get("test-command-name") instanceof TrackCommand);
|
|
57
|
|
- }
|
|
58
|
|
-
|
|
59
|
|
- public void testHandlePost() throws Exception {
|
|
60
|
|
- controller.setCargoRepository(cargoRepository);
|
|
61
|
|
- request.addParameter("trackingId", "ABC");
|
|
62
|
|
- request.setMethod("POST");
|
|
63
|
|
-
|
|
64
|
|
- ModelAndView mav = controller.handleRequest(request, response);
|
|
65
|
|
-
|
|
66
|
|
- assertEquals("test-form", mav.getViewName());
|
|
67
|
|
- // Errors, command are two standard map attributes, the third should be the cargo object
|
|
68
|
|
- assertEquals(3, mav.getModel().size());
|
|
69
|
|
- CargoTrackingViewAdapter cargo = (CargoTrackingViewAdapter) mav.getModel().get("cargo");
|
|
70
|
|
- assertEquals("ABC", cargo.getTrackingId());
|
|
71
|
|
- }
|
|
72
|
|
-
|
|
73
|
|
- public void testUnknownCargo() throws Exception {
|
|
74
|
|
- CargoRepository cargoRepository = EasyMock.createNiceMock(CargoRepository.class);
|
|
75
|
|
- EasyMock.replay(cargoRepository);
|
|
76
|
|
- controller.setCargoRepository(cargoRepository);
|
|
77
|
|
-
|
|
78
|
|
- request.setMethod("POST");
|
|
79
|
|
- request.setParameter("trackingId", "unknown-id");
|
|
80
|
|
-
|
|
81
|
|
- ModelAndView mav = controller.handleRequest(request, response);
|
|
82
|
|
-
|
|
83
|
|
- assertEquals("test-form", mav.getViewName());
|
|
84
|
|
- assertEquals(2, mav.getModel().size());
|
|
85
|
|
-
|
|
86
|
|
- TrackCommand command = (TrackCommand) mav.getModel().get(controller.getCommandName());
|
|
87
|
|
- assertEquals("unknown-id", command.getTrackingId());
|
|
88
|
|
-
|
|
89
|
|
- Errors errors = (Errors) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + controller.getCommandName());
|
|
90
|
|
- FieldError fe = errors.getFieldError("trackingId");
|
|
91
|
|
- assertEquals("cargo.unknown_id", fe.getCode());
|
|
92
|
|
- assertEquals(1, fe.getArguments().length);
|
|
93
|
|
- assertEquals(command.getTrackingId(), fe.getArguments()[0]);
|
|
94
|
|
- }
|
|
95
|
|
-
|
|
96
|
|
-}
|