View Javadoc

1   /**
2    * Copyright 2011-2013 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.push.controller;
16  
17  import static org.junit.Assert.assertTrue;
18  import static org.mockito.Matchers.any;
19  import static org.mockito.Mockito.when;
20  
21  import java.sql.Timestamp;
22  import java.util.ArrayList;
23  import java.util.Calendar;
24  import java.util.List;
25  import java.util.TimeZone;
26  
27  import net.sf.json.JSONSerializer;
28  
29  import org.apache.log4j.Logger;
30  import org.junit.Before;
31  import org.junit.BeforeClass;
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  import org.kuali.mobility.push.controllers.PreferencesController;
35  import org.kuali.mobility.push.entity.Preference;
36  import org.kuali.mobility.push.entity.Sender;
37  import org.kuali.mobility.push.service.PreferenceService;
38  import org.kuali.mobility.push.service.SenderService;
39  import org.kuali.mobility.security.authn.util.AuthenticationConstants;
40  import org.kuali.mobility.security.user.api.User;
41  import org.kuali.mobility.security.user.api.UserDao;
42  import org.kuali.mobility.security.user.entity.UserImpl;
43  import org.mockito.Mock;
44  import org.springframework.http.HttpStatus;
45  import org.springframework.http.ResponseEntity;
46  import org.springframework.mock.web.MockHttpServletRequest;
47  import org.springframework.mock.web.MockHttpServletResponse;
48  import org.springframework.mock.web.MockHttpSession;
49  import org.springframework.mock.web.MockServletContext;
50  import org.springframework.ui.ExtendedModelMap;
51  import org.springframework.ui.Model;
52  
53  /**
54   * @author Kuali Mobility Team (mobility.collab@kuali.org)
55   */
56  /**
57   * @author mtwagner
58   *
59   */
60  @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
61  public class PreferencesControllerTest {
62  
63  	/** A reference to a logger */
64  	private static final Logger LOG = Logger.getLogger(PreferencesControllerTest.class);
65  	
66  	private static MockServletContext servletContext;
67  	private PreferencesController controller;
68  	@Mock
69      private PreferenceService prefService;
70  
71  	@Mock
72      private SenderService senderService;
73  	
74  	private MockHttpServletRequest request;
75  	private MockHttpServletResponse response;
76  	private Model uiModel;
77  
78  	@Mock
79  	private Preference preference;
80  	@Mock
81  	private User user;
82  	@Mock
83  	private UserDao userDao;
84  	
85  	
86  	@BeforeClass
87  	public static void setUpClass() throws Exception {
88  		servletContext = new MockServletContext();
89  	}
90  	
91  	@Before
92  	public void preTest() {
93  		this.setController(new PreferencesController());
94  		this.getController().setPreferenceService(prefService);
95  		this.getController().setSenderService(senderService);
96  		this.setRequest(new MockHttpServletRequest(servletContext));
97  		this.setResponse(new MockHttpServletResponse());
98  		this.setUiModel(new ExtendedModelMap());
99  		this.getRequest().setSession(new MockHttpSession(servletContext));
100 		this.getRequest().getSession().setAttribute(AuthenticationConstants.KME_USER_KEY, getUser());
101 	}
102 
103 	@Test
104 	public void testPing() {
105 		ResponseEntity <String> responseEntity = getController().ping();
106 		ResponseEntity<String> expectedEntity = new ResponseEntity<String>(HttpStatus.OK);
107 		assertTrue("Expected response of OK was not found.",(expectedEntity.getStatusCode()).equals(responseEntity.getStatusCode()));
108 	}
109 	
110 	@Test
111 	public void testLogin() {
112 		String viewName = getController().login(getRequest(), getUiModel(), "key");
113 		List<Sender> senders = new ArrayList<Sender>();
114 		when(getSenderService().findAllSenders()).thenReturn(senders);
115 		assertTrue("View name does not match expected value.","redirect:/pushprefs".equals(viewName));
116 	}
117 	
118 	@Test
119 	public void testGetPreferencesWithNullUsername(){
120 		String expected = "{\"preferences\":[]}";
121 		User user = new UserImpl();
122 		List<Preference> preferences = new ArrayList<Preference>();
123 		when(getPrefService().findPreferencesByUsername("")).thenReturn(preferences);
124 		
125 		String output = getController().getPreferences(getUiModel(), getRequest(), "");		
126 		LOG.info("expected: [" + expected + "]");
127 		LOG.info("output:   [" + output + "]");
128 		assertTrue("Output does not match expected value.", expected.equals(output));
129 	}
130 
131 	@Test
132 	public void testGetPreferencesWithUsername(){
133 		Timestamp ts = new Timestamp(123456789L);
134 		String timestampJson = new JSONSerializer().toJSON(ts).toString();
135 		String expected = "{\"preferences\":[{\"enabled\":false,\"id\":1,\"postedTimestamp\":"+timestampJson+",\"pushSenderID\":1,\"senderBlocked\":true,\"username\":\"userA\",\"versionNumber\":0}]}";
136 		Preference preference = new Preference();
137 		preference.setId(1L);
138 		preference.setPushSenderID(1L);
139 		preference.setUsername("userA");
140 		
141 		preference.setPostedTimestamp(ts);
142 		List<Preference> preferences = new ArrayList<Preference>();
143 		preferences.add(preference);
144 		
145 		when(getPrefService().findPreferencesByUsername("userA")).thenReturn(preferences);		
146 		String output = getController().getPreferences(getUiModel(), getRequest(), "userA");
147 		LOG.info("expected: [" + expected + "]");
148 		LOG.info("output:   [" + output + "]");
149 
150 		assertTrue("Output does not match expected value.", expected.equals(output));
151 	}
152 	
153 	@Test
154 	public void testRegisterNewPreferences() {
155 		String data = "{\"enabled\":true,\"username\":\"bob\",\"senderId\":1}";
156 		when(getPrefService().findPreference(any(String.class), any(Sender.class))).thenReturn(null);
157 		ResponseEntity<String> responseEntity = getController().register(data);
158 		ResponseEntity<String> expectedEntity = new ResponseEntity<String>(HttpStatus.OK);
159 		assertTrue("Expected response of OK was not found.", (expectedEntity.getStatusCode()).equals(responseEntity.getStatusCode()));
160 	}
161 
162 	@Test
163 	public void testRegisterPreferencesWithInvalidData() {
164 		String data = "{\"enabled:true,\"username\":\"bob\",\"senderId\":1}";
165 		when(getPrefService().findPreference(any(String.class), any(Sender.class))).thenReturn(null);
166 		ResponseEntity<String> responseEntity = getController().register(data);
167 		ResponseEntity<String> expectedEntity = new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
168 		assertTrue("Expected response of BAD_REQUEST was not found.", (expectedEntity.getStatusCode()).equals(responseEntity.getStatusCode()));
169 	}
170 
171 	@Test
172 	public void testRegisterPreferencesWithNullData() {
173 		when(getPrefService().findPreference(any(String.class), any(Sender.class))).thenReturn(null);
174 		ResponseEntity<String> responseEntity = getController().register(null);
175 		ResponseEntity<String> expectedEntity = new ResponseEntity<String>(HttpStatus.NO_CONTENT);
176 		assertTrue("Expected response of NO_CONTENT was not found.", (expectedEntity.getStatusCode()).equals(responseEntity.getStatusCode()));
177 	}
178 	
179 	/**
180 	 * @return the servletContext
181 	 */
182 	public static MockServletContext getServletContext() {
183 		return servletContext;
184 	}
185 	/**
186 	 * @param servletContext the servletContext to set
187 	 */
188 	public static void setServletContext(MockServletContext servletContext) {
189 		PreferencesControllerTest.servletContext = servletContext;
190 	}
191 	/**
192 	 * @return the controller
193 	 */
194 	public PreferencesController getController() {
195 		return controller;
196 	}
197 	/**
198 	 * @param controller the controller to set
199 	 */
200 	public void setController(PreferencesController controller) {
201 		this.controller = controller;
202 	}
203 
204 	
205 	
206 	/**
207 	 * @return the prefService
208 	 */
209 	public PreferenceService getPrefService() {
210 		return prefService;
211 	}
212 
213 	/**
214 	 * @param prefService the prefService to set
215 	 */
216 	public void setPrefService(PreferenceService prefService) {
217 		this.prefService = prefService;
218 	}
219 
220 	/**
221 	 * @return the senderService
222 	 */
223 	public SenderService getSenderService() {
224 		return senderService;
225 	}
226 
227 	/**
228 	 * @param senderService the senderService to set
229 	 */
230 	public void setSenderService(SenderService senderService) {
231 		this.senderService = senderService;
232 	}
233 
234 	/**
235 	 * @return the request
236 	 */
237 	public MockHttpServletRequest getRequest() {
238 		return request;
239 	}
240 	/**
241 	 * @param request the request to set
242 	 */
243 	public void setRequest(MockHttpServletRequest request) {
244 		this.request = request;
245 	}
246 	/**
247 	 * @return the response
248 	 */
249 	public MockHttpServletResponse getResponse() {
250 		return response;
251 	}
252 	/**
253 	 * @param response the response to set
254 	 */
255 	public void setResponse(MockHttpServletResponse response) {
256 		this.response = response;
257 	}
258 	/**
259 	 * @return the uiModel
260 	 */
261 	public Model getUiModel() {
262 		return uiModel;
263 	}
264 	/**
265 	 * @param uiModel the uiModel to set
266 	 */
267 	public void setUiModel(Model uiModel) {
268 		this.uiModel = uiModel;
269 	}
270 	/**
271 	 * @return the preference
272 	 */
273 	public Preference getPreference() {
274 		return preference;
275 	}
276 
277 	/**
278 	 * @param preference the preference to set
279 	 */
280 	public void setPreference(Preference preference) {
281 		this.preference = preference;
282 	}
283 
284 	/**
285 	 * @return the user
286 	 */
287 	public User getUser() {
288 		return user;
289 	}
290 	/**
291 	 * @param user the user to set
292 	 */
293 	public void setUser(User user) {
294 		this.user = user;
295 	}
296 	/**
297 	 * @return the userDao
298 	 */
299 	public UserDao getUserDao() {
300 		return userDao;
301 	}
302 	/**
303 	 * @param userDao the userDao to set
304 	 */
305 	public void setUserDao(UserDao userDao) {
306 		this.userDao = userDao;
307 	}
308 	
309 	
310 }