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;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertNull;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.InjectMocks;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.onap.portalapp.command.EPLoginBean;
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.util.EPUserUtils;
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.onap.portalsdk.core.web.support.AppUtils;
68 import org.powermock.api.mockito.PowerMockito;
69 import org.powermock.core.classloader.annotations.PrepareForTest;
70 import org.powermock.modules.junit4.PowerMockRunner;
71 import org.springframework.web.client.RestTemplate;
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({ EPUserUtils.class, CipherUtil.class, AppUtils.class, SystemProperties.class,
75 EPCommonSystemProperties.class })
76 public class EPLoginServiceImplTest {
79 DataAccessService dataAccessService = new DataAccessServiceImpl();
82 EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
85 SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
88 RestTemplate template = new RestTemplate();
92 MockitoAnnotations.initMocks(this);
96 EPLoginServiceImpl epLoginServiceImpl = new EPLoginServiceImpl();
98 public EPApp mockApp() {
99 EPApp app = new EPApp();
101 app.setImageUrl("test");
102 app.setNameSpace("com.test.app");
103 app.setCentralAuth(true);
104 app.setDescription("test");
105 app.setNotes("test");
108 app.setAppRestEndpoint("test");
109 app.setAlternateUrl("test");
111 app.setMlAppName("test");
112 app.setMlAppAdminId("test");
113 app.setUsername("test");
114 app.setAppPassword("test");
116 app.setEnabled(true);
117 app.setUebKey("test");
118 app.setUebSecret("test");
119 app.setUebTopicName("test");
124 MockEPUser mockUser = new MockEPUser();
126 @SuppressWarnings({ "rawtypes", "unchecked" })
128 public void findUserTest() throws Exception {
129 EPUser user = mockUser.mockEPUser();
130 EPLoginBean expected = new EPLoginBean();
131 expected.setOrgUserId("guestT");
132 Map<String, String> params = new HashMap<>();
133 params.put("org_user_id", expected.getOrgUserId());
134 List list = new ArrayList<>();
136 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
137 EPLoginBean actual = epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
138 assertNotNull(actual);
141 @SuppressWarnings({ "rawtypes", "unchecked" })
143 public void findUserPasswordMatchTest() throws Exception {
144 PowerMockito.mockStatic(EPCommonSystemProperties.class);
145 PowerMockito.mockStatic(EcompPortalUtils.class);
146 PowerMockito.mockStatic(SystemProperties.class);
147 PowerMockito.mockStatic(AppUtils.class);
148 PowerMockito.mockStatic(CipherUtil.class);
149 EPUser user = mockUser.mockEPUser();
150 user.setLoginId("guestT");
151 user.setLoginPwd("abc");
152 EPLoginBean expected = new EPLoginBean();
153 expected.setLoginId(user.getLoginId());
154 expected.setLoginPwd("xyz");
155 Map<String, String> params = new HashMap<>();
156 params.put("org_user_id", user.getOrgUserId());
157 List list = new ArrayList<>();
159 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
160 Map<String, String> params2 = new HashMap<>();
161 params2.put("login_id", user.getOrgUserId());
162 List list2 = new ArrayList<>();
164 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByLoginId", params2, new HashMap())).thenReturn(list2);
165 Mockito.when(CipherUtil.decryptPKC(user.getLoginPwd())).thenReturn("xyz");
166 EPLoginBean actual = epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
167 assertNotNull(actual);
170 @SuppressWarnings({ "rawtypes", "unchecked" })
172 public void findUserExcpetionTest() throws Exception {
173 PowerMockito.mockStatic(EPCommonSystemProperties.class);
174 PowerMockito.mockStatic(EcompPortalUtils.class);
175 PowerMockito.mockStatic(SystemProperties.class);
176 PowerMockito.mockStatic(AppUtils.class);
177 PowerMockito.mockStatic(CipherUtil.class);
178 EPUser user = mockUser.mockEPUser();
179 user.setLoginId("guestT");
180 user.setLoginPwd("abc");
181 EPLoginBean expected = new EPLoginBean();
182 expected.setLoginId(user.getLoginId());
183 expected.setLoginPwd("xyz");
184 Map<String, String> params = new HashMap<>();
185 params.put("org_user_id", user.getOrgUserId());
186 List list = new ArrayList<>();
188 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
189 Map<String, String> params2 = new HashMap<>();
190 params2.put("login_id", user.getOrgUserId());
191 List list2 = new ArrayList<>();
193 Mockito.doThrow(new NullPointerException()).when(dataAccessService).executeNamedQuery("getEPUserByLoginId", params2, new HashMap());
194 Mockito.when(CipherUtil.decryptPKC(user.getLoginPwd())).thenReturn("xyz");
195 EPLoginBean actual = epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
196 assertEquals(expected,actual);
199 @SuppressWarnings({ "rawtypes", "unchecked" })
201 public void findUserAppUtilsExcpetionTest() throws Exception {
202 PowerMockito.mockStatic(EPCommonSystemProperties.class);
203 PowerMockito.mockStatic(EPUserUtils.class);
204 PowerMockito.mockStatic(SystemProperties.class);
205 PowerMockito.mockStatic(AppUtils.class);
206 PowerMockito.mockStatic(CipherUtil.class);
207 EPUser user = mockUser.mockEPUser();
208 user.setLoginId("guestT");
209 user.setLoginPwd("abc");
210 user.setActive(false);
211 EPLoginBean expected = new EPLoginBean();
212 expected.setOrgUserId(user.getOrgUserId());
213 Map<String, String> params = new HashMap<>();
214 params.put("org_user_id", user.getOrgUserId());
215 List list = new ArrayList<>();
217 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
218 Mockito.when(EPUserUtils.hasRole(user, SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID))).thenReturn(false);
219 Mockito.when(AppUtils.isApplicationLocked()).thenReturn(true);
220 Mockito.when(EPUserUtils.hasRole(user, SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID))).thenReturn(false);
221 Mockito.when(AppUtils.isApplicationLocked()).thenReturn(true);
222 EPLoginBean actual = epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
223 assertEquals(expected,actual);
227 @SuppressWarnings({ "rawtypes", "unchecked" })
229 public void findUserWithoutPwdTest() {
230 EPUser user = mockUser.mockEPUser();
231 Map<String, String> params = new HashMap<>();
232 params.put("login_id", user.getOrgUserId());
233 List list = new ArrayList<>();
235 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByLoginId", params, new HashMap())).thenReturn(list);
236 EPUser actual = epLoginServiceImpl.findUserWithoutPwd(user.getOrgUserId());
237 assertEquals(user.getOrgUserId(), actual.getOrgUserId());