1
2
3
4
5
6
7
8
9
10
11
12
13
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.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.kuali.mobility.push.controllers.PushController;
34 import org.kuali.mobility.push.entity.Device;
35 import org.kuali.mobility.push.entity.Push;
36 import org.kuali.mobility.push.service.DeviceService;
37 import org.kuali.mobility.push.service.PushService;
38 import org.kuali.mobility.push.service.SenderService;
39 import org.kuali.mobility.security.group.api.Group;
40 import org.kuali.mobility.security.group.api.GroupDao;
41 import org.kuali.mobility.security.group.entity.GroupImpl;
42 import org.kuali.mobility.security.user.api.User;
43 import org.kuali.mobility.shared.Constants;
44 import org.mockito.Mock;
45 import org.mortbay.log.Log;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.mock.web.MockHttpServletRequest;
49 import org.springframework.mock.web.MockHttpServletResponse;
50 import org.springframework.mock.web.MockHttpSession;
51 import org.springframework.mock.web.MockServletContext;
52 import org.springframework.ui.ExtendedModelMap;
53 import org.springframework.ui.Model;
54 import org.springframework.validation.BindingResult;
55 import org.springframework.validation.MapBindingResult;
56
57
58
59
60 @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
61 public class PushControllerTest {
62
63 private static final String EMPTY = "";
64 private static final String PUSH = "Push";
65 private static final String COUNT = "pushCount";
66 private static final String PAST_PUSHES = "pastPushes";
67 private static final String[] RECIPIENTS = {"all","usernameless","corwin","bleys"};
68 private static final String TITLE = "Test Title";
69 private static final String MESSAGE = "Test push message.";
70 private static final String URL = "/test/url";
71 private static final String[] SENDERS = {"72241k6tlukzrs7mtqs4","abcdefghijklmnopqrst"};
72
73 private static MockServletContext servletContext;
74 private PushController controller;
75 @Mock
76 private PushService pushService;
77 @Mock
78 private DeviceService deviceService;
79
80 @Mock
81 private GroupDao groupDao;
82
83 @Mock
84 private SenderService senderService;
85 private MockHttpServletRequest request;
86 private MockHttpServletResponse response;
87 private Model uiModel;
88 private Push push;
89
90
91 @BeforeClass
92 public static void setUpClass() throws Exception {
93 servletContext = new MockServletContext();
94 }
95
96 @Before
97 public void preTest() {
98 this.setController(new PushController());
99 this.getController().setPushService(this.getPushService());
100 this.getController().setDeviceService(this.getDeviceService());
101 this.getController().setSenderService(this.getSenderService());
102 this.setRequest(new MockHttpServletRequest(servletContext));
103 this.setResponse(new MockHttpServletResponse());
104 this.setUiModel(new ExtendedModelMap());
105 this.getRequest().setSession(new MockHttpSession(servletContext));
106
107 Push p = new Push();
108 p.setMessage(MESSAGE);
109 p.setTitle(TITLE);
110 p.setEmergency(true);
111 p.setSender(SENDERS[0]);
112 p.setPushId(new Long(87));
113 this.setPush(p);
114
115 }
116
117 @Test
118 public void testIndex() {
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 }
136
137 @Test
138 public void testSendToChosenDevice() {
139 when(getDeviceService().findDeviceById(any(Long.class))).thenReturn(new Device());
140
141
142
143
144 }
145
146 @Test
147 public void testGetPushDetailsWithNullHash() {
148 User user = (User)request.getSession().getAttribute(Constants.KME_USER_KEY);
149
150 Map<String, Group> groupMap = getGroupDao().getGroupMap();
151 Set<String> names = groupMap.keySet();
152 Iterator<String> it = names.iterator();
153 while(it.hasNext()){
154 Log.info(it.next() + "\n");
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168 String viewName = getController().getPushDetails(getUiModel(),getRequest(),null);
169 assertTrue("Failed to retrieve proper view.","push".equals(viewName));
170 }
171
172 @Test
173 public void testGetPushDetails() {
174 Long id = new Long(62376);
175 Push push = new Push();
176 push.setPushId(id);
177 List<Device> devices = new ArrayList<Device>();
178 devices.add(new Device());
179 devices.add(new Device());
180 devices.get(0).setDeviceId("A");
181 devices.get(1).setDeviceId("B");
182 when(getPushService().findPushById(id)).thenReturn(push);
183 when(getPushService().findDevicesForPush(push)).thenReturn(devices);
184
185
186
187
188
189
190
191
192
193 String viewName = getController().getPushDetails(getUiModel(),getRequest(),id);
194
195 assertTrue("Failed to retrieve proper view.","push/pushdetail".equals(viewName));
196 assertTrue("Failed to find push in Model.",null!=getUiModel().asMap().get("thispush"));
197 assertTrue("Failed to find device list in Model.",null!=getUiModel().asMap().get("thesedevices"));
198 assertTrue("Device list is the wrong size.", 2 == ((List<Device>) (getUiModel().asMap().get("thesedevices"))).size());
199 }
200
201 @Test
202 public void testGetUserDetails() {
203 when(getPushService().findPushById(getPush().getPushId())).thenReturn(getPush());
204 String jsonp = getController().getUserDetails(getUiModel(),getRequest(),getPush().getPushId());
205 String expectedJson = "pushJSON('{\"emergency\":true,\"message\":\"Test push message.\",\"postedTimestamp\":null,\"pushId\":87,\"recipients\":0,\"sender\":\"72241k6tlukzrs7mtqs4\",\"title\":\"Test Title\",\"url\":\"\",\"versionNumber\":0}');";
206 assertTrue("Failed to get expected jsonp string.",expectedJson.equals(jsonp));
207 }
208
209 @Test
210 public void testHistory() {
211 int count = 42;
212 List<Push> pushes = new ArrayList<Push>();
213 pushes.add(new Push());
214 pushes.add(new Push());
215 pushes.add(new Push());
216 when(getPushService().countPushes()).thenReturn(count);
217 when(getPushService().findAllPush()).thenReturn(pushes);
218 String viewName = getController().history(getRequest(),getUiModel(),null);
219 assertTrue("Failed to find proper view name: history.","push/history".equals(viewName));
220 assertTrue("Push count did not match expected value.",count==((Integer)(getUiModel().asMap().get(COUNT))).intValue());
221 assertTrue("Push not found in model",null != getUiModel().asMap().get(PAST_PUSHES));
222 assertTrue("Push list not expected size.",3==((List<Push>)(getUiModel().asMap().get(PAST_PUSHES))).size());
223 }
224
225 @Test
226 public void testSubmitPushToAll() {
227 Push p = getPush();
228 getUiModel().addAttribute("Push",p);
229 BindingResult bindingResult = new MapBindingResult(new HashMap<String,String>(),new String());
230 String sender = EMPTY;
231 String recipient = RECIPIENTS[0];
232 List<Device> devices = new ArrayList<Device>();
233 devices.add(new Device());
234 devices.add(new Device());
235 devices.get(0).setDeviceId("A");
236 devices.get(1).setDeviceId("B");
237 List<Push> pushes = new ArrayList<Push>();
238 pushes.add(new Push());
239 pushes.add(new Push());
240 pushes.add(new Push());
241
242 when(getDeviceService().findAllDevices()).thenReturn(devices);
243 when(getPushService().sendPush(p,devices)).thenReturn(devices.size());
244 when(getPushService().countPushes()).thenReturn(pushes.size());
245 when(getPushService().findAllPush()).thenReturn(pushes);
246
247 String viewName = getController().submitPush(getUiModel(),p,bindingResult,null,sender,recipient);
248
249 assertTrue("Failed to find proper view name: history.","push/history".equals(viewName));
250 assertTrue("Push count did not match expected value.",(pushes.size())==((Integer)(getUiModel().asMap().get(COUNT))).intValue());
251 assertTrue("Push not found in model.",null != getUiModel().asMap().get(PAST_PUSHES));
252 assertTrue("Push list not expected size.",3==((List<Push>)(getUiModel().asMap().get(PAST_PUSHES))).size());
253 }
254
255 @Test
256 public void testSubmitPushToIdWithInvalidPushMessage() {
257 Push p = getPush();
258 p.setMessage(EMPTY);
259 getUiModel().addAttribute("Push",p);
260 BindingResult bindingResult = new MapBindingResult(new HashMap<String,String>(),new String());
261 Device device = new Device();
262 device.setDeviceId("A");
263
264 when(getDeviceService().findDeviceById(p.getPushId())).thenReturn(device);
265
266 String viewName = getController().submitPush(getUiModel(),p,bindingResult,p.getPushId(),null,null);
267
268 assertTrue("Failed to find proper view name: history.","push/index".equals(viewName));
269 assertTrue("Push count exists and should not have.",!(getUiModel().asMap().containsKey(COUNT)));
270 assertTrue("Past pushes found and should not have been.",!getUiModel().asMap().containsKey(PAST_PUSHES));
271 }
272
273 @Test
274 public void testSubmitPushToUsernamelessWithNullPushMessage() {
275 Push p = getPush();
276 p.setMessage(null);
277 getUiModel().addAttribute("Push",p);
278 BindingResult bindingResult = new MapBindingResult(new HashMap<String,String>(),new String());
279 List<Device> devices = new ArrayList<Device>();
280 devices.add(new Device());
281 devices.add(new Device());
282 devices.get(0).setDeviceId("A");
283 devices.get(1).setDeviceId("B");
284
285 when(getDeviceService().findDevicesWithoutUsername()).thenReturn(devices);
286
287 String viewName = getController().submitPush(getUiModel(),p,bindingResult,null,null,RECIPIENTS[1]);
288
289 assertTrue("Failed to find proper view name: history.","push/index".equals(viewName));
290 assertTrue("Push count exists and should not have.",!(getUiModel().asMap().containsKey(COUNT)));
291 assertTrue("Past pushes found and should not have been.",!getUiModel().asMap().containsKey(PAST_PUSHES));
292 }
293
294 @Test
295 public void testSubmitPushToEmptyRecipientWithNullPushTitle() {
296 Push p = getPush();
297 p.setTitle(null);
298 getUiModel().addAttribute("Push",p);
299 BindingResult bindingResult = new MapBindingResult(new HashMap<String,String>(),new String());
300
301 String viewName = getController().submitPush(getUiModel(),p,bindingResult,null,null,EMPTY);
302
303 assertTrue("Failed to find proper view name: history.","push/index".equals(viewName));
304 assertTrue("Push count exists and should not have.",!(getUiModel().asMap().containsKey(COUNT)));
305 assertTrue("Past pushes found and should not have been.",!getUiModel().asMap().containsKey(PAST_PUSHES));
306 }
307
308 @Test
309 public void testSubmitPushToEmptyRecipientWithEmptyPushTitle() {
310 Push p = getPush();
311 p.setTitle(EMPTY);
312 getUiModel().addAttribute("Push",p);
313 BindingResult bindingResult = new MapBindingResult(new HashMap<String,String>(),new String());
314
315 String viewName = getController().submitPush(getUiModel(),p,bindingResult,null,null,null);
316
317 assertTrue("Failed to find proper view name: history.", "push/index".equals(viewName));
318 assertTrue("Push count exists and should not have.",!(getUiModel().asMap().containsKey(COUNT)));
319 assertTrue("Past pushes found and should not have been.",!getUiModel().asMap().containsKey(PAST_PUSHES));
320 }
321
322 @Test
323 public void testServiceWithNullData() {
324
325
326 }
327
328 @Test
329 public void testServiceWithMalformedJsonData() {
330
331
332 }
333
334 @Test
335 public void testService() {
336 String json = "{\"title\":\""+TITLE+"\",\"message\":\""+MESSAGE+"\",\"url\":\""+URL+"\",\"senderKey\":\""+SENDERS[0]+"\",\"recipients\":\""+RECIPIENTS[0]+"\",\"devices\":\""+SENDERS[0]+"\"}";
337
338
339 }
340
341 @Test
342 public void testServiceWithBadSender() {
343 String json = "{\"title\":\""+TITLE+"\",\"message\":\""+MESSAGE+"\",\"url\":\""+URL+"\",\"senderKey\":\""+SENDERS[1]+"\",\"devices\":\""+SENDERS[0]+"\"}";
344
345
346 }
347
348 @Test
349 public void testServiceWithNoRecipientsOrDevices() {
350 String json = "{\"title\":\""+TITLE+"\",\"message\":\""+MESSAGE+"\",\"url\":\""+URL+"\",\"senderKey\":\""+SENDERS[0]+"\"}";
351
352
353 }
354
355 @Test
356 public void testServiceWithMultipleRecipientsAndDevices() {
357 String json = "{\"title\":\""+TITLE+"\",\"message\":\""+MESSAGE+"\",\"url\":\""+URL+"\",\"senderKey\":\""+SENDERS[0]+"\",\"recipients\":[\""+RECIPIENTS[2]+"\",\""+RECIPIENTS[3]+"\"],\"devices\":[\""+RECIPIENTS[2]+"\",\""+RECIPIENTS[3]+"\"]}";
358
359
360 }
361
362 @Test
363 public void testSetKeyListFIle() {
364 getController().setKeyListFile(PUSH);
365 assertTrue("Failed to set key list file.",PUSH.equals(getController().getKeyListFile()));
366 }
367
368 @Test
369 public void testReceivedNotificationWithNullData() {
370 boolean didFail = false;
371 try {
372 ResponseEntity<String> httpResponse = getController().receivedNotification(null);
373 } catch( Exception e ) {
374 didFail = true;
375
376 }
377 assertTrue("Threw an exception when calling receivedNotification with null data.",!didFail);
378 }
379
380 @Test
381 public void testReceivedNotification() {
382 String jsonIn = "{\"deviceId\":\"abcdefg\",\"widget\":\"bob\"}";
383 when(getDeviceService().findDevicesByDeviceId(any(String.class))).thenReturn(null);
384 ResponseEntity<String> httpResponse = getController().receivedNotification(jsonIn);
385 assertTrue("HTTP Response was not OK.",HttpStatus.OK.equals(httpResponse.getStatusCode()));
386 }
387
388 @Test
389 public void testReceivedNotification2() {
390 String jsonIn = "{\"deviceId\":\"abcdefg\",\"widget\":\"bob\"}";
391 when(getDeviceService().findDevicesByDeviceId(any(String.class))).thenReturn(new ArrayList<Device>());
392 ResponseEntity<String> httpResponse = getController().receivedNotification(jsonIn);
393 assertTrue("HTTP Response was not OK.",HttpStatus.OK.equals(httpResponse.getStatusCode()));
394 }
395
396 @Test
397 public void testReceivedNotification3() {
398 String jsonIn = "{\"deviceId\":\"abcdefg\",\"widget\":\"bob\"}";
399 List<Device> devices = new ArrayList<Device>();
400 devices.add(new Device());
401 devices.add(new Device());
402 when(getDeviceService().findDevicesByDeviceId(any(String.class))).thenReturn(devices);
403 ResponseEntity<String> httpResponse = getController().receivedNotification(jsonIn);
404 assertTrue("HTTP Response was not OK.",HttpStatus.OK.equals(httpResponse.getStatusCode()));
405 }
406
407 protected Push getPush() {
408 return push;
409 }
410
411 private void setPush(Push push) {
412 this.push = push;
413 }
414
415 public PushController getController() {
416 return controller;
417 }
418
419 public void setController(PushController controller) {
420 this.controller = controller;
421 }
422
423 public MockHttpServletRequest getRequest() {
424 return request;
425 }
426
427 public void setRequest(MockHttpServletRequest request) {
428 this.request = request;
429 }
430
431 public MockHttpServletResponse getResponse() {
432 return response;
433 }
434
435 public void setResponse(MockHttpServletResponse response) {
436 this.response = response;
437 }
438
439 public Model getUiModel() {
440 return uiModel;
441 }
442
443 public void setUiModel(Model uiModel) {
444 this.uiModel = uiModel;
445 }
446
447 public PushService getPushService() {
448 return pushService;
449 }
450
451 public void setPushService(PushService pushService) {
452 this.pushService = pushService;
453 }
454
455 public DeviceService getDeviceService() {
456 return deviceService;
457 }
458
459 public void setDeviceService(DeviceService deviceService) {
460 this.deviceService = deviceService;
461 }
462
463
464
465
466 public GroupDao getGroupDao() {
467 return groupDao;
468 }
469
470
471
472
473 public void setGroupDao(GroupDao groupDao) {
474 this.groupDao = groupDao;
475 }
476
477 public SenderService getSenderService() {
478 return senderService;
479 }
480
481 public void setSenderService(SenderService senderService) {
482 this.senderService = senderService;
483 }
484 }