2f827bd94d217276005a0b7a1b1288b793d77d00
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.core.service;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.TreeSet;
46
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;
66
67
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest({ PortalApiProperties.class, AppUtils.class, UserUtils.class, SystemProperties.class })
70 public class LoginServiceCentralizedImplTest {
71
72         @InjectMocks
73         private LoginServiceCentralizedImpl loginServiceCentrImpl;
74         
75         @Mock
76         private DataAccessService dataAccessService;
77
78         @Mock
79         private RestApiRequestBuilder restApiRequestBuilder;
80
81         @Mock
82         private UserService userService;
83         
84         @Test
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();
91                 
92                 User mockUser = new User();
93                 mockUser.setOrgUserId("G1234");
94                 mockUser.setLoginId(userId);
95                 
96                 RoleFunction roleFunction = new RoleFunction();
97                 roleFunction.setId(12L);
98                 roleFunction.setName("Role Function");
99                 
100                 Set roleFunctions = new TreeSet();
101                 roleFunctions.add(roleFunction);
102                 
103                 Role role = new Role();
104                 role.setName("Role");
105                 role.setActive(true);
106                 role.setRoleFunctions(roleFunctions);
107                 
108                 Set userApps = new TreeSet();
109                 UserApp userApp = new UserApp();
110                 
111                 App app = new App();
112                 app.setId(new Long(1));
113                 app.setName("Default");
114                 userApp.setUserId(1L);
115                 userApp.setApp(app);
116                 userApp.setRole(role);
117                 userApps.add(userApp);
118                 mockUser.setUserApps(userApps);
119                 
120                 mockUser.setActive(true);
121                 
122                 Mockito.when(restApiRequestBuilder.getViaREST("/user/" + bean.getUserid(), true, bean.getUserid())).thenReturn("Dummy Response");
123                 Mockito.when(userService.userMapper(Mockito.anyString())).thenReturn(mockUser);
124                 
125                 Map<String, String> params = new HashMap<>();
126                 params.put("orgUserId", mockUser.getOrgUserId());
127                 
128                 List idList = new ArrayList();
129                 idList.add(1L);
130                 Mockito.when(dataAccessService.executeNamedQuery("getUserIdByorgUserId", params, null)).thenReturn(idList);
131                 
132                 LoginBean loginBean = loginServiceCentrImpl.findUser(bean, menuPropertiesFilename, additionalParams);
133                 Assert.assertEquals(loginBean.getUserid(), userId);
134         }
135         
136         
137         @Test
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();
144                 
145                 User mockUser = new User();
146                 mockUser.setOrgUserId("G1234");
147                 mockUser.setLoginId(userId);
148                 
149                 mockUser.setActive(false);
150                 
151                 Mockito.when(restApiRequestBuilder.getViaREST("/user/" + bean.getUserid(), true, bean.getUserid())).thenReturn("Dummy Response");
152                 Mockito.when(userService.userMapper(Mockito.anyString())).thenReturn(mockUser);
153                 
154                 PowerMockito.mockStatic(AppUtils.class);
155                 Mockito.when(AppUtils.isApplicationLocked()).thenReturn(true);
156                 
157                 PowerMockito.mockStatic(SystemProperties.class);
158                 Mockito.when(SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID)).thenReturn("SYSTEMS");
159                 
160                 PowerMockito.mockStatic(UserUtils.class);
161                 Mockito.when(UserUtils.hasRole(Mockito.any(User.class), Mockito.any(String.class))).thenReturn(false);
162                 
163                 LoginBean loginBean = loginServiceCentrImpl.findUser(bean, menuPropertiesFilename, additionalParams);
164                 Assert.assertEquals(loginBean.getUserid(), userId);
165         }
166         
167         @Test
168         public void findUserWihtoutUserIdTest() throws Exception {
169                 LoginBean bean = new LoginBean();
170                 String menuPropertiesFilename ="";
171                 Map additionalParams = new HashMap();
172                 
173                 User mockUser = new User();
174                 mockUser.setOrgUserId("G1234");
175                 mockUser.setLoginId("L1234");
176                 
177                 LoginBean loginBean = loginServiceCentrImpl.findUser(bean, menuPropertiesFilename, additionalParams);
178                 Assert.assertNull(loginBean.getLoginId());
179                 
180         }
181         
182         @Test
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());
189         }
190         
191         @Test
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());
198                 
199         }
200 }