summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/direct/TestAAIRestApiProvider.java
blob: efbe1f8f88cd773823daaf134d3c524eef5c21c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright 2016-2017, Nokia Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import okhttp3.Interceptor;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.onap.aai.api.CloudInfrastructureApi;
import org.onap.aai.api.ExternalSystemApi;
import org.onap.aai.api.NetworkApi;
import org.onap.aai.auth.HttpBasicAuth;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;

import static junit.framework.TestCase.assertEquals;
import static org.mockito.Mockito.*;

class ResultCaptor<T> implements Answer {
    private T result = null;

    public T getResult() {
        return result;
    }

    @Override
    public T answer(InvocationOnMock invocationOnMock) throws Throwable {
        result = (T) invocationOnMock.callRealMethod();
        return result;
    }
}

public class TestAAIRestApiProvider extends TestBase {
    private AAIRestApiProvider aaiRestApiProvider;
    @Mock
    private HostnameVerifier hostnameVerifier;
    private AaiSecurityProvider aaiSecurityProviderReal = new AaiSecurityProvider();
    private AaiSecurityProvider aaiSecurityProvider = spy(aaiSecurityProviderReal);

    @Before
    public void init() {
        aaiRestApiProvider = new AAIRestApiProvider(msbApiProvider, aaiSecurityProvider);
    }

    /**
     * test building a client to access AAI API
     */
    @Test
    public void testApiClientBuilder() throws Exception {
        when(aaiSecurityProvider.skipCertificateVerification()).thenReturn(true);
        when(aaiSecurityProvider.skipHostnameVerification()).thenReturn(true);
        setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
        setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
        ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
        doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
        when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.NETWORK.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
        when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
        //when
        org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
        //verify
        assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
        assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
        assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
        HttpBasicAuth basic = (HttpBasicAuth) apiClient.getApiAuthorizations().get("basic");
        assertEquals("username", basic.getUsername());
        assertEquals("aaiPassword", basic.getPassword());

        //given
        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();
        Interceptor.Chain chain = Mockito.mock(Interceptor.Chain.class);
        when(chain.request()).thenReturn(new Request.Builder().url("http://1.2.3.4/d").build());
        ArgumentCaptor<Request> modifedRequest = ArgumentCaptor.forClass(Request.class);
        when(chain.proceed(modifedRequest.capture())).thenReturn(resp);
        //when
        apiClient.getOkBuilder().interceptors().get(0).intercept(chain);
        //verify
        assertEquals(SelfRegistrationManager.SERVICE_NAME, modifedRequest.getValue().header("X-FromAppId"));

    }

