Add doc folder.
[portal.git] / ecomp-portal-BE-common-test / src / main / java / org / openecomp / portalapp / portal / test / controller / UserNotificationControllerTest.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.test.controller;
21
22 import static org.junit.Assert.assertTrue;
23
24 import java.util.ArrayList;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.List;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import javax.servlet.http.HttpSession;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
37 import org.mockito.Matchers;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.openecomp.portalapp.portal.controller.UserNotificationController;
42 import org.openecomp.portalapp.portal.domain.EPUser;
43 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
44 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
45 import org.openecomp.portalapp.portal.service.FunctionalMenuService;
46 import org.openecomp.portalapp.portal.service.FunctionalMenuServiceImpl;
47 import org.openecomp.portalapp.portal.service.UserNotificationService;
48 import org.openecomp.portalapp.portal.service.UserNotificationServiceImpl;
49 import org.openecomp.portalapp.portal.test.core.MockEPUser;
50 import org.openecomp.portalapp.portal.transport.EpNotificationItem;
51 import org.openecomp.portalapp.portal.transport.EpNotificationItemVO;
52 import org.openecomp.portalapp.portal.transport.FunctionalMenuRole;
53 import org.openecomp.portalapp.test.framework.MockitoTestSuite;
54 import org.openecomp.portalapp.util.EPUserUtils;
55 import org.openecomp.portalsdk.core.util.SystemProperties;
56 import org.openecomp.portalsdk.core.web.support.UserUtils;
57 import org.powermock.api.mockito.PowerMockito;
58 import org.powermock.core.classloader.annotations.PrepareForTest;
59 import org.powermock.modules.junit4.PowerMockRunner;
60
61 @RunWith(PowerMockRunner.class)
62 @PrepareForTest(UserUtils.class)
63 public class UserNotificationControllerTest {
64
65         @Mock
66         FunctionalMenuService functionalMenuService = new FunctionalMenuServiceImpl();
67
68         @Mock
69         UserNotificationService userNotificationService = new UserNotificationServiceImpl();
70
71         @InjectMocks
72         UserNotificationController userNotificationController = new UserNotificationController();
73
74         @Before
75         public void setup() {
76                 MockitoAnnotations.initMocks(this);
77         }
78
79         MockEPUser mockUser = new MockEPUser();
80         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
81
82         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
83         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
84         NullPointerException nullPointerException = new NullPointerException();
85
86         @Test
87         public void getMenuIdRoleIdTest() {
88                 List<FunctionalMenuRole> expectedMenuRoleList = new ArrayList<FunctionalMenuRole>();
89                 FunctionalMenuRole functionalMenuRole = new FunctionalMenuRole();
90                 functionalMenuRole.setId(new Integer(99999999));
91                 functionalMenuRole.setMenuId((long) 137);
92                 functionalMenuRole.setAppId(new Integer(456));
93                 functionalMenuRole.setRoleId(new Integer(6214));
94                 expectedMenuRoleList.add(functionalMenuRole);
95                 List<FunctionalMenuRole> actualFunctionalMenuRoleList = null;
96                 Mockito.when(functionalMenuService.getFunctionalMenuRole()).thenReturn(expectedMenuRoleList);
97                 actualFunctionalMenuRoleList = userNotificationController.getMenuIdRoleId(mockedRequest, mockedResponse);
98                 assertTrue(actualFunctionalMenuRoleList.equals(expectedMenuRoleList));
99
100         }
101
102         @Test
103         public void getNotificationsTest() {
104                 EPUser user = mockUser.mockEPUser();
105                 HttpSession session = mockedRequest.getSession();
106                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
107                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
108                 List<EpNotificationItem> expectedEpNotificationList = new ArrayList<EpNotificationItem>();
109                 EpNotificationItem epNotificationItem = new EpNotificationItem();
110                 epNotificationItem.setNotificationId((long) 200);
111                 expectedEpNotificationList.add(epNotificationItem);
112                 PortalRestResponse<List<EpNotificationItem>> expectedportalRestResponse = new PortalRestResponse<List<EpNotificationItem>>();
113                 expectedportalRestResponse.setMessage("success");
114                 expectedportalRestResponse.setResponse(expectedEpNotificationList);
115                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
116                 PortalRestResponse<List<EpNotificationItem>> actualPortalRestResponse = null;
117                 Mockito.when(userNotificationService.getNotifications(user.getId())).thenReturn(expectedEpNotificationList);
118                 actualPortalRestResponse = userNotificationController.getNotifications(mockedRequest, mockedResponse);
119                 assertTrue(expectedportalRestResponse.equals(actualPortalRestResponse));
120
121         }
122
123         @Test
124         public void getNotificationsCatchesExceptionTest() {
125                 EPUser user = mockUser.mockEPUser();
126                 HttpSession session = mockedRequest.getSession();
127                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
128                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
129                 List<EpNotificationItem> expectedEpNotificationList = null;
130                 PortalRestResponse<List<EpNotificationItem>> expectedportalRestResponse = new PortalRestResponse<List<EpNotificationItem>>();
131                 expectedportalRestResponse.setMessage(null);
132                 expectedportalRestResponse.setResponse(expectedEpNotificationList);
133                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
134                 PortalRestResponse<List<EpNotificationItem>> actualPortalRestResponse = null;
135                 Mockito.when(userNotificationService.getNotifications(user.getId())).thenThrow(new NullPointerException());
136                 actualPortalRestResponse = userNotificationController.getNotifications(mockedRequest, mockedResponse);
137                 assertTrue(expectedportalRestResponse.equals(actualPortalRestResponse));
138         }
139
140         @Test
141         public void getAdminNotificationsTest() {
142                 EPUser user = mockUser.mockEPUser();
143                 HttpSession session = mockedRequest.getSession();
144                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
145                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
146                 List<EpNotificationItemVO> actualEpNotificationsList = new ArrayList<EpNotificationItemVO>();
147                 List<EpNotificationItemVO> expectedEpNotificationsList = new ArrayList<EpNotificationItemVO>();
148                 EpNotificationItemVO epNotificationItemVO = new EpNotificationItemVO();
149                 epNotificationItemVO.setId((long) 1);
150                 expectedEpNotificationsList.add(epNotificationItemVO);
151                 Mockito.when(userNotificationService.getAdminNotificationVOS(Matchers.anyLong())).thenReturn(expectedEpNotificationsList);
152                 actualEpNotificationsList = userNotificationController.getAdminNotifications(mockedRequest, mockedResponse);
153                 assertTrue(actualEpNotificationsList.equals(expectedEpNotificationsList));
154         }
155
156         @Test
157         public void saveTestWhenNotificationIsNull() throws Exception {
158                 EPUser user = mockUser.mockEPUser();
159                 HttpSession session = mockedRequest.getSession();
160                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
161                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
162
163                 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
164                 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
165                 expectedPortalRestResponse.setMessage("FAILURE");
166                 expectedPortalRestResponse.setResponse("Notification Header cannot be null or empty");
167                 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
168
169                 EpNotificationItem notificationItem = null;
170                 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
171                 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
172         }
173
174         @Test
175         public void saveTestWhenEndTimeIsGreater() 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);
180                 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
181                 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
182                 expectedPortalRestResponse.setMessage("FAILURE");
183                 expectedPortalRestResponse.setResponse("End Time should be greater than  start time");
184                 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
185                 EpNotificationItem notificationItem = new EpNotificationItem();
186                 notificationItem.setNotificationId((long) 1);
187                 notificationItem.setMsgHeader("Test");
188                 Date currentDate = new Date();
189                 Calendar c = Calendar.getInstance();
190                 c.setTime(currentDate);
191                 c.add(Calendar.DATE, 1);
192                 Date currentDatePlusOne = c.getTime();
193                 notificationItem.setStartTime(currentDatePlusOne);
194                 notificationItem.setEndTime(currentDate);
195
196                 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
197                 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
198
199         }
200
201         @Test
202         public void saveTestWhenNoRoleIDExists() throws Exception {
203                 EPUser user = mockUser.mockEPUser();
204                 HttpSession session = mockedRequest.getSession();
205                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
206                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
207                 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
208                 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
209                 expectedPortalRestResponse.setMessage("FAILURE");
210                 expectedPortalRestResponse.setResponse("No Roles Ids Exist for the selected Roles");
211                 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
212                 EpNotificationItem notificationItem = new EpNotificationItem();
213                 notificationItem.setNotificationId((long) 1);
214                 notificationItem.setMsgHeader("Test");
215                 notificationItem.setIsForAllRoles("N");
216                 Date currentDate = new Date();
217                 Calendar c = Calendar.getInstance();
218                 c.setTime(currentDate);
219                 c.add(Calendar.DATE, 1);
220                 Date currentDatePlusOne = c.getTime();
221                 notificationItem.setStartTime(currentDate);
222                 notificationItem.setEndTime(currentDatePlusOne);
223                 List<Long> roleList = new ArrayList<Long>();
224                 notificationItem.setRoleIds(roleList);
225                 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
226                 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
227         }
228
229         @Test
230         public void saveTest() throws Exception {
231                 EPUser user = mockUser.mockEPUser();
232                 HttpSession session = mockedRequest.getSession();
233                 session.setAttribute("user", user);
234                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
235                 PortalRestResponse<String> actualPortalRestResponse = new PortalRestResponse<String>();
236                 PortalRestResponse<String> expectedPortalRestResponse = new PortalRestResponse<String>();
237                 expectedPortalRestResponse.setMessage("SUCCESS");
238                 expectedPortalRestResponse.setResponse("");
239                 expectedPortalRestResponse.setStatus(PortalRestStatusEnum.OK);
240                 EpNotificationItem notificationItem = new EpNotificationItem();
241                 notificationItem.setNotificationId((long) 1);
242                 notificationItem.setMsgHeader("Test");
243                 notificationItem.setIsForAllRoles("Y");
244                 Date currentDate = new Date();
245                 Calendar c = Calendar.getInstance();
246                 c.setTime(currentDate);
247                 c.add(Calendar.DATE, 1);
248                 Date currentDatePlusOne = c.getTime();
249                 notificationItem.setStartTime(currentDate);
250                 notificationItem.setEndTime(currentDatePlusOne);
251                 List<Long> roleList = new ArrayList<Long>();
252                 Long role1 = (long) 1;
253                 roleList.add(role1);
254                 notificationItem.setRoleIds(roleList);
255                 HttpServletRequest request = mockitoTestSuite.getMockedRequest();
256                 PowerMockito.mockStatic(UserUtils.class);
257                 Mockito.when(UserUtils.getUserIdAsLong(request)).thenReturn((long) 1);
258                 Mockito.when(userNotificationService.saveNotification(notificationItem)).thenReturn("Test");
259                 actualPortalRestResponse = userNotificationController.save(mockedRequest, mockedResponse, notificationItem);
260                 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
261         }
262
263 }