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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.onap.portalapp.portal.core.MockEPUser;
58 import org.onap.portalapp.portal.domain.EPApp;
59 import org.onap.portalapp.portal.domain.EPUser;
60 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
61 import org.onap.portalapp.portal.utils.EcompPortalUtils;
62 import org.onap.portalapp.portal.utils.PortalConstants;
63 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
64 import org.onap.portalsdk.core.service.DataAccessService;
65 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
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;
71 @RunWith(PowerMockRunner.class)
72 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, PortalConstants.class,
73 EPCommonSystemProperties.class, Criterion.class, CipherUtil.class, Restrictions.class })
74 public class UserServiceImplTest {
77 DataAccessService dataAccessService = new DataAccessServiceImpl();
80 UserServiceImpl userServiceImpl= new UserServiceImpl();
84 MockitoAnnotations.initMocks(this);
87 public EPApp mockApp() {
88 EPApp app = new EPApp();
90 app.setImageUrl("test");
91 app.setNameSpace("com.test.app");
92 app.setCentralAuth(true);
93 app.setDescription("test");
97 app.setAppRestEndpoint("test");
98 app.setAlternateUrl("test");
100 app.setMlAppName("test");
101 app.setMlAppAdminId("test");
102 app.setUsername("test");
103 app.setAppPassword("test");
105 app.setEnabled(true);
106 app.setUebKey("test");
107 app.setUebSecret("test");
108 app.setUebTopicName("test");
113 MockEPUser mockUser = new MockEPUser();
115 @SuppressWarnings("unchecked")
117 public void getUserByUserIdExceptionTest() throws Exception {
118 PowerMockito.mockStatic(SystemProperties.class);
119 EPUser user = mockUser.mockEPUser();
120 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
121 Mockito.when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER)).thenReturn("http://www.test.com");
122 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
123 JSONObject response = new JSONObject();
124 JSONObject userJson = new JSONObject();
125 userJson.put("id", 1);
126 userJson.put("givenName", "Guest");
127 userJson.put("familyName", "Test");
128 userJson.put("email", "test@123.com");
129 List<JSONObject> userListJson = new ArrayList<>();
130 userListJson.add(userJson);
131 response.put("response", userListJson);
132 ByteArrayInputStream getBody = new ByteArrayInputStream(response.toString().getBytes("UTF-8"));
133 PowerMockito.when(connection.getInputStream()).thenReturn(getBody);
134 userServiceImpl.getUserByUserId(user.getOrgUserId());
137 @SuppressWarnings("unchecked")
139 public void saveNewUserTest() throws Exception {
140 PowerMockito.mockStatic(Restrictions.class);
141 PowerMockito.mockStatic(Criterion.class);
142 PowerMockito.mockStatic(CipherUtil.class);
143 EPUser user = mockUser.mockEPUser();
144 List<EPUser> users = new ArrayList<>();
145 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
146 List<Criterion> restrictionsList = new ArrayList<Criterion>();
147 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",user.getLoginId());
148 restrictionsList.add(orgUserIdCriterion);
149 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)).thenReturn(users);
150 String actual = userServiceImpl.saveNewUser(user, "No");
151 assertEquals("success", actual);
154 @SuppressWarnings("unchecked")
156 public void saveExistingUserTest() throws Exception {
157 PowerMockito.mockStatic(Restrictions.class);
158 PowerMockito.mockStatic(Criterion.class);
159 PowerMockito.mockStatic(CipherUtil.class);
160 EPUser user = mockUser.mockEPUser();
161 user.setLoginPwd("xyz");
162 List<EPUser> users = new ArrayList<>();
164 EPUser oldUser = mockUser.mockEPUser();
165 oldUser.setLoginPwd("abc");
166 List<EPUser> oldUsers = new ArrayList<>();
167 oldUsers.add(oldUser);
168 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
169 List<Criterion> restrictionsList = new ArrayList<Criterion>();
170 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",user.getLoginId());
171 restrictionsList.add(orgUserIdCriterion);
172 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)).thenReturn(oldUsers);
173 String actual = userServiceImpl.saveNewUser(user, "No");
174 assertEquals("success", actual);