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.Interceptor;
 
  21 import okhttp3.Protocol;
 
  22 import okhttp3.Request;
 
  23 import okhttp3.Response;
 
  24 import org.junit.Before;
 
  25 import org.junit.Test;
 
  26 import org.mockito.ArgumentCaptor;
 
  27 import org.mockito.Mock;
 
  28 import org.mockito.Mockito;
 
  29 import org.mockito.invocation.InvocationOnMock;
 
  30 import org.mockito.stubbing.Answer;
 
  31 import org.onap.aai.api.CloudInfrastructureApi;
 
  32 import org.onap.aai.api.ExternalSystemApi;
 
  33 import org.onap.aai.api.NetworkApi;
 
  34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
 
  35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
 
  37 import static junit.framework.TestCase.assertEquals;
 
  38 import static org.mockito.Mockito.*;
 
  39 import static org.springframework.test.util.ReflectionTestUtils.setField;
 
  41 class ResultCaptor<T> implements Answer {
 
  42     private T result = null;
 
  44     public T getResult() {
 
  49     public T answer(InvocationOnMock invocationOnMock) throws Throwable {
 
  50         result = (T) invocationOnMock.callRealMethod();
 
  55 public class TestAAIRestApiProvider extends TestBase {
 
  56     private AAIRestApiProvider aaiRestApiProvider;
 
  58     private HostnameVerifier hostnameVerifier;
 
  59     private AaiSecurityProvider aaiSecurityProvider = spy(new AaiSecurityProvider());
 
  63         aaiRestApiProvider = new AAIRestApiProvider(msbApiProvider, aaiSecurityProvider);
 
  67      * test building a client to access AAI API
 
  70     public void testApiClientBuilder() throws Exception {
 
  71         setField(aaiSecurityProvider, "skipCertificateVerification", true);
 
  72         setField(aaiSecurityProvider, "skipHostnameVerification", true);
 
  73         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
 
  74         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
 
  75         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
 
  76         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
 
  77         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.NETWORK.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
 
  78         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
 
  80         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
 
  82         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
 
  83         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
 
  84         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
 
  87         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();
 
  89         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
 
  91         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
 
  94         Interceptor.Chain chain = Mockito.mock(Interceptor.Chain.class);
 
  95         when(chain.request()).thenReturn(new Request.Builder().url("http://1.2.3.4/d").build());
 
  96         ArgumentCaptor<Request> modifedRequest = ArgumentCaptor.forClass(Request.class);
 
  97         when(chain.proceed(modifedRequest.capture())).thenReturn(resp);
 
  99         apiClient.getOkBuilder().interceptors().get(0).intercept(chain);
 
 101         assertEquals(SelfRegistrationManager.SERVICE_NAME, modifedRequest.getValue().header("X-FromAppId"));
 
 106      * is slash is missing from micro service URL it is added
 
 109     public void testApiClientBuilderMissingSlash() throws Exception {
 
 110         setField(aaiSecurityProvider, "skipCertificateVerification", true);
 
 111         setField(aaiSecurityProvider, "skipHostnameVerification", true);
 
 112         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
 
 113         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
 
 114         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
 
 115         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
 
 116         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.NETWORK.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a");
 
 117         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
 
 119         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
 
 121         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
 
 122         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
 
 123         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
 
 124         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();
 
 125         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
 
 126         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
 
 130      * test building a client to access AAI API
 
 133     public void testApiClientBuilderForCloud() throws Exception {
 
 134         setField(aaiSecurityProvider, "skipCertificateVerification", true);
 
 135         setField(aaiSecurityProvider, "skipHostnameVerification", true);
 
 136         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
 
 137         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
 
 138         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
 
 139         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
 
 140         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.CLOUD.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
 
 141         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
 
 143         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.CLOUD);
 
 145         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
 
 146         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
 
 147         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
 
 148         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();
 
 149         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
 
 150         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
 
 154      * test building a client to access AAI API
 
 157     public void testApiClientBuilderForExternalSystems() throws Exception {
 
 158         setField(aaiSecurityProvider, "skipCertificateVerification", true);
 
 159         setField(aaiSecurityProvider, "skipHostnameVerification", true);
 
 160         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
 
 161         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
 
 162         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
 
 163         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
 
 164         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.ESR.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
 
 165         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
 
 167         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.ESR);
 
 169         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
 
 170         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
 
 171         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
 
 172         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();
 
 173         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
 
 174         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
 
 178      * Test API wrapping for NetworkApi
 
 179      * (questionable benefit [ this is more less ensured by Java type safety) ]
 
 182     public void testNetworkApiAPiWrapping() {
 
 183         org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
 
 184         class TestClasss extends AAIRestApiProvider {
 
 185             public TestClasss() {
 
 186                 super(msbApiProvider, aaiSecurityProvider);
 
 190             org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
 
 194         NetworkApi defaultApi = Mockito.mock(NetworkApi.class);
 
 195         when(c.createService(NetworkApi.class)).thenReturn(defaultApi);
 
 197         TestClasss testInstnace = new TestClasss();
 
 198         assertEquals(defaultApi, testInstnace.getNetworkApi());
 
 202      * Test API wrapping for CloudInfrastructureApi
 
 203      * (questionable benefit [ this is more less ensured by Java type safety) ]
 
 206     public void testCloudInfrastructureApiWrapping() {
 
 207         org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
 
 208         class TestClasss extends AAIRestApiProvider {
 
 209             public TestClasss() {
 
 210                 super(msbApiProvider, aaiSecurityProvider);
 
 214             org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
 
 218         CloudInfrastructureApi defaultApi = Mockito.mock(CloudInfrastructureApi.class);
 
 219         when(c.createService(CloudInfrastructureApi.class)).thenReturn(defaultApi);
 
 221         TestClasss testInstnace = new TestClasss();
 
 222         assertEquals(defaultApi, testInstnace.getCloudInfrastructureApi());
 
 226      * Test API wrapping for ExternalSystemApi
 
 227      * (questionable benefit [ this is more less ensured by Java type safety) ]
 
 230     public void testExternalSystemApiWrapping() {
 
 231         org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
 
 232         class TestClasss extends AAIRestApiProvider {
 
 233             public TestClasss() {
 
 234                 super(msbApiProvider, aaiSecurityProvider);
 
 238             org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
 
 242         ExternalSystemApi defaultApi = Mockito.mock(ExternalSystemApi.class);
 
 243         when(c.createService(ExternalSystemApi.class)).thenReturn(defaultApi);
 
 245         TestClasss testInstnace = new TestClasss();
 
 246         assertEquals(defaultApi, testInstnace.getExternalSystemApi());