2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct;
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;
34 import static junit.framework.TestCase.assertEquals;
35 import static org.mockito.Mockito.*;
36 import static org.springframework.test.util.ReflectionTestUtils.setField;
38 class ResultCaptor<T> implements Answer {
39 private T result = null;
41 public T getResult() {
46 public T answer(InvocationOnMock invocationOnMock) throws Throwable {
47 result = (T) invocationOnMock.callRealMethod();
52 public class TestAAIRestApiProvider extends TestBase {
53 private AAIRestApiProvider aaiRestApiProvider;
55 private HostnameVerifier hostnameVerifier;
56 private AaiSecurityProvider aaiSecurityProvider = spy(new AaiSecurityProvider());
60 aaiRestApiProvider = new AAIRestApiProvider(msbApiProvider, aaiSecurityProvider);
64 * test building a client to access AAI API
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);
77 org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
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"));
88 * is slash is missing from micro service URL it is added
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);
101 org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
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"));
112 * test building a client to access AAI API
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);
125 org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.CLOUD);
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"));
136 * test building a client to access AAI API
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);
149 org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.ESR);
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"));
160 * Test API wrapping for NetworkApi
161 * (questionable benefit [ this is more less ensured by Java type safety) ]
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);
172 org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
176 NetworkApi defaultApi = Mockito.mock(NetworkApi.class);
177 when(c.createService(NetworkApi.class)).thenReturn(defaultApi);
179 TestClasss testInstnace = new TestClasss();
180 assertEquals(defaultApi, testInstnace.getNetworkApi());
184 * Test API wrapping for CloudInfrastructureApi
185 * (questionable benefit [ this is more less ensured by Java type safety) ]
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);
196 org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
200 CloudInfrastructureApi defaultApi = Mockito.mock(CloudInfrastructureApi.class);
201 when(c.createService(CloudInfrastructureApi.class)).thenReturn(defaultApi);
203 TestClasss testInstnace = new TestClasss();
204 assertEquals(defaultApi, testInstnace.getCloudInfrastructureApi());
208 * Test API wrapping for ExternalSystemApi
209 * (questionable benefit [ this is more less ensured by Java type safety) ]
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);
220 org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
224 ExternalSystemApi defaultApi = Mockito.mock(ExternalSystemApi.class);
225 when(c.createService(ExternalSystemApi.class)).thenReturn(defaultApi);
227 TestClasss testInstnace = new TestClasss();
228 assertEquals(defaultApi, testInstnace.getExternalSystemApi());