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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
19 import com.nokia.cbam.catalog.v1.api.DefaultApi;
20 import com.nokia.cbam.lcm.v32.ApiClient;
21 import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi;
22 import com.nokia.cbam.lcm.v32.api.VnfsApi;
23 import com.nokia.cbam.lcn.v32.api.SubscriptionsApi;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
28 import org.onap.msb.sdk.discovery.entity.NodeInfo;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
34 import org.onap.vnfmdriver.model.VnfmInfo;
35 import org.springframework.core.env.Environment;
37 import java.util.Base64;
38 import java.util.HashSet;
41 import static junit.framework.TestCase.*;
42 import static org.mockito.Mockito.spy;
43 import static org.mockito.Mockito.when;
44 import static org.springframework.test.util.ReflectionTestUtils.setField;
46 public class TestCbamRestApiProvider extends TestBase {
48 private Environment environment;
50 private CbamTokenProvider cbamTokenProvider;
51 private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
52 private Set<NodeInfo> nodes = new HashSet<>();
54 private CbamRestApiProvider cbamRestApiProvider;
58 microServiceInfo.setNodes(nodes);
59 CbamRestApiProvider real = new CbamRestApiProvider(driverProperties, cbamTokenProvider, vnfmInfoProvider);
60 setField(real, "trustedCertificates", "mytrustedCertificates");
61 setField(real, "skipCertificateVerification", true);
62 cbamRestApiProvider = spy(real);
63 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
64 when(environment.getProperty(VnfmInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, Long.valueOf(10 * 60 * 1000))).thenReturn(10 * 60 * 1000L);
68 * test CBAM LCM API retrieval without SSL verification
71 public void testCbamLcmApi() throws Exception {
72 VnfmInfo expectedVnfmInfo = new VnfmInfo();
73 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
74 expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
75 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
77 VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID);
79 ApiClient apiClient = cbamLcmApi.getApiClient();
80 assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
81 assertNull(apiClient.getSslCaCert());
82 assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
83 assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
84 assertTrue(!cbamLcmApi.getApiClient().isVerifyingSsl());
88 * test CBAM LCM API retrieval with SSL verification
91 public void testCbamLcmApiWithSslVerfy() throws Exception {
92 VnfmInfo expectedVnfmInfo = new VnfmInfo();
93 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
94 expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
95 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
96 setField(cbamRestApiProvider, "skipCertificateVerification", false);
97 setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
99 VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID);
101 ApiClient apiClient = cbamLcmApi.getApiClient();
102 assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
103 assertNotNull(apiClient.getSslCaCert());
104 assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
105 assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
106 assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl());
110 * test CBAM Catalog API retrieval without SSL verification
113 public void testCbamCatalogApi() throws Exception {
114 VnfmInfo expectedVnfmInfo = new VnfmInfo();
115 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
116 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
117 when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path");
119 DefaultApi cbamCatalogApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID);
121 com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamCatalogApi.getApiClient();
122 assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
123 assertNull(apiClient.getSslCaCert());
124 assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
125 assertEquals(2, cbamCatalogApi.getApiClient().getAuthentications().size());
126 assertTrue(!cbamCatalogApi.getApiClient().isVerifyingSsl());
130 * test CBAM Catalog API retrieval with SSL verification
133 public void testCbamCatalogApiWithSslVerfy() throws Exception {
134 VnfmInfo expectedVnfmInfo = new VnfmInfo();
135 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
136 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
137 when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path");
138 setField(cbamRestApiProvider, "skipCertificateVerification", false);
139 setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
141 DefaultApi cbamLcmApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID);
143 com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamLcmApi.getApiClient();
144 assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
145 assertNotNull(apiClient.getSslCaCert());
146 assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
147 assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
148 assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl());
152 * test CBAM Lcn API retrieval without SSL verification
155 public void testCbamLcnApi() throws Exception {
156 VnfmInfo expectedVnfmInfo = new VnfmInfo();
157 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
158 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
159 when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path");
161 SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID);
163 com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient();
164 assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
165 assertNull(apiClient.getSslCaCert());
166 assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
167 assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
168 assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl());
172 * test CBAM Lcn API retrieval with SSL verification
175 public void testCbamLcnApiWithSslVerfy() throws Exception {
176 VnfmInfo expectedVnfmInfo = new VnfmInfo();
177 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
178 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
179 when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path");
180 setField(cbamRestApiProvider, "skipCertificateVerification", false);
181 setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
183 SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID);
185 com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient();
186 assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
187 assertNotNull(apiClient.getSslCaCert());
188 assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
189 assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
190 assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl());
194 * test CBAM operation exeution API retrieval without SSL verification
197 public void testCbamOpexApi() throws Exception {
198 VnfmInfo expectedVnfmInfo = new VnfmInfo();
199 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
200 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
201 when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
202 expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
204 OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID);
206 ApiClient apiClient = cbamLcnApi.getApiClient();
207 assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
208 assertNull(apiClient.getSslCaCert());
209 assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
210 assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
211 assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl());
215 * test CBAM operation execution API retrieval with SSL verification
218 public void testCbamOpexApiWithSslVerfy() throws Exception {
219 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
220 setField(cbamRestApiProvider, "skipCertificateVerification", false);
221 setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
222 VnfmInfo expectedVnfmInfo = new VnfmInfo();
223 when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
224 expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
225 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
227 OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID);
229 ApiClient apiClient = cbamLcnApi.getApiClient();
230 assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
231 assertNotNull(apiClient.getSslCaCert());
232 assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
233 assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
234 assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl());