Security/ Package Name changes
[portal.git] / ecomp-portal-BE-os / src / test / java / org / onap / portalapp / portal / controller / ExternalAppsRestfulControllerOSTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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.onap.portalapp.portal.controller;
39
40 import static org.junit.Assert.*;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
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;
70 import org.slf4j.MDC;
71
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({ MDC.class, EPSystemProperties.class , EcompPortalUtils.class})
74 public class ExternalAppsRestfulControllerOSTest {
75
76         
77         @InjectMocks
78         ExternalAppsRestfulController externalAppsRestfulController = new ExternalAppsRestfulController();
79         @Mock
80         FunctionalMenuService functionalMenuService;
81         
82         @Mock
83         EPLoginService epLoginService;
84         
85         @Mock
86         AdminRolesService adminRolesService;
87         
88         
89         
90         @Before
91         public void setup() {
92                 MockitoAnnotations.initMocks(this);
93         }
94
95         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
96
97         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
98         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
99         NullPointerException nullPointerException = new NullPointerException();
100
101         MockEPUser mockUser = new MockEPUser();
102         
103         @Test
104         public void getFavoritesForUserTest() throws Exception
105         {
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);
124         }
125         
126         @Test
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,
144                                 mockedResponse);
145                 assertNull(actualList.get(0).menuId);
146         }
147
148          @Test(expected = Exception.class)
149          public void getFunctionalMenuItemsForUserIfUSerNullTest() throws
150          Exception
151          {
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,
160          mockedResponse);
161          }
162          
163             @Test
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,
181                                         mockedResponse);
182                         assertNull(actualList.get(0).menuId);
183                 }
184
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);
196                 }
197
198                 
199 }