1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
package org.openecomp.portalapp.portal.test.controller;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.openecomp.portalapp.portal.controller.DashboardController;
import org.openecomp.portalapp.portal.domain.EPUser;
import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
import org.openecomp.portalapp.portal.service.DashboardSearchService;
import org.openecomp.portalapp.portal.service.DashboardSearchServiceImpl;
import org.openecomp.portalapp.portal.test.core.MockEPUser;
import org.openecomp.portalapp.portal.transport.CommonWidget;
import org.openecomp.portalapp.portal.transport.CommonWidgetMeta;
import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
import org.openecomp.portalapp.test.framework.MockitoTestSuite;
import org.openecomp.portalapp.util.EPUserUtils;
import org.openecomp.portalsdk.core.domain.support.CollaborateList;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({EPUserUtils.class, CollaborateList.class, SystemProperties.class, EPCommonSystemProperties.class})
public class DashboardControllerTest {
@Mock
DashboardSearchService searchService = new DashboardSearchServiceImpl();
@InjectMocks
DashboardController dashboardController = new DashboardController();
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
NullPointerException nullPointerException = new NullPointerException();
MockEPUser mockUser = new MockEPUser();
public CommonWidgetMeta mockCommonWidgetMeta() {
CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
List<CommonWidget> widgetList = new ArrayList<>();
CommonWidget commonWidget = new CommonWidget();
commonWidget.setId((long) 1);
commonWidget.setCategory("test");
commonWidget.setHref("testhref");
commonWidget.setTitle("testTitle");
commonWidget.setContent("testcontent");
commonWidget.setEventDate("testDate");
commonWidget.setSortOrder(1);
widgetList.add(commonWidget);
commonWidgetMeta.setItems(widgetList);
return commonWidgetMeta;
}
public CommonWidget mockCommonWidget() {
CommonWidget commonWidget = new CommonWidget();
commonWidget.setId((long) 1);
commonWidget.setCategory("test");
commonWidget.setHref("testhref");
commonWidget.setTitle("testTitle");
commonWidget.setContent("testcontent");
commonWidget.setEventDate("testDate");
commonWidget.setSortOrder(1);
return commonWidget;
}
@Test
public void getWidgetDataTest() throws IOException {
String resourceType = null;
PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("Unexpected resource type null");
expectedData.setResponse(null);
PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
assertEquals(expectedData,actualResponse);
}
@Test
public void getWidgetDataWithValidResourceTest() throws IOException {
String resourceType = "EVENTS";
CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
commonWidgetMeta.setCategory(null);
Mockito.when(searchService.getWidgetData(resourceType)).thenReturn(commonWidgetMeta);
PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
expectedData.setStatus(PortalRestStatusEnum.OK);
expectedData.setMessage("success");
expectedData.setResponse(commonWidgetMeta);
PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
System.out.println(actualResponse);
assertEquals(expectedData,actualResponse);
}
@Test
public void saveWidgetDataBulkNullTest() throws IOException {
CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
commonWidgetMeta.setCategory(null);
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("ERROR");
expectedData.setResponse("Category cannot be null or empty");
PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
assertEquals(expectedData,actualResponse);
}
@Test
public void saveWidgetUnexpectedDataBulkTest() throws IOException {
CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
commonWidgetMeta.setCategory("Unexpected Data");
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("Unexpected resource type Unexpected Data");
expectedData.setResponse(null);
PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
assertEquals(expectedData,actualResponse);
}
@Test
public void saveWidgetInvalidDataBulkTest() throws IOException {
CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
commonWidgetMeta.setCategory("EVENTS");
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("Invalid category: test");
expectedData.setResponse(null);
PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
assertEquals(expectedData,actualResponse);
}
@Test
public void saveWidgetDataBulkTest() throws IOException {
List<CommonWidget> widgetList = new ArrayList<>();
CommonWidget commonWidget = new CommonWidget("EVENTS", "http://test.com", "testTitle", "testcontent", "2017-07-01", 1);
widgetList.add(commonWidget);
CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta("EVENTS", widgetList);
/* commonWidgetMeta.setItems(widgetList);
commonWidgetMeta.setCategory("EVENTS");*/
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.OK);
expectedData.setMessage("success");
expectedData.setResponse("success");
Mockito.when(searchService.saveWidgetDataBulk(commonWidgetMeta)).thenReturn("success");
PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
assertEquals(expectedData,actualResponse);
}
@Test
public void saveWidgetDataNullTest() throws IOException {
CommonWidget commonWidget = mockCommonWidget();
commonWidget.setId((long)1);
commonWidget.setContent("test");
commonWidget.setCategory(null);
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("ERROR");
expectedData.setResponse("Category cannot be null or empty");
PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget);
assertEquals(expectedData,actualResponse);
}
@Test
public void saveWidgetDataErrorTest() throws IOException {
CommonWidget commonWidget = mockCommonWidget();
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("Invalid category: test");
expectedData.setResponse(null);
PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget);
assertEquals(expectedData,actualResponse);
}
@Test
public void saveWidgetDataTest() throws IOException {
CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
List<CommonWidget> widgetList = new ArrayList<>();
CommonWidget commonWidget = new CommonWidget();
commonWidget.setId((long) 1);
commonWidget.setCategory("EVENTS");
commonWidget.setHref("http://test.com");
commonWidget.setTitle("testTitle");
commonWidget.setContent("testcontent");
commonWidget.setEventDate("2017-07-01");
commonWidget.setSortOrder(1);
widgetList.add(commonWidget);
commonWidgetMeta.setItems(widgetList);
commonWidgetMeta.setCategory("EVENTS");
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.OK);
expectedData.setMessage("success");
expectedData.setResponse("success");
Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget);
assertEquals(expectedData,actualResponse);
}
@Test
public void deleteWidgetDataTest() throws IOException {
CommonWidget commonWidget = mockCommonWidget();
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.OK);
expectedData.setMessage("success");
expectedData.setResponse(null);
Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
PortalRestResponse<String> actualResponse = dashboardController.deleteWidgetData(commonWidget);
assertEquals(expectedData,actualResponse);
}
@Test
public void getActiveUsersTest(){
List<String> activeUsers = new ArrayList<>();
List<String> expectedUsersList = new ArrayList<>();
EPUser user = mockUser.mockEPUser();
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
String userId = user.getOrgUserId();
Mockito.when(searchService.getRelatedUsers(userId)).thenReturn(activeUsers);
expectedUsersList= dashboardController.getActiveUsers(mockedRequest);
assertEquals(expectedUsersList, activeUsers);
}
@Test
public void getActiveUsersExceptionTest(){
List<String> activeUsers = new ArrayList<>();
List<String> expectedUsersList = new ArrayList<>();
EPUser user = mockUser.mockEPUser();
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
String userId = user.getOrgUserId();
Mockito.when(searchService.getRelatedUsers(userId)).thenThrow(nullPointerException);
expectedUsersList = dashboardController.getActiveUsers(mockedRequest);
assertEquals(expectedUsersList, activeUsers);
}
@Test
public void getOnlineUserUpdateRateTest(){
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.OK);
expectedData.setMessage("success");
expectedData.setResponse("{onlineUserUpdateRate=1400000, onlineUserUpdateDuration=1400000}");
PowerMockito.mockStatic(SystemProperties.class);
PowerMockito.mockStatic(EPCommonSystemProperties.class);
Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenReturn("1400");
Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenReturn("1400");
PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
assertEquals(expectedData.getStatus(),actualResponse.getStatus());
}
@Test
public void getOnlineUserUpdateRateExceptionTest(){
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("java.lang.NullPointerException");
expectedData.setResponse(null);
PowerMockito.mockStatic(SystemProperties.class);
PowerMockito.mockStatic(EPCommonSystemProperties.class);
Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenThrow(nullPointerException);
Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenThrow(nullPointerException);
PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
assertEquals(expectedData,actualResponse);
}
@Test
public void getWindowWidthThresholdForRightMenuTest(){
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.OK);
expectedData.setMessage("success");
expectedData.setResponse("{windowWidth=1400}");
PowerMockito.mockStatic(SystemProperties.class);
PowerMockito.mockStatic(EPCommonSystemProperties.class);
Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenReturn("1400");
PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
assertEquals(expectedData.getStatus(),actualResponse.getStatus());
}
@Test
public void getWindowWidthThresholdForRightMenuExceptionTest(){
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("java.lang.NullPointerException");
expectedData.setResponse(null);
PowerMockito.mockStatic(SystemProperties.class);
PowerMockito.mockStatic(EPCommonSystemProperties.class);
Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenThrow(nullPointerException);
PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
assertEquals(expectedData,actualResponse);
}
@Test
public void getWindowWidthThresholdForLeftMenuTest(){
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.OK);
expectedData.setMessage("success");
expectedData.setResponse("{windowWidth=1400}");
PowerMockito.mockStatic(SystemProperties.class);
PowerMockito.mockStatic(EPCommonSystemProperties.class);
Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenReturn("1400");
PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
assertEquals(expectedData.getStatus(),actualResponse.getStatus());
}
@Test
public void getWindowWidthThresholdForLeftMenuExceptionTest(){
PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("java.lang.NullPointerException");
expectedData.setResponse(null);
PowerMockito.mockStatic(SystemProperties.class);
PowerMockito.mockStatic(EPCommonSystemProperties.class);
Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenThrow(nullPointerException);
PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
assertEquals(expectedData,actualResponse);
}
@Test
public void getActiveUsersNullTest(){
PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
expectedData.setStatus(PortalRestStatusEnum.ERROR);
expectedData.setMessage("User object is null? - check logs");
expectedData.setResponse(new ArrayList<>());
PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
assertEquals(expectedData,actualResponse);
}
@Test
public void activeUsersTest(){
EPUser user = mockUser.mockEPUser();
PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
expectedData.setStatus(PortalRestStatusEnum.OK);
expectedData.setMessage("success");
expectedData.setResponse(new ArrayList<>());
PowerMockito.mockStatic(EPUserUtils.class);
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
assertEquals(expectedData,actualResponse);
}
// @Test
// public void activeUsersExceptionTest(){
// EPUser user = mockUser.mockEPUser();
// user.setLoginId("test");
// String loginId = "abc";
// PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
// expectedData.setStatus(PortalRestStatusEnum.ERROR);
// expectedData.setMessage("java.lang.NullPointerException");
// expectedData.setResponse(null);
//
//// PowerMockito.mockStatic(EPUserUtils.class);
// Mockito.when(searchService.getRelatedUsers(user.getLoginId(user.getLoginId()))).thenReturn(nullPointerException);
// PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
// System.out.println(actualResponse);
// //assertEquals(expectedData,actualResponse);
// }
}
|