5eaa40fdfa846a5ab8d85fbd69d5cf4b6bc56455
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.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;
72
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({ EPUserUtils.class, CipherUtil.class, AppUtils.class, SystemProperties.class,
75                 EPCommonSystemProperties.class })
76 public class EPLoginServiceImplTest {
77
78         @Mock
79         DataAccessService dataAccessService = new DataAccessServiceImpl();
80
81         @Mock
82         EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
83
84         @Mock
85         SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
86
87         @Mock
88         RestTemplate template = new RestTemplate();
89
90         @Before
91         public void setup() {
92                 MockitoAnnotations.initMocks(this);
93         }
94         
95         @InjectMocks
96         EPLoginServiceImpl epLoginServiceImpl = new EPLoginServiceImpl();
97         
98         public EPApp mockApp() {
99                 EPApp app = new EPApp();
100                 app.setName("Test");
101                 app.setImageUrl("test");
102                 app.setNameSpace("com.test.app");
103                 app.setCentralAuth(true);
104                 app.setDescription("test");
105                 app.setNotes("test");
106                 app.setUrl("test");
107                 app.setId((long) 1);
108                 app.setAppRestEndpoint("test");
109                 app.setAlternateUrl("test");
110                 app.setName("test");
111                 app.setMlAppName("test");
112                 app.setMlAppAdminId("test");
113                 app.setUsername("test");
114                 app.setAppPassword("test");
115                 app.setOpen(false);
116                 app.setEnabled(true);
117                 app.setUebKey("test");
118                 app.setUebSecret("test");
119                 app.setUebTopicName("test");
120                 app.setAppType(1);
121                 return app;
122         }
123
124         MockEPUser mockUser = new MockEPUser();
125         
126         @SuppressWarnings({ "rawtypes", "unchecked" })
127         @Test
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<>();
135                 list.add(user);
136                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap())).thenReturn(list);
137                 EPLoginBean actual =  epLoginServiceImpl.findUser(expected, "test", new HashMap<>());
138                 assertNotNull(actual);
139         }
140         
141         @SuppressWarnings({ "rawtypes", "unchecked" })
142         @Test
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<>();
158                 list.add(user);
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<>();
163                 list2.add(user);
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);
168         }
169         
170         @SuppressWarnings({ "rawtypes", "unchecked" })
171         @Test
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<>();
187                 list.add(user);
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<>();
192                 list2.add(user);
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);
197         }
198         
199         @SuppressWarnings({ "rawtypes", "unchecked" })
200         @Test
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<>();
216                 list.add(user);
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);
224         }
225         
226         
227         @SuppressWarnings({ "rawtypes", "unchecked" })
228         @Test 
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<>();
234                 list.add(user);
235                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByLoginId", params, new HashMap())).thenReturn(list);
236                 EPUser actual = epLoginServiceImpl.findUserWithoutPwd(user.getOrgUserId());
237                 assertEquals(user.getOrgUserId(), actual.getOrgUserId());
238         }
239 }