4bd3288a209f0c0004e175e0bb048a647a0f9e00
[portal.git] / ecomp-portal-BE-common / src / test / java / org / openecomp / portalapp / portal / controller / ExternalAppsRestfulControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.controller;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
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.openecomp.portalapp.portal.framework.MockitoTestSuite;
57 import org.openecomp.portalapp.portal.controller.ExternalAppsRestfulController;
58 import org.openecomp.portalapp.portal.core.MockEPUser;
59 import org.openecomp.portalapp.portal.domain.EPUser;
60 import org.openecomp.portalapp.portal.service.AdminRolesService;
61 import org.openecomp.portalapp.portal.service.AdminRolesServiceImpl;
62 import org.openecomp.portalapp.portal.service.EPLoginService;
63 import org.openecomp.portalapp.portal.service.EPLoginServiceImpl;
64 import org.openecomp.portalapp.portal.service.EPRoleService;
65 import org.openecomp.portalapp.portal.service.EPRoleServiceImpl;
66 import org.openecomp.portalapp.portal.service.FunctionalMenuService;
67 import org.openecomp.portalapp.portal.service.FunctionalMenuServiceImpl;
68 import org.openecomp.portalapp.portal.service.UserNotificationService;
69 import org.openecomp.portalapp.portal.service.UserNotificationServiceImpl;
70 import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
71 import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;
72 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
73 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
74 import org.openecomp.portalsdk.core.service.DataAccessService;
75 import org.openecomp.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;
79 import org.slf4j.MDC;
80
81 @RunWith(PowerMockRunner.class)
82 @PrepareForTest({ MDC.class, EPCommonSystemProperties.class })
83 public class ExternalAppsRestfulControllerTest {
84
85         @InjectMocks
86         ExternalAppsRestfulController externalAppsRestfulController = new ExternalAppsRestfulController();
87         @Mock
88         FunctionalMenuService functionalMenuService = new FunctionalMenuServiceImpl();
89
90         @Mock
91         EPLoginService epLoginService = new EPLoginServiceImpl();
92
93         @Mock
94         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
95
96         @Mock
97         UserNotificationService userNotificationService = new UserNotificationServiceImpl();
98
99         @Mock
100         EPRoleService epRoleService = new EPRoleServiceImpl();
101
102         @Mock
103         EcompPortalUtils EcompPortalUtils = new EcompPortalUtils();
104         
105         @Mock
106         DataAccessService DataAccessService = new DataAccessServiceImpl();
107
108         @Before
109         public void setup() {
110                 MockitoAnnotations.initMocks(this);
111         }
112
113         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
114
115         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
116         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
117         NullPointerException nullPointerException = new NullPointerException();
118
119         MockEPUser mockUser = new MockEPUser();
120
121          @Test(expected = Exception.class)
122          public void getFunctionalMenuItemsForUserIfUSerNullTest() throws
123          Exception
124          {
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,
132          mockedResponse);
133          }
134         
135         @Test
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,
152                                 mockedResponse);
153                 assertNull(actualList.get(0).menuId);
154         }
155
156         @Test
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,
174                                 mockedResponse);
175                 assertNull(actualList.get(0).menuId);
176         }
177
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);
189         }
190
191         @Test
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);
210         }
211 }