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.*;
42 import java.util.ArrayList;
43 import java.util.List;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.onap.portalapp.portal.controller.ExternalAppsRestfulController;
56 import org.onap.portalapp.portal.domain.EPUser;
57 import org.onap.portalapp.portal.framework.MockEPUser;
58 import org.onap.portalapp.portal.framework.MockitoTestSuite;
59 import org.onap.portalapp.portal.service.AdminRolesService;
60 import org.onap.portalapp.portal.service.EPLoginService;
61 import org.onap.portalapp.portal.service.FunctionalMenuService;
62 import org.onap.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
63 import org.onap.portalapp.portal.transport.FunctionalMenuItem;
64 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
65 import org.onap.portalapp.portal.utils.EPSystemProperties;
66 import org.onap.portalapp.portal.utils.EcompPortalUtils;
67 import org.powermock.api.mockito.PowerMockito;
68 import org.powermock.core.classloader.annotations.PrepareForTest;
69 import org.powermock.modules.junit4.PowerMockRunner;
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({ MDC.class, EPSystemProperties.class , EcompPortalUtils.class})
74 public class ExternalAppsRestfulControllerOSTest {
78 ExternalAppsRestfulController externalAppsRestfulController = new ExternalAppsRestfulController();
80 FunctionalMenuService functionalMenuService;
83 EPLoginService epLoginService;
86 AdminRolesService adminRolesService;
92 MockitoAnnotations.initMocks(this);
95 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
97 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
98 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
99 NullPointerException nullPointerException = new NullPointerException();
101 MockEPUser mockUser = new MockEPUser();
104 public void getFavoritesForUserTest() throws Exception
106 PowerMockito.mockStatic(EPSystemProperties.class);
107 PowerMockito.mockStatic(MDC.class);
108 PowerMockito.mockStatic(EcompPortalUtils.class);
109 EPUser epUser = mockUser.mockEPUser();
110 epUser.setId((long) 1);
111 epUser.setLoginId("guestT");
112 String loginId = "guestT";
113 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
114 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
115 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
116 favorites.add(favoritesFunctionalMenuItemJson);
117 Mockito.when(mockedRequest.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
118 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
119 Mockito.when(epLoginService.findUserWithoutPwd("Login_URL")).thenReturn(epUser);
120 Mockito.when(functionalMenuService.getFavoriteItems(epUser.getId())).thenReturn(favorites);
121 List<FavoritesFunctionalMenuItemJson> actaulFavorites = externalAppsRestfulController
122 .getFavoritesForUser(mockedRequest, mockedResponse);
123 assertEquals(actaulFavorites.size(), 1);
127 public void getFunctionalMenuItemsForUserIfSuperAdminTest() throws Exception {
128 PowerMockito.mockStatic(EPSystemProperties.class);
129 PowerMockito.mockStatic(MDC.class);
130 PowerMockito.mockStatic(EcompPortalUtils.class);
131 EPUser epUser = mockUser.mockEPUser();
132 epUser.setId((long) 1);
133 epUser.setLoginId("guestT");
134 String loginId = "guestT";
135 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
136 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
137 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
138 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
139 expectedList.add(functionalMenuItem);
140 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
141 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(true);
142 Mockito.when(functionalMenuService.getFunctionalMenuItems()).thenReturn(expectedList);
143 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
145 assertNull(actualList.get(0).menuId);
148 @Test(expected = Exception.class)
149 public void getFunctionalMenuItemsForUserIfUSerNullTest() throws
152 PowerMockito.mockStatic(EPSystemProperties.class);
153 PowerMockito.mockStatic(EcompPortalUtils.class);
154 PowerMockito.mockStatic(MDC.class);
155 EPUser epUser = null;
156 String loginId = "guestT";
157 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
158 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
159 externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
164 public void getFunctionalMenuItemsForUserTest() throws Exception {
165 PowerMockito.mockStatic(EPSystemProperties.class);
166 PowerMockito.mockStatic(MDC.class);
167 EPUser epUser = mockUser.mockEPUser();
168 epUser.setId((long) 1);
169 epUser.setLoginId("guestT");
170 String loginId = "guestT";
171 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
172 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
173 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
174 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
175 expectedList.add(functionalMenuItem);
176 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
177 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(false);
178 Mockito.when(functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId()))
179 .thenReturn(expectedList);
180 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
182 assertNull(actualList.get(0).menuId);
185 @Test(expected = Exception.class)
186 public void getFavoritesForUserIfUserNullTest() throws Exception {
187 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
188 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
189 favorites.add(favoritesFunctionalMenuItemJson);
190 PowerMockito.mockStatic(EPSystemProperties.class);
191 PowerMockito.mockStatic(MDC.class);
192 Mockito.when(mockedRequest.getHeader(EPSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
193 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
194 EPUser epUser = null;
195 externalAppsRestfulController.getFavoritesForUser(mockedRequest, mockedResponse);