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.domain.EpAppType;
61 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
62 import org.onap.portalapp.portal.utils.EcompPortalUtils;
63 import org.onap.portalapp.util.EPUserUtils;
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.onap.portalsdk.core.web.support.AppUtils;
69 import org.powermock.api.mockito.PowerMockito;
70 import org.powermock.core.classloader.annotations.PrepareForTest;
71 import org.powermock.modules.junit4.PowerMockRunner;
72 import org.springframework.web.client.RestTemplate;
74 @RunWith(PowerMockRunner.class)
75 @PrepareForTest({ EPUserUtils.class, CipherUtil.class, AppUtils.class, SystemProperties.class,
76 EPCommonSystemProperties.class })
77 public class EPLoginServiceImplTest {
80 DataAccessService dataAccessService = new DataAccessServiceImpl();
83 EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
86 SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
89 RestTemplate template = new RestTemplate();
93 MockitoAnnotations.initMocks(this);
97 EPLoginServiceImpl epLoginServiceImpl = new EPLoginServiceImpl();
99 public EPApp mockApp() {
100 EPApp app = new EPApp();
102 app.setImageUrl("test");
103 app.setNameSpace("com.test.app");
104 app.setRolesInAAF(true);
105 app.setAppDescription("test");
106 app.setAppNotes("test");
107 app.setLandingPage("test");
109 app.setAppRestEndpoint("test");
110 app.setAlternateLandingPage("test");
112 app.setMlAppName("test");
113 app.setMlAppAdminId("test");
114 app.setAppBasicAuthUsername("test");
115 app.setAppBasicAuthPassword("test");
117 app.setEnabled(true);
118 app.setUebKey("test");
119 app.setUebSecret("test");
120 app.setUebTopicName("test");
121 app.setAppType(EpAppType.GUI);
125 MockEPUser mockUser = new MockEPUser();
127 @SuppressWarnings({ "rawtypes", "unchecked" })
129 public void findUserTest() throws Exception {
130 EPUser user = mockUser.mockEPUser();
131 EPLoginBean expected = new EPLoginBean();
132 expected.setOrgUserId("guestT");
133 Map<String, String> params = new HashMap<>();
134 params.put("org_user_id", expected.getOrgUserId());
135 List list = new ArrayList<>();
137 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
138 EPLoginBean actual = epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
139 assertNotNull(actual);
142 @SuppressWarnings({ "rawtypes", "unchecked" })
144 public void findUserPasswordMatchTest() throws Exception {
145 PowerMockito.mockStatic(EPCommonSystemProperties.class);
146 PowerMockito.mockStatic(EcompPortalUtils.class);
147 PowerMockito.mockStatic(SystemProperties.class);
148 PowerMockito.mockStatic(AppUtils.class);
149 PowerMockito.mockStatic(CipherUtil.class);
150 EPUser user = mockUser.mockEPUser();
151 user.setLoginId("guestT");
152 user.setLoginPwd("abc");
153 EPLoginBean expected = new EPLoginBean();
154 expected.setLoginId(user.getLoginId());
155 expected.setLoginPwd("xyz");
156 Map<String, String> params = new HashMap<>();
157 params.put("org_user_id", user.getOrgUserId());
158 List list = new ArrayList<>();
160 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
161 Map<String, String> params2 = new HashMap<>();
162 params2.put("login_id", user.getOrgUserId());
163 List list2 = new ArrayList<>();
165 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByLoginId", params2, new HashMap())).thenReturn(list2);
166 Mockito.when(CipherUtil.decryptPKC(user.getLoginPwd())).thenReturn("xyz");
167 EPLoginBean actual = epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
168 assertNotNull(actual);
171 @SuppressWarnings({ "rawtypes", "unchecked" })
173 public void findUserExcpetionTest() throws Exception {
174 PowerMockito.mockStatic(EPCommonSystemProperties.class);
175 PowerMockito.mockStatic(EcompPortalUtils.class);
176 PowerMockito.mockStatic(SystemProperties.class);
177 PowerMockito.mockStatic(AppUtils.class);
178 PowerMockito.mockStatic(CipherUtil.class);
179 EPUser user = mockUser.mockEPUser();
180 user.setLoginId("guestT");
181 user.setLoginPwd("abc");
182 EPLoginBean expected = new EPLoginBean();
183 expected.setLoginId(user.getLoginId());
184 expected.setLoginPwd("xyz");
185 Map<String, String> params = new HashMap<>();
186 params.put("org_user_id", user.getOrgUserId());
187 List list = new ArrayList<>();
189 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
190 Map<String, String> params2 = new HashMap<>();
191 params2.put("login_id", user.getOrgUserId());
192 List list2 = new ArrayList<>();
194 Mockito.doThrow(new NullPointerException()).when(dataAccessService).executeNamedQuery("getEPUserByLoginId", params2, new HashMap());
195 Mockito.when(CipherUtil.decryptPKC(user.getLoginPwd())).thenReturn("xyz");
196 EPLoginBean actual = epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
197 assertEquals(expected,actual);
200 @SuppressWarnings({ "rawtypes", "unchecked" })
202 public void findUserAppUtilsExcpetionTest() throws Exception {
203 PowerMockito.mockStatic(EPCommonSystemProperties.class);
204 PowerMockito.mockStatic(EPUserUtils.class);
205 PowerMockito.mockStatic(SystemProperties.class);
206 PowerMockito.mockStatic(AppUtils.class);
207 PowerMockito.mockStatic(CipherUtil.class);
208 EPUser user = mockUser.mockEPUser();
209 user.setLoginId("guestT");
210 user.setLoginPwd("abc");
211 user.setActive(false);
212 EPLoginBean expected = new EPLoginBean();
213 expected.setOrgUserId(user.getOrgUserId());
214 Map<String, String> params = new HashMap<>();
215 params.put("org_user_id", user.getOrgUserId());
216 List list = new ArrayList<>();
218 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
219 Mockito.when(EPUserUtils.hasRole(user, SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID))).thenReturn(false);
220 Mockito.when(AppUtils.isApplicationLocked()).thenReturn(true);
221 Mockito.when(EPUserUtils.hasRole(user, SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID))).thenReturn(false);
222 Mockito.when(AppUtils.isApplicationLocked()).thenReturn(true);
223 EPLoginBean actual = epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
224 assertEquals(expected,actual);
228 @SuppressWarnings({ "rawtypes", "unchecked" })
230 public void findUserWithoutPwdTest() {
231 EPUser user = mockUser.mockEPUser();
232 Map<String, String> params = new HashMap<>();
233 params.put("login_id", user.getOrgUserId());
234 List list = new ArrayList<>();
236 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByLoginId", params, new HashMap())).thenReturn(list);
237 EPUser actual = epLoginServiceImpl.findUserWithoutPwd(user.getOrgUserId());
238 assertEquals(user.getOrgUserId(), actual.getOrgUserId());