1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.maps.controller;
16
17 import org.junit.AfterClass;
18 import org.junit.Before;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.kuali.mobility.configparams.service.ConfigParamService;
23 import org.kuali.mobility.maps.controllers.MapsController;
24 import org.kuali.mobility.maps.entity.Location;
25 import org.kuali.mobility.maps.entity.MapsFormSearchResultContainer;
26 import org.kuali.mobility.maps.entity.MapsGroup;
27 import org.kuali.mobility.maps.service.MapsService;
28 import org.kuali.mobility.security.user.api.User;
29 import org.kuali.mobility.security.user.entity.UserImpl;
30 import org.kuali.mobility.shared.Constants;
31 import org.mockito.Mock;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.mock.web.MockHttpServletRequest;
36 import org.springframework.mock.web.MockHttpServletResponse;
37 import org.springframework.mock.web.MockHttpSession;
38 import org.springframework.mock.web.MockServletContext;
39 import org.springframework.ui.ExtendedModelMap;
40 import org.springframework.ui.Model;
41
42 import java.util.HashSet;
43 import java.util.Properties;
44 import java.util.Set;
45
46 import static org.junit.Assert.assertTrue;
47 import static org.mockito.Matchers.any;
48 import static org.mockito.Mockito.when;
49
50 @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
51 public class MapsControllerTest {
52
53 private static final String USER = "fauxUser";
54 private static final String REDIRECT_HOME = "redirect:/campus?toolName=maps";
55 private static final String[] VIEW_CAMPUS = {"ALL","BL","CO","TEST"};
56
57 private static MockServletContext servletContext;
58 private MockHttpServletRequest request;
59 private MockHttpServletResponse response;
60 private Model uiModel;
61 private MapsController mapsController;
62 @Mock
63 private static ApplicationContext applicationContext;
64 @Mock
65 private MapsService mapsService;
66 @Mock
67 private Properties kmeProperties;
68 @Mock
69 private ConfigParamService configParamService;
70
71 @BeforeClass
72 public static void setUpClass() throws Exception {
73 setServletContext(new MockServletContext());
74 }
75
76 @Before
77 public void preTest() {
78 this.setMapsController(new MapsController());
79 this.getMapsController().setApplicationContext(applicationContext);
80 this.getMapsController().setMapsService(this.getMapsService());
81 this.getMapsController().setKmeProperties(kmeProperties);
82 this.getMapsController().setConfigParamService(getConfigParamService());
83 this.setRequest(new MockHttpServletRequest(servletContext));
84 this.setResponse(new MockHttpServletResponse());
85 this.setUiModel(new ExtendedModelMap());
86 this.getRequest().setSession(new MockHttpSession());
87 }
88
89 @AfterClass
90 public static void tearDownClass() throws Exception {
91 }
92
93 @Test
94 public void testGetHome() {
95 User user = new UserImpl();
96 user.setLoginName(USER);
97 this.getRequest().getSession().setAttribute(Constants.KME_USER_KEY,user);
98 String viewName = getMapsController().getHome(getUiModel(), getRequest());
99 assertTrue("did not return " + REDIRECT_HOME + " but instead " + viewName, REDIRECT_HOME.equalsIgnoreCase(viewName));
100
101 user.setViewCampus(VIEW_CAMPUS[1]);
102 Properties properties = new Properties();
103 properties.setProperty("maps.center.lat", "1");
104 properties.setProperty("maps.center.lon", "-1");
105 properties.setProperty("maps.useCampusBounds", "true");
106 when(kmeProperties.getProperty("maps.api","google")).thenReturn("mapquest");
107 when(kmeProperties.getProperty("kme.uiVersion","classic")).thenReturn("classic");
108 when(applicationContext.getBean(any(String.class))).thenReturn(null).thenReturn(properties);
109 viewName = getMapsController().getHome(getUiModel(), getRequest());
110 assertTrue("uiModel initialLatitude was not set to 0.0!", getUiModel().asMap().get("initialLatitude").equals("0.0"));
111 assertTrue("uiModel initialLongitude was not set to 0.0!", getUiModel().asMap().get("initialLongitude").equals("0.0"));
112 assertTrue("view name did not return correct string, expected maps/mapquest and got "+viewName, "maps/mapquest".equalsIgnoreCase(viewName));
113 when(kmeProperties.getProperty("maps.api","google")).thenReturn("google");
114 viewName = getMapsController().getHome(getUiModel(), getRequest());
115 assertTrue("uiModel initialLatitude was not set to 1!", getUiModel().asMap().get("initialLatitude").equals("1"));
116 assertTrue("uiModel initialLongitude was not set to -1!", getUiModel().asMap().get("initialLongitude").equals("-1"));
117 assertTrue("view name did not return correct string, expected maps/home and got "+viewName, "maps/home".equalsIgnoreCase(viewName));
118 }
119
120 @Test
121 public void testGetBuildings() {
122 MapsGroup group = new MapsGroup();
123 group.setId("1");
124 when(mapsService.getMapsGroupById(any(String.class))).thenReturn(null).thenReturn(group);
125 Object viewObject = getMapsController().getBuildings("0");
126 assertTrue("returned class does not match", viewObject.getClass().equals(new ResponseEntity<String>(HttpStatus.NOT_FOUND).getClass()));
127 viewObject = getMapsController().getBuildings("1");
128 assertTrue("returned object does not match test object", viewObject.equals(group.toJson()));
129 }
130
131 @Test
132 public void testGet() {
133 Location location = new Location();
134 location.setId("1");
135 when(mapsService.getLocationById(any(String.class))).thenReturn(null).thenReturn(location);
136 Object viewObject = getMapsController().get("0");
137 assertTrue("returned class does not match", viewObject.getClass().equals(new ResponseEntity<String>(HttpStatus.NOT_FOUND).getClass()));
138 viewObject = getMapsController().get("1");
139 assertTrue("returned object does not match test object", viewObject.equals(location.toJson()));
140 }
141
142 @Test
143 public void testGet2() {
144 Location location = new Location();
145 location.setId("0");
146 when(mapsService.getLocationById(any(String.class))).thenReturn(null).thenReturn(location);
147 Object viewObject = getMapsController().get(getUiModel(), "0");
148 assertTrue("string maps/building was not returned", "maps/building".equalsIgnoreCase(viewObject.toString()));
149 viewObject = getMapsController().get(getUiModel(), "0");
150 assertTrue("location Id does not match test location Id", getUiModel().asMap().get("id").equals(location.getId()));
151 assertTrue("string maps/building was not returned", "maps/building".equalsIgnoreCase(viewObject.toString()));
152 }
153
154 @Test
155 public void testSearchBuildings() {
156 Location location1 = new Location();
157 location1.setLatitude(1.0);
158 location1.setLongitude(1.1);
159 location1.setName("testLocation");
160 location1.setId("testId");
161 Location location2 = new Location();
162 location2.setLatitude(0.0);
163 location2.setLongitude(0.0);
164 location2.setName("testLocation");
165 location2.setId("testId");
166 Set<Location> locationSet = new HashSet<Location>();
167 locationSet.add(location1);
168 locationSet.add(location2);
169 MapsGroup group = new MapsGroup();
170 group.setMapsLocations(locationSet);
171 when(mapsService.getMapsGroupById(any(String.class))).thenReturn(null).thenReturn(group);
172 String viewName = getMapsController().searchBuildings(getUiModel(), "searchString", "searchGroupId");
173 assertTrue("searchResults were found", 0 == ((MapsFormSearchResultContainer) getUiModel().asMap().get("container")).getResults().size());
174 viewName = getMapsController().searchBuildings(getUiModel(), "searchString", "searchGroupId");
175 assertTrue("did not return the correct string maps/search", "maps/search".equalsIgnoreCase(viewName));
176 assertTrue("Only one test location should be returned", 1 == ((MapsFormSearchResultContainer) getUiModel().asMap().get("container")).getResults().size());
177 }
178
179 public static MockServletContext getServletContext() {
180 return servletContext;
181 }
182
183 public static void setServletContext(MockServletContext servletContext) {
184 MapsControllerTest.servletContext = servletContext;
185 }
186
187 public MockHttpServletRequest getRequest() {
188 return request;
189 }
190
191 public void setRequest(MockHttpServletRequest request) {
192 this.request = request;
193 }
194
195 public MockHttpServletResponse getResponse() {
196 return response;
197 }
198
199 public void setResponse(MockHttpServletResponse response) {
200 this.response = response;
201 }
202
203 public Model getUiModel() {
204 return uiModel;
205 }
206
207 public void setUiModel(Model uiModel) {
208 this.uiModel = uiModel;
209 }
210
211 public MapsController getMapsController() {
212 return mapsController;
213 }
214
215 public void setMapsController(MapsController mapsController) {
216 this.mapsController = mapsController;
217 }
218
219 public static ApplicationContext getApplicationContext() {
220 return applicationContext;
221 }
222
223 public static void setApplicationContext(ApplicationContext applicationContext) {
224 MapsControllerTest.applicationContext = applicationContext;
225 }
226
227 public MapsService getMapsService() {
228 return mapsService;
229 }
230
231 public void setMapsService(MapsService mapsService) {
232 this.mapsService = mapsService;
233 }
234
235 public ConfigParamService getConfigParamService() {
236 return configParamService;
237 }
238
239 public void setConfigParamService(ConfigParamService configParamService) {
240 this.configParamService = configParamService;
241 }
242 }