    /**
     * is slash is missing from micro service URL it is added
     */
    @Test
    public void testApiClientBuilderMissingSlash() throws Exception {
        when(aaiSecurityProvider.skipCertificateVerification()).thenReturn(true);
        when(aaiSecurityProvider.skipHostnameVerification()).thenReturn(true);
        setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
        setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
        ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
        doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
        when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.NETWORK.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a");
        when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
        //when
        org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.NETWORK);
        //verify
        assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
        assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
        assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
        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();
        HttpBasicAuth basic = (HttpBasicAuth) apiClient.getApiAuthorizations().get("basic");
        assertEquals("username", basic.getUsername());
        assertEquals("aaiPassword", basic.getPassword());
    }

    /**
     * test building a client to access AAI API
     */
    @Test
    public void testApiClientBuilderForCloud() throws Exception {
        when(aaiSecurityProvider.skipCertificateVerification()).thenReturn(true);
        when(aaiSecurityProvider.skipHostnameVerification()).thenReturn(true);
        setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
        setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
        ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
        doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
        when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.CLOUD.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
        when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
        //when
        org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.CLOUD);
        //verify
        assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
        assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
        assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
        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();
        HttpBasicAuth basic = (HttpBasicAuth) apiClient.getApiAuthorizations().get("basic");
        assertEquals("username", basic.getUsername());
        assertEquals("aaiPassword", basic.getPassword());
    }

    /**
     * test building a client to access AAI API
     */
    @Test
    public void testApiClientBuilderForExternalSystems() throws Exception {
        when(aaiSecurityProvider.skipCertificateVerification()).thenReturn(true);
        when(aaiSecurityProvider.skipHostnameVerification()).thenReturn(true);
        setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiUsername}", "username");
        setFieldWithPropertyAnnotation(aaiRestApiProvider, "${aaiPassword}", "aaiPassword");
        ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
        doAnswer(sslSocketFactoryResultCaptor).when(aaiSecurityProvider).buildSSLSocketFactory();
        when(msbApiProvider.getMicroServiceUrl(AAIRestApiProvider.AAIService.ESR.getServiceName(), "v11")).thenReturn("http://1.2.3.4/a/");
        when(aaiSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
        //when
        org.onap.aai.ApiClient apiClient = aaiRestApiProvider.buildApiClient(AAIRestApiProvider.AAIService.ESR);
        //verify
        assertEquals("http://1.2.3.4/a/", apiClient.getAdapterBuilder().build().baseUrl().toString());
        assertEquals(sslSocketFactoryResultCaptor.getResult(), apiClient.getOkBuilder().build().sslSocketFactory());
        assertEquals(hostnameVerifier, apiClient.getOkBuilder().build().hostnameVerifier());
        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();
        HttpBasicAuth basic = (HttpBasicAuth) apiClient.getApiAuthorizations().get("basic");
        assertEquals("username", basic.getUsername());
        assertEquals("aaiPassword", basic.getPassword());
    }

    /**
     * Test API wrapping for NetworkApi
     * (questionable benefit [ this is more less ensured by Java type safety) ]
     */
    @Test
    public void testNetworkApiAPiWrapping() {
        org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
        class TestClasss extends AAIRestApiProvider {
            public TestClasss() {
                super(msbApiProvider, aaiSecurityProvider);
            }

            @Override
            org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
                return c;
            }
        }
        NetworkApi defaultApi = Mockito.mock(NetworkApi.class);
        when(c.createService(NetworkApi.class)).thenReturn(defaultApi);
        //verify
        TestClasss testInstnace = new TestClasss();
        assertEquals(defaultApi, testInstnace.getNetworkApi());
    }

    /**
     * Test API wrapping for CloudInfrastructureApi
     * (questionable benefit [ this is more less ensured by Java type safety) ]
     */
    @Test
    public void testCloudInfrastructureApiWrapping() {
        org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
        class TestClasss extends AAIRestApiProvider {
            public TestClasss() {
                super(msbApiProvider, aaiSecurityProvider);
            }

            @Override
            org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
                return c;
            }
        }
        CloudInfrastructureApi defaultApi = Mockito.mock(CloudInfrastructureApi.class);
        when(c.createService(CloudInfrastructureApi.class)).thenReturn(defaultApi);
        //verify
        TestClasss testInstnace = new TestClasss();
        assertEquals(defaultApi, testInstnace.getCloudInfrastructureApi());
    }

    /**
     * Test API wrapping for ExternalSystemApi
     * (questionable benefit [ this is more less ensured by Java type safety) ]
     */
    @Test
    public void testExternalSystemApiWrapping() {
        org.onap.aai.ApiClient c = Mockito.mock(org.onap.aai.ApiClient.class);
        class TestClasss extends AAIRestApiProvider {
            public TestClasss() {
                super(msbApiProvider, aaiSecurityProvider);
            }

            @Override
            org.onap.aai.ApiClient buildApiClient(AAIRestApiProvider.AAIService service) {
                return c;
            }
        }
        ExternalSystemApi defaultApi = Mockito.mock(ExternalSystemApi.class);
        when(c.createService(ExternalSystemApi.class)).thenReturn(defaultApi);
        //verify
        TestClasss testInstnace = new TestClasss();
        assertEquals(defaultApi, testInstnace.getExternalSystemApi());
    }
}