fixes for supporting non-gui application access provisioning
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / PortalAdminServiceImplTest.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  * 
37  */
38 package org.onap.portalapp.portal.service;
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.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48 import javax.servlet.http.HttpServletResponse;
49
50 import org.hibernate.Session;
51 import org.hibernate.SessionFactory;
52 import org.hibernate.Transaction;
53 import org.hibernate.criterion.Criterion;
54 import org.hibernate.criterion.Restrictions;
55 import org.junit.After;
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.junit.runner.RunWith;
59 import org.mockito.InjectMocks;
60 import org.mockito.Matchers;
61 import org.mockito.Mock;
62 import org.mockito.Mockito;
63 import org.mockito.MockitoAnnotations;
64 import org.onap.portalapp.portal.core.MockEPUser;
65 import org.onap.portalapp.portal.domain.EPApp;
66 import org.onap.portalapp.portal.domain.EPUser;
67 import org.onap.portalapp.portal.domain.EpAppType;
68 import org.onap.portalapp.portal.transport.FieldsValidator;
69 import org.onap.portalapp.portal.transport.PortalAdmin;
70 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
71 import org.onap.portalapp.portal.utils.EcompPortalUtils;
72 import org.onap.portalapp.portal.utils.PortalConstants;
73 import org.onap.portalsdk.core.service.DataAccessService;
74 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
75 import org.onap.portalsdk.core.util.SystemProperties;
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.springframework.http.HttpEntity;
80 import org.springframework.http.HttpMethod;
81 import org.springframework.http.HttpStatus;
82 import org.springframework.http.ResponseEntity;
83 import org.springframework.web.client.RestTemplate;
84
85 @RunWith(PowerMockRunner.class)
86 @PrepareForTest({ EcompPortalUtils.class, Criterion.class, Restrictions.class, PortalConstants.class,
87                 SystemProperties.class, EPCommonSystemProperties.class })
88 public class PortalAdminServiceImplTest {
89
90         @Mock
91         DataAccessService dataAccessService = new DataAccessServiceImpl();
92
93         @Mock 
94         EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl(); 
95         
96         @Mock
97         SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
98
99         @Mock
100         SessionFactory sessionFactory;
101
102         @Mock
103         Session session;
104
105         @Mock
106         Transaction transaction;
107
108         @Mock
109         RestTemplate template = new RestTemplate();
110
111         @Before
112         public void setup() {
113                 MockitoAnnotations.initMocks(this);
114                 Mockito.when(sessionFactory.openSession()).thenReturn(session);
115                 Mockito.when(session.beginTransaction()).thenReturn(transaction);
116         }
117
118         @After
119         public void after() {
120                 session.close();
121         }
122
123         @InjectMocks
124         PortalAdminServiceImpl portalAdminServiceImpl = new PortalAdminServiceImpl();
125
126         public EPApp mockApp() {
127                 EPApp app = new EPApp();
128                 app.setName("Test");
129                 app.setImageUrl("test");
130                 app.setNameSpace("com.test.app");
131                 app.setRolesInAAF(true);
132                 app.setAppDescription("test");
133                 app.setAppNotes("test");
134                 app.setLandingPage("test");
135                 app.setId((long) 1);
136                 app.setAppRestEndpoint("test");
137                 app.setAlternateLandingPage("test");
138                 app.setName("test");
139                 app.setMlAppName("test");
140                 app.setMlAppAdminId("test");
141                 app.setAppBasicAuthUsername("test");
142                 app.setAppBasicAuthPassword("test");
143                 app.setOpen(false);
144                 app.setEnabled(true);
145                 app.setUebKey("test");
146                 app.setUebSecret("test");
147                 app.setUebTopicName("test");
148                 app.setAppType(EpAppType.GUI);
149                 return app;
150         }
151
152         MockEPUser mockUser = new MockEPUser();
153
154         @SuppressWarnings("unchecked")
155         @Test
156         public void getPortalAdminsTest() {
157                 PowerMockito.mockStatic(SystemProperties.class);
158                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
159                 List<PortalAdmin> portalAdmins = new ArrayList<>();
160                 PortalAdmin portalAdmin = new PortalAdmin();
161                 portalAdmin.setFirstName("guest");
162                 portalAdmin.setLastName("test");
163                 portalAdmin.setLoginId("test");
164                 portalAdmin.setUserId(1l);
165                 portalAdmins.add(portalAdmin);
166                 Map<String, String> params = new HashMap<>();
167                 params.put("adminRoleId", "1");
168                 Mockito.when((List<PortalAdmin>) dataAccessService.executeNamedQuery("getPortalAdmins", params, null))
169                                 .thenReturn(portalAdmins);
170                 List<PortalAdmin> actual = portalAdminServiceImpl.getPortalAdmins();
171                 assertEquals(1, actual.size());
172         }
173
174         @Test
175         public void getPortalAdminsExceptionTest() {
176                 PowerMockito.mockStatic(SystemProperties.class);
177                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
178                 List<PortalAdmin> portalAdmins = new ArrayList<>();
179                 PortalAdmin portalAdmin = new PortalAdmin();
180                 portalAdmin.setFirstName("guest");
181                 portalAdmin.setLastName("test");
182                 portalAdmin.setLoginId("test");
183                 portalAdmin.setUserId(1l);
184                 portalAdmins.add(portalAdmin);
185                 Map<String, String> params = new HashMap<>();
186                 params.put("adminRoleId", "1");
187                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).executeNamedQuery("getPortalAdmins", params,
188                                 null);
189                 List<PortalAdmin> actual = portalAdminServiceImpl.getPortalAdmins();
190                 assertNull(actual);
191         }
192         
193         @SuppressWarnings("unchecked")
194         @Test
195         public void createPortalAdminNewUserTest() {
196                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
197                 PowerMockito.mockStatic(EcompPortalUtils.class);
198                 PowerMockito.mockStatic(PortalConstants.class);
199                 PowerMockito.mockStatic(SystemProperties.class);
200                 PowerMockito.mockStatic(Restrictions.class);
201                 PowerMockito.mockStatic(Criterion.class);
202                 EPUser user = mockUser.mockEPUser();
203                 EPApp app = mockApp();
204                 List<EPUser> users = new ArrayList<>();
205                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
206                 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getOrgUserId());
207                 restrictionsList.add(orgUserIdCriterion);
208                 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null))
209                                 .thenReturn(users);
210                 List<PortalAdmin> portalAdmins = new ArrayList<>();
211                 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any() , Matchers.anyMap()))
212                                 .thenReturn(portalAdmins);
213                 Mockito.when(searchServiceImpl.searchUserByUserId(user.getOrgUserId())).thenReturn(user);
214                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
215                 Mockito.when(
216                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
217                                 .thenReturn(true);
218                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
219                                 .thenReturn("@test.com");
220                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app);
221                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
222                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
223                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
224                 FieldsValidator actual = portalAdminServiceImpl.createPortalAdmin(user.getOrgUserId());
225                 FieldsValidator expected = new FieldsValidator();
226                 expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK));
227         }
228
229         @SuppressWarnings("unchecked")
230         @Test
231         public void createPortalAdminExistingUserTest() {
232                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
233                 PowerMockito.mockStatic(EcompPortalUtils.class);
234                 PowerMockito.mockStatic(PortalConstants.class);
235                 PowerMockito.mockStatic(SystemProperties.class);
236                 PowerMockito.mockStatic(Restrictions.class);
237                 PowerMockito.mockStatic(Criterion.class);
238                 EPUser user = mockUser.mockEPUser();
239                 EPApp app = mockApp();
240                 List<EPUser> users = new ArrayList<>();
241                 users.add(user);
242                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
243                 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getOrgUserId());
244                 restrictionsList.add(orgUserIdCriterion);
245                 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null))
246                                 .thenReturn(users);
247                 List<PortalAdmin> portalAdmins = new ArrayList<>();
248                 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any() , Matchers.anyMap()))
249                                 .thenReturn(portalAdmins);
250                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
251                 Mockito.when(
252                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
253                                 .thenReturn(true);
254                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
255                                 .thenReturn("@test.com");
256                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app);
257                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
258                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
259                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
260                 FieldsValidator actual = portalAdminServiceImpl.createPortalAdmin(user.getOrgUserId());
261                 FieldsValidator expected = new FieldsValidator();
262                 expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK));
263         }
264
265         @SuppressWarnings("unchecked")
266         @Test
267         public void deletePortalAdminTest() {
268                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
269                 PowerMockito.mockStatic(EcompPortalUtils.class);
270                 PowerMockito.mockStatic(PortalConstants.class);
271                 PowerMockito.mockStatic(SystemProperties.class);
272                 PowerMockito.mockStatic(Restrictions.class);
273                 PowerMockito.mockStatic(Criterion.class);
274                 EPUser user = mockUser.mockEPUser();
275                 EPApp app = mockApp();
276                 List<EPUser> users = new ArrayList<>();
277                 users.add(user);
278                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
279                 Criterion orgUserIdCriterion = Restrictions.eq("id", user.getId());
280                 restrictionsList.add(orgUserIdCriterion);
281                 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null))
282                                 .thenReturn(users);
283                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
284                 Mockito.when(
285                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
286                                 .thenReturn(true);
287                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
288                                 .thenReturn("@test.com");
289                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app);
290                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
291                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
292                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
293                 FieldsValidator actual = portalAdminServiceImpl.deletePortalAdmin(user.getId());
294                 FieldsValidator expected = new FieldsValidator();
295                 expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK));
296         }
297
298 }