Fixed health check issue
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / DashboardControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import static org.junit.Assert.*;
41
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
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;
82
83
84 @RunWith(PowerMockRunner.class)
85 @PrepareForTest({EPUserUtils.class, CollaborateList.class, SystemProperties.class, EPCommonSystemProperties.class})
86 public class DashboardControllerTest {
87         
88         @Mock
89         DashboardSearchService searchService = new DashboardSearchServiceImpl();
90
91         @InjectMocks
92         DashboardController dashboardController;
93
94         @Mock
95         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
96         
97         @Autowired
98         AuditService auditService;
99         
100         
101         @Before
102         public void setup() {
103                 MockitoAnnotations.initMocks(this);
104         }
105
106         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
107
108         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
109         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
110
111         NullPointerException nullPointerException = new NullPointerException();
112         
113         MockEPUser mockUser = new MockEPUser();
114         
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);
128                 
129                 return commonWidgetMeta;
130         }
131         
132         public CommonWidget mockCommonWidget() {
133                 
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);
142             
143             return commonWidget;
144         }
145         
146         
147         @Test
148         public void getWidgetDataTest() throws IOException {
149                 
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);
155                 
156                 PortalRestResponse<CommonWidgetMeta> actualResponse =   dashboardController.getWidgetData(mockedRequest, resourceType);
157                 assertEquals(expectedData,actualResponse);              
158         }
159
160         @Test
161         public void getWidgetDataTestXSS() {
162
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);
168
169                 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
170                 assertEquals(expectedData, actualResponse);
171         }
172
173         @Test
174         public void getWidgetDataWithValidResourceTest() throws IOException {
175                 String resourceType = "EVENTS";
176                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
177                 commonWidgetMeta.setCategory(null);
178                                 
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);
184                 
185                 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
186                 assertEquals(expectedData,actualResponse);
187         }
188                 
189         @Test
190         public void saveWidgetDataBulkNullTest() throws IOException {
191                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
192                 commonWidgetMeta.setCategory(null);
193                 
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");
198                 
199                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
200                 assertEquals(expectedData,actualResponse);              
201         }
202
203         @Test
204         public void saveWidgetDataBulkXSSTest() {
205                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
206                 commonWidgetMeta.setCategory("<script>alert(‘XSS’)</script>");
207
208                 PortalRestResponse<String> expectedData = new PortalRestResponse<>();
209                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
210                 expectedData.setResponse("ERROR");
211                 expectedData.setMessage("Unsafe resource type " + commonWidgetMeta.toString());
212
213                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
214                 assertEquals(expectedData,actualResponse);
215         }
216         
217         @Test
218         public void saveWidgetUnexpectedDataBulkTest() throws IOException {
219                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
220                 commonWidgetMeta.setCategory("Unexpected Data");
221                 
222                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
223                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
224                 expectedData.setMessage("Unexpected resource type Unexpected Data");
225                 expectedData.setResponse(null);
226                 
227                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
228                 assertEquals(expectedData,actualResponse);
229                 
230         }
231                 
232         @Test
233         public void saveWidgetInvalidDataBulkTest() throws IOException {
234                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
235                 commonWidgetMeta.setCategory("EVENTS");
236                 
237                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
238                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
239                 expectedData.setMessage("Invalid category: test");
240                 expectedData.setResponse(null);
241                 
242                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
243                 assertEquals(expectedData,actualResponse);              
244         }
245         
246         @Test
247         public void saveWidgetDataBulkTest() throws IOException {
248                 
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");
259                 
260                 Mockito.when(searchService.saveWidgetDataBulk(commonWidgetMeta)).thenReturn("success");
261                 
262                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
263                 assertEquals(expectedData,actualResponse);              
264         }
265         
266         @Test
267         public void saveWidgetDataNullTest() throws IOException {
268                                 
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");
277
278                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
279                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse);
280                 assertEquals(expectedData,actualResponse);
281                 
282         }
283
284         @Test
285         public void saveWidgetDataXSSTest() {
286
287                 CommonWidget commonWidget = mockCommonWidget();
288                 commonWidget.setId((long)1);
289                 commonWidget.setContent("test");
290                 commonWidget.setCategory("<form><a href=\"javascript:\\u0061lert&#x28;1&#x29;\">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());
295
296                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
297                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse);
298                 assertEquals(expectedData,actualResponse);
299
300         }
301         
302         @Test
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());
316         }
317         
318         @Test
319         public void saveWidgetDataErrorTest() throws IOException {
320                                 
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);              
330         }
331         
332         @Test
333         public void saveWidgetDataTest() throws IOException {
334                                 
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);
347                 
348                 commonWidgetMeta.setCategory("EVENTS");
349                 
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);
358                 
359         }
360         
361         @Test
362         public void deleteWidgetDataTest() throws IOException {
363                                 
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); 
370                 
371                 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
372                 
373                 PortalRestResponse<String> actualResponse = dashboardController.deleteWidgetData(commonWidget);
374                 assertEquals(expectedData,actualResponse);
375                 
376         }
377
378         @Test
379         public void deleteWidgetDataXSSTest() {
380
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);
389
390         }
391                 
392         @Test
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);
402         }
403         
404         
405         @Test
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);
415         }
416                 
417         @Test
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}"); 
423                 
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");
428                 
429                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
430                 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
431         }
432         
433         @Test
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); 
439                 
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);
444                 
445                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
446                 assertEquals(expectedData,actualResponse);
447         }
448         
449         @Test
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}"); 
455                 
456                 PowerMockito.mockStatic(SystemProperties.class);
457                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
458                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenReturn("1400");
459                 
460                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
461                 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
462         }
463         
464         @Test
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); 
470                 
471                 PowerMockito.mockStatic(SystemProperties.class);
472                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
473                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenThrow(nullPointerException);
474                 
475                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
476                 assertEquals(expectedData,actualResponse);
477         }
478         
479         @Test
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}"); 
485                 
486                 PowerMockito.mockStatic(SystemProperties.class);
487                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
488                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenReturn("1400");
489                 
490                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
491                 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
492         }
493         
494         @Test
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); 
500                 
501                 PowerMockito.mockStatic(SystemProperties.class);
502                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
503                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenThrow(nullPointerException);
504                 
505                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
506                 assertEquals(expectedData,actualResponse);
507         }
508                 
509         @Test
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<>()); 
515
516                 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
517                 assertEquals(expectedData,actualResponse);
518         }
519         
520         @Test
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);
531         }
532         
533         @Test
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);  
541                 
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);
547         }
548         @Test
549         public void searchPortalTestWhenSearchStringIsNull(){
550                 EPUser user = mockUser.mockEPUser();
551                 user.setLoginId("test");
552                 user.setId(1L);
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());  
559                 
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);
567
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);
572         }
573         
574         @Test
575         public void searchPortalTest(){
576                 EPUser user = null;
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());  
583                 
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);
591
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);
596         }
597
598         @Test
599         public void searchPortalXSSTest(){
600                 EPUser user = null;
601                 String searchString = "\n"
602                         + "<form><textarea &#13; onkeyup='\\u0061\\u006C\\u0065\\u0072\\u0074&#x28;1&#x29;'>";
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);
609
610                 PortalRestResponse<Map<String, List<SearchResultItem>>> actualResponse = dashboardController.searchPortal(mockedRequest, searchString);
611                 assertEquals(expectedResult, actualResponse);
612         }
613
614         @Test
615         public void searchPortalTestWithException(){
616                 EPUser user = mockUser.mockEPUser();
617                 user.setLoginId("test");
618                 user.setId(1L);
619                 
620                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
621                 String searchString = "test";
622                 List<SearchResultItem> searchResultItemList = new ArrayList<SearchResultItem>();
623                 SearchResultItem searchResultItem = new SearchResultItem();
624
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);
633                 
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);
643
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);
647                                 
648                 assertTrue(expectedResult.getStatus().compareTo(PortalRestStatusEnum.ERROR) == 0);
649
650         }
651         
652         @Test
653         public void searchPortalUserNullTest(){
654                 EPUser user = null;
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());
660         }
661         
662         @Test
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());
671         }
672         @Ignore
673         @Test
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());
683         }
684         //@Ignore
685         @Test
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);
693         }
694         
695         
696 }