2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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.openecomp.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.openecomp.portalapp.portal.domain.EPUser;
56 import org.openecomp.portalapp.portal.framework.MockEPUser;
57 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
58 import org.openecomp.portalapp.portal.service.AdminRolesService;
59 import org.openecomp.portalapp.portal.service.EPLoginService;
60 import org.openecomp.portalapp.portal.service.FunctionalMenuService;
61 import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
62 import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;
63 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
64 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
65 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
66 import org.powermock.api.mockito.PowerMockito;
67 import org.powermock.core.classloader.annotations.PrepareForTest;
68 import org.powermock.modules.junit4.PowerMockRunner;
71 @RunWith(PowerMockRunner.class)
72 @PrepareForTest({ MDC.class, EPSystemProperties.class , EcompPortalUtils.class})
73 public class ExternalAppsRestfulControllerOSTest {
77 ExternalAppsRestfulController externalAppsRestfulController = new ExternalAppsRestfulController();
79 FunctionalMenuService functionalMenuService;
82 EPLoginService epLoginService;
85 AdminRolesService adminRolesService;
91 MockitoAnnotations.initMocks(this);
94 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
96 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
97 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
98 NullPointerException nullPointerException = new NullPointerException();
100 MockEPUser mockUser = new MockEPUser();
103 public void getFavoritesForUserTest() throws Exception
105 PowerMockito.mockStatic(EPSystemProperties.class);
106 PowerMockito.mockStatic(MDC.class);
107 PowerMockito.mockStatic(EcompPortalUtils.class);
108 EPUser epUser = mockUser.mockEPUser();
109 epUser.setId((long) 1);
110 epUser.setLoginId("guestT");
111 String loginId = "guestT";
112 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
113 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
114 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
115 favorites.add(favoritesFunctionalMenuItemJson);
116 Mockito.when(mockedRequest.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
117 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
118 Mockito.when(epLoginService.findUserWithoutPwd("Login_URL")).thenReturn(epUser);
119 Mockito.when(functionalMenuService.getFavoriteItems(epUser.getId())).thenReturn(favorites);
120 List<FavoritesFunctionalMenuItemJson> actaulFavorites = externalAppsRestfulController
121 .getFavoritesForUser(mockedRequest, mockedResponse);
122 assertEquals(actaulFavorites.size(), 1);
126 public void getFunctionalMenuItemsForUserIfSuperAdminTest() throws Exception {
127 PowerMockito.mockStatic(EPSystemProperties.class);
128 PowerMockito.mockStatic(MDC.class);
129 PowerMockito.mockStatic(EcompPortalUtils.class);
130 EPUser epUser = mockUser.mockEPUser();
131 epUser.setId((long) 1);
132 epUser.setLoginId("guestT");
133 String loginId = "guestT";
134 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
135 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
136 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
137 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
138 expectedList.add(functionalMenuItem);
139 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
140 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(true);
141 Mockito.when(functionalMenuService.getFunctionalMenuItems()).thenReturn(expectedList);
142 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
144 assertNull(actualList.get(0).menuId);
147 @Test(expected = Exception.class)
148 public void getFunctionalMenuItemsForUserIfUSerNullTest() throws
151 PowerMockito.mockStatic(EPSystemProperties.class);
152 PowerMockito.mockStatic(EcompPortalUtils.class);
153 PowerMockito.mockStatic(MDC.class);
154 EPUser epUser = null;
155 String loginId = "guestT";
156 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
157 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
158 externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
163 public void getFunctionalMenuItemsForUserTest() throws Exception {
164 PowerMockito.mockStatic(EPSystemProperties.class);
165 PowerMockito.mockStatic(MDC.class);
166 EPUser epUser = mockUser.mockEPUser();
167 epUser.setId((long) 1);
168 epUser.setLoginId("guestT");
169 String loginId = "guestT";
170 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
171 Mockito.when(epLoginService.findUserWithoutPwd(loginId)).thenReturn(epUser);
172 List<FunctionalMenuItem> expectedList = new ArrayList<FunctionalMenuItem>();
173 FunctionalMenuItem functionalMenuItem = new FunctionalMenuItem();
174 expectedList.add(functionalMenuItem);
175 Mockito.when(mockedRequest.getHeader("LoginId")).thenReturn("guestT");
176 Mockito.when(adminRolesService.isSuperAdmin(epUser)).thenReturn(false);
177 Mockito.when(functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId()))
178 .thenReturn(expectedList);
179 List<FunctionalMenuItem> actualList = externalAppsRestfulController.getFunctionalMenuItemsForUser(mockedRequest,
181 assertNull(actualList.get(0).menuId);
184 @Test(expected = Exception.class)
185 public void getFavoritesForUserIfUserNullTest() throws Exception {
186 List<FavoritesFunctionalMenuItemJson> favorites = new ArrayList<FavoritesFunctionalMenuItemJson>();
187 FavoritesFunctionalMenuItemJson favoritesFunctionalMenuItemJson = new FavoritesFunctionalMenuItemJson();
188 favorites.add(favoritesFunctionalMenuItemJson);
189 PowerMockito.mockStatic(EPSystemProperties.class);
190 PowerMockito.mockStatic(MDC.class);
191 Mockito.when(mockedRequest.getHeader(EPSystemProperties.MDC_LOGIN_ID)).thenReturn("Login_URL");
192 Mockito.when(MDC.get(EPSystemProperties.PARTNER_NAME)).thenReturn("Test");
193 EPUser epUser = null;
194 externalAppsRestfulController.getFavoritesForUser(mockedRequest, mockedResponse);