fixes for supporting non-gui application access provisioning
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / EPLoginServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertNull;
43
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
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;
73
74 @RunWith(PowerMockRunner.class)
75 @PrepareForTest({ EPUserUtils.class, CipherUtil.class, AppUtils.class, SystemProperties.class,
76                 EPCommonSystemProperties.class })
77 public class EPLoginServiceImplTest {
78
79         @Mock
80         DataAccessService dataAccessService = new DataAccessServiceImpl();
81
82         @Mock
83         EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
84
85         @Mock
86         SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
87
88         @Mock
89         RestTemplate template = new RestTemplate();
90
91         @Before
92         public void setup() {
93                 MockitoAnnotations.initMocks(this);
94         }
95         
96         @InjectMocks
97         EPLoginServiceImpl epLoginServiceImpl = new EPLoginServiceImpl();
98         
99         public EPApp mockApp() {
100                 EPApp app = new EPApp();
101                 app.setName("Test");
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");
108                 app.setId((long) 1);
109                 app.setAppRestEndpoint("test");
110                 app.setAlternateLandingPage("test");
111                 app.setName("test");
112                 app.setMlAppName("test");
113                 app.setMlAppAdminId("test");
114                 app.setAppBasicAuthUsername("test");
115                 app.setAppBasicAuthPassword("test");
116                 app.setOpen(false);
117                 app.setEnabled(true);
118                 app.setUebKey("test");
119                 app.setUebSecret("test");
120                 app.setUebTopicName("test");
121                 app.setAppType(EpAppType.GUI);
122                 return app;
123         }
124
125         MockEPUser mockUser = new MockEPUser();
126         
127         @SuppressWarnings({ "rawtypes", "unchecked" })
128         @Test
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<>();
136                 list.add(user);
137                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
138                 EPLoginBean actual =  epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
139                 assertNotNull(actual);
140         }
141         
142         @SuppressWarnings({ "rawtypes", "unchecked" })
143         @Test
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<>();
159                 list.add(user);
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<>();
164                 list2.add(user);
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);
169         }
170         
171         @SuppressWarnings({ "rawtypes", "unchecked" })
172         @Test
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<>();
188                 list.add(user);
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<>();
193                 list2.add(user);
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);
198         }
199         
200         @SuppressWarnings({ "rawtypes", "unchecked" })
201         @Test
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<>();
217                 list.add(user);
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);
225         }
226         
227         
228         @SuppressWarnings({ "rawtypes", "unchecked" })
229         @Test 
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<>();
235                 list.add(user);
236                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByLoginId", params, new HashMap())).thenReturn(list);
237                 EPUser actual = epLoginServiceImpl.findUserWithoutPwd(user.getOrgUserId());
238                 assertEquals(user.getOrgUserId(), actual.getOrgUserId());
239         }
240 }