Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-os / src / test / java / org / openecomp / portalapp / portal / controller / ExternalAppsRestfulControllerOSTest.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.*;
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.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;
69 import org.slf4j.MDC;
70
71 @RunWith(PowerMockRunner.class)
72 @PrepareForTest({ MDC.class, EPSystemProperties.class , EcompPortalUtils.class})
73 public class ExternalAppsRestfulControllerOSTest {
74
75         
76         @InjectMocks
77         ExternalAppsRestfulController externalAppsRestfulController = new ExternalAppsRestfulController();
78         @Mock
79         FunctionalMenuService functionalMenuService;
80         
81         @Mock
82         EPLoginService epLoginService;
83         
84         @Mock
85         AdminRolesService adminRolesService;
86         
87         
88         
89         @Before
90         public void setup() {
91                 MockitoAnnotations.initMocks(this);
92         }
93
94         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
95
96         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
97         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
98         NullPointerException nullPointerException = new NullPointerException();
99
100         MockEPUser mockUser = new MockEPUser();
101         
102         @Test
103         public void getFavoritesForUserTest() throws Exception
104         {
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);
123         }
124         
125         @Test
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,
143                                 mockedResponse);
144                 assertNull(actualList.get(0).menuId);
145         }
146
147          @Test(expected = Exception.class)
148          public void getFunctionalMenuItemsForUserIfUSerNullTest() throws
149          Exception
150          {
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,
159          mockedResponse);
160          }
161          
162             @Test
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,
180                                         mockedResponse);
181                         assertNull(actualList.get(0).menuId);
182                 }
183
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);
195                 }
196
197                 
198 }