2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017-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.portalapp.portal.service;
40 import static org.junit.Assert.assertEquals;
41 import static org.mockito.Mockito.*;
43 import java.io.ByteArrayInputStream;
44 import java.io.IOException;
45 import java.io.UnsupportedEncodingException;
46 import java.net.HttpURLConnection;
47 import java.util.ArrayList;
48 import java.util.Date;
49 import java.util.List;
51 import org.hibernate.criterion.Criterion;
52 import org.hibernate.criterion.Restrictions;
53 import org.json.simple.JSONObject;
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.InjectMocks;
58 import org.mockito.Mock;
59 import org.mockito.Mockito;
60 import org.mockito.MockitoAnnotations;
61 import org.onap.portalapp.portal.domain.EPUser;
62 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
63 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
64 import org.onap.portalsdk.core.service.DataAccessService;
65 import org.onap.portalsdk.core.util.SystemProperties;
66 import org.powermock.api.mockito.PowerMockito;
67 import org.powermock.core.classloader.annotations.PrepareForTest;
68 import org.powermock.modules.junit4.PowerMockRunner;
70 @RunWith(PowerMockRunner.class)
71 @PrepareForTest({ SystemProperties.class, EPCommonSystemProperties.class, CipherUtil.class })
72 public class UserServiceImplTest {
74 private static final String TEST = "test";
77 UserServiceImpl userServiceImpl = new UserServiceImpl();
80 DataAccessService dataAccessService;
83 HttpURLConnection con;
87 MockitoAnnotations.initMocks(this);
91 public void getUserByUserIdTest() throws UnsupportedEncodingException, IOException {
93 PowerMockito.mockStatic(SystemProperties.class);
94 EPUser user = buildEpUser();
95 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
96 Mockito.when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER))
97 .thenReturn("http://www.test.com");
98 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
100 JSONObject response = new JSONObject();
101 JSONObject userJson = new JSONObject();
102 userJson.put("id", 1);
103 userJson.put("givenName", "Guest");
104 userJson.put("familyName", TEST);
105 userJson.put("email", "test@123.com");
106 List<JSONObject> userListJson = new ArrayList<>();
107 userListJson.add(userJson);
108 response.put("response", userListJson);
109 ByteArrayInputStream getBody = new ByteArrayInputStream(response.toString().getBytes("UTF-8"));
110 PowerMockito.when(connection.getInputStream()).thenReturn(getBody);
111 userServiceImpl.getUserByUserId(user.getOrgUserId());
115 public void testGetUserByNameInvalidODC() throws Exception {
117 PowerMockito.mockStatic(SystemProperties.class);
118 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn(TEST);
119 List list = new ArrayList<>();
120 StringBuffer criteria = new StringBuffer();
121 String firstName = TEST;
122 String lastName = TEST;
123 if (firstName != null)
124 criteria.append(" where first_name = '").append(firstName).append("'");
125 if (lastName != null)
126 criteria.append(" where last_name = '").append(lastName).append("'");
127 when(dataAccessService.getList(EPUser.class, criteria.toString(), null, null)).thenReturn(list);
128 userServiceImpl.getUserByFirstLastName(TEST, TEST);
133 public void testGetUserByName() throws Exception {
135 PowerMockito.mockStatic(SystemProperties.class);
136 EPUser user = buildEpUser();
137 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
138 Mockito.when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER))
139 .thenReturn("http://www.test.com");
140 //HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
141 JSONObject response = new JSONObject();
142 JSONObject userJson = new JSONObject();
143 userJson.put("id", 1);
144 userJson.put("givenName", "Guest");
145 userJson.put("familyName", TEST);
146 userJson.put("email", "test@123.com");
147 List<JSONObject> userListJson = new ArrayList<>();
148 userListJson.add(userJson);
149 response.put("response", userListJson);
150 //ByteArrayInputStream getBody = new ByteArrayInputStream(response.toString().getBytes("UTF-8"));
151 //PowerMockito.when(connection.getInputStream()).thenReturn(getBody);
152 userServiceImpl.getUserByFirstLastName(TEST, TEST);
157 public void saveNewUserTest() throws Exception {
159 PowerMockito.mockStatic(Criterion.class);
160 PowerMockito.mockStatic(CipherUtil.class);
161 EPUser user = buildEpUser();
162 List users = new ArrayList<>();
164 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
165 List<Criterion> restrictionsList = new ArrayList<Criterion>();
166 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getLoginId());
167 restrictionsList.add(orgUserIdCriterion);
169 when( dataAccessService.getList(EPUser.class, null, restrictionsList, null)).thenReturn(users);
170 String actual = userServiceImpl.saveNewUser(user, "No");
171 assertEquals("success", actual);
176 public void saveNewUserEmptyTest() throws Exception {
177 PowerMockito.mockStatic(Restrictions.class);
178 PowerMockito.mockStatic(Criterion.class);
179 PowerMockito.mockStatic(CipherUtil.class);
180 EPUser user = buildEpUser();
181 List users = new ArrayList<>();
182 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
183 List<Criterion> restrictionsList = new ArrayList<Criterion>();
184 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getLoginId());
185 restrictionsList.add(orgUserIdCriterion);
186 StringBuffer criteria = new StringBuffer();
187 criteria.append(" where org_user_id = '").append(user.getLoginId()).append("'");
188 Mockito.when(dataAccessService.getList(EPUser.class, criteria.toString(), null, null)).thenReturn(users);
189 String actual = userServiceImpl.saveNewUser(user, "No");
190 assertEquals("success", actual);
195 public void saveUser()throws Exception {
197 EPUser user = buildEpUser();
198 userServiceImpl.saveUser(user);
202 EPUser buildEpUser() {
203 EPUser epUser = new EPUser();
205 epUser.setId((long) 1);
206 epUser.setManagerId((long) 1234);
207 epUser.setFirstName(TEST);
208 epUser.setLastName(TEST);
209 epUser.setMiddleInitial(TEST);
210 epUser.setPhone(TEST);
212 epUser.setCellular(TEST);
213 epUser.setEmail(TEST);
214 epUser.setAddressId((long) 123);
215 epUser.setAlertMethodCd(TEST);
216 epUser.setHrid(TEST);
217 epUser.setOrgUserId(TEST);
218 epUser.setOrgCode(TEST);
219 epUser.setAddress1(TEST);
220 epUser.setAddress2(TEST);
221 epUser.setCity(TEST);
222 epUser.setState(TEST);
223 epUser.setZipCode(TEST);
224 epUser.setCountry(TEST);
225 epUser.setOrgManagerUserId(TEST);
226 epUser.setLocationClli(TEST);
227 epUser.setBusinessCountryCode(TEST);
228 epUser.setBusinessCountryName(TEST);
229 epUser.setBusinessUnit(TEST);
230 epUser.setBusinessUnitName(TEST);
231 epUser.setDepartment(TEST);
232 epUser.setDepartmentName(TEST);
233 epUser.setCompanyCode(TEST);
234 epUser.setCompany(TEST);
235 epUser.setZipCodeSuffix(TEST);
236 epUser.setJobTitle(TEST);
237 epUser.setCommandChain(TEST);
238 epUser.setSiloStatus(TEST);
239 epUser.setCostCenter(TEST);
240 epUser.setFinancialLocCode(TEST);
241 epUser.setLoginId(TEST);
242 epUser.setLoginPwd(TEST);
243 epUser.setLastLoginDate(new Date());
244 epUser.setActive(false);
245 epUser.setInternal(false);
246 epUser.setSelectedProfileId((long) 12345);
247 epUser.setTimeZoneId((long) 12345);
248 epUser.setOnline(false);
249 epUser.setChatId(TEST);