2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.portalapp.portal.service;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import org.hibernate.criterion.Criterion;
31 import org.hibernate.criterion.Restrictions;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.portalapp.portal.core.MockEPUser;
40 import org.onap.portalapp.portal.domain.BasicAuthCredentials;
41 import org.onap.portalapp.portal.domain.EPEndpoint;
42 import org.onap.portalapp.portal.domain.EPEndpointAccount;
43 import org.onap.portalapp.portal.framework.MockitoTestSuite;
44 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
45 import org.onap.portalsdk.core.onboarding.util.KeyConstants;
46 import org.onap.portalsdk.core.onboarding.util.KeyProperties;
47 import org.onap.portalsdk.core.service.DataAccessService;
48 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
49 import org.onap.portalsdk.core.util.SystemProperties;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.core.classloader.annotations.PrepareForTest;
52 import org.powermock.modules.junit4.PowerMockRunner;
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({ CipherUtil.class , SystemProperties.class, KeyProperties.class, KeyConstants.class})
57 public class BasicAuthAccountServiceImplTest {
59 DataAccessService dataAccessService = new DataAccessServiceImpl();
63 MockitoAnnotations.initMocks(this);
67 BasicAuthAccountServiceImpl basicAuthAccountServiceImpl = new BasicAuthAccountServiceImpl();
70 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
71 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
72 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
73 NullPointerException nullPointerException = new NullPointerException();
74 MockEPUser mockUser = new MockEPUser();
77 public void saveBasicAuthAccountTest() throws Exception {
78 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
79 basicAuthCredentials.setPassword(null);
80 Mockito.doNothing().when(dataAccessService).saveDomainObject(basicAuthCredentials, null);
81 basicAuthAccountServiceImpl.saveBasicAuthAccount(basicAuthCredentials);
85 @Test(expected= Exception.class)
86 public void saveBasicAuthAccountValidTest() throws Exception {
87 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
88 basicAuthCredentials.setPassword("<IMG SRC=\"jav\tascript:alert('XSS');\">");
89 Mockito.doNothing().when(dataAccessService).saveDomainObject(basicAuthCredentials, null);
90 basicAuthAccountServiceImpl.saveBasicAuthAccount(basicAuthCredentials);
95 public void saveBasicAuthAccountTest_password() throws Exception{
96 PowerMockito.mockStatic(CipherUtil.class);
97 PowerMockito.mockStatic(KeyProperties.class);
98 PowerMockito.mockStatic(KeyConstants.class);
99 BasicAuthCredentials credentials = new BasicAuthCredentials();
100 credentials.setPassword("password");
101 String result = null;
102 Mockito.when(CipherUtil.encryptPKC("password", KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY))).thenReturn(result);
103 basicAuthAccountServiceImpl.saveBasicAuthAccount(credentials);
107 public void saveEndpointsTest() throws Exception {
108 List<Criterion> restrictionsList = new ArrayList<Criterion>();
109 Criterion NameCrit = Restrictions.eq("name", "test");
110 restrictionsList.add(NameCrit);
111 List<EPEndpoint> tempList = new ArrayList<>();
112 EPEndpoint endpoint = new EPEndpoint();
114 endpoint.setName("name");
115 tempList.add(endpoint);
116 Mockito.when((List<EPEndpoint>) dataAccessService.getList(EPEndpoint.class, null, restrictionsList, null))
117 .thenReturn(tempList);
118 EPEndpoint epEndpoint= new EPEndpoint();
119 Mockito.doNothing().when(dataAccessService).saveDomainObject(epEndpoint, null);
120 basicAuthAccountServiceImpl.saveEndpoints(epEndpoint);
123 @Test(expected= NullPointerException.class)
124 public void saveEndpointAccountTest() throws Exception {
125 EPEndpointAccount record = new EPEndpointAccount();
126 record.setAccount_id(1l);
128 Mockito.doNothing().when(dataAccessService).saveDomainObject(record, null);
129 basicAuthAccountServiceImpl.saveEndpointAccount(1l, 2l);
133 public void updateBasicAuthAccountTest() throws Exception {
134 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
135 Mockito.doNothing().when(dataAccessService).saveDomainObject(basicAuthCredentials, null);
136 List<EPEndpoint> endpoints = new ArrayList<>();
137 EPEndpoint epEndpoint = new EPEndpoint();
138 epEndpoint.setId(1l);
139 epEndpoint.setName("name");
140 endpoints.add(epEndpoint);
141 basicAuthCredentials.setEndpoints(endpoints);
142 List<EPEndpointAccount> list = null;
143 Map<String, Long> params = new HashMap<>();
144 params.put("account_id", 1l);
145 Mockito.when(dataAccessService.executeNamedQuery("getEPEndpointAccountByAccountId", null, null)).thenReturn(list);
146 EPEndpoint temp_ep = new EPEndpoint();
148 boolean flag = false;
149 Map<String, String> params1 = new HashMap<String, String>();
150 params1.put("accountId", Long.toString(1l));
151 params1.put("epId", Long.toString(1l));
152 Mockito.when(dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params1, null)).thenReturn(null);
153 basicAuthAccountServiceImpl.updateBasicAuthAccount(1l, basicAuthCredentials);
158 public void getAccountDataTest() throws Exception {
159 List<BasicAuthCredentials> list = new ArrayList<>();
160 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
161 Mockito.when((List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null))
163 basicAuthAccountServiceImpl.getAccountData();
167 public void getAccountDataTest_password() throws Exception {
168 PowerMockito.mockStatic(CipherUtil.class);
169 PowerMockito.mockStatic(KeyProperties.class);
170 PowerMockito.mockStatic(KeyConstants.class);
171 List<BasicAuthCredentials> list = new ArrayList<>();
172 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
173 basicAuthCredentials.setPassword("password");
174 list.add(basicAuthCredentials);
175 Mockito.when((List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null))
177 String result = null;
178 Mockito.when(CipherUtil.decryptPKC("password", KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY))).thenReturn(result);
183 public void deleteEndpointAccoutTest() throws Exception {
184 Map<String, String> params = new HashMap<String, String>();
185 params.put("accountId", Long.toString(1l));
186 Mockito.when(dataAccessService.executeNamedQuery("deleteAccountEndpoint", params, null)).thenReturn(null);
187 Mockito.when(dataAccessService.executeNamedQuery("deleteBasicAuthAccount", params, null)).thenReturn(null);
188 basicAuthAccountServiceImpl.deleteEndpointAccout(1l);
193 public void getBasicAuthCredentialsById() throws Exception{
194 List<BasicAuthCredentials> list = new ArrayList<>();
195 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
196 basicAuthCredentials.setPassword("password");
197 basicAuthCredentials.setId(1l);
198 list.add(basicAuthCredentials);
199 Mockito.when((List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null))
201 basicAuthAccountServiceImpl.getBasicAuthCredentialsById(1l);