2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
40 import static org.junit.Assert.assertTrue;
42 import java.util.ArrayList;
43 import java.util.Calendar;
44 import java.util.Date;
45 import java.util.List;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49 import javax.servlet.http.HttpSession;
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;
80 @RunWith(PowerMockRunner.class)
81 @PrepareForTest(UserUtils.class)
82 public class UserNotificationControllerTest {
85 FunctionalMenuService functionalMenuService = new FunctionalMenuServiceImpl();
88 UserNotificationService userNotificationService = new UserNotificationServiceImpl();
91 UserNotificationController userNotificationController = new UserNotificationController();
95 MockitoAnnotations.initMocks(this);
98 MockEPUser mockUser = new MockEPUser();
99 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
101 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
102 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
103 NullPointerException nullPointerException = new NullPointerException();
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));
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));
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));
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));
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);
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);
188 EpNotificationItem notificationItem = null;
189 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
190 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
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);
215 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
216 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
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));
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;
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));
283 public void notificationRead() {
284 PowerMockito.mockStatic(UserUtils.class);
285 Mockito.when(UserUtils.getUserId(mockedRequest)).thenReturn(1);
286 userNotificationController.notificationRead("1", mockedRequest);
290 public void notificationRead_Error() {
291 PowerMockito.mockStatic(UserUtils.class);
292 Mockito.when(UserUtils.getUserId(mockedRequest)).thenReturn(1);
293 userNotificationController.notificationRead("Test", mockedRequest);
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);
307 public void testGetRoles() {
309 List<EpRoleNotificationItem> NotifRoles =new ArrayList<>();
310 EpRoleNotificationItem epRole=new EpRoleNotificationItem();
313 Mockito.when(userNotificationService.getNotificationRoles(1l)).thenReturn(NotifRoles);
314 userNotificationController.testGetRoles(mockedRequest, 1l);
320 public void getNotificationAppRoles() {
321 List<EcompAppRole> epAppRoleList =new ArrayList<>();
322 Mockito.when(userNotificationService.getAppRoleList()).thenReturn(epAppRoleList);
323 userNotificationController.getNotificationAppRoles(mockedRequest, mockedResponse);
328 public void getMessageRecipients() {
330 Mockito.when(userNotificationService.getMessageRecipients(1l)).thenReturn(new ArrayList<>());
331 userNotificationController.getMessageRecipients(1l);