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.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
55
56
57
58
59
60 @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
61 public class PreferencesControllerTest {
62
63
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
181
182 public static MockServletContext getServletContext() {
183 return servletContext;
184 }
185
186
187
188 public static void setServletContext(MockServletContext servletContext) {
189 PreferencesControllerTest.servletContext = servletContext;
190 }
191
192
193
194 public PreferencesController getController() {
195 return controller;
196 }
197
198
199
200 public void setController(PreferencesController controller) {
201 this.controller = controller;
202 }
203
204
205
206
207
208
209 public PreferenceService getPrefService() {
210 return prefService;
211 }
212
213
214
215
216 public void setPrefService(PreferenceService prefService) {
217 this.prefService = prefService;
218 }
219
220
221
222
223 public SenderService getSenderService() {
224 return senderService;
225 }
226
227
228
229
230 public void setSenderService(SenderService senderService) {
231 this.senderService = senderService;
232 }
233
234
235
236
237 public MockHttpServletRequest getRequest() {
238 return request;
239 }
240
241
242
243 public void setRequest(MockHttpServletRequest request) {
244 this.request = request;
245 }
246
247
248
249 public MockHttpServletResponse getResponse() {
250 return response;
251 }
252
253
254
255 public void setResponse(MockHttpServletResponse response) {
256 this.response = response;
257 }
258
259
260
261 public Model getUiModel() {
262 return uiModel;
263 }
264
265
266
267 public void setUiModel(Model uiModel) {
268 this.uiModel = uiModel;
269 }
270
271
272
273 public Preference getPreference() {
274 return preference;
275 }
276
277
278
279
280 public void setPreference(Preference preference) {
281 this.preference = preference;
282 }
283
284
285
286
287 public User getUser() {
288 return user;
289 }
290
291
292
293 public void setUser(User user) {
294 this.user = user;
295 }
296
297
298
299 public UserDao getUserDao() {
300 return userDao;
301 }
302
303
304
305 public void setUserDao(UserDao userDao) {
306 this.userDao = userDao;
307 }
308
309
310 }