View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  package org.kuali.mobility.maps.controller;
16  
17  import static org.junit.Assert.*;
18  
19  import java.util.ArrayList;
20  import java.util.HashSet;
21  import java.util.Properties;
22  import java.util.Set;
23  
24  import org.junit.AfterClass;
25  import org.junit.Before;
26  import org.junit.BeforeClass;
27  import org.junit.Test;
28  import org.junit.runner.RunWith;
29  import org.kuali.mobility.configparams.service.ConfigParamService;
30  import org.kuali.mobility.maps.controllers.MapsController;
31  import org.kuali.mobility.maps.entity.Location;
32  import org.kuali.mobility.maps.entity.MapsFormSearchResultContainer;
33  import org.kuali.mobility.maps.entity.MapsGroup;
34  import org.kuali.mobility.maps.service.MapsService;
35  import org.kuali.mobility.security.user.api.User;
36  import org.kuali.mobility.security.user.entity.UserImpl;
37  import org.kuali.mobility.shared.Constants;
38  import org.mockito.Mock;
39  import static org.mockito.Matchers.any;
40  import static org.mockito.Mockito.when;
41  import static org.mockito.Mockito.doReturn;
42  import org.springframework.context.ApplicationContext;
43  import org.springframework.http.HttpStatus;
44  import org.springframework.http.ResponseEntity;
45  import org.springframework.mock.web.MockHttpServletRequest;
46  import org.springframework.mock.web.MockHttpServletResponse;
47  import org.springframework.mock.web.MockHttpSession;
48  import org.springframework.mock.web.MockServletContext;
49  import org.springframework.ui.ExtendedModelMap;
50  import org.springframework.ui.Model;
51  
52  @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
53  public class MapsControllerTest {
54  	
55  	private static final String USER = "fauxUser";
56  	private static final String REDIRECT_HOME = "redirect:/campus?toolName=maps";
57  	private static final String[] VIEW_CAMPUS = {"ALL","BL","CO","TEST"};
58  	
59  	private static MockServletContext servletContext;
60  	private MockHttpServletRequest request;
61  	private MockHttpServletResponse response;
62  	private Model uiModel;
63  	private MapsController mapsController;
64  	@Mock
65  	private static ApplicationContext applicationContext;
66  	@Mock
67  	private MapsService mapsService;
68  	@Mock
69  	private Properties kmeProperties;
70  	@Mock
71  	private ConfigParamService configParamService;
72  	
73  	@BeforeClass
74  	public static void setUpClass() throws Exception {
75  		setServletContext(new MockServletContext());
76  	}
77  	
78  	@Before
79  	public void preTest() {
80  		this.setMapsController(new MapsController());
81  		this.getMapsController().setApplicationContext(applicationContext);
82  		this.getMapsController().setMapsService(this.getMapsService());
83  		this.getMapsController().setKmeProperties(kmeProperties);
84  		this.getMapsController().setConfigParamService(getConfigParamService());
85  		this.setRequest(new MockHttpServletRequest(servletContext));
86      	this.setResponse(new MockHttpServletResponse());
87      	this.setUiModel(new ExtendedModelMap());
88      	this.getRequest().setSession(new MockHttpSession());
89  	}
90  	
91  	@AfterClass
92      public static void tearDownClass() throws Exception {
93      }
94  	
95  	@Test
96  	public void testGetHome() {
97  		User user = new UserImpl();
98  		user.setLoginName(USER);
99  		this.getRequest().getSession().setAttribute(Constants.KME_USER_KEY,user);
100 		String viewName = getMapsController().getHome(getUiModel(), getRequest());
101 		assertTrue("did not return " + REDIRECT_HOME + " but instead " + viewName, REDIRECT_HOME.equalsIgnoreCase(viewName));
102 		
103 		user.setViewCampus(VIEW_CAMPUS[1]);
104 		Properties properties = new Properties();
105 		properties.setProperty("maps.center.lat", "1");
106 		properties.setProperty("maps.center.lon", "-1");
107 		properties.setProperty("maps.useCampusBounds", "true");
108 		when(applicationContext.getBean(any(String.class))).thenReturn(null).thenReturn(properties);
109 		when(kmeProperties.getProperty(any(String.class), any(String.class))).thenReturn("mapquest").thenReturn("google");
110 		viewName = getMapsController().getHome(getUiModel(), getRequest());
111 		assertTrue("uiModel initialLatitude was not set to 0.0!", getUiModel().asMap().get("initialLatitude").equals("0.0"));
112     	assertTrue("uiModel initialLongitude was not set to 0.0!", getUiModel().asMap().get("initialLongitude").equals("0.0"));
113     	assertTrue("view name did not return correct string", "maps/mapquest".equalsIgnoreCase(viewName));
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", "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 }