1 | package gov.va.med.edp.web.controller;
|
---|
2 |
|
---|
3 | import org.springframework.web.servlet.ModelAndView;
|
---|
4 | import org.springframework.core.io.ResourceLoader;
|
---|
5 | import org.springframework.core.io.AbstractResource;
|
---|
6 | import org.springframework.dao.DataAccessResourceFailureException;
|
---|
7 | import org.springframework.dao.DataAccessException;
|
---|
8 | import org.easymock.MockControl;
|
---|
9 |
|
---|
10 | import gov.va.med.edp.dao.ServerPackageVersionDao;
|
---|
11 | import gov.va.med.edp.dao.SiteCodeLookUpDao;
|
---|
12 | import gov.va.med.edp.dao.TrackingDao;
|
---|
13 |
|
---|
14 | import java.io.InputStream;
|
---|
15 | import java.io.IOException;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * TODO: document ClientVersionSynchronizationControllerTest
|
---|
19 | */
|
---|
20 | public class ClientVersionSynchronizationControllerTest extends AbstractControllerTest {
|
---|
21 | private MockControl mockLoaderControl;
|
---|
22 | private ClientVersionSynchronizationController c;
|
---|
23 | private ResourceLoader mockLoader;
|
---|
24 |
|
---|
25 | protected void setUp() throws Exception {
|
---|
26 | super.setUp();
|
---|
27 |
|
---|
28 | mockLoaderControl = MockControl.createControl(ResourceLoader.class);
|
---|
29 | mockLoader = (ResourceLoader) mockLoaderControl.getMock();
|
---|
30 | webAppContext.setResourceLoader(mockLoader);
|
---|
31 |
|
---|
32 | MockControl siteDaoControl = MockControl.createControl(SiteCodeLookUpDao.class);
|
---|
33 | SiteCodeLookUpDao siteCodeDao = (SiteCodeLookUpDao) siteDaoControl.getMock();
|
---|
34 |
|
---|
35 | MockControl trackingDaoControl = MockControl.createControl(TrackingDao.class);
|
---|
36 | TrackingDao trackingDao = (TrackingDao) trackingDaoControl.getMock();
|
---|
37 |
|
---|
38 |
|
---|
39 | c = new ClientVersionSynchronizationController();
|
---|
40 | c.setApplicationContext(webAppContext);
|
---|
41 | c.setClientArtifactId("foo-bar");
|
---|
42 | c.setViewName("fooBarView");
|
---|
43 | c.setIncompatibilityViewName("fooBarIncompatibleView");
|
---|
44 | c.setTrackingDao(trackingDao);
|
---|
45 | c.setSiteCodeDao(siteCodeDao);
|
---|
46 | c.afterPropertiesSet();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void testDefaultIncompatibilityViewName() {
|
---|
50 | c = new ClientVersionSynchronizationController();
|
---|
51 | assertEquals(ClientVersionSynchronizationController.DEFAULT_INCOMPATIBILITY_VIEW, c.getIncompatibilityViewName());
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void testNoServerPackageVersionOrServerErrorSessionAttribute() throws Exception {
|
---|
55 | try {
|
---|
56 | c.handleRequest(request, response);
|
---|
57 | fail("expected illegal state exception");
|
---|
58 | } catch (IllegalStateException e) {
|
---|
59 | assertTrue(true);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void testNoServerPackageVersionSessionButServerErrorAttribute() throws Exception {
|
---|
64 | DataAccessException ex = new DataAccessResourceFailureException("unable to fetch the thing and the thing and the hey lady!");
|
---|
65 |
|
---|
66 | session.setAttribute(SessionConstants.SERVER_ERROR_KEY, ex);
|
---|
67 |
|
---|
68 | try {
|
---|
69 | c.handleRequest(request, response);
|
---|
70 | fail("expected data access exception");
|
---|
71 | } catch (DataAccessException e) {
|
---|
72 | assertSame(ex, e);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void testViewReturnedWhenServerVersionMatchesCompatibleVersion() throws Exception {
|
---|
77 | session.setAttribute(SessionConstants.SERVER_PACKAGE_VERSION_KEY, "1.0-T15-SNAPSHOT");
|
---|
78 |
|
---|
79 | mockLoaderControl.expectAndReturn(mockLoader.getResource("foo-bar-1.0-T15-SNAPSHOT.swf"), new MockResource(true));
|
---|
80 | mockLoaderControl.replay();
|
---|
81 |
|
---|
82 | ModelAndView mav = c.handleRequest(request, response);
|
---|
83 | assertEquals("fooBarView", mav.getViewName());
|
---|
84 | assertEquals("1.0-T15-SNAPSHOT", mav.getModel().get("serverPackageVersion"));
|
---|
85 | assertEquals("foo-bar", mav.getModel().get("clientArtifactId"));
|
---|
86 | assertEquals("foo-bar-1.0-T15-SNAPSHOT", mav.getModel().get("clientFinalName"));
|
---|
87 |
|
---|
88 | mockLoaderControl.verify();
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void testViewReturnedWhenServerVersionIsIncompatible() throws Exception {
|
---|
92 | session.setAttribute(SessionConstants.SERVER_PACKAGE_VERSION_KEY, "1.0-T15-SNAPSHOT");
|
---|
93 |
|
---|
94 | mockLoaderControl.expectAndReturn(mockLoader.getResource("foo-bar-1.0-T15-SNAPSHOT.swf"), new MockResource(false));
|
---|
95 | mockLoaderControl.replay();
|
---|
96 |
|
---|
97 | ModelAndView mav = c.handleRequest(request, response);
|
---|
98 | assertEquals("fooBarIncompatibleView", mav.getViewName());
|
---|
99 | assertEquals("1.0-T15-SNAPSHOT", mav.getModel().get("serverPackageVersion"));
|
---|
100 | assertEquals("foo-bar", mav.getModel().get("clientArtifactId"));
|
---|
101 | assertEquals("foo-bar-1.0-T15-SNAPSHOT", mav.getModel().get("clientFinalName"));
|
---|
102 |
|
---|
103 | mockLoaderControl.verify();
|
---|
104 | }
|
---|
105 |
|
---|
106 | static class MockResource extends AbstractResource {
|
---|
107 | private boolean exists;
|
---|
108 |
|
---|
109 | public MockResource(boolean exists) {
|
---|
110 | this.exists = exists;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public boolean exists() {
|
---|
114 | return exists;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public String getDescription() {
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public InputStream getInputStream() throws IOException {
|
---|
122 | return null;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|