Fixed health check issue
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / UserNotificationControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017-2018 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.assertTrue;
41
42 import java.util.ArrayList;
43 import java.util.Calendar;
44 import java.util.Date;
45 import java.util.List;
46
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49 import javax.servlet.http.HttpSession;
50
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.InjectMocks;
55 import org.mockito.Matchers;
56 import org.mockito.Mock;
57 import org.mockito.Mockito;
58 import org.mockito.MockitoAnnotations;
59 import org.onap.portalapp.portal.core.MockEPUser;
60 import org.onap.portalapp.portal.domain.EPUser;
61 import org.onap.portalapp.portal.domain.EcompAppRole;
62 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
63 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
64 import org.onap.portalapp.portal.framework.MockitoTestSuite;
65 import org.onap.portalapp.portal.service.FunctionalMenuService;
66 import org.onap.portalapp.portal.service.FunctionalMenuServiceImpl;
67 import org.onap.portalapp.portal.service.UserNotificationService;
68 import org.onap.portalapp.portal.service.UserNotificationServiceImpl;
69 import org.onap.portalapp.portal.transport.EpNotificationItem;
70 import org.onap.portalapp.portal.transport.EpNotificationItemVO;
71 import org.onap.portalapp.portal.transport.EpRoleNotificationItem;
72 import org.onap.portalapp.portal.transport.FunctionalMenuRole;
73 import org.onap.portalapp.util.EPUserUtils;
74 import org.onap.portalsdk.core.util.SystemProperties;
75 import org.onap.portalsdk.core.web.support.UserUtils;
76 import org.powermock.api.mockito.PowerMockito;
77 import org.powermock.core.classloader.annotations.PrepareForTest;
78 import org.powermock.modules.junit4.PowerMockRunner;
79
80 @RunWith(PowerMockRunner.class)
81 @PrepareForTest(UserUtils.class)
82 public class UserNotificationControllerTest {
83
84         @Mock
85         FunctionalMenuService functionalMenuService = new FunctionalMenuServiceImpl();
86
87         @Mock
88         UserNotificationService userNotificationService = new UserNotificationServiceImpl();
89
90         @InjectMocks
91         UserNotificationController userNotificationController = new UserNotificationController();
92
93         @Before
94         public void setup() {
95                 MockitoAnnotations.initMocks(this);
96         }
97
98         MockEPUser mockUser = new MockEPUser();
99         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
100
101         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
102         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
103         NullPointerException nullPointerException = new NullPointerException();
104
105         @Test
106         public void getMenuIdRoleIdTest() {
107                 List<FunctionalMenuRole> expectedMenuRoleList = new ArrayList<FunctionalMenuRole>();
108                 FunctionalMenuRole functionalMenuRole = new FunctionalMenuRole();
109                 functionalMenuRole.setId(new Integer(99999999));
110                 functionalMenuRole.setMenuId((long) 137);
111                 functionalMenuRole.setAppId(new Integer(456));
112                 functionalMenuRole.setRoleId(new Integer(6214));
113                 expectedMenuRoleList.add(functionalMenuRole);
114                 List<FunctionalMenuRole> actualFunctionalMenuRoleList = null;
115                 Mockito.when(functionalMenuService.getFunctionalMenuRole()).thenReturn(expectedMenuRoleList);
116                 actualFunctionalMenuRoleList = userNotificationController.getMenuIdRoleId(mockedRequest, mockedResponse);
117                 assertTrue(actualFunctionalMenuRoleList.equals(expectedMenuRoleList));
118
119         }
120
121         @Test
122         public void getNotificationsTest() {
123                 EPUser user = mockUser.mockEPUser();
124                 HttpSession session = mockedRequest.getSession();
125                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
126                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
127                 List<EpNotificationItem> expectedEpNotificationList = new ArrayList<EpNotificationItem>();
128                 EpNotificationItem epNotificationItem = new EpNotificationItem();
129                 epNotificationItem.setNotificationId((long) 200);
130                 expectedEpNotificationList.add(epNotificationItem);
131                 PortalRestResponse<List<EpNotificationItem>> expectedportalRestResponse = new PortalRestResponse<List<EpNotificationItem>>();
132                 expectedportalRestResponse.setMessage("success");
133                 expectedportalRestResponse.setResponse(expectedEpNotificationList);
134                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
135                 PortalRestResponse<List<EpNotificationItem>> actualPortalRestResponse = null;
136                 Mockito.when(userNotificationService.getNotifications(user.getId())).thenReturn(expectedEpNotificationList);
137                 actualPortalRestResponse = userNotificationController.getNotifications(mockedRequest, mockedResponse);
138                 assertTrue(expectedportalRestResponse.equals(actualPortalRestResponse));
139
140         }
141
142         @Test
143         public void getNotificationsCatchesExceptionTest() {
144                 EPUser user = mockUser.mockEPUser();
145                 HttpSession session = mockedRequest.getSession();
146                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
147                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
148                 List<EpNotificationItem> expectedEpNotificationList = null;
149                 PortalRestResponse<List<EpNotificationItem>> expectedportalRestResponse = new PortalRestResponse<List<EpNotificationItem>>();
150                 expectedportalRestResponse.setMessage(null);
151                 expectedportalRestResponse.setResponse(expectedEpNotificationList);
152                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
153                 PortalRestResponse<List<EpNotificationItem>> actualPortalRestResponse = null;
154                 Mockito.when(userNotificationService.getNotifications(user.getId())).thenThrow(new NullPointerException());
155                 actualPortalRestResponse = userNotificationController.getNotifications(mockedRequest, mockedResponse);
156                 assertTrue(expectedportalRestResponse.equals(actualPortalRestResponse));
157         }
158
159         @Test
160         public void getAdminNotificationsTest() {
161                 EPUser user = mockUser.mockEPUser();
162                 HttpSession session = mockedRequest.getSession();
163                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
164                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
165                 List<EpNotificationItemVO> actualEpNotificationsList = new ArrayList<EpNotificationItemVO>();
166                 List<EpNotificationItemVO> expectedEpNotificationsList = new ArrayList<EpNotificationItemVO>();
167                 EpNotificationItemVO epNotificationItemVO = new EpNotificationItemVO();
168                 epNotificationItemVO.setId((long) 1);
169                 expectedEpNotificationsList.add(epNotificationItemVO);
170                 Mockito.when(userNotificationService.getAdminNotificationVOS(Matchers.anyLong())).thenReturn(expectedEpNotificationsList);
171                 actualEpNotificationsList = userNotificationController.getAdminNotifications(mockedRequest, mockedResponse);
172                 assertTrue(actualEpNotificationsList.equals(expectedEpNotificationsList));
173         }
174
175         @Test
176         public void saveTestWhenNotificationIsNull() throws Exception {
177                 EPUser user = mockUser.mockEPUser();
178                 HttpSession session = mockedRequest.getSession();
179                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
180                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
181
182                 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
183                 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
184                 expectedPortalRestResponse.setMessage("FAILURE");
185                 expectedPortalRestResponse.setResponse("Notification Header cannot be null or empty");
186                 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
187
188                 EpNotificationItem notificationItem = null;
189                 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
190                 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
191         }
192
193         @Test
194         public void saveTestWhenEndTimeIsGreater() throws Exception {
195                 EPUser user = mockUser.mockEPUser();
196                 HttpSession session = mockedRequest.getSession();
197                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
198                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
199                 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
200                 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
201                 expectedPortalRestResponse.setMessage("FAILURE");
202                 expectedPortalRestResponse.setResponse("End Time should be greater than  start time");
203                 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
204                 EpNotificationItem notificationItem = new EpNotificationItem();
205                 notificationItem.setNotificationId((long) 1);
206                 notificationItem.setMsgHeader("Test");
207                 Date currentDate = new Date();
208                 Calendar c = Calendar.getInstance();
209                 c.setTime(currentDate);
210                 c.add(Calendar.DATE, 1);
211                 Date currentDatePlusOne = c.getTime();
212                 notificationItem.setStartTime(currentDatePlusOne);
213                 notificationItem.setEndTime(currentDate);
214
215                 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
216                 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
217
218         }
219
220         @Test
221         public void saveTestWhenNoRoleIDExists() throws Exception {
222                 EPUser user = mockUser.mockEPUser();
223                 HttpSession session = mockedRequest.getSession();
224                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
225                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
226                 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
227                 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
228                 expectedPortalRestResponse.setMessage("FAILURE");
229                 expectedPortalRestResponse.setResponse("No Roles Ids Exist for the selected Roles");
230                 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
231                 EpNotificationItem notificationItem = new EpNotificationItem();
232                 notificationItem.setNotificationId((long) 1);
233                 notificationItem.setMsgHeader("Test");
234                 notificationItem.setIsForAllRoles("N");
235                 Date currentDate = new Date();
236                 Calendar c = Calendar.getInstance();
237                 c.setTime(currentDate);
238                 c.add(Calendar.DATE, 1);
239                 Date currentDatePlusOne = c.getTime();
240                 notificationItem.setStartTime(currentDate);
241                 notificationItem.setEndTime(currentDatePlusOne);
242                 List<Long> roleList = new ArrayList<Long>();
243                 notificationItem.setRoleIds(roleList);
244                 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
245                 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
246         }
247
248         @Test
249         public void saveTest() throws Exception {
250                 EPUser user = mockUser.mockEPUser();
251                 HttpSession session = mockedRequest.getSession();
252                 session.setAttribute("user", user);
253                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
254                 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
255                 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
256                 expectedPortalRestResponse.setMessage("SUCCESS");
257                 expectedPortalRestResponse.setResponse("");
258                 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.OK);
259                 EpNotificationItem notificationItem = new EpNotificationItem();
260                 notificationItem.setNotificationId((long) 1);
261                 notificationItem.setMsgHeader("Test");
262                 notificationItem.setIsForAllRoles("Y");
263                 Date currentDate = new Date();
264                 Calendar c = Calendar.getInstance();
265                 c.setTime(currentDate);
266                 c.add(Calendar.DATE, 1);
267                 Date currentDatePlusOne = c.getTime();
268                 notificationItem.setStartTime(currentDate);
269                 notificationItem.setEndTime(currentDatePlusOne);
270                 List<Long> roleList = new ArrayList<Long>();
271                 Long role1 = (long) 1;
272                 roleList.add(role1);
273                 notificationItem.setRoleIds(roleList);
274                 HttpServletRequest request = mockitoTestSuite.getMockedRequest();
275                 PowerMockito.mockStatic(UserUtils.class);
276                 Mockito.when(UserUtils.getUserIdAsLong(request)).thenReturn((long) 1);
277                 Mockito.when(userNotificationService.saveNotification(notificationItem)).thenReturn("Test");
278                 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
279                 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
280         }
281
282         @Test
283         public void notificationRead() {
284                 PowerMockito.mockStatic(UserUtils.class);               
285                 Mockito.when(UserUtils.getUserId(mockedRequest)).thenReturn(1);
286                 userNotificationController.notificationRead("1", mockedRequest);
287         }
288         
289         @Test
290         public void notificationRead_Error() {
291                 PowerMockito.mockStatic(UserUtils.class);               
292                 Mockito.when(UserUtils.getUserId(mockedRequest)).thenReturn(1);
293                 userNotificationController.notificationRead("Test", mockedRequest);
294         }
295         
296         @Test
297         public void getNotificationHistory() {
298                 PowerMockito.mockStatic(EPUserUtils.class);
299                 EPUser user = mockUser.mockEPUser();
300                 HttpSession session = mockedRequest.getSession();
301                 session.setAttribute("user", user);
302                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
303                 userNotificationController.getNotificationHistory(mockedRequest, mockedResponse);
304         }
305         
306         @Test
307         public void testGetRoles() {
308                 
309                 List<EpRoleNotificationItem> NotifRoles =new ArrayList<>();
310                 EpRoleNotificationItem epRole=new EpRoleNotificationItem();
311                 epRole.setId(1l);
312                 
313                 Mockito.when(userNotificationService.getNotificationRoles(1l)).thenReturn(NotifRoles);
314                 userNotificationController.testGetRoles(mockedRequest, 1l);
315                 
316                 
317         }
318         
319         @Test
320         public void getNotificationAppRoles() {
321                 List<EcompAppRole> epAppRoleList =new ArrayList<>();
322                 Mockito.when(userNotificationService.getAppRoleList()).thenReturn(epAppRoleList);
323                 userNotificationController.getNotificationAppRoles(mockedRequest, mockedResponse);
324         }
325         
326         
327         @Test
328         public void getMessageRecipients() {
329                 
330                 Mockito.when(userNotificationService.getMessageRecipients(1l)).thenReturn(new ArrayList<>());
331                 userNotificationController.getMessageRecipients(1l);
332         }
333         
334 }