Security/ Package Name changes
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.List;
45 import java.util.Map;
46
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49
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;
77
78
79 @RunWith(PowerMockRunner.class)
80 @PrepareForTest({EPUserUtils.class, CollaborateList.class, SystemProperties.class, EPCommonSystemProperties.class})
81 public class DashboardControllerTest {
82         
83         @Mock
84         DashboardSearchService searchService = new DashboardSearchServiceImpl();
85         
86         @InjectMocks
87         DashboardController dashboardController = new DashboardController();
88
89         @Mock
90         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
91         @Before
92         public void setup() {
93                 MockitoAnnotations.initMocks(this);
94         }
95
96         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
97
98         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
99         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
100
101         NullPointerException nullPointerException = new NullPointerException();
102         
103         MockEPUser mockUser = new MockEPUser();
104         
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);
118                 
119                 return commonWidgetMeta;
120         }
121         
122         public CommonWidget mockCommonWidget() {
123                 
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);
132             
133             return commonWidget;
134         }
135         
136         
137         @Test
138         public void getWidgetDataTest() throws IOException {
139                 
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);
145                 
146                 PortalRestResponse<CommonWidgetMeta> actualResponse =   dashboardController.getWidgetData(mockedRequest, resourceType);
147                 assertEquals(expectedData,actualResponse);              
148         }       
149         
150         @Test
151         public void getWidgetDataWithValidResourceTest() throws IOException {
152                 String resourceType = "EVENTS";
153                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
154                 commonWidgetMeta.setCategory(null);
155                                 
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);
161                 
162                 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
163                 assertEquals(expectedData,actualResponse);
164         }
165                 
166         @Test
167         public void saveWidgetDataBulkNullTest() throws IOException {
168                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
169                 commonWidgetMeta.setCategory(null);
170                 
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");
175                 
176                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
177                 assertEquals(expectedData,actualResponse);              
178         }
179         
180         @Test
181         public void saveWidgetUnexpectedDataBulkTest() throws IOException {
182                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
183                 commonWidgetMeta.setCategory("Unexpected Data");
184                 
185                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
186                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
187                 expectedData.setMessage("Unexpected resource type Unexpected Data");
188                 expectedData.setResponse(null);
189                 
190                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
191                 assertEquals(expectedData,actualResponse);
192                 
193         }
194                 
195         @Test
196         public void saveWidgetInvalidDataBulkTest() throws IOException {
197                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
198                 commonWidgetMeta.setCategory("EVENTS");
199                 
200                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
201                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
202                 expectedData.setMessage("Invalid category: test");
203                 expectedData.setResponse(null);
204                 
205                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
206                 assertEquals(expectedData,actualResponse);              
207         }
208         
209         @Test
210         public void saveWidgetDataBulkTest() throws IOException {
211                 
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);
216                 
217                                     
218                                 
219         /*      commonWidgetMeta.setItems(widgetList);
220                 
221                 commonWidgetMeta.setCategory("EVENTS");*/
222                 
223                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
224                 expectedData.setStatus(PortalRestStatusEnum.OK);
225                 expectedData.setMessage("success");
226                 expectedData.setResponse("success");
227                 
228                 Mockito.when(searchService.saveWidgetDataBulk(commonWidgetMeta)).thenReturn("success");
229                 
230                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
231                 assertEquals(expectedData,actualResponse);              
232         }
233         
234         @Test
235         public void saveWidgetDataNullTest() throws IOException {
236                                 
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");
245                 
246                 Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
247                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse);
248                 assertEquals(expectedData,actualResponse);
249                 
250         }
251         
252         @Test
253         public void saveWidgetDataErrorTest() throws IOException {
254                                 
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);              
263         }
264         
265         @Test
266         public void saveWidgetDataTest() throws IOException {
267                                 
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);
280                 
281                 commonWidgetMeta.setCategory("EVENTS");
282                 
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);
291                 
292         }
293         
294         @Test
295         public void deleteWidgetDataTest() throws IOException {
296                                 
297                 CommonWidget commonWidget = mockCommonWidget();
298                 
299                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
300                 expectedData.setStatus(PortalRestStatusEnum.OK);
301                 expectedData.setMessage("success");
302                 expectedData.setResponse(null); 
303                 
304                 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
305                 
306                 PortalRestResponse<String> actualResponse = dashboardController.deleteWidgetData(commonWidget);
307                 assertEquals(expectedData,actualResponse);
308                 
309         }
310                 
311         @Test
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);
321         }
322         
323         
324         @Test
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);
334         }
335                 
336         @Test
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}"); 
342                 
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");
347                 
348                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
349                 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
350         }
351         
352         @Test
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); 
358                 
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);
363                 
364                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
365                 assertEquals(expectedData,actualResponse);
366         }
367         
368         @Test
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}"); 
374                 
375                 PowerMockito.mockStatic(SystemProperties.class);
376                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
377                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenReturn("1400");
378                 
379                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
380                 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
381         }
382         
383         @Test
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); 
389                 
390                 PowerMockito.mockStatic(SystemProperties.class);
391                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
392                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenThrow(nullPointerException);
393                 
394                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
395                 assertEquals(expectedData,actualResponse);
396         }
397         
398         @Test
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}"); 
404                 
405                 PowerMockito.mockStatic(SystemProperties.class);
406                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
407                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenReturn("1400");
408                 
409                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
410                 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
411         }
412         
413         @Test
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); 
419                 
420                 PowerMockito.mockStatic(SystemProperties.class);
421                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
422                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenThrow(nullPointerException);
423                 
424                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
425                 assertEquals(expectedData,actualResponse);
426         }
427                 
428         @Test
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<>()); 
434
435                 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
436                 assertEquals(expectedData,actualResponse);
437         }
438         
439         @Test
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);
450         }
451         
452         @Test
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);  
460                 
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);
466         }
467 }