Merge "Fix sql injection vulnerability"
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / BasicAuthenticationCredentialServiceImplTest.java
1
2
3 /*-
4  * ============LICENSE_START==========================================
5  * ONAP Portal
6  * ===================================================================
7  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  */
39 package org.onap.portalapp.portal.service;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import org.hibernate.criterion.Criterion;
45 import org.hibernate.criterion.ProjectionList;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.InjectMocks;
51 import org.mockito.Matchers;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.onap.portalapp.portal.domain.BasicAuthCredentials;
56 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
57 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
58 import org.onap.portalsdk.core.service.DataAccessService;
59 import org.powermock.api.mockito.PowerMockito;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
62
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest({CipherUtil.class})
65
66 public class BasicAuthenticationCredentialServiceImplTest {
67
68         @Mock
69         private DataAccessService dataAccessService;
70
71         @InjectMocks
72         private BasicAuthenticationCredentialServiceImpl serviceImpl = new BasicAuthenticationCredentialServiceImpl();
73
74         @Before
75         public void setup() throws CipherUtilException {
76                 MockitoAnnotations.initMocks(this);
77                 
78         }
79
80         @Test
81         public void unt_getBasicAuthCredentialByUsernameAndPassword() throws CipherUtilException {
82                 PowerMockito.mockStatic(CipherUtil.class);
83                 PowerMockito.when(CipherUtil.decryptPKC(Matchers.anyString())).thenReturn("password1234");
84                 List credList=new ArrayList<>();
85                 BasicAuthCredentials basicAuthCredentials=new BasicAuthCredentials();
86                 basicAuthCredentials.setPassword("password");
87                 //basicAuthCredentials.setId(123L);
88                 credList.add(basicAuthCredentials);
89                 Mockito.when(dataAccessService.getList(Matchers.any(),(ProjectionList)Matchers.isNull() , Matchers.anyListOf(Criterion.class),(List)Matchers.isNull())).thenReturn(credList);
90                 BasicAuthCredentials credentials = serviceImpl.getBasicAuthCredentialByUsernameAndPassword("abc1234",
91                                 "password1234");
92                 Assert.assertNotNull(credentials);
93                 
94         }
95         
96         @Test
97         public void unt_getBasicAuthCredentialByUsernameAndPassword_withException() {
98                 List credList=new ArrayList<>();
99                 BasicAuthCredentials basicAuthCredentials=new BasicAuthCredentials();
100                 basicAuthCredentials.setPassword("password");
101                 credList.add(basicAuthCredentials);
102                 Mockito.when(dataAccessService.getList(Matchers.any(),(ProjectionList)Matchers.isNull() , Matchers.anyListOf(Criterion.class),(List)Matchers.isNull())).thenReturn(credList);
103                 BasicAuthCredentials credentials = serviceImpl.getBasicAuthCredentialByUsernameAndPassword("abc1234",
104                                 "password1234");
105                 Assert.assertNull(credentials);
106                 
107         }
108
109 }
110
111