Resubmitting KeyProperty code change since tests failed
[portal.git] / ecomp-portal-BE-os / src / test / java / org / onap / portalapp / portal / service / RemoteWebServiceCallServiceImplTest.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  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertTrue;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
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.portal.domain.EPApp;
57 import org.onap.portalapp.portal.framework.MockitoTestSuite;
58 import org.onap.portalapp.service.RemoteWebServiceCallServiceImpl;
59 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
60 import org.onap.portalsdk.core.onboarding.util.KeyConstants;
61 import org.onap.portalsdk.core.onboarding.util.KeyProperties;
62 import org.onap.portalsdk.core.service.DataAccessService;
63 import org.onap.portalsdk.core.util.SystemProperties;
64 import org.powermock.api.mockito.PowerMockito;
65 import org.powermock.core.classloader.annotations.PrepareForTest;
66 import org.powermock.modules.junit4.PowerMockRunner;
67
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest({ CipherUtil.class , SystemProperties.class, KeyProperties.class, KeyConstants.class})
70 public class RemoteWebServiceCallServiceImplTest {
71         
72
73         @InjectMocks
74         RemoteWebServiceCallServiceImpl remoteWebServiceCallServiceImpl = new RemoteWebServiceCallServiceImpl();
75
76         @Mock
77         AppsCacheService appCacheService;
78         
79         @Mock
80         DataAccessService dataAccessService;
81
82         @Before
83         public void setup() {
84                 MockitoAnnotations.initMocks(this);
85         }
86
87         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
88
89         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
90         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
91
92         NullPointerException nullPointerException = new NullPointerException();
93         
94         @Test
95         public void verifyRESTCredentialTest() throws Exception
96         {
97                 PowerMockito.mockStatic(CipherUtil.class);
98                 PowerMockito.mockStatic(KeyProperties.class);
99                 PowerMockito.mockStatic(KeyConstants.class);
100                 String criteria= " where ueb_key = 'requestUebKey'";
101                 List<EPApp> appList = new ArrayList<>();
102                 EPApp app = new EPApp();
103                 app.setAppBasicAuthPassword("password");
104                 appList.add(app);
105                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
106                 String secretKey = null;
107                 Mockito.when(KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY)).thenReturn(secretKey);
108                 Mockito.when(CipherUtil.decryptPKC("password",secretKey == null ? null : secretKey)).thenReturn("pwd");
109                 assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","requestPassword"));
110         }
111         
112         @Test
113         public void verifyRESTCredentialExceptionTest() throws Exception
114         {
115                 PowerMockito.mockStatic(CipherUtil.class);
116                 PowerMockito.mockStatic(KeyProperties.class);
117                 PowerMockito.mockStatic(KeyConstants.class);
118                 String criteria= " where ueb_key = 'requestUebKey'";
119                 List<EPApp> appList = new ArrayList<>();
120                 EPApp app = new EPApp();
121                 app.setAppBasicAuthPassword("password");
122                 app.setAppBasicAuthUsername("requestAppName");
123                 appList.add(app);
124                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
125                 String secretKey = null;
126                 Mockito.when(KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY)).thenReturn(secretKey);
127                 Mockito.when(CipherUtil.decryptPKC("password",secretKey == null ? null : secretKey)).thenReturn("pwd");
128                 assertTrue(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd"));
129         }
130         
131         @Test
132         public void verifyRESTCredentialIfAppNullTest() throws Exception
133         {
134                 PowerMockito.mockStatic(CipherUtil.class);
135                 PowerMockito.mockStatic(SystemProperties.class);
136                 PowerMockito.mockStatic(KeyProperties.class);
137                 PowerMockito.mockStatic(KeyConstants.class);
138                 String criteria= " where ueb_key = 'requestUebKey'";
139                 List<EPApp> appList = new ArrayList<>();
140                 EPApp app = new EPApp();
141                 app.setAppBasicAuthPassword("password");
142                 app.setAppBasicAuthUsername("requestAppName");
143                 appList.add(app);
144                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null);
145                 String secretKey = null;
146                 Mockito.when(KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY)).thenReturn(secretKey);
147                 Mockito.when(CipherUtil.decryptPKC("password",secretKey == null ? null : secretKey)).thenReturn("pwd");
148                 assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd"));
149         }
150         
151         @Test
152         public void verifyAppKeyCredentialIfKeyIsNullTest() throws Exception
153         {
154                 assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential(null));
155         }
156         
157         @Test
158         public void verifyAppKeyCredentialTest() throws Exception
159         {
160                 PowerMockito.mockStatic(CipherUtil.class);
161                 PowerMockito.mockStatic(SystemProperties.class);
162                 StringBuffer criteria = new  StringBuffer("where ueb_key = 'requestUebKey'");
163 //              String criteria= " where ueb_key = 'requestUebKey'";
164                 List<EPApp> appList = new ArrayList<>();
165                 EPApp app = new EPApp();
166                 app.setAppBasicAuthPassword("password");
167                 app.setAppBasicAuthUsername("requestAppName");
168                 appList.add(app);
169                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null);
170                 assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test"));
171         }
172         
173         @Test
174         public void verifyAppKeyCredentialSuccessTest() throws Exception
175         {
176                 PowerMockito.mockStatic(CipherUtil.class);
177                 PowerMockito.mockStatic(SystemProperties.class);
178                 String criteria= " where ueb_key = 'test'";
179                 List<EPApp> appList = new ArrayList<>();
180                 EPApp app = new EPApp();
181                 app.setAppBasicAuthPassword("password");
182                 app.setAppBasicAuthUsername("requestAppName");
183                 appList.add(app);
184                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
185                 assertTrue(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test"));
186         }
187 }