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============================================
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.HashMap;
45 import java.util.List;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
51 import org.junit.Before;
52 import org.junit.Ignore;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.mockito.InjectMocks;
56 import org.mockito.Matchers;
57 import org.mockito.Mock;
58 import org.mockito.Mockito;
59 import org.mockito.MockitoAnnotations;
60 import org.onap.portalapp.portal.core.MockEPUser;
61 import org.onap.portalapp.portal.domain.EPUser;
62 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
63 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
64 import org.onap.portalapp.portal.ecomp.model.SearchResultItem;
65 import org.onap.portalapp.portal.framework.MockitoTestSuite;
66 import org.onap.portalapp.portal.service.AdminRolesService;
67 import org.onap.portalapp.portal.service.AdminRolesServiceImpl;
68 import org.onap.portalapp.portal.service.DashboardSearchService;
69 import org.onap.portalapp.portal.service.DashboardSearchServiceImpl;
70 import org.onap.portalapp.portal.transport.CommonWidget;
71 import org.onap.portalapp.portal.transport.CommonWidgetMeta;
72 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
73 import org.onap.portalapp.util.EPUserUtils;
74 import org.onap.portalsdk.core.domain.AuditLog;
75 import org.onap.portalsdk.core.domain.support.CollaborateList;
76 import org.onap.portalsdk.core.service.AuditService;
77 import org.onap.portalsdk.core.util.SystemProperties;
78 import org.powermock.api.mockito.PowerMockito;
79 import org.powermock.core.classloader.annotations.PrepareForTest;
80 import org.powermock.modules.junit4.PowerMockRunner;
81 import org.springframework.beans.factory.annotation.Autowired;
84 @RunWith(PowerMockRunner.class)
85 @PrepareForTest({EPUserUtils.class, CollaborateList.class, SystemProperties.class, EPCommonSystemProperties.class})
86 public class DashboardControllerTest {
89 DashboardSearchService searchService = new DashboardSearchServiceImpl();
92 DashboardController dashboardController;
95 AdminRolesService adminRolesService = new AdminRolesServiceImpl();
98 AuditService auditService;
102 public void setup() {
103 MockitoAnnotations.initMocks(this);
106 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
108 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
109 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
111 NullPointerException nullPointerException = new NullPointerException();
113 MockEPUser mockUser = new MockEPUser();
115 public CommonWidgetMeta mockCommonWidgetMeta() {
116 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
117 List<CommonWidget> widgetList = new ArrayList<>();
118 CommonWidget commonWidget = new CommonWidget();
119 commonWidget.setId((long) 1);
120 commonWidget.setCategory("test");
121 commonWidget.setHref("testhref");
122 commonWidget.setTitle("testTitle");
123 commonWidget.setContent("testcontent");
124 commonWidget.setEventDate("2017-03-24");
125 commonWidget.setSortOrder(1);
126 widgetList.add(commonWidget);
127 commonWidgetMeta.setItems(widgetList);
129 return commonWidgetMeta;
132 public CommonWidget mockCommonWidget() {
134 CommonWidget commonWidget = new CommonWidget();
135 commonWidget.setId((long) 1);
136 commonWidget.setCategory("test");
137 commonWidget.setHref("testhref");
138 commonWidget.setTitle("testTitle");
139 commonWidget.setContent("testcontent");
140 commonWidget.setEventDate("testDate");
141 commonWidget.setSortOrder(1);
148 public void getWidgetDataTest() throws IOException {
150 String resourceType = null;
151 PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
152 expectedData.setStatus(PortalRestStatusEnum.ERROR);
153 expectedData.setMessage("Unexpected resource type null");
154 expectedData.setResponse(null);
156 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
157 assertEquals(expectedData,actualResponse);
161 public void getWidgetDataTestXSS() {
163 String resourceType = "“><script>alert(“XSS”)</script>";
164 PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<>();
165 expectedData.setStatus(PortalRestStatusEnum.ERROR);
166 expectedData.setMessage("Unexpected resource type “><script>alert(“XSS”)</script>");
167 expectedData.setResponse(null);
169 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
170 assertEquals(expectedData, actualResponse);
174 public void getWidgetDataWithValidResourceTest() throws IOException {
175 String resourceType = "EVENTS";
176 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
177 commonWidgetMeta.setCategory(null);
179 Mockito.when(searchService.getWidgetData(resourceType)).thenReturn(commonWidgetMeta);
180 PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
181 expectedData.setStatus(PortalRestStatusEnum.OK);
182 expectedData.setMessage("success");
183 expectedData.setResponse(commonWidgetMeta);
185 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
186 assertEquals(expectedData,actualResponse);
190 public void saveWidgetDataBulkNullTest() throws IOException {
191 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
192 commonWidgetMeta.setCategory(null);
194 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
195 expectedData.setStatus(PortalRestStatusEnum.ERROR);
196 expectedData.setMessage("ERROR");
197 expectedData.setResponse("Category cannot be null or empty");
199 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
200 assertEquals(expectedData,actualResponse);
204 public void saveWidgetDataBulkXSSTest() {
205 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
206 commonWidgetMeta.setCategory("<script>alert(‘XSS’)</script>");
208 PortalRestResponse<String> expectedData = new PortalRestResponse<>();
209 expectedData.setStatus(PortalRestStatusEnum.ERROR);
210 expectedData.setResponse("ERROR");
211 expectedData.setMessage("Unsafe resource type " + commonWidgetMeta.toString());
213 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
214 assertEquals(expectedData,actualResponse);
218 public void saveWidgetUnexpectedDataBulkTest() throws IOException {
219 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
220 commonWidgetMeta.setCategory("Unexpected Data");
222 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
223 expectedData.setStatus(PortalRestStatusEnum.ERROR);
224 expectedData.setMessage("Unexpected resource type Unexpected Data");
225 expectedData.setResponse(null);
227 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
228 assertEquals(expectedData,actualResponse);
233 public void saveWidgetInvalidDataBulkTest() throws IOException {
234 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
235 commonWidgetMeta.setCategory("EVENTS");
237 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
238 expectedData.setStatus(PortalRestStatusEnum.ERROR);
239 expectedData.setMessage("Invalid category: test");
240 expectedData.setResponse(null);
242 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
243 assertEquals(expectedData,actualResponse);
247 public void saveWidgetDataBulkTest() throws IOException {
249 List<CommonWidget> widgetList = new ArrayList<>();
250 CommonWidget commonWidget = new CommonWidget("EVENTS", "http://test.com", "testTitle", "testcontent", "2017-07-01", 1);
251 widgetList.add(commonWidget);
252 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta("EVENTS", widgetList);
253 commonWidgetMeta.setItems(widgetList);
254 commonWidgetMeta.setCategory("EVENTS");
255 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
256 expectedData.setStatus(PortalRestStatusEnum.OK);
257 expectedData.setMessage("success");
258 expectedData.setResponse("success");
260 Mockito.when(searchService.saveWidgetDataBulk(commonWidgetMeta)).thenReturn("success");
262 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
263 assertEquals(expectedData,actualResponse);
267 public void saveWidgetDataNullTest() throws IOException {
269 CommonWidget commonWidget = mockCommonWidget();
270 commonWidget.setId((long)1);
271 commonWidget.setContent("test");
272 commonWidget.setCategory(null);
273 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
274 expectedData.setStatus(PortalRestStatusEnum.ERROR);
275 expectedData.setMessage("ERROR");
276 expectedData.setResponse("Category cannot be null or empty");
278 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
279 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse);
280 assertEquals(expectedData,actualResponse);
285 public void saveWidgetDataXSSTest() {
287 CommonWidget commonWidget = mockCommonWidget();
288 commonWidget.setId((long)1);
289 commonWidget.setContent("test");
290 commonWidget.setCategory("<form><a href=\"javascript:\\u0061lert(1)\">X");
291 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
292 expectedData.setStatus(PortalRestStatusEnum.ERROR);
293 expectedData.setResponse("ERROR");
294 expectedData.setMessage("Unsafe resource type " + commonWidget.toString());
296 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
297 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse);
298 assertEquals(expectedData,actualResponse);
303 public void saveWidgetDataTitleTest() throws IOException {
304 CommonWidget commonWidget = mockCommonWidget();
305 commonWidget.setId((long)1);
306 commonWidget.setContent("test");
307 commonWidget.setTitle("test");
308 commonWidget.setEventDate("2017-05-06");
309 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
310 expectedData.setStatus(PortalRestStatusEnum.ERROR);
311 expectedData.setMessage("Invalid category: test");
312 expectedData.setResponse(null);
313 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
314 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse);
315 assertEquals(expectedData.getMessage(),actualResponse.getMessage());
319 public void saveWidgetDataErrorTest() throws IOException {
321 CommonWidget commonWidget = mockCommonWidget();
322 commonWidget.setEventDate("2017-03-05");
323 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
324 expectedData.setStatus(PortalRestStatusEnum.ERROR);
325 expectedData.setMessage("Invalid category: test");
326 expectedData.setResponse(null);
327 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
328 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget,mockedRequest, mockedResponse);
329 assertEquals(expectedData,actualResponse);
333 public void saveWidgetDataTest() throws IOException {
335 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
336 List<CommonWidget> widgetList = new ArrayList<>();
337 CommonWidget commonWidget = new CommonWidget();
338 commonWidget.setId((long) 1);
339 commonWidget.setCategory("EVENTS");
340 commonWidget.setHref("http://test.com");
341 commonWidget.setTitle("testTitle");
342 commonWidget.setContent("testcontent");
343 commonWidget.setEventDate("2017-07-01");
344 commonWidget.setSortOrder(1);
345 widgetList.add(commonWidget);
346 commonWidgetMeta.setItems(widgetList);
348 commonWidgetMeta.setCategory("EVENTS");
350 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
351 expectedData.setStatus(PortalRestStatusEnum.OK);
352 expectedData.setMessage("success");
353 expectedData.setResponse("success");
354 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
355 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
356 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse);
357 assertEquals(expectedData,actualResponse);
362 public void deleteWidgetDataTest() throws IOException {
364 CommonWidget commonWidget = mockCommonWidget();
365 commonWidget.setEventDate("2017-03-25");
366 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
367 expectedData.setStatus(PortalRestStatusEnum.OK);
368 expectedData.setMessage("success");
369 expectedData.setResponse(null);
371 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
373 PortalRestResponse<String> actualResponse = dashboardController.deleteWidgetData(commonWidget);
374 assertEquals(expectedData,actualResponse);
379 public void deleteWidgetDataXSSTest() {
381 CommonWidget commonWidget = mockCommonWidget();
382 commonWidget.setCategory("<svg><script x:href='https://dl.dropbox.com/u/13018058/js.js' {Opera}");
383 PortalRestResponse<String> expectedData = new PortalRestResponse<>();
384 expectedData.setStatus(PortalRestStatusEnum.ERROR);
385 expectedData.setMessage("Unsafe resource type " + commonWidget.toString());
386 expectedData.setResponse("ERROR");
387 PortalRestResponse<String> actualResponse = dashboardController.deleteWidgetData(commonWidget);
388 assertEquals(expectedData,actualResponse);
393 public void getActiveUsersTest(){
394 List<String> activeUsers = new ArrayList<>();
395 List<String> expectedUsersList = new ArrayList<>();
396 EPUser user = mockUser.mockEPUser();
397 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
398 String userId = user.getOrgUserId();
399 Mockito.when(searchService.getRelatedUsers(userId)).thenReturn(activeUsers);
400 expectedUsersList= dashboardController.getActiveUsers(mockedRequest);
401 assertEquals(expectedUsersList, activeUsers);
406 public void getActiveUsersExceptionTest(){
407 List<String> activeUsers = new ArrayList<>();
408 List<String> expectedUsersList = new ArrayList<>();
409 EPUser user = mockUser.mockEPUser();
410 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
411 String userId = user.getOrgUserId();
412 Mockito.when(searchService.getRelatedUsers(userId)).thenThrow(nullPointerException);
413 expectedUsersList = dashboardController.getActiveUsers(mockedRequest);
414 assertEquals(expectedUsersList, activeUsers);
418 public void getOnlineUserUpdateRateTest(){
419 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
420 expectedData.setStatus(PortalRestStatusEnum.OK);
421 expectedData.setMessage("success");
422 expectedData.setResponse("{onlineUserUpdateRate=1400000, onlineUserUpdateDuration=1400000}");
424 PowerMockito.mockStatic(SystemProperties.class);
425 PowerMockito.mockStatic(EPCommonSystemProperties.class);
426 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenReturn("1400");
427 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenReturn("1400");
429 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
430 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
434 public void getOnlineUserUpdateRateExceptionTest(){
435 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
436 expectedData.setStatus(PortalRestStatusEnum.ERROR);
437 expectedData.setMessage("java.lang.NullPointerException");
438 expectedData.setResponse(null);
440 PowerMockito.mockStatic(SystemProperties.class);
441 PowerMockito.mockStatic(EPCommonSystemProperties.class);
442 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenThrow(nullPointerException);
443 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenThrow(nullPointerException);
445 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
446 assertEquals(expectedData,actualResponse);
450 public void getWindowWidthThresholdForRightMenuTest(){
451 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
452 expectedData.setStatus(PortalRestStatusEnum.OK);
453 expectedData.setMessage("success");
454 expectedData.setResponse("{windowWidth=1400}");
456 PowerMockito.mockStatic(SystemProperties.class);
457 PowerMockito.mockStatic(EPCommonSystemProperties.class);
458 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenReturn("1400");
460 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
461 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
465 public void getWindowWidthThresholdForRightMenuExceptionTest(){
466 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
467 expectedData.setStatus(PortalRestStatusEnum.ERROR);
468 expectedData.setMessage("java.lang.NullPointerException");
469 expectedData.setResponse(null);
471 PowerMockito.mockStatic(SystemProperties.class);
472 PowerMockito.mockStatic(EPCommonSystemProperties.class);
473 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenThrow(nullPointerException);
475 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
476 assertEquals(expectedData,actualResponse);
480 public void getWindowWidthThresholdForLeftMenuTest(){
481 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
482 expectedData.setStatus(PortalRestStatusEnum.OK);
483 expectedData.setMessage("success");
484 expectedData.setResponse("{windowWidth=1400}");
486 PowerMockito.mockStatic(SystemProperties.class);
487 PowerMockito.mockStatic(EPCommonSystemProperties.class);
488 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenReturn("1400");
490 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
491 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
495 public void getWindowWidthThresholdForLeftMenuExceptionTest(){
496 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
497 expectedData.setStatus(PortalRestStatusEnum.ERROR);
498 expectedData.setMessage("java.lang.NullPointerException");
499 expectedData.setResponse(null);
501 PowerMockito.mockStatic(SystemProperties.class);
502 PowerMockito.mockStatic(EPCommonSystemProperties.class);
503 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenThrow(nullPointerException);
505 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
506 assertEquals(expectedData,actualResponse);
510 public void getActiveUsersNullTest(){
511 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
512 expectedData.setStatus(PortalRestStatusEnum.ERROR);
513 expectedData.setMessage("User object is null? - check logs");
514 expectedData.setResponse(new ArrayList<>());
516 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
517 assertEquals(expectedData,actualResponse);
521 public void activeUsersTest(){
522 EPUser user = mockUser.mockEPUser();
523 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
524 expectedData.setStatus(PortalRestStatusEnum.OK);
525 expectedData.setMessage("success");
526 expectedData.setResponse(new ArrayList<>());
527 PowerMockito.mockStatic(EPUserUtils.class);
528 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
529 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
530 assertEquals(expectedData,actualResponse);
534 public void activeUsersExceptionTest(){
535 EPUser user = mockUser.mockEPUser();
536 user.setLoginId("test");
537 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
538 expectedData.setStatus(PortalRestStatusEnum.ERROR);
539 expectedData.setMessage("null - check logs.");
540 expectedData.setResponse(null);
542 PowerMockito.mockStatic(EPUserUtils.class);
543 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
544 Mockito.when(searchService.getRelatedUsers(user.getLoginId())).thenThrow(nullPointerException);
545 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
546 assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.ERROR) == 0);
549 public void searchPortalTestWhenSearchStringIsNull(){
550 EPUser user = mockUser.mockEPUser();
551 user.setLoginId("test");
553 String searchString = null;
554 //user.setLoginId("test");
555 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
556 expectedData.setStatus(PortalRestStatusEnum.ERROR);
557 expectedData.setMessage("null - check logs.");
558 expectedData.setResponse(Matchers.any());
560 PowerMockito.mockStatic(EPUserUtils.class);
561 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
562 PortalRestResponse<Map<String, List<SearchResultItem>>> expectedResult = new PortalRestResponse<Map<String, List<SearchResultItem>>>();
563 expectedResult.setMessage("null - check logs.");
564 expectedResult.setResponse(null);
565 expectedResult.setStatus(PortalRestStatusEnum.ERROR);
566 //Mockito.doNothing().when(auditService).logActivity(auditLog, null);
568 //Mockito.when(auditService.logActivity(auditLog, null).;
569 //Mockito.when(searchService.searchResults(user.getLoginId(), searchString )).thenReturn((Map<String, List<SearchResultItem>>) expectedResult);
570 PortalRestResponse<Map<String, List<SearchResultItem>>> actualResponse = dashboardController.searchPortal(mockedRequest, null);
571 assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.ERROR) == 0);
575 public void searchPortalTest(){
577 String searchString = null;
578 //user.setLoginId("test");
579 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
580 expectedData.setStatus(PortalRestStatusEnum.ERROR);
581 expectedData.setMessage("null - check logs.");
582 expectedData.setResponse(Matchers.any());
584 PowerMockito.mockStatic(EPUserUtils.class);
585 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
586 PortalRestResponse<Map<String, List<SearchResultItem>>> expectedResult = new PortalRestResponse<Map<String, List<SearchResultItem>>>();
587 expectedResult.setMessage("null - check logs.");
588 expectedResult.setResponse(null);
589 expectedResult.setStatus(PortalRestStatusEnum.ERROR);
590 //Mockito.doNothing().when(auditService).logActivity(auditLog, null);
592 //Mockito.when(auditService.logActivity(auditLog, null).;
593 //Mockito.when(searchService.searchResults(user.getLoginId(), searchString )).thenReturn((Map<String, List<SearchResultItem>>) expectedResult);
594 PortalRestResponse<Map<String, List<SearchResultItem>>> actualResponse = dashboardController.searchPortal(mockedRequest, null);
595 assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.ERROR) == 0);
599 public void searchPortalXSSTest(){
601 String searchString = "\n"
602 + "<form><textarea onkeyup='\\u0061\\u006C\\u0065\\u0072\\u0074(1)'>";
603 PowerMockito.mockStatic(EPUserUtils.class);
604 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
605 PortalRestResponse<Map<String, List<SearchResultItem>>> expectedResult = new PortalRestResponse<>();
606 expectedResult.setMessage("searchPortal: String string is not safe");
607 expectedResult.setResponse(new HashMap<>());
608 expectedResult.setStatus(PortalRestStatusEnum.ERROR);
610 PortalRestResponse<Map<String, List<SearchResultItem>>> actualResponse = dashboardController.searchPortal(mockedRequest, searchString);
611 assertEquals(expectedResult, actualResponse);
615 public void searchPortalTestWithException(){
616 EPUser user = mockUser.mockEPUser();
617 user.setLoginId("test");
620 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
621 String searchString = "test";
622 List<SearchResultItem> searchResultItemList = new ArrayList<SearchResultItem>();
623 SearchResultItem searchResultItem = new SearchResultItem();
625 searchResultItem.setId((long) 1);
626 searchResultItem.setCategory("test");
627 searchResultItem.setName("test_name");
628 searchResultItem.setTarget("test_target");
629 searchResultItem.setUuid("test_UUId");
630 searchResultItemList.add(searchResultItem);
631 Map<String, List<SearchResultItem>> expectedResultMap = new HashMap<String, List<SearchResultItem>>();
632 expectedResultMap.put(searchString, searchResultItemList);
634 AuditLog auditLog = new AuditLog();
635 auditLog.setUserId(1L);
636 auditLog.setActivityCode("test");
637 auditLog.setComments("test");
638 PortalRestResponse<Map<String, List<SearchResultItem>>> expectedResult = new PortalRestResponse<Map<String, List<SearchResultItem>>>();
639 expectedResult.setMessage("null - check logs.");
640 expectedResult.setResponse(null);
641 expectedResult.setStatus(PortalRestStatusEnum.ERROR);
642 //Mockito.doNothing().when(auditService).logActivity(auditLog, null);
644 //Mockito.when(auditService.logActivity(auditLog, null).;
645 Mockito.when(searchService.searchResults(user.getLoginId(), searchString)).thenReturn(expectedResultMap);
646 PortalRestResponse<Map<String, List<SearchResultItem>>> actualResult = dashboardController.searchPortal(mockedRequest, searchString);
648 assertTrue(expectedResult.getStatus().compareTo(PortalRestStatusEnum.ERROR) == 0);
653 public void searchPortalUserNullTest(){
655 PortalRestResponse<Map<String, List<SearchResultItem>>> expectedData = new PortalRestResponse<Map<String, List<SearchResultItem>>>();
656 expectedData.setMessage("searchPortal: User object is null? - check logs");
657 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
658 PortalRestResponse<Map<String, List<SearchResultItem>>> actualData = dashboardController.searchPortal(mockedRequest, null);
659 assertEquals(actualData.getMessage(), expectedData.getMessage());
663 public void searchPortalsearchStringNullTest(){
664 EPUser user = mockUser.mockEPUser();
665 String searchString = null;
666 PortalRestResponse<Map<String, List<SearchResultItem>>> expectedData = new PortalRestResponse<Map<String, List<SearchResultItem>>>();
667 expectedData.setMessage("searchPortal: String string is null");
668 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
669 PortalRestResponse<Map<String, List<SearchResultItem>>> actualData = dashboardController.searchPortal(mockedRequest, searchString);
670 assertEquals(actualData.getMessage(), expectedData.getMessage());
674 public void searchPortalsearchStringTest(){
675 EPUser user = mockUser.mockEPUser();
676 String searchString = "test";
677 PortalRestResponse<Map<String, List<SearchResultItem>>> expectedData = new PortalRestResponse<Map<String, List<SearchResultItem>>>();
678 expectedData.setMessage("success");
679 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
680 Mockito.doNothing().when(auditService).logActivity(null, null);
681 PortalRestResponse<Map<String, List<SearchResultItem>>> actualData = dashboardController.searchPortal(mockedRequest, searchString);
682 assertEquals(actualData.getMessage(), expectedData.getMessage());
686 public void searchPortalsearchStringExceptionTest(){
687 EPUser user = mockUser.mockEPUser();
688 String searchString = "test";
689 PortalRestResponse<Map<String, List<SearchResultItem>>> expectedData = new PortalRestResponse<Map<String, List<SearchResultItem>>>();
690 expectedData.setMessage("searchPortal: String string is null");
691 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
692 Mockito.when(dashboardController.searchPortal(mockedRequest, searchString)).thenThrow(nullPointerException);