Fix sonar issues
[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.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;
36
37 import static junit.framework.TestCase.assertEquals;
38 import static org.mockito.Mockito.*;
39 import static org.springframework.test.util.ReflectionTestUtils.setField;
40
41 class ResultCaptor<T> implements Answer {
42     private T result = null;
43
44     public T getResult() {
45         return result;
46     }
47
48     @Override
49     public T answer(InvocationOnMock invocationOnMock) throws Throwable {
50         result = (T) invocationOnMock.callRealMethod();
51         return result;
52     }
53 }
54
55 public class TestAAIRestApiProvider extends TestBase {
56     private AAIRestApiProvider aaiRestApiProvider;
57     @Mock
58     private HostnameVerifier hostnameVerifier;
59     private AaiSecurityProvider aaiSecurityProviderReal = new AaiSecurityProvider();
60     private AaiSecurityProvider aaiSecurityProvider = spy(aaiSecurityProviderReal);
61
62     @Before
63     public void init() {
64         aaiRestApiProvider = new AAIRestApiProvider(msbApiProvider, aaiSecurityProvider);
65     }
66
67     /**
68      * test building a client to access AAI API
69      */
70     @Test
71     public void testApiClientBuilder() throws Exception {
72         when(aaiSecurityProvider.skipCertificateVerification()).thenReturn(true);
73         when(aaiSecurityProvider.skipHostnameVerification()).thenReturn(true);
74         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
75         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
76         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
77         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
78         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.NETWORK.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
79         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
80         //when
81         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
82         //verify
83         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
84         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
85         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
86
87         //given
88         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         //when
90         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
91         //verify
92         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
93
94         //given
95         Interceptor.Chain chain = Mockito.mock(Interceptor.Chain.class);
96         when(chain.request()).thenReturn(new Request.Builder().url("http://1.2.3.4/d").build());
97         ArgumentCaptor<Request> modifedRequest = ArgumentCaptor.forClass(Request.class);
98         when(chain.proceed(modifedRequest.capture())).thenReturn(resp);
99         //when
100         apiClient.getOkBuilder().interceptors().get(0).intercept(chain);
101         //verify
102         assertEquals(SelfRegistrationManager.SERVICE_NAME, modifedRequest.getValue().header("X-FromAppId"));
103
104     }
105
106     /**
107      * is slash is missing from micro service URL it is added
108      */
109     @Test
110     public void testApiClientBuilderMissingSlash() throws Exception {
111         when(aaiSecurityProvider.skipCertificateVerification()).thenReturn(true);
112         when(aaiSecurityProvider.skipHostnameVerification()).thenReturn(true);
113         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
114         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
115         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
116         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
117         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.NETWORK.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a");
118         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
119         //when
120         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
121         //verify
122         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
123         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
124         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
125         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();
126         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
127         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
128     }
129
130     /**
131      * test building a client to access AAI API
132      */
133     @Test
134     public void testApiClientBuilderForCloud() throws Exception {
135         when(aaiSecurityProvider.skipCertificateVerification()).thenReturn(true);
136         when(aaiSecurityProvider.skipHostnameVerification()).thenReturn(true);
137         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
138         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
139         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
140         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
141         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.CLOUD.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
142         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
143         //when
144         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.CLOUD);
145         //verify
146         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
147         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
148         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
149         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();
150         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
151         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
152     }
153
154     /**
155      * test building a client to access AAI API
156      */
157     @Test
158     public void testApiClientBuilderForExternalSystems() throws Exception {
159         when(aaiSecurityProvider.skipCertificateVerification()).thenReturn(true);
160         when(aaiSecurityProvider.skipHostnameVerification()).thenReturn(true);
161         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
162         setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
163         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
164         doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
165         when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.ESR.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
166         when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
167         //when
168         org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.ESR);
169         //verify
170         assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
171         assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
172         assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
173         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();
174         Request authenticate = apiClient.getOkBuilder().build().authenticator().authenticate(null, resp);
175         assertEquals("Basic dXNlcm5hbWU6YWFpUGFzc3dvcmQ=", authenticate.headers().get("Authorization"));
176     }
177
178     /**
179      * Test API wrapping for NetworkApi
180      * (questionable benefit [ this is more less ensured by Java type safety) ]
181      */
182     @Test
183     public void testNetworkApiAPiWrapping() {
184         org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
185         class TestClasss extends AAIRestApiProvider {
186             public TestClasss() {
187                 super(msbApiProvider, aaiSecurityProvider);
188             }
189
190             @Override
191             org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
192                 return c;
193             }
194         }
195         NetworkApi defaultApi = Mockito.mock(NetworkApi.class);
196         when(c.createService(NetworkApi.class)).thenReturn(defaultApi);
197         //verify
198         TestClasss testInstnace = new TestClasss();
199         assertEquals(defaultApi, testInstnace.getNetworkApi());
200     }
201
202     /**
203      * Test API wrapping for CloudInfrastructureApi
204      * (questionable benefit [ this is more less ensured by Java type safety) ]
205      */
206     @Test
207     public void testCloudInfrastructureApiWrapping() {
208         org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
209         class TestClasss extends AAIRestApiProvider {
210             public TestClasss() {
211                 super(msbApiProvider, aaiSecurityProvider);
212             }
213
214             @Override
215             org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
216                 return c;
217             }
218         }
219         CloudInfrastructureApi defaultApi = Mockito.mock(CloudInfrastructureApi.class);
220         when(c.createService(CloudInfrastructureApi.class)).thenReturn(defaultApi);
221         //verify
222         TestClasss testInstnace = new TestClasss();
223         assertEquals(defaultApi, testInstnace.getCloudInfrastructureApi());
224     }
225
226     /**
227      * Test API wrapping for ExternalSystemApi
228      * (questionable benefit [ this is more less ensured by Java type safety) ]
229      */
230     @Test
231     public void testExternalSystemApiWrapping() {
232         org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
233         class TestClasss extends AAIRestApiProvider {
234             public TestClasss() {
235                 super(msbApiProvider, aaiSecurityProvider);
236             }
237
238             @Override
239             org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
240                 return c;
241             }
242         }
243         ExternalSystemApi defaultApi = Mockito.mock(ExternalSystemApi.class);
244         when(c.createService(ExternalSystemApi.class)).thenReturn(defaultApi);
245         //verify
246         TestClasss testInstnace = new TestClasss();
247         assertEquals(defaultApi, testInstnace.getExternalSystemApi());
248     }
249 }