2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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.openecomp.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.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
58 import org.openecomp.portalapp.portal.controller.DashboardController;
59 import org.openecomp.portalapp.portal.core.MockEPUser;
60 import org.openecomp.portalapp.portal.domain.EPUser;
61 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
62 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
63 import org.openecomp.portalapp.portal.service.DashboardSearchService;
64 import org.openecomp.portalapp.portal.service.DashboardSearchServiceImpl;
65 import org.openecomp.portalapp.portal.transport.CommonWidget;
66 import org.openecomp.portalapp.portal.transport.CommonWidgetMeta;
67 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
68 import org.openecomp.portalapp.util.EPUserUtils;
69 import org.openecomp.portalsdk.core.domain.support.CollaborateList;
70 import org.openecomp.portalsdk.core.util.SystemProperties;
71 import org.powermock.api.mockito.PowerMockito;
72 import org.powermock.core.classloader.annotations.PrepareForTest;
73 import org.powermock.modules.junit4.PowerMockRunner;
76 @RunWith(PowerMockRunner.class)
77 @PrepareForTest({EPUserUtils.class, CollaborateList.class, SystemProperties.class, EPCommonSystemProperties.class})
78 public class DashboardControllerTest {
81 DashboardSearchService searchService = new DashboardSearchServiceImpl();
84 DashboardController dashboardController = new DashboardController();
88 MockitoAnnotations.initMocks(this);
91 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
93 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
94 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
96 NullPointerException nullPointerException = new NullPointerException();
98 MockEPUser mockUser = new MockEPUser();
100 public CommonWidgetMeta mockCommonWidgetMeta() {
101 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
102 List<CommonWidget> widgetList = new ArrayList<>();
103 CommonWidget commonWidget = new CommonWidget();
104 commonWidget.setId((long) 1);
105 commonWidget.setCategory("test");
106 commonWidget.setHref("testhref");
107 commonWidget.setTitle("testTitle");
108 commonWidget.setContent("testcontent");
109 commonWidget.setEventDate("testDate");
110 commonWidget.setSortOrder(1);
111 widgetList.add(commonWidget);
112 commonWidgetMeta.setItems(widgetList);
114 return commonWidgetMeta;
117 public CommonWidget mockCommonWidget() {
119 CommonWidget commonWidget = new CommonWidget();
120 commonWidget.setId((long) 1);
121 commonWidget.setCategory("test");
122 commonWidget.setHref("testhref");
123 commonWidget.setTitle("testTitle");
124 commonWidget.setContent("testcontent");
125 commonWidget.setEventDate("testDate");
126 commonWidget.setSortOrder(1);
133 public void getWidgetDataTest() throws IOException {
135 String resourceType = null;
136 PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
137 expectedData.setStatus(PortalRestStatusEnum.ERROR);
138 expectedData.setMessage("Unexpected resource type null");
139 expectedData.setResponse(null);
141 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
142 assertEquals(expectedData,actualResponse);
146 public void getWidgetDataWithValidResourceTest() throws IOException {
147 String resourceType = "EVENTS";
148 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
149 commonWidgetMeta.setCategory(null);
151 Mockito.when(searchService.getWidgetData(resourceType)).thenReturn(commonWidgetMeta);
152 PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
153 expectedData.setStatus(PortalRestStatusEnum.OK);
154 expectedData.setMessage("success");
155 expectedData.setResponse(commonWidgetMeta);
157 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
158 System.out.println(actualResponse);
159 assertEquals(expectedData,actualResponse);
163 public void saveWidgetDataBulkNullTest() throws IOException {
164 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
165 commonWidgetMeta.setCategory(null);
167 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
168 expectedData.setStatus(PortalRestStatusEnum.ERROR);
169 expectedData.setMessage("ERROR");
170 expectedData.setResponse("Category cannot be null or empty");
172 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
173 assertEquals(expectedData,actualResponse);
177 public void saveWidgetUnexpectedDataBulkTest() throws IOException {
178 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
179 commonWidgetMeta.setCategory("Unexpected Data");
181 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
182 expectedData.setStatus(PortalRestStatusEnum.ERROR);
183 expectedData.setMessage("Unexpected resource type Unexpected Data");
184 expectedData.setResponse(null);
186 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
187 assertEquals(expectedData,actualResponse);
192 public void saveWidgetInvalidDataBulkTest() throws IOException {
193 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
194 commonWidgetMeta.setCategory("EVENTS");
196 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
197 expectedData.setStatus(PortalRestStatusEnum.ERROR);
198 expectedData.setMessage("Invalid category: test");
199 expectedData.setResponse(null);
201 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
202 assertEquals(expectedData,actualResponse);
206 public void saveWidgetDataBulkTest() throws IOException {
208 List<CommonWidget> widgetList = new ArrayList<>();
209 CommonWidget commonWidget = new CommonWidget("EVENTS", "http://test.com", "testTitle", "testcontent", "2017-07-01", 1);
210 widgetList.add(commonWidget);
211 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta("EVENTS", widgetList);
215 /* commonWidgetMeta.setItems(widgetList);
217 commonWidgetMeta.setCategory("EVENTS");*/
219 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
220 expectedData.setStatus(PortalRestStatusEnum.OK);
221 expectedData.setMessage("success");
222 expectedData.setResponse("success");
224 Mockito.when(searchService.saveWidgetDataBulk(commonWidgetMeta)).thenReturn("success");
226 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
227 assertEquals(expectedData,actualResponse);
231 public void saveWidgetDataNullTest() throws IOException {
233 CommonWidget commonWidget = mockCommonWidget();
234 commonWidget.setId((long)1);
235 commonWidget.setContent("test");
236 commonWidget.setCategory(null);
237 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
238 expectedData.setStatus(PortalRestStatusEnum.ERROR);
239 expectedData.setMessage("ERROR");
240 expectedData.setResponse("Category cannot be null or empty");
242 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget);
243 assertEquals(expectedData,actualResponse);
248 public void saveWidgetDataErrorTest() throws IOException {
250 CommonWidget commonWidget = mockCommonWidget();
251 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
252 expectedData.setStatus(PortalRestStatusEnum.ERROR);
253 expectedData.setMessage("Invalid category: test");
254 expectedData.setResponse(null);
256 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget);
257 assertEquals(expectedData,actualResponse);
261 public void saveWidgetDataTest() throws IOException {
263 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
264 List<CommonWidget> widgetList = new ArrayList<>();
265 CommonWidget commonWidget = new CommonWidget();
266 commonWidget.setId((long) 1);
267 commonWidget.setCategory("EVENTS");
268 commonWidget.setHref("http://test.com");
269 commonWidget.setTitle("testTitle");
270 commonWidget.setContent("testcontent");
271 commonWidget.setEventDate("2017-07-01");
272 commonWidget.setSortOrder(1);
273 widgetList.add(commonWidget);
274 commonWidgetMeta.setItems(widgetList);
276 commonWidgetMeta.setCategory("EVENTS");
278 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
279 expectedData.setStatus(PortalRestStatusEnum.OK);
280 expectedData.setMessage("success");
281 expectedData.setResponse("success");
283 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
285 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget);
286 assertEquals(expectedData,actualResponse);
291 public void deleteWidgetDataTest() throws IOException {
293 CommonWidget commonWidget = mockCommonWidget();
295 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
296 expectedData.setStatus(PortalRestStatusEnum.OK);
297 expectedData.setMessage("success");
298 expectedData.setResponse(null);
300 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
302 PortalRestResponse<String> actualResponse = dashboardController.deleteWidgetData(commonWidget);
303 assertEquals(expectedData,actualResponse);
308 public void getActiveUsersTest(){
309 List<String> activeUsers = new ArrayList<>();
310 List<String> expectedUsersList = new ArrayList<>();
311 EPUser user = mockUser.mockEPUser();
312 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
313 String userId = user.getOrgUserId();
314 Mockito.when(searchService.getRelatedUsers(userId)).thenReturn(activeUsers);
315 expectedUsersList= dashboardController.getActiveUsers(mockedRequest);
316 assertEquals(expectedUsersList, activeUsers);
321 public void getActiveUsersExceptionTest(){
322 List<String> activeUsers = new ArrayList<>();
323 List<String> expectedUsersList = new ArrayList<>();
324 EPUser user = mockUser.mockEPUser();
325 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
326 String userId = user.getOrgUserId();
327 Mockito.when(searchService.getRelatedUsers(userId)).thenThrow(nullPointerException);
328 expectedUsersList = dashboardController.getActiveUsers(mockedRequest);
329 assertEquals(expectedUsersList, activeUsers);
333 public void getOnlineUserUpdateRateTest(){
334 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
335 expectedData.setStatus(PortalRestStatusEnum.OK);
336 expectedData.setMessage("success");
337 expectedData.setResponse("{onlineUserUpdateRate=1400000, onlineUserUpdateDuration=1400000}");
339 PowerMockito.mockStatic(SystemProperties.class);
340 PowerMockito.mockStatic(EPCommonSystemProperties.class);
341 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenReturn("1400");
342 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenReturn("1400");
344 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
345 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
349 public void getOnlineUserUpdateRateExceptionTest(){
350 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
351 expectedData.setStatus(PortalRestStatusEnum.ERROR);
352 expectedData.setMessage("java.lang.NullPointerException");
353 expectedData.setResponse(null);
355 PowerMockito.mockStatic(SystemProperties.class);
356 PowerMockito.mockStatic(EPCommonSystemProperties.class);
357 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenThrow(nullPointerException);
358 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenThrow(nullPointerException);
360 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
361 assertEquals(expectedData,actualResponse);
365 public void getWindowWidthThresholdForRightMenuTest(){
366 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
367 expectedData.setStatus(PortalRestStatusEnum.OK);
368 expectedData.setMessage("success");
369 expectedData.setResponse("{windowWidth=1400}");
371 PowerMockito.mockStatic(SystemProperties.class);
372 PowerMockito.mockStatic(EPCommonSystemProperties.class);
373 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenReturn("1400");
375 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
376 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
380 public void getWindowWidthThresholdForRightMenuExceptionTest(){
381 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
382 expectedData.setStatus(PortalRestStatusEnum.ERROR);
383 expectedData.setMessage("java.lang.NullPointerException");
384 expectedData.setResponse(null);
386 PowerMockito.mockStatic(SystemProperties.class);
387 PowerMockito.mockStatic(EPCommonSystemProperties.class);
388 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenThrow(nullPointerException);
390 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
391 assertEquals(expectedData,actualResponse);
395 public void getWindowWidthThresholdForLeftMenuTest(){
396 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
397 expectedData.setStatus(PortalRestStatusEnum.OK);
398 expectedData.setMessage("success");
399 expectedData.setResponse("{windowWidth=1400}");
401 PowerMockito.mockStatic(SystemProperties.class);
402 PowerMockito.mockStatic(EPCommonSystemProperties.class);
403 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenReturn("1400");
405 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
406 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
410 public void getWindowWidthThresholdForLeftMenuExceptionTest(){
411 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
412 expectedData.setStatus(PortalRestStatusEnum.ERROR);
413 expectedData.setMessage("java.lang.NullPointerException");
414 expectedData.setResponse(null);
416 PowerMockito.mockStatic(SystemProperties.class);
417 PowerMockito.mockStatic(EPCommonSystemProperties.class);
418 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenThrow(nullPointerException);
420 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
421 assertEquals(expectedData,actualResponse);
425 public void getActiveUsersNullTest(){
426 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
427 expectedData.setStatus(PortalRestStatusEnum.ERROR);
428 expectedData.setMessage("User object is null? - check logs");
429 expectedData.setResponse(new ArrayList<>());
431 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
432 assertEquals(expectedData,actualResponse);
436 public void activeUsersTest(){
437 EPUser user = mockUser.mockEPUser();
438 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
439 expectedData.setStatus(PortalRestStatusEnum.OK);
440 expectedData.setMessage("success");
441 expectedData.setResponse(new ArrayList<>());
442 PowerMockito.mockStatic(EPUserUtils.class);
443 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
444 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
445 assertEquals(expectedData,actualResponse);
449 public void activeUsersExceptionTest(){
450 EPUser user = mockUser.mockEPUser();
451 user.setLoginId("test");
452 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
453 expectedData.setStatus(PortalRestStatusEnum.ERROR);
454 expectedData.setMessage("null - check logs.");
455 expectedData.setResponse(null);
457 PowerMockito.mockStatic(EPUserUtils.class);
458 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
459 Mockito.when(searchService.getRelatedUsers(user.getLoginId())).thenThrow(nullPointerException);
460 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
461 assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.ERROR) == 0);