Merge "Fix sonar issues"
[portal.git] / ecomp-portal-BE-os / src / test / java / org / onap / portalapp / portal / service / UserServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017-2018 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.mockito.Mockito.*;
42
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;
50
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.portalapp.portal.utils.EPSystemProperties;
64 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
65 import org.onap.portalsdk.core.service.DataAccessService;
66 import org.onap.portalsdk.core.util.SystemProperties;
67 import org.powermock.api.mockito.PowerMockito;
68 import org.powermock.core.classloader.annotations.PrepareForTest;
69 import org.powermock.modules.junit4.PowerMockRunner;
70
71 @RunWith(PowerMockRunner.class)
72 @PrepareForTest({ SystemProperties.class, EPSystemProperties.class, CipherUtil.class })
73 public class UserServiceImplTest {
74
75         private static final String TEST = "test";
76
77         @InjectMocks
78         UserServiceImpl userServiceImpl = new UserServiceImpl();
79
80         @Mock
81         DataAccessService dataAccessService;
82
83         @Mock
84         HttpURLConnection con;
85
86         @Before
87         public void setup() {
88                 MockitoAnnotations.initMocks(this);
89         }
90
91         @Test
92         public void getUserByUserIdTest() throws UnsupportedEncodingException, IOException {
93
94                 PowerMockito.mockStatic(SystemProperties.class);
95                 EPUser user = buildEpUser();
96                 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
97                 Mockito.when(EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER))
98                                 .thenReturn("http://www.test.com");
99                 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
100
101                 JSONObject response = new JSONObject();
102                 JSONObject userJson = new JSONObject();
103                 userJson.put("id", 1);
104                 userJson.put("givenName", "Guest");
105                 userJson.put("familyName", TEST);
106                 userJson.put("email", "test@123.com");
107                 List<JSONObject> userListJson = new ArrayList<>();
108                 userListJson.add(userJson);
109                 response.put("response", userListJson);
110                 ByteArrayInputStream getBody = new ByteArrayInputStream(response.toString().getBytes("UTF-8"));
111                 PowerMockito.when(connection.getInputStream()).thenReturn(getBody);
112                 userServiceImpl.getUserByUserId(user.getOrgUserId());
113         }
114
115         @Test
116         public void testGetUserByNameInvalidODC() throws Exception {
117
118                 PowerMockito.mockStatic(SystemProperties.class);
119                 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn(TEST);
120                 List list = new ArrayList<>();
121                 StringBuffer criteria = new StringBuffer();
122                 String firstName = TEST;
123                 String lastName = TEST;
124                 if (firstName != null)
125                         criteria.append(" where first_name = '").append(firstName).append("'");
126                 if (lastName != null)
127                         criteria.append(" where last_name = '").append(lastName).append("'");
128                 when(dataAccessService.getList(EPUser.class, criteria.toString(), null, null)).thenReturn(list);
129                 userServiceImpl.getUserByFirstLastName(TEST, TEST);
130
131         }
132
133         @Test
134         public void testGetUserByName() throws Exception {
135
136                 PowerMockito.mockStatic(SystemProperties.class);
137                 EPUser user = buildEpUser();
138                 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
139                 Mockito.when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER))
140                                 .thenReturn("http://www.test.com");
141                 //HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
142                 JSONObject response = new JSONObject();
143                 JSONObject userJson = new JSONObject();
144                 userJson.put("id", 1);
145                 userJson.put("givenName", "Guest");
146                 userJson.put("familyName", TEST);
147                 userJson.put("email", "test@123.com");
148                 List<JSONObject> userListJson = new ArrayList<>();
149                 userListJson.add(userJson);
150                 response.put("response", userListJson);
151                 //ByteArrayInputStream getBody = new ByteArrayInputStream(response.toString().getBytes("UTF-8"));
152                 //PowerMockito.when(connection.getInputStream()).thenReturn(getBody);
153                 userServiceImpl.getUserByFirstLastName(TEST, TEST);
154
155         }
156
157         @Test
158         public void saveNewUserTest() throws Exception {
159                 PowerMockito.mockStatic(Restrictions.class);
160                 PowerMockito.mockStatic(Criterion.class);
161                 PowerMockito.mockStatic(CipherUtil.class);
162                 EPUser user = buildEpUser();
163                 List users = new ArrayList<>();
164                 users.add(user);
165                 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
166                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
167                 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getLoginId());
168                 restrictionsList.add(orgUserIdCriterion);
169                 StringBuffer criteria = new StringBuffer();
170                 criteria.append(" where org_user_id = '").append(user.getLoginId()).append("'");
171                 Mockito.when(dataAccessService.getList(EPUser.class, criteria.toString(), null, null)).thenReturn(users);
172                 String actual = userServiceImpl.saveNewUser(user, "No");
173                 assertEquals("success", actual);
174
175         }
176
177         @Test
178         public void saveNewUserEmptyTest() throws Exception {
179                 PowerMockito.mockStatic(Restrictions.class);
180                 PowerMockito.mockStatic(Criterion.class);
181                 PowerMockito.mockStatic(CipherUtil.class);
182                 EPUser user = buildEpUser();
183                 List users = new ArrayList<>();
184                 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
185                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
186                 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getLoginId());
187                 restrictionsList.add(orgUserIdCriterion);
188                 StringBuffer criteria = new StringBuffer();
189                 criteria.append(" where org_user_id = '").append(user.getLoginId()).append("'");
190                 Mockito.when(dataAccessService.getList(EPUser.class, criteria.toString(), null, null)).thenReturn(users);
191                 String actual = userServiceImpl.saveNewUser(user, "No");
192                 assertEquals("success", actual);
193
194         }
195
196         EPUser buildEpUser() {
197                 EPUser epUser = new EPUser();
198
199                 epUser.setId((long) 1);
200                 epUser.setManagerId((long) 1234);
201                 epUser.setFirstName(TEST);
202                 epUser.setLastName(TEST);
203                 epUser.setMiddleInitial(TEST);
204                 epUser.setPhone(TEST);
205                 epUser.setFax(TEST);
206                 epUser.setCellular(TEST);
207                 epUser.setEmail(TEST);
208                 epUser.setAddressId((long) 123);
209                 epUser.setAlertMethodCd(TEST);
210                 epUser.setHrid(TEST);
211                 epUser.setOrgUserId(TEST);
212                 epUser.setOrgCode(TEST);
213                 epUser.setAddress1(TEST);
214                 epUser.setAddress2(TEST);
215                 epUser.setCity(TEST);
216                 epUser.setState(TEST);
217                 epUser.setZipCode(TEST);
218                 epUser.setCountry(TEST);
219                 epUser.setOrgManagerUserId(TEST);
220                 epUser.setLocationClli(TEST);
221                 epUser.setBusinessCountryCode(TEST);
222                 epUser.setBusinessCountryName(TEST);
223                 epUser.setBusinessUnit(TEST);
224                 epUser.setBusinessUnitName(TEST);
225                 epUser.setDepartment(TEST);
226                 epUser.setDepartmentName(TEST);
227                 epUser.setCompanyCode(TEST);
228                 epUser.setCompany(TEST);
229                 epUser.setZipCodeSuffix(TEST);
230                 epUser.setJobTitle(TEST);
231                 epUser.setCommandChain(TEST);
232                 epUser.setSiloStatus(TEST);
233                 epUser.setCostCenter(TEST);
234                 epUser.setFinancialLocCode(TEST);
235                 epUser.setLoginId(TEST);
236                 epUser.setLoginPwd(TEST);
237                 epUser.setLastLoginDate(new Date());
238                 epUser.setActive(false);
239                 epUser.setInternal(false);
240                 epUser.setSelectedProfileId((long) 12345);
241                 epUser.setTimeZoneId((long) 12345);
242                 epUser.setOnline(false);
243                 epUser.setChatId(TEST);
244                 return epUser;
245         }
246 }