2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.portal.controller;
40 import static org.junit.Assert.*;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.List;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.InjectMocks;
54 import org.mockito.Matchers;
55 import org.mockito.Mock;
56 import org.mockito.Mockito;
57 import org.mockito.MockitoAnnotations;
58 import org.onap.portalapp.portal.controller.DashboardController;
59 import org.onap.portalapp.portal.core.MockEPUser;
60 import org.onap.portalapp.portal.domain.EPUser;
61 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
62 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
63 import org.onap.portalapp.portal.framework.MockitoTestSuite;
64 import org.onap.portalapp.portal.service.AdminRolesService;
65 import org.onap.portalapp.portal.service.AdminRolesServiceImpl;
66 import org.onap.portalapp.portal.service.DashboardSearchService;
67 import org.onap.portalapp.portal.service.DashboardSearchServiceImpl;
68 import org.onap.portalapp.portal.transport.CommonWidget;
69 import org.onap.portalapp.portal.transport.CommonWidgetMeta;
70 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
71 import org.onap.portalapp.util.EPUserUtils;
72 import org.onap.portalsdk.core.domain.support.CollaborateList;
73 import org.onap.portalsdk.core.util.SystemProperties;
74 import org.powermock.api.mockito.PowerMockito;
75 import org.powermock.core.classloader.annotations.PrepareForTest;
76 import org.powermock.modules.junit4.PowerMockRunner;
79 @RunWith(PowerMockRunner.class)
80 @PrepareForTest({EPUserUtils.class, CollaborateList.class, SystemProperties.class, EPCommonSystemProperties.class})
81 public class DashboardControllerTest {
84 DashboardSearchService searchService = new DashboardSearchServiceImpl();
87 DashboardController dashboardController = new DashboardController();
90 AdminRolesService adminRolesService = new AdminRolesServiceImpl();
93 MockitoAnnotations.initMocks(this);
96 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
98 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
99 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
101 NullPointerException nullPointerException = new NullPointerException();
103 MockEPUser mockUser = new MockEPUser();
105 public CommonWidgetMeta mockCommonWidgetMeta() {
106 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
107 List<CommonWidget> widgetList = new ArrayList<>();
108 CommonWidget commonWidget = new CommonWidget();
109 commonWidget.setId((long) 1);
110 commonWidget.setCategory("test");
111 commonWidget.setHref("testhref");
112 commonWidget.setTitle("testTitle");
113 commonWidget.setContent("testcontent");
114 commonWidget.setEventDate("testDate");
115 commonWidget.setSortOrder(1);
116 widgetList.add(commonWidget);
117 commonWidgetMeta.setItems(widgetList);
119 return commonWidgetMeta;
122 public CommonWidget mockCommonWidget() {
124 CommonWidget commonWidget = new CommonWidget();
125 commonWidget.setId((long) 1);
126 commonWidget.setCategory("test");
127 commonWidget.setHref("testhref");
128 commonWidget.setTitle("testTitle");
129 commonWidget.setContent("testcontent");
130 commonWidget.setEventDate("testDate");
131 commonWidget.setSortOrder(1);
138 public void getWidgetDataTest() throws IOException {
140 String resourceType = null;
141 PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
142 expectedData.setStatus(PortalRestStatusEnum.ERROR);
143 expectedData.setMessage("Unexpected resource type null");
144 expectedData.setResponse(null);
146 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
147 assertEquals(expectedData,actualResponse);
151 public void getWidgetDataWithValidResourceTest() throws IOException {
152 String resourceType = "EVENTS";
153 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
154 commonWidgetMeta.setCategory(null);
156 Mockito.when(searchService.getWidgetData(resourceType)).thenReturn(commonWidgetMeta);
157 PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
158 expectedData.setStatus(PortalRestStatusEnum.OK);
159 expectedData.setMessage("success");
160 expectedData.setResponse(commonWidgetMeta);
162 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
163 assertEquals(expectedData,actualResponse);
167 public void saveWidgetDataBulkNullTest() throws IOException {
168 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
169 commonWidgetMeta.setCategory(null);
171 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
172 expectedData.setStatus(PortalRestStatusEnum.ERROR);
173 expectedData.setMessage("ERROR");
174 expectedData.setResponse("Category cannot be null or empty");
176 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
177 assertEquals(expectedData,actualResponse);
181 public void saveWidgetUnexpectedDataBulkTest() throws IOException {
182 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
183 commonWidgetMeta.setCategory("Unexpected Data");
185 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
186 expectedData.setStatus(PortalRestStatusEnum.ERROR);
187 expectedData.setMessage("Unexpected resource type Unexpected Data");
188 expectedData.setResponse(null);
190 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
191 assertEquals(expectedData,actualResponse);
196 public void saveWidgetInvalidDataBulkTest() throws IOException {
197 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
198 commonWidgetMeta.setCategory("EVENTS");
200 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
201 expectedData.setStatus(PortalRestStatusEnum.ERROR);
202 expectedData.setMessage("Invalid category: test");
203 expectedData.setResponse(null);
205 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
206 assertEquals(expectedData,actualResponse);
210 public void saveWidgetDataBulkTest() throws IOException {
212 List<CommonWidget> widgetList = new ArrayList<>();
213 CommonWidget commonWidget = new CommonWidget("EVENTS", "http://test.com", "testTitle", "testcontent", "2017-07-01", 1);
214 widgetList.add(commonWidget);
215 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta("EVENTS", widgetList);
219 /* commonWidgetMeta.setItems(widgetList);
221 commonWidgetMeta.setCategory("EVENTS");*/
223 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
224 expectedData.setStatus(PortalRestStatusEnum.OK);
225 expectedData.setMessage("success");
226 expectedData.setResponse("success");
228 Mockito.when(searchService.saveWidgetDataBulk(commonWidgetMeta)).thenReturn("success");
230 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
231 assertEquals(expectedData,actualResponse);
235 public void saveWidgetDataNullTest() throws IOException {
237 CommonWidget commonWidget = mockCommonWidget();
238 commonWidget.setId((long)1);
239 commonWidget.setContent("test");
240 commonWidget.setCategory(null);
241 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
242 expectedData.setStatus(PortalRestStatusEnum.ERROR);
243 expectedData.setMessage("ERROR");
244 expectedData.setResponse("Category cannot be null or empty");
246 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
247 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse);
248 assertEquals(expectedData,actualResponse);
253 public void saveWidgetDataErrorTest() throws IOException {
255 CommonWidget commonWidget = mockCommonWidget();
256 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
257 expectedData.setStatus(PortalRestStatusEnum.ERROR);
258 expectedData.setMessage("Invalid category: test");
259 expectedData.setResponse(null);
260 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
261 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget,mockedRequest, mockedResponse);
262 assertEquals(expectedData,actualResponse);
266 public void saveWidgetDataTest() throws IOException {
268 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
269 List<CommonWidget> widgetList = new ArrayList<>();
270 CommonWidget commonWidget = new CommonWidget();
271 commonWidget.setId((long) 1);
272 commonWidget.setCategory("EVENTS");
273 commonWidget.setHref("http://test.com");
274 commonWidget.setTitle("testTitle");
275 commonWidget.setContent("testcontent");
276 commonWidget.setEventDate("2017-07-01");
277 commonWidget.setSortOrder(1);
278 widgetList.add(commonWidget);
279 commonWidgetMeta.setItems(widgetList);
281 commonWidgetMeta.setCategory("EVENTS");
283 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
284 expectedData.setStatus(PortalRestStatusEnum.OK);
285 expectedData.setMessage("success");
286 expectedData.setResponse("success");
287 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
288 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
289 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse);
290 assertEquals(expectedData,actualResponse);
295 public void deleteWidgetDataTest() throws IOException {
297 CommonWidget commonWidget = mockCommonWidget();
299 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
300 expectedData.setStatus(PortalRestStatusEnum.OK);
301 expectedData.setMessage("success");
302 expectedData.setResponse(null);
304 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
306 PortalRestResponse<String> actualResponse = dashboardController.deleteWidgetData(commonWidget);
307 assertEquals(expectedData,actualResponse);
312 public void getActiveUsersTest(){
313 List<String> activeUsers = new ArrayList<>();
314 List<String> expectedUsersList = new ArrayList<>();
315 EPUser user = mockUser.mockEPUser();
316 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
317 String userId = user.getOrgUserId();
318 Mockito.when(searchService.getRelatedUsers(userId)).thenReturn(activeUsers);
319 expectedUsersList= dashboardController.getActiveUsers(mockedRequest);
320 assertEquals(expectedUsersList, activeUsers);
325 public void getActiveUsersExceptionTest(){
326 List<String> activeUsers = new ArrayList<>();
327 List<String> expectedUsersList = new ArrayList<>();
328 EPUser user = mockUser.mockEPUser();
329 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
330 String userId = user.getOrgUserId();
331 Mockito.when(searchService.getRelatedUsers(userId)).thenThrow(nullPointerException);
332 expectedUsersList = dashboardController.getActiveUsers(mockedRequest);
333 assertEquals(expectedUsersList, activeUsers);
337 public void getOnlineUserUpdateRateTest(){
338 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
339 expectedData.setStatus(PortalRestStatusEnum.OK);
340 expectedData.setMessage("success");
341 expectedData.setResponse("{onlineUserUpdateRate=1400000, onlineUserUpdateDuration=1400000}");
343 PowerMockito.mockStatic(SystemProperties.class);
344 PowerMockito.mockStatic(EPCommonSystemProperties.class);
345 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenReturn("1400");
346 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenReturn("1400");
348 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
349 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
353 public void getOnlineUserUpdateRateExceptionTest(){
354 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
355 expectedData.setStatus(PortalRestStatusEnum.ERROR);
356 expectedData.setMessage("java.lang.NullPointerException");
357 expectedData.setResponse(null);
359 PowerMockito.mockStatic(SystemProperties.class);
360 PowerMockito.mockStatic(EPCommonSystemProperties.class);
361 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenThrow(nullPointerException);
362 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenThrow(nullPointerException);
364 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
365 assertEquals(expectedData,actualResponse);
369 public void getWindowWidthThresholdForRightMenuTest(){
370 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
371 expectedData.setStatus(PortalRestStatusEnum.OK);
372 expectedData.setMessage("success");
373 expectedData.setResponse("{windowWidth=1400}");
375 PowerMockito.mockStatic(SystemProperties.class);
376 PowerMockito.mockStatic(EPCommonSystemProperties.class);
377 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenReturn("1400");
379 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
380 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
384 public void getWindowWidthThresholdForRightMenuExceptionTest(){
385 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
386 expectedData.setStatus(PortalRestStatusEnum.ERROR);
387 expectedData.setMessage("java.lang.NullPointerException");
388 expectedData.setResponse(null);
390 PowerMockito.mockStatic(SystemProperties.class);
391 PowerMockito.mockStatic(EPCommonSystemProperties.class);
392 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenThrow(nullPointerException);
394 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
395 assertEquals(expectedData,actualResponse);
399 public void getWindowWidthThresholdForLeftMenuTest(){
400 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
401 expectedData.setStatus(PortalRestStatusEnum.OK);
402 expectedData.setMessage("success");
403 expectedData.setResponse("{windowWidth=1400}");
405 PowerMockito.mockStatic(SystemProperties.class);
406 PowerMockito.mockStatic(EPCommonSystemProperties.class);
407 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenReturn("1400");
409 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
410 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
414 public void getWindowWidthThresholdForLeftMenuExceptionTest(){
415 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
416 expectedData.setStatus(PortalRestStatusEnum.ERROR);
417 expectedData.setMessage("java.lang.NullPointerException");
418 expectedData.setResponse(null);
420 PowerMockito.mockStatic(SystemProperties.class);
421 PowerMockito.mockStatic(EPCommonSystemProperties.class);
422 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenThrow(nullPointerException);
424 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
425 assertEquals(expectedData,actualResponse);
429 public void getActiveUsersNullTest(){
430 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
431 expectedData.setStatus(PortalRestStatusEnum.ERROR);
432 expectedData.setMessage("User object is null? - check logs");
433 expectedData.setResponse(new ArrayList<>());
435 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
436 assertEquals(expectedData,actualResponse);
440 public void activeUsersTest(){
441 EPUser user = mockUser.mockEPUser();
442 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
443 expectedData.setStatus(PortalRestStatusEnum.OK);
444 expectedData.setMessage("success");
445 expectedData.setResponse(new ArrayList<>());
446 PowerMockito.mockStatic(EPUserUtils.class);
447 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
448 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
449 assertEquals(expectedData,actualResponse);
453 public void activeUsersExceptionTest(){
454 EPUser user = mockUser.mockEPUser();
455 user.setLoginId("test");
456 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
457 expectedData.setStatus(PortalRestStatusEnum.ERROR);
458 expectedData.setMessage("null - check logs.");
459 expectedData.setResponse(null);
461 PowerMockito.mockStatic(EPUserUtils.class);
462 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
463 Mockito.when(searchService.getRelatedUsers(user.getLoginId())).thenThrow(nullPointerException);
464 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
465 assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.ERROR) == 0);