2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 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;
42 import java.io.ByteArrayInputStream;
43 import java.net.HttpURLConnection;
44 import java.util.ArrayList;
45 import java.util.List;
47 import org.hibernate.criterion.Criterion;
48 import org.hibernate.criterion.Restrictions;
49 import org.json.simple.JSONObject;
50 import org.junit.Before;
51 import org.junit.Ignore;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.InjectMocks;
55 import org.mockito.Mock;
56 import org.mockito.Mockito;
57 import org.mockito.MockitoAnnotations;
58 import org.onap.portalapp.portal.core.MockEPUser;
59 import org.onap.portalapp.portal.domain.EPApp;
60 import org.onap.portalapp.portal.domain.EPUser;
61 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
62 import org.onap.portalapp.portal.utils.EcompPortalUtils;
63 import org.onap.portalapp.portal.utils.PortalConstants;
64 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
65 import org.onap.portalsdk.core.service.DataAccessService;
66 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
67 import org.onap.portalsdk.core.util.SystemProperties;
68 import org.powermock.api.mockito.PowerMockito;
69 import org.powermock.core.classloader.annotations.PrepareForTest;
70 import org.powermock.modules.junit4.PowerMockRunner;
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, PortalConstants.class,
74 EPCommonSystemProperties.class, Criterion.class, CipherUtil.class, Restrictions.class })
76 public class UserServiceImplTest {
79 DataAccessService dataAccessService = new DataAccessServiceImpl();
82 UserServiceImpl userServiceImpl= new UserServiceImpl();
86 MockitoAnnotations.initMocks(this);
89 public EPApp mockApp() {
90 EPApp app = new EPApp();
92 app.setImageUrl("test");
93 app.setNameSpace("com.test.app");
94 app.setCentralAuth(true);
95 app.setDescription("test");
99 app.setAppRestEndpoint("test");
100 app.setAlternateUrl("test");
102 app.setMlAppName("test");
103 app.setMlAppAdminId("test");
104 app.setUsername("test");
105 app.setAppPassword("test");
107 app.setEnabled(true);
108 app.setUebKey("test");
109 app.setUebSecret("test");
110 app.setUebTopicName("test");
115 MockEPUser mockUser = new MockEPUser();
117 @SuppressWarnings("unchecked")
119 public void getUserByUserIdExceptionTest() throws Exception {
120 PowerMockito.mockStatic(SystemProperties.class);
121 EPUser user = mockUser.mockEPUser();
122 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
123 Mockito.when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER)).thenReturn("http://www.test.com");
124 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
125 JSONObject response = new JSONObject();
126 JSONObject userJson = new JSONObject();
127 userJson.put("id", 1);
128 userJson.put("givenName", "Guest");
129 userJson.put("familyName", "Test");
130 userJson.put("email", "test@123.com");
131 List<JSONObject> userListJson = new ArrayList<>();
132 userListJson.add(userJson);
133 response.put("response", userListJson);
134 ByteArrayInputStream getBody = new ByteArrayInputStream(response.toString().getBytes("UTF-8"));
135 PowerMockito.when(connection.getInputStream()).thenReturn(getBody);
136 userServiceImpl.getUserByUserId(user.getOrgUserId());
139 @SuppressWarnings("unchecked")
141 public void saveNewUserTest() throws Exception {
142 PowerMockito.mockStatic(Restrictions.class);
143 PowerMockito.mockStatic(Criterion.class);
144 PowerMockito.mockStatic(CipherUtil.class);
145 EPUser user = mockUser.mockEPUser();
146 List<EPUser> users = new ArrayList<>();
147 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
148 List<Criterion> restrictionsList = new ArrayList<Criterion>();
149 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",user.getLoginId());
150 restrictionsList.add(orgUserIdCriterion);
151 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)).thenReturn(users);
152 String actual = userServiceImpl.saveNewUser(user, "No");
153 assertEquals("success", actual);
156 @SuppressWarnings("unchecked")
158 public void saveExistingUserTest() throws Exception {
159 PowerMockito.mockStatic(Restrictions.class);
160 PowerMockito.mockStatic(Criterion.class);
161 PowerMockito.mockStatic(CipherUtil.class);
162 EPUser user = mockUser.mockEPUser();
163 user.setLoginPwd("xyz");
164 List<EPUser> users = new ArrayList<>();
166 EPUser oldUser = mockUser.mockEPUser();
167 oldUser.setLoginPwd("abc");
168 List<EPUser> oldUsers = new ArrayList<>();
169 oldUsers.add(oldUser);
170 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
171 List<Criterion> restrictionsList = new ArrayList<Criterion>();
172 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",user.getLoginId());
173 restrictionsList.add(orgUserIdCriterion);
174 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)).thenReturn(oldUsers);
175 String actual = userServiceImpl.saveNewUser(user, "No");
176 assertEquals("success", actual);