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.util.TestUtil;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
33 import org.onap.vnfmdriver.model.VnfmInfo;
34 import org.springframework.core.env.Environment;
36 import java.util.Base64;
37 import java.util.HashSet;
40 import static junit.framework.TestCase.*;
41 import static org.mockito.Mockito.spy;
42 import static org.mockito.Mockito.when;
43 import static org.springframework.test.util.ReflectionTestUtils.setField;
45 public class TestCbamRestApiProvider extends TestBase {
47 private Environment environment;
49 private CbamTokenProvider cbamTokenProvider;
50 private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
51 private Set<NodeInfo> nodes = new HashSet<>();
53 private CbamRestApiProvider cbamRestApiProvider;
57 microServiceInfo.setNodes(nodes);
58 CbamRestApiProvider real = new CbamRestApiProvider(driverProperties, cbamTokenProvider, vnfmInfoProvider);
59 setField(real, "trustedCertificates", "mytrustedCertificates");
60 setField(real, "skipCertificateVerification", true);
61 cbamRestApiProvider = spy(real);
62 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
63 when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, Long.valueOf(10 * 60 * 1000))).thenReturn(10 * 60 * 1000L);
67 * test CBAM LCM API retrieval without SSL verification
70 public void testCbamLcmApi() throws Exception {
71 VnfmInfo expectedVnfmInfo = new VnfmInfo();
72 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
73 expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
74 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
76 VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID);
78 ApiClient apiClient = cbamLcmApi.getApiClient();
79 assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
80 assertNull(apiClient.getSslCaCert());
81 assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
82 assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
83 assertTrue(!cbamLcmApi.getApiClient().isVerifyingSsl());
87 * test CBAM LCM API retrieval with SSL verification
90 public void testCbamLcmApiWithSslVerfy() throws Exception {
91 VnfmInfo expectedVnfmInfo = new VnfmInfo();
92 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
93 expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
94 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
95 setField(cbamRestApiProvider, "skipCertificateVerification", false);
96 setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
98 VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID);
100 ApiClient apiClient = cbamLcmApi.getApiClient();
101 assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
102 assertNotNull(apiClient.getSslCaCert());
103 assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
104 assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
105 assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl());
109 * test CBAM Catalog API retrieval without SSL verification
112 public void testCbamCatalogApi() throws Exception {
113 VnfmInfo expectedVnfmInfo = new VnfmInfo();
114 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
115 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
116 when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path");
118 DefaultApi cbamCatalogApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID);
120 com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamCatalogApi.getApiClient();
121 assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
122 assertNull(apiClient.getSslCaCert());
123 assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
124 assertEquals(2, cbamCatalogApi.getApiClient().getAuthentications().size());
125 assertTrue(!cbamCatalogApi.getApiClient().isVerifyingSsl());
129 * test CBAM Catalog API retrieval with SSL verification
132 public void testCbamCatalogApiWithSslVerfy() throws Exception {
133 VnfmInfo expectedVnfmInfo = new VnfmInfo();
134 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
135 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
136 when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path");
137 setField(cbamRestApiProvider, "skipCertificateVerification", false);
138 setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
140 DefaultApi cbamLcmApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID);
142 com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamLcmApi.getApiClient();
143 assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
144 assertNotNull(apiClient.getSslCaCert());
145 assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
146 assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
147 assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl());
151 * test CBAM Lcn API retrieval without SSL verification
154 public void testCbamLcnApi() throws Exception {
155 VnfmInfo expectedVnfmInfo = new VnfmInfo();
156 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
157 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
158 when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path");
160 SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID);
162 com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient();
163 assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
164 assertNull(apiClient.getSslCaCert());
165 assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
166 assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
167 assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl());
171 * test CBAM Lcn API retrieval with SSL verification
174 public void testCbamLcnApiWithSslVerfy() throws Exception {
175 VnfmInfo expectedVnfmInfo = new VnfmInfo();
176 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
177 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
178 when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path");
179 setField(cbamRestApiProvider, "skipCertificateVerification", false);
180 setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
182 SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID);
184 com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient();
185 assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
186 assertNotNull(apiClient.getSslCaCert());
187 assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
188 assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
189 assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl());
193 * test CBAM operation exeution API retrieval without SSL verification
196 public void testCbamOpexApi() throws Exception {
197 VnfmInfo expectedVnfmInfo = new VnfmInfo();
198 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
199 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
200 when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
201 expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
203 OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID);
205 ApiClient apiClient = cbamLcnApi.getApiClient();
206 assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
207 assertNull(apiClient.getSslCaCert());
208 assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
209 assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
210 assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl());
214 * test CBAM operation execution API retrieval with SSL verification
217 public void testCbamOpexApiWithSslVerfy() throws Exception {
218 when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
219 setField(cbamRestApiProvider, "skipCertificateVerification", false);
220 setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
221 VnfmInfo expectedVnfmInfo = new VnfmInfo();
222 when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
223 expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
224 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
226 OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID);
228 ApiClient apiClient = cbamLcnApi.getApiClient();
229 assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
230 assertNotNull(apiClient.getSslCaCert());
231 assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
232 assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
233 assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl());