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.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
18 import com.nokia.cbam.catalog.v1.model.CatalogAdapterVnfpackage;
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import okhttp3.Headers;
25 import okhttp3.RequestBody;
26 import okhttp3.ResponseBody;
27 import okhttp3.internal.http.RealResponseBody;
29 import okio.BufferedSource;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IPackageProvider;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
40 import static junit.framework.TestCase.*;
41 import static org.junit.Assert.assertArrayEquals;
42 import static org.mockito.Mockito.*;
43 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
44 import static org.springframework.test.util.ReflectionTestUtils.setField;
46 public class TestCbamCatalogManager extends TestBase {
48 private static final String CSAR_ID = "csarId";
49 private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID";
50 private CatalogManager cbamCatalogManager;
52 private IPackageProvider packageProvider;
54 private List<CatalogAdapterVnfpackage> existingVnfPackages = new ArrayList<>();
55 private ArgumentCaptor<RequestBody> uploadedFile = ArgumentCaptor.forClass(RequestBody.class);
58 public void initMocks() throws Exception {
59 setField(CatalogManager.class, "logger", logger);
60 when(cbamCatalogApi.list()).thenReturn(buildObservable(existingVnfPackages));
61 cbamCatalogManager = new CatalogManager(cbamRestApiProvider, packageProvider);
65 * the package is transferred from source to CBAM catalog
68 public void testPackageTransfer() throws Exception {
69 CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
70 existingPackage.setVnfdId("unknownId");
71 existingVnfPackages.add(existingPackage);
72 CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
73 createdPackage.setVnfdId(CBAM_VNFD_ID);
74 when(cbamCatalogApi.create(uploadedFile.capture())).thenReturn(buildObservable(createdPackage));
75 byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
76 when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
77 when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
79 CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
81 byte[] a2 = getContent(uploadedFile.getValue());
82 assertArrayEquals(getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray(), a2);
83 assertEquals(createdPackage, cbamPackage);
87 * the package is transfer fails, but the package has been uploaded (possibly by other thread / work flow)
88 * the transfer succeeds
91 public void testPackageTransferConcurrency() throws Exception {
92 CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
93 existingPackage.setVnfdId("unknownId");
94 existingVnfPackages.add(existingPackage);
95 CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
96 createdPackage.setVnfdId(CBAM_VNFD_ID);
97 byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
98 when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
99 when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
100 RuntimeException can_not_upload_package = new RuntimeException("Can not upload package");
101 when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() {
103 public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
104 //this is done by an other thread
105 existingVnfPackages.add(createdPackage);
106 when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(buildObservable(createdPackage));
107 throw can_not_upload_package;
111 CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
113 //the correct portion of the package is extracted and uploaded to CBAM
114 byte[] expectedContentToUpload = getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray();
115 assertTrue(Arrays.equals(expectedContentToUpload, getContent(uploadedFile.getValue())));
116 assertEquals(createdPackage, cbamPackage);
117 verify(logger).debug("Probably concurrent package uploads", can_not_upload_package);
121 * If the package already exists in CBAM catalog it is not re-uploaded
124 public void testIdempotentPackageUpload() throws Exception {
125 CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
126 createdPackage.setVnfdId(CBAM_VNFD_ID);
127 when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() {
129 public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
130 return createdPackage;
133 when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
134 CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
135 existingPackage.setVnfdId(CBAM_VNFD_ID);
136 existingVnfPackages.add(existingPackage);
137 when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(buildObservable(existingPackage));
139 CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
141 verify(cbamCatalogApi, never()).create(Mockito.any());
142 assertEquals(existingPackage, cbamPackage);
143 verify(packageProvider, never()).getPackage(CSAR_ID);
147 * failure to list package in CBAM results in error
150 public void testFailureToListVnfPackagesInCbam() throws Exception {
151 when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
152 RuntimeException expectedException = new RuntimeException();
153 when(cbamCatalogApi.list()).thenThrow(expectedException);
156 cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
158 } catch (Exception e) {
159 verify(logger).error("Unable to determine if the VNF package has been replicated in CBAM", expectedException);
160 assertEquals(expectedException, e.getCause());
165 * failure to query package from CBAM results in error
168 public void testFailureToQueryVnfPackagesFromCbam() throws Exception {
169 when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
170 CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
171 existingPackage.setVnfdId(CBAM_VNFD_ID);
172 existingVnfPackages.add(existingPackage);
173 RuntimeException expectedException = new RuntimeException();
174 when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenThrow(expectedException);
177 cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
179 } catch (Exception e) {
180 verify(logger).error("Unable to query VNF package with CBAM_VNFD_ID from CBAM", expectedException);
181 assertEquals(expectedException, e.getCause());
186 * failure to create package in CBAM results in error
189 public void testFailureToCreatePackageInCbam() throws Exception {
190 CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
191 existingPackage.setVnfdId("unknownId");
192 existingVnfPackages.add(existingPackage);
193 when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
194 byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
195 when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
196 RuntimeException expectedException = new RuntimeException();
197 when(cbamCatalogApi.create(Mockito.any())).thenThrow(expectedException);
199 cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
201 } catch (Exception e) {
202 verify(logger).error("Unable to create VNF with csarId CSAR identifier in package in CBAM", expectedException);
203 assertEquals(expectedException, e.getCause());
208 * the VNFD is extracted from zip
211 public void testExtractVnfdFromPackage() throws Exception {
212 when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(buildObservable(buildResponse(TestUtil.loadFile("unittests/cbam.package.zip"))));
214 String content = cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
216 assertEquals("dummy vnfd\n", content);
220 * if VNFD the Tosca meta can not be extracted sensible error is returned
223 public void testEmptyCbamPackage() throws Exception {
224 when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(buildObservable(buildResponse(TestUtil.loadFile("unittests/empty.zip"))));
227 cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
229 } catch (RuntimeException e) {
230 verify(logger).error("Unable to get package with (CBAM_VNFD_ID)", e.getCause());
231 assertEquals("Unable to find the TOSCA-Metadata/TOSCA.meta in archive found: []", e.getCause().getMessage());
236 * if VNFD can not be extracted sensible error is returned
239 public void testMissingVnfdCbamPackage() throws Exception {
240 byte[] bytes = TestUtil.loadFile("unittests/missing.vnfd.zip");
241 when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(buildObservable(buildResponse(bytes)));
244 cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
246 } catch (RuntimeException e) {
247 verify(logger).error("Unable to get package with (" + CBAM_VNFD_ID + ")", e.getCause());
248 assertTrue("Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/, TOSCA-Metadata/TOSCA.meta]".equals(e.getCause().getMessage())
249 || "Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/TOSCA.meta, TOSCA-Metadata/]".equals(e.getCause().getMessage())
254 private ResponseBody buildResponse(byte[] content) throws IOException {
255 Headers headers = new Headers.Builder().build();
256 Buffer buffer = new Buffer();
257 buffer.write(content);
258 BufferedSource response = buffer;
259 return new RealResponseBody(headers, response);