2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.external.authorization.service;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertNull;
44 import java.util.ArrayList;
45 import java.util.Date;
46 import java.util.LinkedHashMap;
47 import java.util.List;
50 import java.util.TreeSet;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
55 import org.json.JSONObject;
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.portalsdk.core.command.PostSearchBean;
65 import org.onap.portalsdk.core.command.support.SearchResult;
66 import org.onap.portalsdk.core.domain.App;
67 import org.onap.portalsdk.core.domain.Role;
68 import org.onap.portalsdk.core.domain.RoleFunction;
69 import org.onap.portalsdk.core.domain.User;
70 import org.onap.portalsdk.core.domain.UserApp;
71 import org.onap.portalsdk.core.service.AppService;
72 import org.onap.portalsdk.core.service.DataAccessService;
73 import org.onap.portalsdk.core.service.LdapService;
74 import org.onap.portalsdk.core.service.PostSearchService;
75 import org.onap.portalsdk.core.util.SystemProperties;
76 import org.onap.portalsdk.external.authorization.util.EcompExternalAuthProperties;
77 import org.onap.portalsdk.external.authorization.util.EcompExternalAuthUtils;
78 import org.onap.portalsdk.external.framework.MockitoTestSuite;
79 import org.powermock.api.mockito.PowerMockito;
80 import org.powermock.core.classloader.annotations.PrepareForTest;
81 import org.powermock.modules.junit4.PowerMockRunner;
82 import org.springframework.http.HttpEntity;
83 import org.springframework.http.HttpHeaders;
84 import org.springframework.http.HttpMethod;
85 import org.springframework.http.HttpStatus;
86 import org.springframework.http.ResponseEntity;
87 import org.springframework.web.client.HttpClientErrorException;
88 import org.springframework.web.client.RestTemplate;
90 import com.fasterxml.jackson.databind.ObjectMapper;
92 @SuppressWarnings({ "rawtypes", "unchecked" })
93 @RunWith(PowerMockRunner.class)
94 @PrepareForTest({ EcompExternalAuthProperties.class, EcompExternalAuthUtils.class })
95 public class UserApiServiceImplTest {
97 private static final String APP_ID = "appId";
99 private static final String PRIORITY = "priority";
101 private static final String ACTIVE = "active";
103 private static final String ROLE_NAME = "name";
105 private static final String APP_ROLE_ID = "appRoleId";
107 private static final String ID = "id";
110 private UserApiServiceImpl UserApiServiceImpl;
113 private DataAccessService dataAccessService;
116 private LoginExternalAuthService loginAAFService;
119 private LdapService ldapService;
122 private PostSearchService postSearchService;
125 private AppService appService;
128 RestTemplate template = new RestTemplate();
131 public void setup() throws Exception {
132 PowerMockito.mockStatic(EcompExternalAuthProperties.class);
133 PowerMockito.mockStatic(EcompExternalAuthUtils.class);
134 PowerMockito.mockStatic(SystemProperties.class);
135 Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_NAMESPACE))
136 .thenReturn("com.test.app2");
137 Mockito.when(EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth(Matchers.anyString(), Matchers.anyString())).thenReturn(new HttpHeaders());
138 Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_USER_DOMAIN))
139 .thenReturn("@test.com");
140 MockitoAnnotations.initMocks(this);
143 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
145 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
146 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
148 private User userObj() {
149 User user = new User();
150 user.setEmail("test@mail.com");
151 user.setFirstName("Test_firstname");
152 user.setHrid("test_hrid");
153 user.setJobTitle("test_jobtitle");
154 user.setLastName("test_lastname");
155 user.setLoginId("test123");
156 user.setOrgManagerUserId("test456");
157 user.setMiddleInitial("test_middlename");
158 user.setOrgCode("testcode");
160 user.setOrgUserId("test123");
161 user.setActive(true);
162 user.setLastLoginDate(new Date());
163 RoleFunction roleFunction = new RoleFunction();
164 roleFunction.setId(12L);
165 roleFunction.setName("Role Function");
167 Set roleFunctions = new TreeSet();
168 roleFunctions.add(roleFunction);
170 Role role = new Role();
171 role.setName("Role");
172 role.setActive(true);
173 role.setRoleFunctions(roleFunctions);
174 Set userApps = new TreeSet();
175 UserApp userApp = new UserApp();
176 userApp.setUserId(1L);
177 userApp.setApp(getApp());
178 userApp.setRole(role);
179 userApps.add(userApp);
180 user.setUserApps(userApps);
184 public App getApp() {
186 app.setId(new Long(1));
187 app.setName("Default");
192 public void getUserTest() throws Exception {
193 ObjectMapper mapper = new ObjectMapper();
194 Map<String, String> roleDesc = new LinkedHashMap<>();
195 roleDesc.put(ID, "1");
196 roleDesc.put(ROLE_NAME, "test_role");
197 roleDesc.put(ACTIVE, String.valueOf(true));
198 roleDesc.put(PRIORITY, String.valueOf(1));
199 roleDesc.put(APP_ID, String.valueOf(1));
200 roleDesc.put(APP_ROLE_ID, String.valueOf(1l));
201 String addDesc = mapper.writeValueAsString(roleDesc);
202 JSONObject mockJsonObjectRole = new JSONObject();
203 JSONObject mockJsonObjectRole2 = new JSONObject();
204 JSONObject mockJsonObjectRole3 = new JSONObject();
205 JSONObject mockJsonObjectRole4 = new JSONObject();
206 JSONObject mockJsonObjectRole5 = new JSONObject();
207 JSONObject mockJsonObjectPerm1 = new JSONObject();
208 JSONObject mockJsonObjectPerm2 = new JSONObject();
209 mockJsonObjectPerm1.put("type", "com.test.app2.test_type");
210 mockJsonObjectPerm1.put("instance", "test_instance");
211 mockJsonObjectPerm1.put("action", "*");
212 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
213 mockJsonObjectPerm2.put("instance", "test_instance2");
214 mockJsonObjectPerm2.put("action", "*");
215 List<JSONObject> permsList = new ArrayList<>();
216 permsList.add(mockJsonObjectPerm1);
217 permsList.add(mockJsonObjectPerm2);
218 mockJsonObjectRole.put("name", "com.test.app2.test_role");
219 mockJsonObjectRole2.put("name", "com.test.app2.test_role2");
220 mockJsonObjectRole2.put("perms", permsList);
221 mockJsonObjectRole2.put("description", addDesc);
222 mockJsonObjectRole3.put("name", "com.test.app2.Account_Administrator");
223 mockJsonObjectRole4.put("name", "com.test.app2.admin");
224 mockJsonObjectRole5.put("name", "com.test.app2.owner");
225 List<JSONObject> userRolesList = new ArrayList<>();
226 JSONObject mockJsonObjectFinalUserRole = new JSONObject();
227 userRolesList.add(mockJsonObjectRole);
228 userRolesList.add(mockJsonObjectRole2);
229 userRolesList.add(mockJsonObjectRole3);
230 userRolesList.add(mockJsonObjectRole4);
231 userRolesList.add(mockJsonObjectRole5);
232 mockJsonObjectFinalUserRole.put("role", userRolesList);
233 Mockito.when(EcompExternalAuthUtils.isJSONValid(addDesc)).thenReturn(true);
234 ResponseEntity<String> response = new ResponseEntity<>(mockJsonObjectFinalUserRole.toString(), HttpStatus.OK);
235 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
236 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(response);
237 Mockito.when(EcompExternalAuthUtils.checkNameSpaceMatching(Matchers.anyString(), Matchers.anyString()))
239 Mockito.when(loginAAFService.findUserWithoutPwd("test123")).thenReturn(userObj());
240 Mockito.when(appService.getApp(1l)).thenReturn(getApp());
241 User actual = UserApiServiceImpl.getUser("test123", mockedRequest);
242 assertNotNull(actual);
246 public void getNewUserNullExceptionTest() throws Exception {
247 JSONObject mockJsonObjectRole = new JSONObject();
248 JSONObject mockJsonObjectRole2 = new JSONObject();
249 JSONObject mockJsonObjectRole3 = new JSONObject();
250 JSONObject mockJsonObjectPerm1 = new JSONObject();
251 JSONObject mockJsonObjectPerm2 = new JSONObject();
252 mockJsonObjectPerm1.put("type", "com.test.app2.test_type");
253 mockJsonObjectPerm1.put("instance", "test_instance");
254 mockJsonObjectPerm1.put("action", "*");
255 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
256 mockJsonObjectPerm2.put("instance", "test_instance2");
257 mockJsonObjectPerm2.put("action", "*");
258 List<JSONObject> permsList = new ArrayList<>();
259 permsList.add(mockJsonObjectPerm1);
260 permsList.add(mockJsonObjectPerm2);
261 mockJsonObjectRole.put("name", "com.test.app2.test_role");
262 mockJsonObjectRole2.put("name", "com.test.app2.test_role2");
263 mockJsonObjectRole2.put("perms", permsList);
264 mockJsonObjectRole3.put("name", "com.test.app2.Account_Administrator");
265 List<JSONObject> userRolesList = new ArrayList<>();
266 JSONObject mockJsonObjectFinalUserRole = new JSONObject();
267 userRolesList.add(mockJsonObjectRole);
268 userRolesList.add(mockJsonObjectRole2);
269 userRolesList.add(mockJsonObjectRole3);
270 mockJsonObjectFinalUserRole.put("role", userRolesList);
271 ResponseEntity<String> response = new ResponseEntity<>(mockJsonObjectFinalUserRole.toString(), HttpStatus.OK);
272 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
273 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(response);
274 Mockito.when(EcompExternalAuthUtils.checkNameSpaceMatching(Matchers.anyString(), Matchers.anyString()))
276 Mockito.when(loginAAFService.findUserWithoutPwd("test123")).thenReturn(null);
277 Mockito.when(appService.getApp(1l)).thenReturn(getApp());
278 PostSearchBean postSearchBean = new PostSearchBean();
279 postSearchBean.setOrgUserId("test123");
280 SearchResult result = new SearchResult();
281 result.add(userObj());
282 Mockito.when(ldapService.searchPost(postSearchBean.getUser(), postSearchBean.getSortBy1(),
283 postSearchBean.getSortBy2(), postSearchBean.getSortBy3(), postSearchBean.getPageNo(),
284 postSearchBean.getNewDataSize(), 1)).thenReturn(result);
285 User user = UserApiServiceImpl.getUser("test123", mockedRequest);
290 public void getRoleFunctionsTest() throws Exception {
291 JSONObject mockJsonObjectPerms = mockUserPerms();
292 ResponseEntity<String> response = new ResponseEntity<>(mockJsonObjectPerms.toString(), HttpStatus.OK);
293 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
294 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(response);
295 List<RoleFunction> actual = UserApiServiceImpl.getRoleFunctions("test123");
296 assertNotNull(actual);
299 private JSONObject mockUserPerms() {
300 JSONObject mockJsonObjectPerms = new JSONObject();
301 JSONObject mockJsonObjectPerm1 = new JSONObject();
302 JSONObject mockJsonObjectPerm2 = new JSONObject();
303 JSONObject mockJsonObjectPerm3 = new JSONObject();
304 mockJsonObjectPerm1.put("type", "com.test.app2.test_type");
305 mockJsonObjectPerm1.put("instance", "test_instance");
306 mockJsonObjectPerm1.put("action", "*");
307 mockJsonObjectPerm2.put("type", "com.test.app2.test_type2");
308 mockJsonObjectPerm2.put("instance", "test_instance2");
309 mockJsonObjectPerm2.put("action", "*");
310 mockJsonObjectPerm2.put("description", "test_name");
311 mockJsonObjectPerm3.put("type", "com.test.app3.test_type3");
312 mockJsonObjectPerm3.put("instance", "test_instance3");
313 mockJsonObjectPerm3.put("action", "*");
314 List<JSONObject> permsList = new ArrayList<>();
315 permsList.add(mockJsonObjectPerm1);
316 permsList.add(mockJsonObjectPerm2);
317 mockJsonObjectPerms.put("perm", permsList);
318 return mockJsonObjectPerms;
322 public void checkUserExistsTest() throws Exception {
323 ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
324 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
325 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(response);
326 ResponseEntity<String> actual = UserApiServiceImpl.checkUserExists("test", "test");
327 assertNotNull(actual);
330 @Test(expected = HttpClientErrorException.class)
331 public void checkUserExistsExceptionTest() throws Exception {
332 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
333 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED));
334 UserApiServiceImpl.checkUserExists("test", "test");
338 public void getIfUserPermsExistsTest() throws Exception {
339 JSONObject mockJsonObjectPerms = mockUserPerms();
340 ResponseEntity<String> response = new ResponseEntity<>(mockJsonObjectPerms.toString(), HttpStatus.OK);
341 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
342 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(response);
343 UserApiServiceImpl.getIfUserPermsExists("test123@test.com");
346 @Test(expected = HttpClientErrorException.class)
347 public void getIfUserPermsExistsInvalidUserTest() throws Exception {
348 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
349 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED));
350 UserApiServiceImpl.getIfUserPermsExists("test1");