summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestCbamRestApiProvider.java
blob: 45a3216a42118f069004432226afed19afa07705 (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
/*
 * 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.core;

import com.nokia.cbam.catalog.v1.api.DefaultApi;
import com.nokia.cbam.lcm.v32.ApiClient;
import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi;
import com.nokia.cbam.lcm.v32.api.VnfsApi;
import com.nokia.cbam.lcn.v32.api.SubscriptionsApi;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
import org.onap.msb.sdk.discovery.entity.NodeInfo;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
import org.onap.vnfmdriver.model.VnfmInfo;
import org.springframework.core.env.Environment;

import java.util.Base64;
import java.util.HashSet;
import java.util.Set;

import static junit.framework.TestCase.*;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.springframework.test.util.ReflectionTestUtils.setField;

public class TestCbamRestApiProvider extends TestBase {
    @Mock
    private Environment environment;
    @Mock
    private CbamTokenProvider cbamTokenProvider;
    private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
    private Set<NodeInfo> nodes = new HashSet<>();

    private CbamRestApiProvider cbamRestApiProvider;

    @Before
    public void init() {
        microServiceInfo.setNodes(nodes);
        CbamRestApiProvider real = new CbamRestApiProvider(driverProperties, cbamTokenProvider, vnfmInfoProvider);
        setField(real, "trustedCertificates", "mytrustedCertificates");
        setField(real, "skipCertificateVerification", true);
        cbamRestApiProvider = spy(real);
        when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
        when(environment.getProperty(VnfmInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, Long.valueOf(10 * 60 * 1000))).thenReturn(10 * 60 * 1000L);
    }

    /**
     * test CBAM LCM API retrieval without SSL verification
     */
    @Test
    public void testCbamLcmApi() throws Exception {
        VnfmInfo expectedVnfmInfo = new VnfmInfo();
        when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
        when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
        //when
        VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID);
        //verify
        ApiClient apiClient = cbamLcmApi.getApiClient();
        assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
        assertNull(apiClient.getSslCaCert());
        assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
        assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
        assertTrue(!cbamLcmApi.getApiClient().isVerifyingSsl());
    }

    /**
     * test CBAM LCM API retrieval with SSL verification
     */
    @Test
    public void testCbamLcmApiWithSslVerfy() throws Exception {
        VnfmInfo expectedVnfmInfo = new VnfmInfo();
        when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
        when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
        setField(cbamRestApiProvider, "skipCertificateVerification", false);
        setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
        //when
        VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID);
        //verify
        ApiClient apiClient = cbamLcmApi.getApiClient();
        assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
        assertNotNull(apiClient.getSslCaCert());
        assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
        assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
        assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl());
    }

    /**
     * test CBAM Catalog API retrieval without SSL verification
     */
    @Test
    public void testCbamCatalogApi() throws Exception {
        VnfmInfo expectedVnfmInfo = new VnfmInfo();
        when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
        when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path");
        //when
        DefaultApi cbamCatalogApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID);
        //verify
        com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamCatalogApi.getApiClient();
        assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
        assertNull(apiClient.getSslCaCert());
        assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
        assertEquals(2, cbamCatalogApi.getApiClient().getAuthentications().size());
        assertTrue(!cbamCatalogApi.getApiClient().isVerifyingSsl());
    }

    /**
     * test CBAM Catalog API retrieval with SSL verification
     */
    @Test
    public void testCbamCatalogApiWithSslVerfy() throws Exception {
        VnfmInfo expectedVnfmInfo = new VnfmInfo();
        when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
        when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path");
        setField(cbamRestApiProvider, "skipCertificateVerification", false);
        setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
        //when
        DefaultApi cbamLcmApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID);
        //verify
        com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamLcmApi.getApiClient();
        assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
        assertNotNull(apiClient.getSslCaCert());
        assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
        assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
        assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl());
    }

    /**
     * test CBAM Lcn API retrieval without SSL verification
     */
    @Test
    public void testCbamLcnApi() throws Exception {
        VnfmInfo expectedVnfmInfo = new VnfmInfo();
        when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
        when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path");
        //when
        SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID);
        //verify
        com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient();
        assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
        assertNull(apiClient.getSslCaCert());
        assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
        assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
        assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl());
    }

    /**
     * test CBAM Lcn API retrieval with SSL verification
     */
    @Test
    public void testCbamLcnApiWithSslVerfy() throws Exception {
        VnfmInfo expectedVnfmInfo = new VnfmInfo();
        when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
        when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path");
        setField(cbamRestApiProvider, "skipCertificateVerification", false);
        setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
        //when
        SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID);
        //verify
        com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient();
        assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
        assertNotNull(apiClient.getSslCaCert());
        assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
        assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
        assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl());
    }

    /**
     * test CBAM operation exeution API retrieval without SSL verification
     */
    @Test
    public void testCbamOpexApi() throws Exception {
        VnfmInfo expectedVnfmInfo = new VnfmInfo();
        when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
        when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
        //when
        OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID);
        //verify
        ApiClient apiClient = cbamLcnApi.getApiClient();
        assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
        assertNull(apiClient.getSslCaCert());
        assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
        assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
        assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl());
    }

    /**
     * test CBAM operation execution API retrieval with SSL verification
     */
    @Test
    public void testCbamOpexApiWithSslVerfy() throws Exception {
        when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
        setField(cbamRestApiProvider, "skipCertificateVerification", false);
        setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
        VnfmInfo expectedVnfmInfo = new VnfmInfo();
        when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
        when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
        //when
        OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID);
        //verify
        ApiClient apiClient = cbamLcnApi.getApiClient();
        assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
        assertNotNull(apiClient.getSslCaCert());
        assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
        assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
        assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl());
    }
}