2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.controller.UserNotificationController;
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.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.FunctionalMenuRole;
72 import org.onap.portalapp.util.EPUserUtils;
73 import org.onap.portalsdk.core.util.SystemProperties;
74 import org.onap.portalsdk.core.web.support.UserUtils;
75 import org.powermock.api.mockito.PowerMockito;
76 import org.powermock.core.classloader.annotations.PrepareForTest;
77 import org.powermock.modules.junit4.PowerMockRunner;
79 @RunWith(PowerMockRunner.class)
80 @PrepareForTest(UserUtils.class)
81 public class UserNotificationControllerTest {
84 FunctionalMenuService functionalMenuService = new FunctionalMenuServiceImpl();
87 UserNotificationService userNotificationService = new UserNotificationServiceImpl();
90 UserNotificationController userNotificationController = new UserNotificationController();
94 MockitoAnnotations.initMocks(this);
97 MockEPUser mockUser = new MockEPUser();
98 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
100 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
101 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
102 NullPointerException nullPointerException = new NullPointerException();
105 public void getMenuIdRoleIdTest() {
106 List<FunctionalMenuRole> expectedMenuRoleList = new ArrayList<FunctionalMenuRole>();
107 FunctionalMenuRole functionalMenuRole = new FunctionalMenuRole();
108 functionalMenuRole.setId(new Integer(99999999));
109 functionalMenuRole.setMenuId((long) 137);
110 functionalMenuRole.setAppId(new Integer(456));
111 functionalMenuRole.setRoleId(new Integer(6214));
112 expectedMenuRoleList.add(functionalMenuRole);
113 List<FunctionalMenuRole> actualFunctionalMenuRoleList = null;
114 Mockito.when(functionalMenuService.getFunctionalMenuRole()).thenReturn(expectedMenuRoleList);
115 actualFunctionalMenuRoleList = userNotificationController.getMenuIdRoleId(mockedRequest, mockedResponse);
116 assertTrue(actualFunctionalMenuRoleList.equals(expectedMenuRoleList));
121 public void getNotificationsTest() {
122 EPUser user = mockUser.mockEPUser();
123 HttpSession session = mockedRequest.getSession();
124 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
125 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
126 List<EpNotificationItem> expectedEpNotificationList = new ArrayList<EpNotificationItem>();
127 EpNotificationItem epNotificationItem = new EpNotificationItem();
128 epNotificationItem.setNotificationId((long) 200);
129 expectedEpNotificationList.add(epNotificationItem);
130 PortalRestResponse<List<EpNotificationItem>> expectedportalRestResponse = new PortalRestResponse<List<EpNotificationItem>>();
131 expectedportalRestResponse.setMessage("success");
132 expectedportalRestResponse.setResponse(expectedEpNotificationList);
133 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
134 PortalRestResponse<List<EpNotificationItem>> actualPortalRestResponse = null;
135 Mockito.when(userNotificationService.getNotifications(user.getId())).thenReturn(expectedEpNotificationList);
136 actualPortalRestResponse = userNotificationController.getNotifications(mockedRequest, mockedResponse);
137 assertTrue(expectedportalRestResponse.equals(actualPortalRestResponse));
142 public void getNotificationsCatchesExceptionTest() {
143 EPUser user = mockUser.mockEPUser();
144 HttpSession session = mockedRequest.getSession();
145 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
146 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
147 List<EpNotificationItem> expectedEpNotificationList = null;
148 PortalRestResponse<List<EpNotificationItem>> expectedportalRestResponse = new PortalRestResponse<List<EpNotificationItem>>();
149 expectedportalRestResponse.setMessage(null);
150 expectedportalRestResponse.setResponse(expectedEpNotificationList);
151 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
152 PortalRestResponse<List<EpNotificationItem>> actualPortalRestResponse = null;
153 Mockito.when(userNotificationService.getNotifications(user.getId())).thenThrow(new NullPointerException());
154 actualPortalRestResponse = userNotificationController.getNotifications(mockedRequest, mockedResponse);
155 assertTrue(expectedportalRestResponse.equals(actualPortalRestResponse));
159 public void getAdminNotificationsTest() {
160 EPUser user = mockUser.mockEPUser();
161 HttpSession session = mockedRequest.getSession();
162 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
163 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
164 List<EpNotificationItemVO> actualEpNotificationsList = new ArrayList<EpNotificationItemVO>();
165 List<EpNotificationItemVO> expectedEpNotificationsList = new ArrayList<EpNotificationItemVO>();
166 EpNotificationItemVO epNotificationItemVO = new EpNotificationItemVO();
167 epNotificationItemVO.setId((long) 1);
168 expectedEpNotificationsList.add(epNotificationItemVO);
169 Mockito.when(userNotificationService.getAdminNotificationVOS(Matchers.anyLong())).thenReturn(expectedEpNotificationsList);
170 actualEpNotificationsList = userNotificationController.getAdminNotifications(mockedRequest, mockedResponse);
171 assertTrue(actualEpNotificationsList.equals(expectedEpNotificationsList));
175 public void saveTestWhenNotificationIsNull() throws Exception {
176 EPUser user = mockUser.mockEPUser();
177 HttpSession session = mockedRequest.getSession();
178 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
179 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
181 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
182 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
183 expectedPortalRestResponse.setMessage("FAILURE");
184 expectedPortalRestResponse.setResponse("Notification Header cannot be null or empty");
185 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
187 EpNotificationItem notificationItem = null;
188 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
189 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
193 public void saveTestWhenEndTimeIsGreater() throws Exception {
194 EPUser user = mockUser.mockEPUser();
195 HttpSession session = mockedRequest.getSession();
196 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
197 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
198 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
199 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
200 expectedPortalRestResponse.setMessage("FAILURE");
201 expectedPortalRestResponse.setResponse("End Time should be greater than start time");
202 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
203 EpNotificationItem notificationItem = new EpNotificationItem();
204 notificationItem.setNotificationId((long) 1);
205 notificationItem.setMsgHeader("Test");
206 Date currentDate = new Date();
207 Calendar c = Calendar.getInstance();
208 c.setTime(currentDate);
209 c.add(Calendar.DATE, 1);
210 Date currentDatePlusOne = c.getTime();
211 notificationItem.setStartTime(currentDatePlusOne);
212 notificationItem.setEndTime(currentDate);
214 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
215 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
220 public void saveTestWhenNoRoleIDExists() throws Exception {
221 EPUser user = mockUser.mockEPUser();
222 HttpSession session = mockedRequest.getSession();
223 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
224 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
225 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
226 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
227 expectedPortalRestResponse.setMessage("FAILURE");
228 expectedPortalRestResponse.setResponse("No Roles Ids Exist for the selected Roles");
229 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
230 EpNotificationItem notificationItem = new EpNotificationItem();
231 notificationItem.setNotificationId((long) 1);
232 notificationItem.setMsgHeader("Test");
233 notificationItem.setIsForAllRoles("N");
234 Date currentDate = new Date();
235 Calendar c = Calendar.getInstance();
236 c.setTime(currentDate);
237 c.add(Calendar.DATE, 1);
238 Date currentDatePlusOne = c.getTime();
239 notificationItem.setStartTime(currentDate);
240 notificationItem.setEndTime(currentDatePlusOne);
241 List<Long> roleList = new ArrayList<Long>();
242 notificationItem.setRoleIds(roleList);
243 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
244 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
248 public void saveTest() throws Exception {
249 EPUser user = mockUser.mockEPUser();
250 HttpSession session = mockedRequest.getSession();
251 session.setAttribute("user", user);
252 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
253 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
254 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
255 expectedPortalRestResponse.setMessage("SUCCESS");
256 expectedPortalRestResponse.setResponse("");
257 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.OK);
258 EpNotificationItem notificationItem = new EpNotificationItem();
259 notificationItem.setNotificationId((long) 1);
260 notificationItem.setMsgHeader("Test");
261 notificationItem.setIsForAllRoles("Y");
262 Date currentDate = new Date();
263 Calendar c = Calendar.getInstance();
264 c.setTime(currentDate);
265 c.add(Calendar.DATE, 1);
266 Date currentDatePlusOne = c.getTime();
267 notificationItem.setStartTime(currentDate);
268 notificationItem.setEndTime(currentDatePlusOne);
269 List<Long> roleList = new ArrayList<Long>();
270 Long role1 = (long) 1;
272 notificationItem.setRoleIds(roleList);
273 HttpServletRequest request = mockitoTestSuite.getMockedRequest();
274 PowerMockito.mockStatic(UserUtils.class);
275 Mockito.when(UserUtils.getUserIdAsLong(request)).thenReturn((long) 1);
276 Mockito.when(userNotificationService.saveNotification(notificationItem)).thenReturn("Test");
277 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
278 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));