0e8b74d8ae2a1257122bc6ebef49edf189a1e455
[portal.git] / ecomp-portal-BE-common / src / test / java / org / openecomp / portalapp / portal / controller / DashboardControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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.openecomp.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.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
58 import org.openecomp.portalapp.portal.controller.DashboardController;
59 import org.openecomp.portalapp.portal.core.MockEPUser;
60 import org.openecomp.portalapp.portal.domain.EPUser;
61 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
62 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
63 import org.openecomp.portalapp.portal.service.DashboardSearchService;
64 import org.openecomp.portalapp.portal.service.DashboardSearchServiceImpl;
65 import org.openecomp.portalapp.portal.transport.CommonWidget;
66 import org.openecomp.portalapp.portal.transport.CommonWidgetMeta;
67 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
68 import org.openecomp.portalapp.util.EPUserUtils;
69 import org.openecomp.portalsdk.core.domain.support.CollaborateList;
70 import org.openecomp.portalsdk.core.util.SystemProperties;
71 import org.powermock.api.mockito.PowerMockito;
72 import org.powermock.core.classloader.annotations.PrepareForTest;
73 import org.powermock.modules.junit4.PowerMockRunner;
74
75
76 @RunWith(PowerMockRunner.class)
77 @PrepareForTest({EPUserUtils.class, CollaborateList.class, SystemProperties.class, EPCommonSystemProperties.class})
78 public class DashboardControllerTest {
79         
80         @Mock
81         DashboardSearchService searchService = new DashboardSearchServiceImpl();
82         
83         @InjectMocks
84         DashboardController dashboardController = new DashboardController();
85
86         @Before
87         public void setup() {
88                 MockitoAnnotations.initMocks(this);
89         }
90
91         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
92
93         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
94         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
95
96         NullPointerException nullPointerException = new NullPointerException();
97         
98         MockEPUser mockUser = new MockEPUser();
99         
100         public CommonWidgetMeta mockCommonWidgetMeta() {
101                 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
102                 List<CommonWidget> widgetList = new ArrayList<>();
103                 CommonWidget commonWidget = new CommonWidget();         
104                 commonWidget.setId((long) 1);
105                 commonWidget.setCategory("test");
106                 commonWidget.setHref("testhref");
107                 commonWidget.setTitle("testTitle");
108             commonWidget.setContent("testcontent");
109             commonWidget.setEventDate("testDate");
110             commonWidget.setSortOrder(1);                   
111                 widgetList.add(commonWidget);           
112                 commonWidgetMeta.setItems(widgetList);
113                 
114                 return commonWidgetMeta;
115         }
116         
117         public CommonWidget mockCommonWidget() {
118                 
119                 CommonWidget commonWidget = new CommonWidget();         
120                 commonWidget.setId((long) 1);
121                 commonWidget.setCategory("test");
122                 commonWidget.setHref("testhref");
123                 commonWidget.setTitle("testTitle");
124             commonWidget.setContent("testcontent");
125             commonWidget.setEventDate("testDate");
126             commonWidget.setSortOrder(1);
127             
128             return commonWidget;
129         }
130         
131         
132         @Test
133         public void getWidgetDataTest() throws IOException {
134                 
135                 String resourceType = null;
136                 PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
137                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
138                 expectedData.setMessage("Unexpected resource type null");
139                 expectedData.setResponse(null);
140                 
141                 PortalRestResponse<CommonWidgetMeta> actualResponse =   dashboardController.getWidgetData(mockedRequest, resourceType);
142                 assertEquals(expectedData,actualResponse);              
143         }       
144         
145         @Test
146         public void getWidgetDataWithValidResourceTest() throws IOException {
147                 String resourceType = "EVENTS";
148                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
149                 commonWidgetMeta.setCategory(null);
150                                 
151                 Mockito.when(searchService.getWidgetData(resourceType)).thenReturn(commonWidgetMeta);
152                 PortalRestResponse<CommonWidgetMeta> expectedData = new PortalRestResponse<CommonWidgetMeta>();
153                 expectedData.setStatus(PortalRestStatusEnum.OK);
154                 expectedData.setMessage("success");
155                 expectedData.setResponse(commonWidgetMeta);
156                 
157                 PortalRestResponse<CommonWidgetMeta> actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType);
158                 System.out.println(actualResponse);
159                 assertEquals(expectedData,actualResponse);
160         }
161                 
162         @Test
163         public void saveWidgetDataBulkNullTest() throws IOException {
164                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
165                 commonWidgetMeta.setCategory(null);
166                 
167                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
168                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
169                 expectedData.setMessage("ERROR");
170                 expectedData.setResponse("Category cannot be null or empty");
171                 
172                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
173                 assertEquals(expectedData,actualResponse);              
174         }
175         
176         @Test
177         public void saveWidgetUnexpectedDataBulkTest() throws IOException {
178                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
179                 commonWidgetMeta.setCategory("Unexpected Data");
180                 
181                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
182                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
183                 expectedData.setMessage("Unexpected resource type Unexpected Data");
184                 expectedData.setResponse(null);
185                 
186                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
187                 assertEquals(expectedData,actualResponse);
188                 
189         }
190                 
191         @Test
192         public void saveWidgetInvalidDataBulkTest() throws IOException {
193                 CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta();
194                 commonWidgetMeta.setCategory("EVENTS");
195                 
196                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
197                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
198                 expectedData.setMessage("Invalid category: test");
199                 expectedData.setResponse(null);
200                 
201                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
202                 assertEquals(expectedData,actualResponse);              
203         }
204         
205         @Test
206         public void saveWidgetDataBulkTest() throws IOException {
207                 
208                 List<CommonWidget> widgetList = new ArrayList<>();              
209                 CommonWidget commonWidget = new CommonWidget("EVENTS", "http://test.com", "testTitle", "testcontent", "2017-07-01", 1);
210                 widgetList.add(commonWidget);
211                 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta("EVENTS", widgetList);
212                 
213                                     
214                                 
215         /*      commonWidgetMeta.setItems(widgetList);
216                 
217                 commonWidgetMeta.setCategory("EVENTS");*/
218                 
219                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
220                 expectedData.setStatus(PortalRestStatusEnum.OK);
221                 expectedData.setMessage("success");
222                 expectedData.setResponse("success");
223                 
224                 Mockito.when(searchService.saveWidgetDataBulk(commonWidgetMeta)).thenReturn("success");
225                 
226                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta);
227                 assertEquals(expectedData,actualResponse);              
228         }
229         
230         @Test
231         public void saveWidgetDataNullTest() throws IOException {
232                                 
233                 CommonWidget commonWidget = mockCommonWidget(); 
234                 commonWidget.setId((long)1);
235                 commonWidget.setContent("test");
236                 commonWidget.setCategory(null);
237                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
238                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
239                 expectedData.setMessage("ERROR");
240                 expectedData.setResponse("Category cannot be null or empty");
241                 
242                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget);
243                 assertEquals(expectedData,actualResponse);
244                 
245         }
246         
247         @Test
248         public void saveWidgetDataErrorTest() throws IOException {
249                                 
250                 CommonWidget commonWidget = mockCommonWidget();         
251                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
252                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
253                 expectedData.setMessage("Invalid category: test");
254                 expectedData.setResponse(null); 
255                 
256                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget);
257                 assertEquals(expectedData,actualResponse);              
258         }
259         
260         @Test
261         public void saveWidgetDataTest() throws IOException {
262                                 
263                 CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta();
264                 List<CommonWidget> widgetList = new ArrayList<>();
265                 CommonWidget commonWidget = new CommonWidget();         
266                 commonWidget.setId((long) 1);
267                 commonWidget.setCategory("EVENTS");
268                 commonWidget.setHref("http://test.com");
269                 commonWidget.setTitle("testTitle");
270             commonWidget.setContent("testcontent");
271             commonWidget.setEventDate("2017-07-01");
272             commonWidget.setSortOrder(1);                   
273                 widgetList.add(commonWidget);           
274                 commonWidgetMeta.setItems(widgetList);
275                 
276                 commonWidgetMeta.setCategory("EVENTS");
277                 
278                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
279                 expectedData.setStatus(PortalRestStatusEnum.OK);
280                 expectedData.setMessage("success");
281                 expectedData.setResponse("success"); 
282                 
283                 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
284                 
285                 PortalRestResponse<String> actualResponse = dashboardController.saveWidgetData(commonWidget);
286                 assertEquals(expectedData,actualResponse);
287                 
288         }
289         
290         @Test
291         public void deleteWidgetDataTest() throws IOException {
292                                 
293                 CommonWidget commonWidget = mockCommonWidget();
294                 
295                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
296                 expectedData.setStatus(PortalRestStatusEnum.OK);
297                 expectedData.setMessage("success");
298                 expectedData.setResponse(null); 
299                 
300                 Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success");
301                 
302                 PortalRestResponse<String> actualResponse = dashboardController.deleteWidgetData(commonWidget);
303                 assertEquals(expectedData,actualResponse);
304                 
305         }
306                 
307         @Test
308         public void getActiveUsersTest(){
309                 List<String> activeUsers = new ArrayList<>();
310                 List<String> expectedUsersList = new ArrayList<>();
311                 EPUser user = mockUser.mockEPUser();
312                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
313                 String userId = user.getOrgUserId();
314                 Mockito.when(searchService.getRelatedUsers(userId)).thenReturn(activeUsers);
315                 expectedUsersList=      dashboardController.getActiveUsers(mockedRequest);
316                 assertEquals(expectedUsersList, activeUsers);
317         }
318         
319         
320         @Test
321         public void getActiveUsersExceptionTest(){
322                 List<String> activeUsers = new ArrayList<>();
323                 List<String> expectedUsersList = new ArrayList<>();
324                 EPUser user = mockUser.mockEPUser();
325                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
326                 String userId = user.getOrgUserId();
327                 Mockito.when(searchService.getRelatedUsers(userId)).thenThrow(nullPointerException);
328                 expectedUsersList = dashboardController.getActiveUsers(mockedRequest);
329                 assertEquals(expectedUsersList, activeUsers);
330         }
331                 
332         @Test
333         public void getOnlineUserUpdateRateTest(){
334                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
335                 expectedData.setStatus(PortalRestStatusEnum.OK);
336                 expectedData.setMessage("success");
337                 expectedData.setResponse("{onlineUserUpdateRate=1400000, onlineUserUpdateDuration=1400000}"); 
338                 
339                 PowerMockito.mockStatic(SystemProperties.class);
340                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
341                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenReturn("1400"); 
342                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenReturn("1400");
343                 
344                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
345                 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
346         }
347         
348         @Test
349         public void getOnlineUserUpdateRateExceptionTest(){
350                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
351                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
352                 expectedData.setMessage("java.lang.NullPointerException");
353                 expectedData.setResponse(null); 
354                 
355                 PowerMockito.mockStatic(SystemProperties.class);
356                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
357                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_RATE)).thenThrow(nullPointerException); 
358                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.ONLINE_USER_UPDATE_DURATION)).thenThrow(nullPointerException);
359                 
360                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getOnlineUserUpdateRate(mockedRequest);
361                 assertEquals(expectedData,actualResponse);
362         }
363         
364         @Test
365         public void getWindowWidthThresholdForRightMenuTest(){
366                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
367                 expectedData.setStatus(PortalRestStatusEnum.OK);
368                 expectedData.setMessage("success");
369                 expectedData.setResponse("{windowWidth=1400}"); 
370                 
371                 PowerMockito.mockStatic(SystemProperties.class);
372                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
373                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenReturn("1400");
374                 
375                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
376                 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
377         }
378         
379         @Test
380         public void getWindowWidthThresholdForRightMenuExceptionTest(){
381                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
382                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
383                 expectedData.setMessage("java.lang.NullPointerException");
384                 expectedData.setResponse(null); 
385                 
386                 PowerMockito.mockStatic(SystemProperties.class);
387                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
388                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_RIGHT_MENU)).thenThrow(nullPointerException);
389                 
390                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForRightMenu(mockedRequest);
391                 assertEquals(expectedData,actualResponse);
392         }
393         
394         @Test
395         public void getWindowWidthThresholdForLeftMenuTest(){
396                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
397                 expectedData.setStatus(PortalRestStatusEnum.OK);
398                 expectedData.setMessage("success");             
399                 expectedData.setResponse("{windowWidth=1400}"); 
400                 
401                 PowerMockito.mockStatic(SystemProperties.class);
402                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
403                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenReturn("1400");
404                 
405                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
406                 assertEquals(expectedData.getStatus(),actualResponse.getStatus());
407         }
408         
409         @Test
410         public void getWindowWidthThresholdForLeftMenuExceptionTest(){
411                 PortalRestResponse<String> expectedData = new PortalRestResponse<String>();
412                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
413                 expectedData.setMessage("java.lang.NullPointerException");
414                 expectedData.setResponse(null); 
415                 
416                 PowerMockito.mockStatic(SystemProperties.class);
417                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
418                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WINDOW_WIDTH_THRESHOLD_LEFT_MENU)).thenThrow(nullPointerException);
419                 
420                 PortalRestResponse<Map<String, String>> actualResponse = dashboardController.getWindowWidthThresholdForLeftMenu(mockedRequest);
421                 assertEquals(expectedData,actualResponse);
422         }
423                 
424         @Test
425         public void getActiveUsersNullTest(){
426                 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
427                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
428                 expectedData.setMessage("User object is null? - check logs");
429                 expectedData.setResponse(new ArrayList<>()); 
430
431                 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
432                 assertEquals(expectedData,actualResponse);
433         }
434         
435         @Test
436         public void activeUsersTest(){
437                 EPUser user = mockUser.mockEPUser();
438                 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
439                 expectedData.setStatus(PortalRestStatusEnum.OK);
440                 expectedData.setMessage("success");
441                 expectedData.setResponse(new ArrayList<>()); 
442                 PowerMockito.mockStatic(EPUserUtils.class);
443                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
444                 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
445                 assertEquals(expectedData,actualResponse);
446         }
447         
448         @Test
449         public void activeUsersExceptionTest(){
450                 EPUser user = mockUser.mockEPUser();
451                 user.setLoginId("test");
452                 PortalRestResponse<List<String>> expectedData = new PortalRestResponse<List<String>>();
453                 expectedData.setStatus(PortalRestStatusEnum.ERROR);
454                 expectedData.setMessage("null - check logs.");
455                 expectedData.setResponse(null);  
456                 
457                 PowerMockito.mockStatic(EPUserUtils.class);
458                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
459                 Mockito.when(searchService.getRelatedUsers(user.getLoginId())).thenThrow(nullPointerException);
460                 PortalRestResponse<List<String>> actualResponse = dashboardController.activeUsers(mockedRequest);
461                 assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.ERROR) == 0);
462         }
463 }