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