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.framework.MockitoTestSuite;
70 import org.onap.portalapp.portal.service.AdminRolesService;
71 import org.onap.portalapp.portal.service.AdminRolesServiceImpl;
72 import org.onap.portalapp.portal.service.EPLoginService;
73 import org.onap.portalapp.portal.service.EPLoginServiceImpl;
74 import org.onap.portalapp.portal.service.EPRoleService;
75 import org.onap.portalapp.portal.service.EPRoleServiceImpl;
76 import org.onap.portalapp.portal.service.FunctionalMenuService;
77 import org.onap.portalapp.portal.service.FunctionalMenuServiceImpl;
78 import org.onap.portalapp.portal.service.UserNotificationService;
79 import org.onap.portalapp.portal.service.UserNotificationServiceImpl;
80 import org.onap.portalapp.portal.transport.EpNotificationItem;
81 import org.onap.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
82 import org.onap.portalapp.portal.transport.FunctionalMenuItem;
83 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
84 import org.onap.portalapp.portal.utils.EcompPortalUtils;
85 import org.onap.portalsdk.core.onboarding.crossapi.PortalAPIResponse;
86 import org.onap.portalsdk.core.service.DataAccessService;
87 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
88 import org.powermock.api.mockito.PowerMockito;
89 import org.powermock.core.classloader.annotations.PrepareForTest;
90 import org.powermock.modules.junit4.PowerMockRunner;
93 @RunWith(PowerMockRunner.class)
94 @PrepareForTest({ MDC.class, EPCommonSystemProperties.class ,EPApp.class})
95 public class ExternalAppsRestfulControllerTest {
98 ExternalAppsRestfulController externalAppsRestfulController = new ExternalAppsRestfulController();
100 FunctionalMenuService functionalMenuService = new FunctionalMenuServiceImpl();
103 EPLoginService epLoginService = new EPLoginServiceImpl();
106 AdminRolesService adminRolesService = new AdminRolesServiceImpl();
109 UserNotificationService userNotificationService = new UserNotificationServiceImpl();
112 EPRoleService epRoleService = new EPRoleServiceImpl();
115 EcompPortalUtils EcompPortalUtils = new EcompPortalUtils();
118 DataAccessService DataAccessService = new DataAccessServiceImpl();
121 public void setup() {
122 MockitoAnnotations.initMocks(this);
125 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
127 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
128 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
129 NullPointerException nullPointerException = new NullPointerException();
131 MockEPUser mockUser = new MockEPUser();
133 public EPApp mockApp() {
134 EPApp app = new EPApp();
136 app.setImageUrl("test");
137 app.setNameSpace("com.test.app");
138 app.setCentralAuth(true);
139 app.setDescription("test");
140 app.setNotes("test");
142 app.setId((long) 10);
143 app.setAppRestEndpoint("test");
144 app.setAlternateUrl("test");
146 app.setMlAppName("test");
147 app.setMlAppAdminId("test");
148 app.setUsername("test");
149 app.setAppPassword("test");
151 app.setEnabled(true);
152 app.setUebKey("test");
153 app.setUebSecret("test");
154 app.setUebTopicName("test");
159 @Test(expected = Exception.class)
160 public void getFunctionalMenuItemsForUserIfUSerNullTest() throws
163 PowerMockito.mockStatic(EPCommonSystemProperties.class);
164 PowerMockito.mockStatic(MDC.class);
165 EPUser epUser = null;
166 String loginId = "guestT";
167 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
168 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
169 externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
174 public void getFunctionalMenuItemsForUserIfSuperAdminTest() throws Exception {
175 PowerMockito.mockStatic(EPCommonSystemProperties.class);
176 PowerMockito.mockStatic(MDC.class);
177 EPUser epUser = mockUser.mockEPUser();
178 epUser.setId((long) 1);
179 epUser.setLoginId("guestT");
180 String loginId = "guestT";
181 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
182 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
183 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
184 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
185 expectedList.add(functionalMenuItem);
186 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
187 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(true);
188 Mockito.when(functionalMenuService.getFunctionalMenuItems()).thenReturn(expectedList);
189 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
191 assertNull(actualList.get(0).menuId);
195 public void getFunctionalMenuItemsForUserTest() throws Exception {
196 PowerMockito.mockStatic(EPCommonSystemProperties.class);
197 PowerMockito.mockStatic(MDC.class);
198 EPUser epUser = mockUser.mockEPUser();
199 epUser.setId((long) 1);
200 epUser.setLoginId("guestT");
201 String loginId = "guestT";
202 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
203 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
204 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
205 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
206 expectedList.add(functionalMenuItem);
207 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
208 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(false);
209 Mockito.when(functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId()))
210 .thenReturn(expectedList);
211 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
213 assertNull(actualList.get(0).menuId);
216 @Test(expected = Exception.class)
217 public void getFavoritesForUserIfUserNullTest() throws Exception {
218 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
219 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
220 favorites.add(favoritesFunctionalMenuItemJson);
221 PowerMockito.mockStatic(EPCommonSystemProperties.class);
222 PowerMockito.mockStatic(MDC.class);
223 Mockito.when(mockedRequest.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
224 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
225 EPUser epUser = null;
226 externalAppsRestfulController.getFavoritesForUser(mockedRequest, mockedResponse);
230 public void getFavoritesForUserTest() throws Exception {
231 PowerMockito.mockStatic(EPCommonSystemProperties.class);
232 PowerMockito.mockStatic(MDC.class);
233 EPUser epUser = mockUser.mockEPUser();
234 epUser.setId((long) 1);
235 epUser.setLoginId("guestT");
236 String loginId = "guestT";
237 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
238 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
239 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
240 favorites.add(favoritesFunctionalMenuItemJson);
241 Mockito.when(mockedRequest.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
242 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
243 Mockito.when(epLoginService.findUserWithoutPwd("Login_URL")).thenReturn(epUser);
244 Mockito.when(functionalMenuService.getFavoriteItems(epUser.getId())).thenReturn(favorites);
245 List<FavoritesFunctionalMenuItemJson> actaulFavorites = externalAppsRestfulController
246 .getFavoritesForUser(mockedRequest, mockedResponse);
247 assertEquals(actaulFavorites.size(), 1);
252 public void publishNotificationTest_Success() throws Exception {
254 EpNotificationItem notificationItem = new EpNotificationItem();
255 List<Long> roleList = new ArrayList<Long>();
258 notificationItem.setRoleIds(roleList);
259 notificationItem.setPriority(1L);
260 notificationItem.setMsgHeader("testHeader");
261 notificationItem.setMsgDescription("Test Description");
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);
271 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("RxH3983AHiyBOQmj");
272 Map<String, String> params = new HashMap<>();
273 params.put("appKey", "RxH3983AHiyBOQmj");
274 List<EPApp> apps = new ArrayList<>();
275 EPApp app = new EPApp();
278 Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null)).thenReturn(apps);
279 EPRole role = new EPRole();
281 Mockito.when(epRoleService.getRole(123L, 1L)).thenReturn(role);
284 Mockito.when(userNotificationService.saveNotification(notificationItem)).thenReturn("Test");
285 PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
287 assertNotNull(response);
288 assertEquals("ok", response.getStatus());
289 assertEquals("success", response.getMessage());
290 ArgumentCaptor<EpNotificationItem> capture = ArgumentCaptor.forClass(EpNotificationItem.class);
291 Mockito.verify(userNotificationService).saveNotification(capture.capture());
292 assertNotNull(capture.getValue());
293 EpNotificationItem createdNofification = capture.getValue();
294 assertNotNull(createdNofification.getRoleIds());
295 assertEquals(1, createdNofification.getRoleIds().size());
296 assertEquals(543L, createdNofification.getRoleIds().get(0).longValue());
300 public void publishNotificationTest_EmptyAppHeader() throws Exception {
302 EpNotificationItem notificationItem = new EpNotificationItem();
303 List<Long> roleList = new ArrayList<Long>();
306 notificationItem.setRoleIds(roleList);
307 notificationItem.setPriority(1L);
308 notificationItem.setMsgHeader("testHeader");
309 notificationItem.setMsgDescription("Test Description");
310 Date currentDate = new Date();
311 Calendar c = Calendar.getInstance();
312 c.setTime(currentDate);
313 c.add(Calendar.DATE, 1);
314 Date currentDatePlusOne = c.getTime();
315 notificationItem.setStartTime(currentDate);
316 notificationItem.setEndTime(currentDatePlusOne);
318 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
319 final Map<String, String> params = new HashMap<>();
320 params.put("appKey", null);
321 Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null))
322 .thenThrow(NullPointerException.class);
324 PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
325 assertNotNull(response);
326 assertEquals("ok", response.getStatus());
327 assertEquals("success", response.getMessage());
328 ArgumentCaptor<EpNotificationItem> capture = ArgumentCaptor.forClass(EpNotificationItem.class);
329 Mockito.verify(userNotificationService).saveNotification(capture.capture());
330 assertNotNull(capture.getValue());
331 EpNotificationItem createdNofification = capture.getValue();
332 assertNotNull(createdNofification.getRoleIds());
333 assertEquals(0, createdNofification.getRoleIds().size());
337 public void publishNotificationTest_ErrorResponse() throws Exception {
339 EpNotificationItem notificationItem = new EpNotificationItem();
340 List<Long> roleList = new ArrayList<Long>();
343 notificationItem.setRoleIds(roleList);
344 notificationItem.setPriority(1L);
345 notificationItem.setMsgHeader("testHeader");
346 notificationItem.setMsgDescription("Test Description");
347 Date currentDate = new Date();
348 Calendar c = Calendar.getInstance();
349 c.setTime(currentDate);
350 c.add(Calendar.DATE, 1);
351 Date currentDatePlusOne = c.getTime();
352 notificationItem.setStartTime(currentDate);
353 notificationItem.setEndTime(currentDatePlusOne);
355 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
356 final Map<String, String> params = new HashMap<>();
357 params.put("appKey", null);
358 Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null))
359 .thenThrow(NullPointerException.class);
360 Mockito.when(userNotificationService.saveNotification(any(EpNotificationItem.class))).
361 thenThrow(new NullPointerException("expected message"));
363 PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
364 assertNotNull(response);
365 assertEquals("error", response.getStatus());
366 assertEquals("expected message", response.getMessage());