Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-os / src / test / java / org / openecomp / portalapp / portal / service / RemoteWebServiceCallServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.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.openecomp.portalapp.portal.domain.EPApp;
57 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
58 import org.openecomp.portalapp.service.RemoteWebServiceCallServiceImpl;
59 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
60 import org.openecomp.portalsdk.core.service.DataAccessService;
61 import org.openecomp.portalsdk.core.util.SystemProperties;
62 import org.powermock.api.mockito.PowerMockito;
63 import org.powermock.core.classloader.annotations.PrepareForTest;
64 import org.powermock.modules.junit4.PowerMockRunner;
65
66 @RunWith(PowerMockRunner.class)
67 @PrepareForTest({ CipherUtil.class , SystemProperties.class})
68 public class RemoteWebServiceCallServiceImplTest {
69         
70
71         @InjectMocks
72         RemoteWebServiceCallServiceImpl remoteWebServiceCallServiceImpl = new RemoteWebServiceCallServiceImpl();
73
74         @Mock
75         AppsCacheService appCacheService;
76         
77         @Mock
78         DataAccessService dataAccessService;
79         
80
81         @Before
82         public void setup() {
83                 MockitoAnnotations.initMocks(this);
84         }
85
86         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
87
88         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
89         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
90
91         NullPointerException nullPointerException = new NullPointerException();
92         
93         @Test
94         public void verifyRESTCredentialTest() throws Exception
95         {
96                 PowerMockito.mockStatic(CipherUtil.class);
97                 PowerMockito.mockStatic(SystemProperties.class);
98                 String criteria= " where ueb_key = 'requestUebKey'";
99                 List<EPApp> appList = new ArrayList<>();
100                 EPApp app = new EPApp();
101                 app.setAppPassword("password");
102                 appList.add(app);
103                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
104                 String secretKey = null;
105                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey);
106                 Mockito.when(CipherUtil.decrypt("password",
107                                 secretKey == null ? null : secretKey)).thenReturn("pwd");
108                 assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","requestPassword"));
109         }
110         
111         @Test
112         public void verifyRESTCredentialExceptionTest() throws Exception
113         {
114                 PowerMockito.mockStatic(CipherUtil.class);
115                 PowerMockito.mockStatic(SystemProperties.class);
116                 String criteria= " where ueb_key = 'requestUebKey'";
117                 List<EPApp> appList = new ArrayList<>();
118                 EPApp app = new EPApp();
119                 app.setAppPassword("password");
120                 app.setUsername("requestAppName");
121                 appList.add(app);
122                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
123                 String secretKey = null;
124                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey);
125                 Mockito.when(CipherUtil.decrypt("password",
126                                 secretKey == null ? null : secretKey)).thenReturn("pwd");
127                 assertTrue(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd"));
128         }
129         
130         @Test
131         public void verifyRESTCredentialIfAppNullTest() throws Exception
132         {
133                 PowerMockito.mockStatic(CipherUtil.class);
134                 PowerMockito.mockStatic(SystemProperties.class);
135                 String criteria= " where ueb_key = 'requestUebKey'";
136                 List<EPApp> appList = new ArrayList<>();
137                 EPApp app = new EPApp();
138                 app.setAppPassword("password");
139                 app.setUsername("requestAppName");
140                 appList.add(app);
141                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null);
142                 String secretKey = null;
143                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey);
144                 Mockito.when(CipherUtil.decrypt("password",
145                                 secretKey == null ? null : secretKey)).thenReturn("pwd");
146                 assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd"));
147         }
148         
149         @Test
150         public void verifyAppKeyCredentialIfKeyIsNullTest() throws Exception
151         {
152                 assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential(null));
153         }
154         
155         @Test
156         public void verifyAppKeyCredentialTest() throws Exception
157         {
158                 PowerMockito.mockStatic(CipherUtil.class);
159                 PowerMockito.mockStatic(SystemProperties.class);
160                 StringBuffer criteria = new  StringBuffer("where ueb_key = 'requestUebKey'");
161 //              String criteria= " where ueb_key = 'requestUebKey'";
162                 List<EPApp> appList = new ArrayList<>();
163                 EPApp app = new EPApp();
164                 app.setAppPassword("password");
165                 app.setUsername("requestAppName");
166                 appList.add(app);
167                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null);
168                 assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test"));
169         }
170         
171         @Test
172         public void verifyAppKeyCredentialSuccessTest() throws Exception
173         {
174                 PowerMockito.mockStatic(CipherUtil.class);
175                 PowerMockito.mockStatic(SystemProperties.class);
176                 String criteria= " where ueb_key = 'test'";
177                 List<EPApp> appList = new ArrayList<>();
178                 EPApp app = new EPApp();
179                 app.setAppPassword("password");
180                 app.setUsername("requestAppName");
181                 appList.add(app);
182                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
183                 assertTrue(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test"));
184         }
185 }