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.assertEquals;
 
  41 import static org.junit.Assert.assertNull;
 
  43 import java.util.ArrayList;
 
  44 import java.util.List;
 
  46 import javax.servlet.http.HttpServletRequest;
 
  47 import javax.servlet.http.HttpServletResponse;
 
  49 import org.junit.Before;
 
  50 import org.junit.Test;
 
  51 import org.junit.runner.RunWith;
 
  52 import org.mockito.InjectMocks;
 
  53 import org.mockito.Mock;
 
  54 import org.mockito.Mockito;
 
  55 import org.mockito.MockitoAnnotations;
 
  56 import org.onap.portalapp.portal.controller.ExternalAppsRestfulController;
 
  57 import org.onap.portalapp.portal.core.MockEPUser;
 
  58 import org.onap.portalapp.portal.domain.EPUser;
 
  59 import org.onap.portalapp.portal.framework.MockitoTestSuite;
 
  60 import org.onap.portalapp.portal.service.AdminRolesService;
 
  61 import org.onap.portalapp.portal.service.AdminRolesServiceImpl;
 
  62 import org.onap.portalapp.portal.service.EPLoginService;
 
  63 import org.onap.portalapp.portal.service.EPLoginServiceImpl;
 
  64 import org.onap.portalapp.portal.service.EPRoleService;
 
  65 import org.onap.portalapp.portal.service.EPRoleServiceImpl;
 
  66 import org.onap.portalapp.portal.service.FunctionalMenuService;
 
  67 import org.onap.portalapp.portal.service.FunctionalMenuServiceImpl;
 
  68 import org.onap.portalapp.portal.service.UserNotificationService;
 
  69 import org.onap.portalapp.portal.service.UserNotificationServiceImpl;
 
  70 import org.onap.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
 
  71 import org.onap.portalapp.portal.transport.FunctionalMenuItem;
 
  72 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
 
  73 import org.onap.portalapp.portal.utils.EcompPortalUtils;
 
  74 import org.onap.portalsdk.core.service.DataAccessService;
 
  75 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
 
  76 import org.powermock.api.mockito.PowerMockito;
 
  77 import org.powermock.core.classloader.annotations.PrepareForTest;
 
  78 import org.powermock.modules.junit4.PowerMockRunner;
 
  81 @RunWith(PowerMockRunner.class)
 
  82 @PrepareForTest({ MDC.class, EPCommonSystemProperties.class })
 
  83 public class ExternalAppsRestfulControllerTest {
 
  86         ExternalAppsRestfulController externalAppsRestfulController = new ExternalAppsRestfulController();
 
  88         FunctionalMenuService functionalMenuService = new FunctionalMenuServiceImpl();
 
  91         EPLoginService epLoginService = new EPLoginServiceImpl();
 
  94         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
 
  97         UserNotificationService userNotificationService = new UserNotificationServiceImpl();
 
 100         EPRoleService epRoleService = new EPRoleServiceImpl();
 
 103         EcompPortalUtils EcompPortalUtils = new EcompPortalUtils();
 
 106         DataAccessService DataAccessService = new DataAccessServiceImpl();
 
 109         public void setup() {
 
 110                 MockitoAnnotations.initMocks(this);
 
 113         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
 
 115         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
 
 116         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
 
 117         NullPointerException nullPointerException = new NullPointerException();
 
 119         MockEPUser mockUser = new MockEPUser();
 
 121          @Test(expected = Exception.class)
 
 122          public void getFunctionalMenuItemsForUserIfUSerNullTest() throws
 
 125          PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 126          PowerMockito.mockStatic(MDC.class);
 
 127          EPUser epUser = null;
 
 128          String loginId = "guestT";
 
 129          Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
 
 130          Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
 
 131          externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
 
 136         public void getFunctionalMenuItemsForUserIfSuperAdminTest() throws Exception {
 
 137                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 138                 PowerMockito.mockStatic(MDC.class);
 
 139                 EPUser epUser = mockUser.mockEPUser();
 
 140                 epUser.setId((long) 1);
 
 141                 epUser.setLoginId("guestT");
 
 142                 String loginId = "guestT";
 
 143                 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
 
 144                 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
 
 145                 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
 
 146                 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
 
 147                 expectedList.add(functionalMenuItem);
 
 148                 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
 
 149                 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(true);
 
 150                 Mockito.when(functionalMenuService.getFunctionalMenuItems()).thenReturn(expectedList);
 
 151                 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
 
 153                 assertNull(actualList.get(0).menuId);
 
 157         public void getFunctionalMenuItemsForUserTest() throws Exception {
 
 158                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 159                 PowerMockito.mockStatic(MDC.class);
 
 160                 EPUser epUser = mockUser.mockEPUser();
 
 161                 epUser.setId((long) 1);
 
 162                 epUser.setLoginId("guestT");
 
 163                 String loginId = "guestT";
 
 164                 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
 
 165                 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
 
 166                 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
 
 167                 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
 
 168                 expectedList.add(functionalMenuItem);
 
 169                 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
 
 170                 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(false);
 
 171                 Mockito.when(functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId()))
 
 172                                 .thenReturn(expectedList);
 
 173                 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
 
 175                 assertNull(actualList.get(0).menuId);
 
 178         @Test(expected = Exception.class)
 
 179         public void getFavoritesForUserIfUserNullTest() throws Exception {
 
 180                 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
 
 181                 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
 
 182                 favorites.add(favoritesFunctionalMenuItemJson);
 
 183                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 184                 PowerMockito.mockStatic(MDC.class);
 
 185                 Mockito.when(mockedRequest.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
 
 186                 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
 
 187                 EPUser epUser = null;
 
 188                 externalAppsRestfulController.getFavoritesForUser(mockedRequest, mockedResponse);
 
 192         public void getFavoritesForUserTest() throws Exception {
 
 193                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 194                 PowerMockito.mockStatic(MDC.class);
 
 195                 EPUser epUser = mockUser.mockEPUser();
 
 196                 epUser.setId((long) 1);
 
 197                 epUser.setLoginId("guestT");
 
 198                 String loginId = "guestT";
 
 199                 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
 
 200                 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
 
 201                 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
 
 202                 favorites.add(favoritesFunctionalMenuItemJson);
 
 203                 Mockito.when(mockedRequest.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
 
 204                 Mockito.when(MDC.get(EPCommonSystemProperties.PARTNER_NAME)).thenReturn("Test");
 
 205                 Mockito.when(epLoginService.findUserWithoutPwd("Login_URL")).thenReturn(epUser);
 
 206                 Mockito.when(functionalMenuService.getFavoriteItems(epUser.getId())).thenReturn(favorites);
 
 207                 List<FavoritesFunctionalMenuItemJson> actaulFavorites = externalAppsRestfulController
 
 208                                 .getFavoritesForUser(mockedRequest, mockedResponse);
 
 209                 assertEquals(actaulFavorites.size(), 1);