2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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.portalsdk.core.service;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
45 import java.util.TreeSet;
47 import org.junit.Assert;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.onap.portalsdk.core.command.LoginBean;
54 import org.onap.portalsdk.core.domain.App;
55 import org.onap.portalsdk.core.domain.Role;
56 import org.onap.portalsdk.core.domain.RoleFunction;
57 import org.onap.portalsdk.core.domain.User;
58 import org.onap.portalsdk.core.domain.UserApp;
59 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
60 import org.onap.portalsdk.core.util.SystemProperties;
61 import org.onap.portalsdk.core.web.support.AppUtils;
62 import org.onap.portalsdk.core.web.support.UserUtils;
63 import org.powermock.api.mockito.PowerMockito;
64 import org.powermock.core.classloader.annotations.PrepareForTest;
65 import org.powermock.modules.junit4.PowerMockRunner;
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest({ PortalApiProperties.class, AppUtils.class, UserUtils.class, SystemProperties.class })
70 public class LoginServiceCentralizedImplTest {
73 private LoginServiceCentralizedImpl loginServiceCentrImpl;
76 private DataAccessService dataAccessService;
79 private RestApiRequestBuilder restApiRequestBuilder;
82 private UserService userService;
85 public void findUserTest() throws Exception {
86 String userId = "S1234";
87 LoginBean bean = new LoginBean();
88 bean.setUserid(userId);
89 String menuPropertiesFilename ="";
90 Map additionalParams = new HashMap();
92 User mockUser = new User();
93 mockUser.setOrgUserId("G1234");
94 mockUser.setLoginId(userId);
96 RoleFunction roleFunction = new RoleFunction();
97 roleFunction.setId(12L);
98 roleFunction.setName("Role Function");
100 Set roleFunctions = new TreeSet();
101 roleFunctions.add(roleFunction);
103 Role role = new Role();
104 role.setName("Role");
105 role.setActive(true);
106 role.setRoleFunctions(roleFunctions);
108 Set userApps = new TreeSet();
109 UserApp userApp = new UserApp();
112 app.setId(new Long(1));
113 app.setName("Default");
114 userApp.setUserId(1L);
116 userApp.setRole(role);
117 userApps.add(userApp);
118 mockUser.setUserApps(userApps);
120 mockUser.setActive(true);
122 Mockito.when(restApiRequestBuilder.getViaREST("/user/" + bean.getUserid(), true, bean.getUserid())).thenReturn("Dummy Response");
123 Mockito.when(userService.userMapper(Mockito.anyString())).thenReturn(mockUser);
125 Map<String, String> params = new HashMap<>();
126 params.put("orgUserId", mockUser.getOrgUserId());
128 List idList = new ArrayList();
130 Mockito.when(dataAccessService.executeNamedQuery("getUserIdByorgUserId", params, null)).thenReturn(idList);
132 LoginBean loginBean = loginServiceCentrImpl.findUser(bean, menuPropertiesFilename, additionalParams);
133 Assert.assertEquals(loginBean.getUserid(), userId);
138 public void findUserWithErroMsgTest() throws Exception {
139 String userId = "S1234";
140 LoginBean bean = new LoginBean();
141 bean.setUserid(userId);
142 String menuPropertiesFilename ="";
143 Map additionalParams = new HashMap();
145 User mockUser = new User();
146 mockUser.setOrgUserId("G1234");
147 mockUser.setLoginId(userId);
149 mockUser.setActive(false);
151 Mockito.when(restApiRequestBuilder.getViaREST("/user/" + bean.getUserid(), true, bean.getUserid())).thenReturn("Dummy Response");
152 Mockito.when(userService.userMapper(Mockito.anyString())).thenReturn(mockUser);
154 PowerMockito.mockStatic(AppUtils.class);
155 Mockito.when(AppUtils.isApplicationLocked()).thenReturn(true);
157 PowerMockito.mockStatic(SystemProperties.class);
158 Mockito.when(SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID)).thenReturn("SYSTEMS");
160 PowerMockito.mockStatic(UserUtils.class);
161 Mockito.when(UserUtils.hasRole(Mockito.any(User.class), Mockito.any(String.class))).thenReturn(false);
163 LoginBean loginBean = loginServiceCentrImpl.findUser(bean, menuPropertiesFilename, additionalParams);
164 Assert.assertEquals(loginBean.getUserid(), userId);
168 public void findUserWihtoutUserIdTest() throws Exception {
169 LoginBean bean = new LoginBean();
170 String menuPropertiesFilename ="";
171 Map additionalParams = new HashMap();
173 User mockUser = new User();
174 mockUser.setOrgUserId("G1234");
175 mockUser.setLoginId("L1234");
177 LoginBean loginBean = loginServiceCentrImpl.findUser(bean, menuPropertiesFilename, additionalParams);
178 Assert.assertNull(loginBean.getLoginId());
183 public void findUserWithoutUserIdTest() throws Exception {
184 LoginBean bean = new LoginBean();
185 String menuPropertiesFilename ="";
186 Map additionalParams = new HashMap();
187 LoginBean loginBean = loginServiceCentrImpl.findUser(bean, menuPropertiesFilename, additionalParams);
188 Assert.assertNull(loginBean.getLoginId());
192 public void findUserWithoutUserIdAndPasswordTest() throws Exception {
193 LoginBean bean = new LoginBean();
194 String menuPropertiesFilename ="";
195 Map additionalParams = new HashMap();
196 LoginBean loginBean = loginServiceCentrImpl.findUser(bean, menuPropertiesFilename, additionalParams, false);
197 Assert.assertNull(loginBean.getLoginId());