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.service.DataAccessService;
46 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
47 import org.onap.portalsdk.core.util.SystemProperties;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({ CipherUtil.class , SystemProperties.class})
55 public class BasicAuthAccountServiceImplTest {
57 DataAccessService dataAccessService = new DataAccessServiceImpl();
61 MockitoAnnotations.initMocks(this);
65 BasicAuthAccountServiceImpl basicAuthAccountServiceImpl = new BasicAuthAccountServiceImpl();
68 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
69 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
70 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
71 NullPointerException nullPointerException = new NullPointerException();
72 MockEPUser mockUser = new MockEPUser();
75 public void saveBasicAuthAccountTest() throws Exception {
76 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
77 basicAuthCredentials.setPassword(null);
78 Mockito.doNothing().when(dataAccessService).saveDomainObject(basicAuthCredentials, null);
79 basicAuthAccountServiceImpl.saveBasicAuthAccount(basicAuthCredentials);
84 public void saveBasicAuthAccountTest_password() throws Exception{
85 PowerMockito.mockStatic(CipherUtil.class);
86 PowerMockito.mockStatic(SystemProperties.class);
87 BasicAuthCredentials credentials = new BasicAuthCredentials();
88 credentials.setPassword("password");
90 Mockito.when(CipherUtil.encryptPKC("password", SystemProperties.getProperty(SystemProperties.Decryption_Key))).thenReturn(result);
91 basicAuthAccountServiceImpl.saveBasicAuthAccount(credentials);
95 public void saveEndpointsTest() throws Exception {
96 List<Criterion> restrictionsList = new ArrayList<Criterion>();
97 Criterion NameCrit = Restrictions.eq("name", "test");
98 restrictionsList.add(NameCrit);
99 List<EPEndpoint> tempList = new ArrayList<>();
100 EPEndpoint endpoint = new EPEndpoint();
102 endpoint.setName("name");
103 tempList.add(endpoint);
104 Mockito.when((List<EPEndpoint>) dataAccessService.getList(EPEndpoint.class, null, restrictionsList, null))
105 .thenReturn(tempList);
106 EPEndpoint epEndpoint= new EPEndpoint();
107 Mockito.doNothing().when(dataAccessService).saveDomainObject(epEndpoint, null);
108 basicAuthAccountServiceImpl.saveEndpoints(epEndpoint);
111 @Test(expected= NullPointerException.class)
112 public void saveEndpointAccountTest() throws Exception {
113 EPEndpointAccount record = new EPEndpointAccount();
114 record.setAccount_id(1l);
116 Mockito.doNothing().when(dataAccessService).saveDomainObject(record, null);
117 basicAuthAccountServiceImpl.saveEndpointAccount(1l, 2l);
121 public void updateBasicAuthAccountTest() throws Exception {
122 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
123 Mockito.doNothing().when(dataAccessService).saveDomainObject(basicAuthCredentials, null);
124 List<EPEndpoint> endpoints = new ArrayList<>();
125 EPEndpoint epEndpoint = new EPEndpoint();
126 epEndpoint.setId(1l);
127 epEndpoint.setName("name");
128 endpoints.add(epEndpoint);
129 basicAuthCredentials.setEndpoints(endpoints);
130 List<EPEndpointAccount> list = null;
131 Map<String, Long> params = new HashMap<>();
132 params.put("account_id", 1l);
133 Mockito.when(dataAccessService.executeNamedQuery("getEPEndpointAccountByAccountId", null, null)).thenReturn(list);
134 EPEndpoint temp_ep = new EPEndpoint();
136 boolean flag = false;
137 Map<String, String> params1 = new HashMap<String, String>();
138 params1.put("accountId", Long.toString(1l));
139 params1.put("epId", Long.toString(1l));
140 Mockito.when(dataAccessService.executeNamedQuery("deleteAccountEndpointRecord", params1, null)).thenReturn(null);
141 basicAuthAccountServiceImpl.updateBasicAuthAccount(1l, basicAuthCredentials);
146 public void getAccountDataTest() throws Exception {
147 List<BasicAuthCredentials> list = new ArrayList<>();
148 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
149 Mockito.when((List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null))
151 basicAuthAccountServiceImpl.getAccountData();
155 public void getAccountDataTest_password() throws Exception {
156 PowerMockito.mockStatic(CipherUtil.class);
157 PowerMockito.mockStatic(SystemProperties.class);
158 List<BasicAuthCredentials> list = new ArrayList<>();
159 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
160 basicAuthCredentials.setPassword("password");
161 list.add(basicAuthCredentials);
162 Mockito.when((List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null))
164 String result = null;
165 Mockito.when(CipherUtil.decryptPKC("password", SystemProperties.getProperty(SystemProperties.Decryption_Key))).thenReturn(result);
170 public void deleteEndpointAccoutTest() throws Exception {
171 Map<String, String> params = new HashMap<String, String>();
172 params.put("accountId", Long.toString(1l));
173 Mockito.when(dataAccessService.executeNamedQuery("deleteAccountEndpoint", params, null)).thenReturn(null);
174 Mockito.when(dataAccessService.executeNamedQuery("deleteBasicAuthAccount", params, null)).thenReturn(null);
175 basicAuthAccountServiceImpl.deleteEndpointAccout(1l);