2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
10 * Unless otherwise specified, all software contained herein is licensed
11 * under the Apache License, Version 2.0 (the "License");
12 * you may not use this software except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * Unless otherwise specified, all documentation contained herein is licensed
24 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 * you may not use this documentation except in compliance with the License.
26 * You may obtain a copy of the License at
28 * https://creativecommons.org/licenses/by/4.0/
30 * Unless required by applicable law or agreed to in writing, documentation
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
36 * ============LICENSE_END============================================
40 package org.onap.portalapp.portal.controller;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertNotNull;
44 import static org.junit.Assert.assertNull;
45 import static org.mockito.Matchers.any;
47 import java.util.ArrayList;
48 import java.util.Calendar;
49 import java.util.Date;
50 import java.util.HashMap;
51 import java.util.List;
54 import javax.servlet.http.HttpServletRequest;
55 import javax.servlet.http.HttpServletResponse;
57 import org.junit.Before;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 import org.mockito.ArgumentCaptor;
61 import org.mockito.InjectMocks;
62 import org.mockito.Mock;
63 import org.mockito.Mockito;
64 import org.mockito.MockitoAnnotations;
65 import org.onap.portalapp.portal.core.MockEPUser;
66 import org.onap.portalapp.portal.domain.EPApp;
67 import org.onap.portalapp.portal.domain.EPRole;
68 import org.onap.portalapp.portal.domain.EPUser;
69 import org.onap.portalapp.portal.domain.EpAppType;
70 import org.onap.portalapp.portal.framework.MockitoTestSuite;
71 import org.onap.portalapp.portal.service.AdminRolesService;
72 import org.onap.portalapp.portal.service.AdminRolesServiceImpl;
73 import org.onap.portalapp.portal.service.EPLoginService;
74 import org.onap.portalapp.portal.service.EPLoginServiceImpl;
75 import org.onap.portalapp.portal.service.EPRoleService;
76 import org.onap.portalapp.portal.service.EPRoleServiceImpl;
77 import org.onap.portalapp.portal.service.FunctionalMenuService;
78 import org.onap.portalapp.portal.service.FunctionalMenuServiceImpl;
79 import org.onap.portalapp.portal.service.UserNotificationService;
80 import org.onap.portalapp.portal.service.UserNotificationServiceImpl;
81 import org.onap.portalapp.portal.transport.EpNotificationItem;
82 import org.onap.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
83 import org.onap.portalapp.portal.transport.FunctionalMenuItem;
84 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
85 import org.onap.portalapp.portal.utils.EcompPortalUtils;
86 import org.onap.portalsdk.core.onboarding.crossapi.PortalAPIResponse;
87 import org.onap.portalsdk.core.service.DataAccessService;
88 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
89 import org.powermock.api.mockito.PowerMockito;
90 import org.powermock.core.classloader.annotations.PrepareForTest;
91 import org.powermock.modules.junit4.PowerMockRunner;
94 @RunWith(PowerMockRunner.class)
95 @PrepareForTest({ MDC.class, EPCommonSystemProperties.class ,EPApp.class})
96 public class ExternalAppsRestfulControllerTest {
99 ExternalAppsRestfulController externalAppsRestfulController = new ExternalAppsRestfulController();
101 FunctionalMenuService functionalMenuService = new FunctionalMenuServiceImpl();
104 EPLoginService epLoginService = new EPLoginServiceImpl();
107 AdminRolesService adminRolesService = new AdminRolesServiceImpl();
110 UserNotificationService userNotificationService = new UserNotificationServiceImpl();
113 EPRoleService epRoleService = new EPRoleServiceImpl();
116 EcompPortalUtils EcompPortalUtils = new EcompPortalUtils();
119 DataAccessService DataAccessService = new DataAccessServiceImpl();
122 public void setup() {
123 MockitoAnnotations.initMocks(this);
126 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
128 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
129 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
130 NullPointerException nullPointerException = new NullPointerException();
132 MockEPUser mockUser = new MockEPUser();
134 public EPApp mockApp() {
135 EPApp app = new EPApp();
137 app.setImageUrl("test");
138 app.setNameSpace("com.test.app");
139 app.setRolesInAAF(true);
140 app.setAppDescription("test");
141 app.setAppNotes("test");
142 app.setLandingPage("test");
143 app.setId((long) 10);
144 app.setAppRestEndpoint("test");
145 app.setAlternateLandingPage("test");
147 app.setMlAppName("test");
148 app.setMlAppAdminId("test");
149 app.setAppBasicAuthUsername("test");
150 app.setAppBasicAuthPassword("test");
152 app.setEnabled(true);
153 app.setUebKey("test");
154 app.setUebSecret("test");
155 app.setUebTopicName("test");
156 app.setAppType(EpAppType.GUI);
160 @Test(expected = Exception.class)
161 public void getFunctionalMenuItemsForUserIfUSerNullTest() throws
164 PowerMockito.mockStatic(EPCommonSystemProperties.class);
165 PowerMockito.mockStatic(MDC.class);
166 EPUser epUser = null;
167 String loginId = "guestT";
168 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
169 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
170 externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
175 public void getFunctionalMenuItemsForUserIfSuperAdminTest() throws Exception {
176 PowerMockito.mockStatic(EPCommonSystemProperties.class);
177 PowerMockito.mockStatic(MDC.class);
178 EPUser epUser = mockUser.mockEPUser();
179 epUser.setId((long) 1);
180 epUser.setLoginId("guestT");
181 String loginId = "guestT";
182 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
183 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
184 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
185 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
186 expectedList.add(functionalMenuItem);
187 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
188 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(true);
189 Mockito.when(functionalMenuService.getFunctionalMenuItems()).thenReturn(expectedList);
190 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
192 assertNull(actualList.get(0).menuId);
196 public void getFunctionalMenuItemsForUserTest() throws Exception {
197 PowerMockito.mockStatic(EPCommonSystemProperties.class);
198 PowerMockito.mockStatic(MDC.class);
199 EPUser epUser = mockUser.mockEPUser();
200 epUser.setId((long) 1);
201 epUser.setLoginId("guestT");
202 String loginId = "guestT";
203 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
204 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
205 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
206 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
207 expectedList.add(functionalMenuItem);
208 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
209 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(false);
210 Mockito.when(functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId()))
211 .thenReturn(expectedList);
212 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
214 assertNull(actualList.get(0).menuId);
217 @Test(expected = Exception.class)
218 public void getFavoritesForUserIfUserNullTest() throws Exception {
219 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
220 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
221 favorites.add(favoritesFunctionalMenuItemJson);
222 PowerMockito.mockStatic(EPCommonSystemProperties.class);
223 PowerMockito.mockStatic(MDC.class);
224 Mockito.when(mockedRequest.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
225 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
226 EPUser epUser = null;
227 externalAppsRestfulController.getFavoritesForUser(mockedRequest, mockedResponse);
231 public void getFavoritesForUserTest() throws Exception {
232 PowerMockito.mockStatic(EPCommonSystemProperties.class);
233 PowerMockito.mockStatic(MDC.class);
234 EPUser epUser = mockUser.mockEPUser();
235 epUser.setId((long) 1);
236 epUser.setLoginId("guestT");
237 String loginId = "guestT";
238 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
239 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
240 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
241 favorites.add(favoritesFunctionalMenuItemJson);
242 Mockito.when(mockedRequest.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
243 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
244 Mockito.when(epLoginService.findUserWithoutPwd("Login_URL")).thenReturn(epUser);
245 Mockito.when(functionalMenuService.getFavoriteItems(epUser.getId())).thenReturn(favorites);
246 List<FavoritesFunctionalMenuItemJson> actaulFavorites = externalAppsRestfulController
247 .getFavoritesForUser(mockedRequest, mockedResponse);
248 assertEquals(actaulFavorites.size(), 1);
253 public void publishNotificationTest_Success() throws Exception {
255 EpNotificationItem notificationItem = new EpNotificationItem();
256 List<Long> roleList = new ArrayList<Long>();
259 notificationItem.setRoleIds(roleList);
260 notificationItem.setPriority(1L);
261 notificationItem.setMsgHeader("testHeader");
262 notificationItem.setMsgDescription("Test Description");
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);
272 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("RxH3983AHiyBOQmj");
273 Map<String, String> params = new HashMap<>();
274 params.put("appKey", "RxH3983AHiyBOQmj");
275 List<EPApp> apps = new ArrayList<>();
276 EPApp app = new EPApp();
279 Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null)).thenReturn(apps);
280 EPRole role = new EPRole();
282 Mockito.when(epRoleService.getRole(123L, 1L)).thenReturn(role);
285 Mockito.when(userNotificationService.saveNotification(notificationItem)).thenReturn("Test");
286 PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
288 assertNotNull(response);
289 assertEquals("ok", response.getStatus());
290 assertEquals("success", response.getMessage());
291 ArgumentCaptor<EpNotificationItem> capture = ArgumentCaptor.forClass(EpNotificationItem.class);
292 Mockito.verify(userNotificationService).saveNotification(capture.capture());
293 assertNotNull(capture.getValue());
294 EpNotificationItem createdNofification = capture.getValue();
295 assertNotNull(createdNofification.getRoleIds());
296 assertEquals(1, createdNofification.getRoleIds().size());
297 assertEquals(543L, createdNofification.getRoleIds().get(0).longValue());
301 public void publishNotificationXSSTest() throws Exception {
303 EpNotificationItem notificationItem = new EpNotificationItem();
304 List<Long> roleList = new ArrayList<Long>();
307 notificationItem.setRoleIds(roleList);
308 notificationItem.setPriority(1L);
309 notificationItem.setMsgHeader("<script>alert(‘XSS’)</script>");
310 notificationItem.setMsgDescription("Test Description");
311 Date currentDate = new Date();
312 Calendar c = Calendar.getInstance();
313 c.setTime(currentDate);
314 c.add(Calendar.DATE, 1);
315 Date currentDatePlusOne = c.getTime();
316 notificationItem.setStartTime(currentDate);
317 notificationItem.setEndTime(currentDatePlusOne);
320 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("RxH3983AHiyBOQmj");
321 Map<String, String> params = new HashMap<>();
322 params.put("appKey", "RxH3983AHiyBOQmj");
323 List<EPApp> apps = new ArrayList<>();
324 EPApp app = new EPApp();
327 Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null)).thenReturn(apps);
328 EPRole role = new EPRole();
330 Mockito.when(epRoleService.getRole(123L, 1L)).thenReturn(role);
333 Mockito.when(userNotificationService.saveNotification(notificationItem)).thenReturn("Test");
334 PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
336 assertNotNull(response);
337 assertEquals("error", response.getStatus());
338 assertEquals("failed", response.getMessage());
342 public void publishNotificationTest_EmptyAppHeader() throws Exception {
344 EpNotificationItem notificationItem = new EpNotificationItem();
345 List<Long> roleList = new ArrayList<Long>();
348 notificationItem.setRoleIds(roleList);
349 notificationItem.setPriority(1L);
350 notificationItem.setMsgHeader("testHeader");
351 notificationItem.setMsgDescription("Test Description");
352 Date currentDate = new Date();
353 Calendar c = Calendar.getInstance();
354 c.setTime(currentDate);
355 c.add(Calendar.DATE, 1);
356 Date currentDatePlusOne = c.getTime();
357 notificationItem.setStartTime(currentDate);
358 notificationItem.setEndTime(currentDatePlusOne);
360 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
361 final Map<String, String> params = new HashMap<>();
362 params.put("appKey", null);
363 Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null))
364 .thenThrow(NullPointerException.class);
366 PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
367 assertNotNull(response);
368 assertEquals("ok", response.getStatus());
369 assertEquals("success", response.getMessage());
370 ArgumentCaptor<EpNotificationItem> capture = ArgumentCaptor.forClass(EpNotificationItem.class);
371 Mockito.verify(userNotificationService).saveNotification(capture.capture());
372 assertNotNull(capture.getValue());
373 EpNotificationItem createdNofification = capture.getValue();
374 assertNotNull(createdNofification.getRoleIds());
375 assertEquals(0, createdNofification.getRoleIds().size());
379 public void publishNotificationTest_ErrorResponse() throws Exception {
381 EpNotificationItem notificationItem = new EpNotificationItem();
382 List<Long> roleList = new ArrayList<Long>();
385 notificationItem.setRoleIds(roleList);
386 notificationItem.setPriority(1L);
387 notificationItem.setMsgHeader("testHeader");
388 notificationItem.setMsgDescription("Test Description");
389 Date currentDate = new Date();
390 Calendar c = Calendar.getInstance();
391 c.setTime(currentDate);
392 c.add(Calendar.DATE, 1);
393 Date currentDatePlusOne = c.getTime();
394 notificationItem.setStartTime(currentDate);
395 notificationItem.setEndTime(currentDatePlusOne);
397 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
398 final Map<String, String> params = new HashMap<>();
399 params.put("appKey", null);
400 Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null))
401 .thenThrow(NullPointerException.class);
402 Mockito.when(userNotificationService.saveNotification(any(EpNotificationItem.class))).
403 thenThrow(new NullPointerException("expected message"));
405 PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
406 assertNotNull(response);
407 assertEquals("error", response.getStatus());
408 assertEquals("expected message", response.getMessage());