4409a4fc188f9e316e47271a0a7e3259cc21bbed
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / BasicAuthAccountServiceImplTest.java
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP  PORTAL
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
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
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=========================================================
19 */
20 package org.onap.portalapp.portal.service;
21
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
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;
51
52
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({ CipherUtil.class , SystemProperties.class})
55 public class BasicAuthAccountServiceImplTest {
56         @Mock
57         DataAccessService dataAccessService = new DataAccessServiceImpl();
58                 
59         @Before
60         public void setup() {
61                 MockitoAnnotations.initMocks(this);
62         }
63         
64         @InjectMocks
65         BasicAuthAccountServiceImpl  basicAuthAccountServiceImpl = new BasicAuthAccountServiceImpl();
66         
67         
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();
73         
74         @Test
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);
80                 
81         }
82         
83         @Test
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");
89                 String result = null;
90                 Mockito.when(CipherUtil.encryptPKC("password", SystemProperties.getProperty(SystemProperties.Decryption_Key))).thenReturn(result);
91                 basicAuthAccountServiceImpl.saveBasicAuthAccount(credentials);
92         }
93         
94         @Test
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();
101                 endpoint.setId(1l);
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);
109         }
110         
111         @Test(expected= NullPointerException.class)
112         public void saveEndpointAccountTest() throws Exception {
113                 EPEndpointAccount record = new EPEndpointAccount();
114                 record.setAccount_id(1l);
115                 record.setEp_id(2l);
116                 Mockito.doNothing().when(dataAccessService).saveDomainObject(record,  null);
117                 basicAuthAccountServiceImpl.saveEndpointAccount(1l, 2l);
118         }
119         
120         @Test
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();
135                 temp_ep.setId(1l);
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);
142         }
143         
144                 
145         @Test
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))
150                 .thenReturn(list);
151                 basicAuthAccountServiceImpl.getAccountData();
152         }
153         
154         @Test
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))
163                 .thenReturn(list);
164                 String result = null;
165                 Mockito.when(CipherUtil.decryptPKC("password", SystemProperties.getProperty(SystemProperties.Decryption_Key))).thenReturn(result);
166                 
167         }
168
169         @Test
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);
176                 
177         }
178         
179         @Test
180         public void getBasicAuthCredentialsById() throws Exception{
181                 List<BasicAuthCredentials> list = new ArrayList<>();
182                 BasicAuthCredentials basicAuthCredentials = new BasicAuthCredentials();
183                 basicAuthCredentials.setPassword("password");
184                 basicAuthCredentials.setId(1l);
185                 list.add(basicAuthCredentials);
186                 Mockito.when((List<BasicAuthCredentials>) dataAccessService.getList(BasicAuthCredentials.class, null))
187                 .thenReturn(list);
188                 basicAuthAccountServiceImpl.getBasicAuthCredentialsById(1l);
189                 
190         }
191 }