Removing jackson to mitigate cve-2017-4995
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / TestAAIRestApiProvider.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct;
17
18 import javax.net.ssl.HostnameVerifier;
19 import javax.net.ssl.SSLSocketFactory;
20 import okhttp3.Protocol;
21 import okhttp3.Request;
22 import okhttp3.Response;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29 import org.onap.aai.api.CloudInfrastructureApi;
30 import org.onap.aai.api.ExternalSystemApi;
31 import org.onap.aai.api.NetworkApi;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
33
34 import static junit.framework.TestCase.assertEquals;
35 import static org.mockito.Mockito.*;
36 import static org.springframework.test.util.ReflectionTestUtils.setField;
37
38 class ResultCaptor<T> implements Answer {
39     private T result = null;
40
41     public T getResult() {
42         return result;
43     }
44
45     @Override
46     public T answer(InvocationOnMock invocationOnMock) throws Throwable {
47         result = (T) invocationOnMock.callRealMethod();
48         return result;
49     }
50 }
51
52 public class TestAAIRestApiProvider extends TestBase {
53     private AAIRestApiProvider aaiRestApiProvider;
54     @Mock
55     private HostnameVerifier hostnameVerifier;
56     private AaiSecurityProvider aaiSecurityProvider = spy(new AaiSecurityProvider());
57
58     @Before
59     public void init() {
60         aaiRestApiProvider = new AAIRestApiProvider(msbApiProvider, aaiSecurityProvider);
61     }
62
63     /**
64      * test building a client to access AAI API
65      */
66     @Test
67     public void testApiClientBuilder() throws Exception {
68         setField(aaiSecurityProvider, "skipCertificateVerification", true);
69         setField(aaiSecurityProvider, "skipHostnameVerification", true);
70         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
71         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
72         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
73         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
74         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.NETWORK.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
75         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
76         //when
77         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
78         //verify
79         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
80         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
81         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
82         Response resp = new Response.Builder().message("a").code(200).protocol(Protocol.HTTP_1_0).request(new Request.Builder().url("http://1.2.3.4/d").build()).build();
83         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
84         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
85     }
86
87     /**
88      * is slash is missing from micro service URL it is added
89      */
90     @Test
91     public void testApiClientBuilderMissingSlash() throws Exception {
92         setField(aaiSecurityProvider, "skipCertificateVerification", true);
93         setField(aaiSecurityProvider, "skipHostnameVerification", true);
94         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
95         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
96         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
97         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
98         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.NETWORK.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a");
99         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
100         //when
101         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
102         //verify
103         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
104         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
105         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
106         Response resp = new Response.Builder().message("a").code(200).protocol(Protocol.HTTP_1_0).request(new Request.Builder().url("http://1.2.3.4/d").build()).build();
107         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
108         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
109     }
110
111     /**
112      * test building a client to access AAI API
113      */
114     @Test
115     public void testApiClientBuilderForCloud() throws Exception {
116         setField(aaiSecurityProvider, "skipCertificateVerification", true);
117         setField(aaiSecurityProvider, "skipHostnameVerification", true);
118         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
119         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
120         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
121         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
122         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.CLOUD.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
123         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
124         //when
125         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.CLOUD);
126         //verify
127         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
128         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
129         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
130         Response resp = new Response.Builder().message("a").code(200).protocol(Protocol.HTTP_1_0).request(new Request.Builder().url("http://1.2.3.4/d").build()).build();
131         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
132         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
133     }
134
135     /**
136      * test building a client to access AAI API
137      */
138     @Test
139     public void testApiClientBuilderForExternalSystems() throws Exception {
140         setField(aaiSecurityProvider, "skipCertificateVerification", true);
141         setField(aaiSecurityProvider, "skipHostnameVerification", true);
142         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
143         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
144         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
145         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
146         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.ESR.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
147         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
148         //when
149         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.ESR);
150         //verify
151         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
152         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
153         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
154         Response resp = new Response.Builder().message("a").code(200).protocol(Protocol.HTTP_1_0).request(new Request.Builder().url("http://1.2.3.4/d").build()).build();
155         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
156         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
157     }
158
159     /**
160      * Test API wrapping for NetworkApi
161      * (questionable benefit [ this is more less ensured by Java type safety) ]
162      */
163     @Test
164     public void testNetworkApiAPiWrapping() {
165         org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
166         class TestClasss extends AAIRestApiProvider {
167             public TestClasss() {
168                 super(msbApiProvider, aaiSecurityProvider);
169             }
170
171             @Override
172             org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
173                 return c;
174             }
175         }
176         NetworkApi defaultApi = Mockito.mock(NetworkApi.class);
177         when(c.createService(NetworkApi.class)).thenReturn(defaultApi);
178         //verify
179         TestClasss testInstnace = new TestClasss();
180         assertEquals(defaultApi, testInstnace.getNetworkApi());
181     }
182
183     /**
184      * Test API wrapping for CloudInfrastructureApi
185      * (questionable benefit [ this is more less ensured by Java type safety) ]
186      */
187     @Test
188     public void testCloudInfrastructureApiWrapping() {
189         org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
190         class TestClasss extends AAIRestApiProvider {
191             public TestClasss() {
192                 super(msbApiProvider, aaiSecurityProvider);
193             }
194
195             @Override
196             org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
197                 return c;
198             }
199         }
200         CloudInfrastructureApi defaultApi = Mockito.mock(CloudInfrastructureApi.class);
201         when(c.createService(CloudInfrastructureApi.class)).thenReturn(defaultApi);
202         //verify
203         TestClasss testInstnace = new TestClasss();
204         assertEquals(defaultApi, testInstnace.getCloudInfrastructureApi());
205     }
206
207     /**
208      * Test API wrapping for ExternalSystemApi
209      * (questionable benefit [ this is more less ensured by Java type safety) ]
210      */
211     @Test
212     public void testExternalSystemApiWrapping() {
213         org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
214         class TestClasss extends AAIRestApiProvider {
215             public TestClasss() {
216                 super(msbApiProvider, aaiSecurityProvider);
217             }
218
219             @Override
220             org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
221                 return c;
222             }
223         }
224         ExternalSystemApi defaultApi = Mockito.mock(ExternalSystemApi.class);
225         when(c.createService(ExternalSystemApi.class)).thenReturn(defaultApi);
226         //verify
227         TestClasss testInstnace = new TestClasss();
228         assertEquals(defaultApi, testInstnace.getExternalSystemApi());
229     }
230 }