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;
 
  39 import retrofit2.Call;
 
  41 import static junit.framework.TestCase.*;
 
  42 import static org.junit.Assert.assertArrayEquals;
 
  43 import static org.mockito.Mockito.*;
 
  44 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
 
  45 import static org.springframework.test.util.ReflectionTestUtils.setField;
 
  47 public class TestCbamCatalogManager extends TestBase {
 
  49     private static final String CSAR_ID = "csarId";
 
  50     private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID";
 
  51     private CatalogManager cbamCatalogManager;
 
  53     private IPackageProvider packageProvider;
 
  55     private List<CatalogAdapterVnfpackage> existingVnfPackages = new ArrayList<>();
 
  56     private ArgumentCaptor<RequestBody> uploadedFile = ArgumentCaptor.forClass(RequestBody.class);
 
  59     public void initMocks() throws Exception {
 
  60         setField(CatalogManager.class, "logger", logger);
 
  61         Call<List<CatalogAdapterVnfpackage>> value = buildCall(existingVnfPackages);
 
  62         when(cbamCatalogApi.list()).thenReturn(value);
 
  63         cbamCatalogManager = new CatalogManager(cbamRestApiProvider, packageProvider);
 
  67      * the package is transferred from source to CBAM catalog
 
  70     public void testPackageTransfer() throws Exception {
 
  71         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
 
  72         existingPackage.setVnfdId("unknownId");
 
  73         existingVnfPackages.add(existingPackage);
 
  74         CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
 
  75         createdPackage.setVnfdId(CBAM_VNFD_ID);
 
  76         Call<CatalogAdapterVnfpackage> catalogAdapterVnfpackageCall = buildCall(createdPackage);
 
  77         when(cbamCatalogApi.create(uploadedFile.capture())).thenReturn(catalogAdapterVnfpackageCall);
 
  78         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
 
  79         when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
 
  80         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
 
  82         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
 
  84         byte[] a2 = getContent(uploadedFile.getValue());
 
  85         assertArrayEquals(getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray(), a2);
 
  86         assertEquals(createdPackage, cbamPackage);
 
  90      * the package is transfer fails, but the package has been uploaded (possibly by other thread / work flow)
 
  91      * the transfer succeeds
 
  94     public void testPackageTransferConcurrency() throws Exception {
 
  95         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
 
  96         existingPackage.setVnfdId("unknownId");
 
  97         existingVnfPackages.add(existingPackage);
 
  98         CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
 
  99         createdPackage.setVnfdId(CBAM_VNFD_ID);
 
 100         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
 
 101         when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
 
 102         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
 
 103         RuntimeException can_not_upload_package = new RuntimeException("Can not upload package");
 
 104         when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() {
 
 106             public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
 
 107                 //this is done by an other thread
 
 108                 existingVnfPackages.add(createdPackage);
 
 109                 Call<CatalogAdapterVnfpackage> catalogAdapterVnfpackageCall = buildCall(createdPackage);
 
 110                 when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(catalogAdapterVnfpackageCall);
 
 111                 throw can_not_upload_package;
 
 115         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
 
 117         //the correct portion of the package is extracted and uploaded to CBAM
 
 118         byte[] expectedContentToUpload = getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray();
 
 119         assertTrue(Arrays.equals(expectedContentToUpload, getContent(uploadedFile.getValue())));
 
 120         assertEquals(createdPackage, cbamPackage);
 
 121         verify(logger).debug("Probably concurrent package uploads", can_not_upload_package);
 
 125      * If the package already exists in CBAM catalog it is not re-uploaded
 
 128     public void testIdempotentPackageUpload() throws Exception {
 
 129         CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
 
 130         createdPackage.setVnfdId(CBAM_VNFD_ID);
 
 131         when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() {
 
 133             public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
 
 134                 return createdPackage;
 
 137         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
 
 138         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
 
 139         existingPackage.setVnfdId(CBAM_VNFD_ID);
 
 140         existingVnfPackages.add(existingPackage);
 
 141         Call<CatalogAdapterVnfpackage> catalogAdapterVnfpackageCall = buildCall(existingPackage);
 
 142         when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(catalogAdapterVnfpackageCall);
 
 144         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
 
 146         verify(cbamCatalogApi, never()).create(Mockito.any());
 
 147         assertEquals(existingPackage, cbamPackage);
 
 148         verify(packageProvider, never()).getPackage(CSAR_ID);
 
 152      * failure to list package in CBAM results in error
 
 155     public void testFailureToListVnfPackagesInCbam() throws Exception {
 
 156         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
 
 157         RuntimeException expectedException = new RuntimeException();
 
 158         when(cbamCatalogApi.list()).thenThrow(expectedException);
 
 161             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
 
 163         } catch (Exception e) {
 
 164             verify(logger).error("Unable to determine if the VNF package has been replicated in CBAM", expectedException);
 
 165             assertEquals(expectedException, e.getCause());
 
 170      * failure to query package from CBAM results in error
 
 173     public void testFailureToQueryVnfPackagesFromCbam() throws Exception {
 
 174         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
 
 175         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
 
 176         existingPackage.setVnfdId(CBAM_VNFD_ID);
 
 177         existingVnfPackages.add(existingPackage);
 
 178         RuntimeException expectedException = new RuntimeException();
 
 179         when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenThrow(expectedException);
 
 182             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
 
 184         } catch (Exception e) {
 
 185             verify(logger).error("Unable to query VNF package with CBAM_VNFD_ID from CBAM", expectedException);
 
 186             assertEquals(expectedException, e.getCause());
 
 191      * failure to create package in CBAM results in error
 
 194     public void testFailureToCreatePackageInCbam() throws Exception {
 
 195         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
 
 196         existingPackage.setVnfdId("unknownId");
 
 197         existingVnfPackages.add(existingPackage);
 
 198         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
 
 199         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
 
 200         when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
 
 201         RuntimeException expectedException = new RuntimeException();
 
 202         when(cbamCatalogApi.create(Mockito.any())).thenThrow(expectedException);
 
 204             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
 
 206         } catch (Exception e) {
 
 207             verify(logger).error("Unable to create VNF with csarId CSAR identifier in package in CBAM", expectedException);
 
 208             assertEquals(expectedException, e.getCause());
 
 213      * the VNFD is extracted from zip
 
 216     public void testExtractVnfdFromPackage() throws Exception {
 
 217         Call<ResponseBody> responseBodyCall = buildCall(buildResponse(TestUtil.loadFile("unittests/cbam.package.zip")));
 
 218         when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(responseBodyCall);
 
 220         String content = cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
 
 222         assertEquals("dummy vnfd\n", content);
 
 226      * if VNFD the Tosca meta can not be extracted sensible error is returned
 
 229     public void testEmptyCbamPackage() throws Exception {
 
 230         Call<ResponseBody> responseBodyCall = buildCall(buildResponse(TestUtil.loadFile("unittests/empty.zip")));
 
 231         when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(responseBodyCall);
 
 234             cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
 
 236         } catch (RuntimeException e) {
 
 237             verify(logger).error("Unable to get package with (CBAM_VNFD_ID)", e.getCause());
 
 238             assertEquals("Unable to find the TOSCA-Metadata/TOSCA.meta in archive found: []", e.getCause().getMessage());
 
 243      * if VNFD can not be extracted sensible error is returned
 
 246     public void testMissingVnfdCbamPackage() throws Exception {
 
 247         byte[] bytes = TestUtil.loadFile("unittests/missing.vnfd.zip");
 
 248         Call<ResponseBody> response = buildCall(buildResponse(bytes));
 
 249         when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(response);
 
 252             cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
 
 254         } catch (RuntimeException e) {
 
 255             verify(logger).error("Unable to get package with (" + CBAM_VNFD_ID + ")", e.getCause());
 
 256             assertTrue("Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/, TOSCA-Metadata/TOSCA.meta]".equals(e.getCause().getMessage())
 
 257                     || "Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/TOSCA.meta, TOSCA-Metadata/]".equals(e.getCause().getMessage())
 
 262     private ResponseBody buildResponse(byte[] content) throws IOException {
 
 263         Headers headers = new Headers.Builder().build();
 
 264         Buffer buffer = new Buffer();
 
 265         buffer.write(content);
 
 266         BufferedSource response = buffer;
 
 267         return new RealResponseBody(headers, response);