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============================================
 
  38 package org.onap.portalapp.portal.controller;
 
  40 import static org.junit.Assert.assertEquals;
 
  41 import static org.junit.Assert.assertNull;
 
  42 import static org.junit.Assert.assertTrue;
 
  44 import java.util.ArrayList;
 
  45 import java.util.Calendar;
 
  46 import java.util.Date;
 
  47 import java.util.HashMap;
 
  48 import java.util.List;
 
  51 import javax.servlet.http.HttpServletRequest;
 
  52 import javax.servlet.http.HttpServletResponse;
 
  54 import org.junit.Before;
 
  55 import org.junit.Test;
 
  56 import org.junit.runner.RunWith;
 
  57 import org.mockito.InjectMocks;
 
  58 import org.mockito.Matchers;
 
  59 import org.mockito.Mock;
 
  60 import org.mockito.Mockito;
 
  61 import org.mockito.MockitoAnnotations;
 
  62 import org.onap.portalapp.portal.controller.ExternalAppsRestfulController;
 
  63 import org.onap.portalapp.portal.core.MockEPUser;
 
  64 import org.onap.portalapp.portal.domain.EPApp;
 
  65 import org.onap.portalapp.portal.domain.EPUser;
 
  66 import org.onap.portalapp.portal.domain.UserRole;
 
  67 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
 
  68 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
 
  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.menu.MenuBuilder;
 
  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.setCentralAuth(true);
 
 140                 app.setDescription("test");
 
 141                 app.setNotes("test");
 
 143                 app.setId((long) 10);
 
 144                 app.setAppRestEndpoint("test");
 
 145                 app.setAlternateUrl("test");
 
 147                 app.setMlAppName("test");
 
 148                 app.setMlAppAdminId("test");
 
 149                 app.setUsername("test");
 
 150                 app.setAppPassword("test");
 
 152                 app.setEnabled(true);
 
 153                 app.setUebKey("test");
 
 154                 app.setUebSecret("test");
 
 155                 app.setUebTopicName("test");
 
 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);
 
 251         @Test(expected=NullPointerException.class)
 
 252         public void publishNotificationTest() throws Exception{
 
 253                 EPApp appTest=new EPApp();
 
 254                 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("RxH3983AHiyBOQmj");
 
 255                 appTest.setUebKey("123456");
 
 256                 String appKey="123456";
 
 257                 EpNotificationItem notificationItem=new EpNotificationItem();
 
 258                 List<Long> roleList = new ArrayList<Long>();
 
 259                 Long role1 = (long) 1;
 
 261                 notificationItem.setRoleIds(roleList);
 
 262                 notificationItem.setIsForAllRoles("N");
 
 263                 notificationItem.setIsForOnlineUsers("N");
 
 264                 notificationItem.setActiveYn("Y");
 
 265                 notificationItem.setPriority(1L);
 
 266                 notificationItem.setMsgHeader("testHeader");
 
 267                 notificationItem.setMsgDescription("Test Description");
 
 268                 Date currentDate = new Date();
 
 269                 Calendar c = Calendar.getInstance();
 
 270                 c.setTime(currentDate);
 
 271                 c.add(Calendar.DATE, 1);
 
 272                 Date currentDatePlusOne = c.getTime();
 
 273                 notificationItem.setStartTime(currentDate);
 
 274                 notificationItem.setEndTime(currentDatePlusOne);
 
 275                 notificationItem.setCreatedDate(c.getTime());
 
 277                 PortalAPIResponse actualPortalRestResponse = new PortalAPIResponse(true, appKey);
 
 278                 PortalAPIResponse expectedPortalRestResponse = new PortalAPIResponse(true, appKey);
 
 279                 expectedPortalRestResponse.setMessage("SUCCESS");
 
 280                 expectedPortalRestResponse.setStatus("ok");
 
 281                 Map<String, String> params = new HashMap<>();
 
 282                 params.put("appKey", "1234567");
 
 284                 Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null)).thenReturn(null);
 
 286                 Mockito.when(userNotificationService.saveNotification(notificationItem)).thenReturn("Test");
 
 287                 actualPortalRestResponse = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
 
 288                 assertTrue(actualPortalRestResponse.equals(expectedPortalRestResponse));
 
 292         @Test(expected=NullPointerException.class)
 
 293         public void publishNotificationTest1() throws Exception{
 
 294                 EpNotificationItem notificationItem=new EpNotificationItem();
 
 295                 List<Long> roleList = new ArrayList<Long>();
 
 296                 Long role1 = (long) 1;
 
 298                 notificationItem.setRoleIds(roleList);
 
 299                 notificationItem.setIsForAllRoles("N");
 
 300                 notificationItem.setIsForOnlineUsers("N");
 
 301                 notificationItem.setActiveYn("Y");
 
 302                 notificationItem.setPriority(1L);
 
 303                 notificationItem.setMsgHeader("testHeader");
 
 304                 notificationItem.setMsgDescription("Test Description");
 
 305                 Date currentDate = new Date();
 
 306                 Calendar c = Calendar.getInstance();
 
 307                 c.setTime(currentDate);
 
 308                 c.add(Calendar.DATE, 1);
 
 309                 Date currentDatePlusOne = c.getTime();
 
 310                 notificationItem.setStartTime(currentDate);
 
 311                 notificationItem.setEndTime(currentDatePlusOne);
 
 312                 notificationItem.setCreatedDate(c.getTime());
 
 314                 //PowerMockito.mockStatic(EPApp.class);
 
 317                 List<EPApp> appList = new ArrayList<>();
 
 318                 EPApp app = mockApp();
 
 322                 final Map<String, String> appUebkeyParams = new HashMap<>();
 
 323                 appUebkeyParams.put("appKey", "test-ueb-key");
 
 325                 Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
 
 326                 .thenReturn(appList);
 
 327                 //EPApp epApp=new EPApp();
 
 329                 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("RxH3983AHiyBOQmj");
 
 331                  externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);