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.assertNotNull;
41 import static org.junit.Assert.assertNull;
43 import java.util.ArrayList;
44 import java.util.Date;
45 import java.util.LinkedHashMap;
46 import java.util.List;
49 import java.util.TreeSet;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
54 import org.json.JSONObject;
55 import org.junit.Before;
56 import org.junit.Test;
57 import org.junit.runner.RunWith;
58 import org.mockito.InjectMocks;
59 import org.mockito.Matchers;
60 import org.mockito.Mock;
61 import org.mockito.Mockito;
62 import org.mockito.MockitoAnnotations;
63 import org.onap.portalsdk.core.command.PostSearchBean;
64 import org.onap.portalsdk.core.command.support.SearchResult;
65 import org.onap.portalsdk.core.domain.App;
66 import org.onap.portalsdk.core.domain.Role;
67 import org.onap.portalsdk.core.domain.RoleFunction;
68 import org.onap.portalsdk.core.domain.User;
69 import org.onap.portalsdk.core.domain.UserApp;
70 import org.onap.portalsdk.core.service.AppService;
71 import org.onap.portalsdk.core.service.DataAccessService;
72 import org.onap.portalsdk.core.service.LdapService;
73 import org.onap.portalsdk.core.service.PostSearchService;
74 import org.onap.portalsdk.core.util.SystemProperties;
75 import org.onap.portalsdk.external.authorization.util.EcompExternalAuthProperties;
76 import org.onap.portalsdk.external.authorization.util.EcompExternalAuthUtils;
77 import org.onap.portalsdk.external.framework.MockitoTestSuite;
78 import org.powermock.api.mockito.PowerMockito;
79 import org.powermock.core.classloader.annotations.PrepareForTest;
80 import org.powermock.modules.junit4.PowerMockRunner;
81 import org.springframework.http.HttpEntity;
82 import org.springframework.http.HttpHeaders;
83 import org.springframework.http.HttpMethod;
84 import org.springframework.http.HttpStatus;
85 import org.springframework.http.ResponseEntity;
86 import org.springframework.web.client.RestTemplate;
88 import com.fasterxml.jackson.databind.ObjectMapper;
90 @SuppressWarnings({ "rawtypes", "unchecked" })
91 @RunWith(PowerMockRunner.class)
92 @PrepareForTest({ EcompExternalAuthProperties.class, EcompExternalAuthUtils.class })
93 public class UserApiServiceImplTest {
95 private static final String APP_ID = "appId";
97 private static final String PRIORITY = "priority";
99 private static final String ACTIVE = "active";
101 private static final String ROLE_NAME = "name";
103 private static final String APP_ROLE_ID = "appRoleId";
105 private static final String ID = "id";
108 private UserApiServiceImpl UserApiServiceImpl;
111 private DataAccessService dataAccessService;
114 private LoginExternalAuthService loginAAFService;
117 private LdapService ldapService;
120 private PostSearchService postSearchService;
123 private AppService appService;
126 RestTemplate template = new RestTemplate();
129 public void setup() throws Exception {
130 PowerMockito.mockStatic(EcompExternalAuthProperties.class);
131 PowerMockito.mockStatic(EcompExternalAuthUtils.class);
132 PowerMockito.mockStatic(SystemProperties.class);
133 Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_NAMESPACE))
134 .thenReturn("com.test.app2");
135 Mockito.when(EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(new HttpHeaders());
136 Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_USER_DOMAIN))
137 .thenReturn("@test.com");
138 MockitoAnnotations.initMocks(this);
141 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
143 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
144 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
146 private User userObj() {
147 User user = new User();
148 user.setEmail("test@mail.com");
149 user.setFirstName("Test_firstname");
150 user.setHrid("test_hrid");
151 user.setJobTitle("test_jobtitle");
152 user.setLastName("test_lastname");
153 user.setLoginId("test123");
154 user.setOrgManagerUserId("test456");
155 user.setMiddleInitial("test_middlename");
156 user.setOrgCode("testcode");
158 user.setOrgUserId("test123");
159 user.setActive(true);
160 user.setLastLoginDate(new Date());
161 RoleFunction roleFunction = new RoleFunction();
162 roleFunction.setId(12L);
163 roleFunction.setName("Role Function");
165 Set roleFunctions = new TreeSet();
166 roleFunctions.add(roleFunction);
168 Role role = new Role();
169 role.setName("Role");
170 role.setActive(true);
171 role.setRoleFunctions(roleFunctions);
172 Set userApps = new TreeSet();
173 UserApp userApp = new UserApp();
174 userApp.setUserId(1L);
175 userApp.setApp(getApp());
176 userApp.setRole(role);
177 userApps.add(userApp);
178 user.setUserApps(userApps);
182 public App getApp() {
184 app.setId(new Long(1));
185 app.setName("Default");
190 public void getUserTest() throws Exception {
191 ObjectMapper mapper = new ObjectMapper();
192 Map<String, String> roleDesc = new LinkedHashMap<>();
193 roleDesc.put(ID, "1");
194 roleDesc.put(ROLE_NAME, "test_role");
195 roleDesc.put(ACTIVE, String.valueOf(true));
196 roleDesc.put(PRIORITY, String.valueOf(1));
197 roleDesc.put(APP_ID, String.valueOf(1));
198 roleDesc.put(APP_ROLE_ID, String.valueOf(1l));
199 String addDesc = mapper.writeValueAsString(roleDesc);
200 JSONObject mockJsonObjectRole = new JSONObject();
201 JSONObject mockJsonObjectRole2 = new JSONObject();
202 JSONObject mockJsonObjectRole3 = new JSONObject();
203 JSONObject mockJsonObjectRole4 = new JSONObject();
204 JSONObject mockJsonObjectRole5 = new JSONObject();
205 JSONObject mockJsonObjectPerm1 = new JSONObject();
206 JSONObject mockJsonObjectPerm2 = new JSONObject();
207 mockJsonObjectPerm1.put("type", "com.test.app2.test_type");
208 mockJsonObjectPerm1.put("instance", "test_instance");
209 mockJsonObjectPerm1.put("action", "*");
210 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
211 mockJsonObjectPerm2.put("instance", "test_instance2");
212 mockJsonObjectPerm2.put("action", "*");
213 List<JSONObject> permsList = new ArrayList<>();
214 permsList.add(mockJsonObjectPerm1);
215 permsList.add(mockJsonObjectPerm2);
216 mockJsonObjectRole.put("name", "com.test.app2.test_role");
217 mockJsonObjectRole2.put("name", "com.test.app2.test_role2");
218 mockJsonObjectRole2.put("perms", permsList);
219 mockJsonObjectRole2.put("description", addDesc);
220 mockJsonObjectRole3.put("name", "com.test.app2.Account_Administrator");
221 mockJsonObjectRole4.put("name", "com.test.app2.admin");
222 mockJsonObjectRole5.put("name", "com.test.app2.owner");
223 List<JSONObject> userRolesList = new ArrayList<>();
224 JSONObject mockJsonObjectFinalUserRole = new JSONObject();
225 userRolesList.add(mockJsonObjectRole);
226 userRolesList.add(mockJsonObjectRole2);
227 userRolesList.add(mockJsonObjectRole3);
228 userRolesList.add(mockJsonObjectRole4);
229 userRolesList.add(mockJsonObjectRole5);
230 mockJsonObjectFinalUserRole.put("role", userRolesList);
231 Mockito.when(EcompExternalAuthUtils.isJSONValid(addDesc)).thenReturn(true);
232 ResponseEntity<String> response = new ResponseEntity<>(mockJsonObjectFinalUserRole.toString(), HttpStatus.OK);
233 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
234 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(response);
235 Mockito.when(EcompExternalAuthUtils.checkNameSpaceMatching(Matchers.anyString(), Matchers.anyString()))
237 Mockito.when(loginAAFService.findUserWithoutPwd("test123")).thenReturn(userObj());
238 Mockito.when(appService.getApp(1l)).thenReturn(getApp());
239 User actual = UserApiServiceImpl.getUser("test123", mockedRequest);
240 assertNotNull(actual);
244 public void getNewUserNullExceptionTest() throws Exception {
245 JSONObject mockJsonObjectRole = new JSONObject();
246 JSONObject mockJsonObjectRole2 = new JSONObject();
247 JSONObject mockJsonObjectRole3 = new JSONObject();
248 JSONObject mockJsonObjectPerm1 = new JSONObject();
249 JSONObject mockJsonObjectPerm2 = new JSONObject();
250 mockJsonObjectPerm1.put("type", "com.test.app2.test_type");
251 mockJsonObjectPerm1.put("instance", "test_instance");
252 mockJsonObjectPerm1.put("action", "*");
253 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
254 mockJsonObjectPerm2.put("instance", "test_instance2");
255 mockJsonObjectPerm2.put("action", "*");
256 List<JSONObject> permsList = new ArrayList<>();
257 permsList.add(mockJsonObjectPerm1);
258 permsList.add(mockJsonObjectPerm2);
259 mockJsonObjectRole.put("name", "com.test.app2.test_role");
260 mockJsonObjectRole2.put("name", "com.test.app2.test_role2");
261 mockJsonObjectRole2.put("perms", permsList);
262 mockJsonObjectRole3.put("name", "com.test.app2.Account_Administrator");
263 List<JSONObject> userRolesList = new ArrayList<>();
264 JSONObject mockJsonObjectFinalUserRole = new JSONObject();
265 userRolesList.add(mockJsonObjectRole);
266 userRolesList.add(mockJsonObjectRole2);
267 userRolesList.add(mockJsonObjectRole3);
268 mockJsonObjectFinalUserRole.put("role", userRolesList);
269 ResponseEntity<String> response = new ResponseEntity<>(mockJsonObjectFinalUserRole.toString(), HttpStatus.OK);
270 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
271 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(response);
272 Mockito.when(EcompExternalAuthUtils.checkNameSpaceMatching(Matchers.anyString(), Matchers.anyString()))
274 Mockito.when(loginAAFService.findUserWithoutPwd("test123")).thenReturn(null);
275 Mockito.when(appService.getApp(1l)).thenReturn(getApp());
276 PostSearchBean postSearchBean = new PostSearchBean();
277 postSearchBean.setOrgUserId("test123");
278 SearchResult result = new SearchResult();
279 result.add(userObj());
280 Mockito.when(ldapService.searchPost(postSearchBean.getUser(), postSearchBean.getSortBy1(),
281 postSearchBean.getSortBy2(), postSearchBean.getSortBy3(), postSearchBean.getPageNo(),
282 postSearchBean.getNewDataSize(), 1)).thenReturn(result);
283 User user = UserApiServiceImpl.getUser("test123", mockedRequest);
288 public void getRoleFunctionsTest() throws Exception {
289 JSONObject mockJsonObjectPerms = new JSONObject();
290 JSONObject mockJsonObjectPerm1 = new JSONObject();
291 JSONObject mockJsonObjectPerm2 = new JSONObject();
292 JSONObject mockJsonObjectPerm3 = new JSONObject();
293 mockJsonObjectPerm1.put("type", "com.test.app2.test_type");
294 mockJsonObjectPerm1.put("instance", "test_instance");
295 mockJsonObjectPerm1.put("action", "*");
296 mockJsonObjectPerm2.put("type", "com.test.app2.test_type2");
297 mockJsonObjectPerm2.put("instance", "test_instance2");
298 mockJsonObjectPerm2.put("action", "*");
299 mockJsonObjectPerm2.put("description", "test_name");
300 mockJsonObjectPerm3.put("type", "com.test.app3.test_type3");
301 mockJsonObjectPerm3.put("instance", "test_instance3");
302 mockJsonObjectPerm3.put("action", "*");
303 List<JSONObject> permsList = new ArrayList<>();
304 permsList.add(mockJsonObjectPerm1);
305 permsList.add(mockJsonObjectPerm2);
306 mockJsonObjectPerms.put("perm", permsList);
307 ResponseEntity<String> response = new ResponseEntity<>(mockJsonObjectPerms.toString(), HttpStatus.OK);
308 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
309 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(response);
310 List<RoleFunction> actual = UserApiServiceImpl.getRoleFunctions("test123");
311 assertNotNull(actual);