Use the ETSI config from package
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / TestCbamCatalogManager.java
index f38758f..28e4709 100644 (file)
 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
 
 import com.nokia.cbam.catalog.v1.model.CatalogAdapterVnfpackage;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import okhttp3.Headers;
+import okhttp3.RequestBody;
+import okhttp3.ResponseBody;
+import okhttp3.internal.http.RealResponseBody;
+import okio.Buffer;
+import okio.BufferedSource;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
@@ -26,15 +37,6 @@ import org.mockito.stubbing.Answer;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IPackageProvider;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
 
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.UUID;
-
 import static junit.framework.TestCase.*;
 import static org.junit.Assert.assertArrayEquals;
 import static org.mockito.Mockito.*;
@@ -50,12 +52,12 @@ public class TestCbamCatalogManager extends TestBase {
     private IPackageProvider packageProvider;
 
     private List<CatalogAdapterVnfpackage> existingVnfPackages = new ArrayList<>();
-    private ArgumentCaptor<File> uploadedFile = ArgumentCaptor.forClass(File.class);
+    private ArgumentCaptor<RequestBody> uploadedFile = ArgumentCaptor.forClass(RequestBody.class);
 
     @Before
     public void initMocks() throws Exception {
         setField(CatalogManager.class, "logger", logger);
-        when(cbamCatalogApi.list()).thenReturn(existingVnfPackages);
+        when(cbamCatalogApi.list()).thenReturn(buildObservable(existingVnfPackages));
         cbamCatalogManager = new CatalogManager(cbamRestApiProvider, packageProvider);
     }
 
@@ -69,19 +71,14 @@ public class TestCbamCatalogManager extends TestBase {
         existingVnfPackages.add(existingPackage);
         CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage();
         createdPackage.setVnfdId(CBAM_VNFD_ID);
-        when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() {
-            @Override
-            public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
-                return createdPackage;
-            }
-        });
+        when(cbamCatalogApi.create(uploadedFile.capture())).thenReturn(buildObservable(createdPackage));
         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
         when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
         //when
         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
         //verify
-        byte[] a2 = Files.readAllBytes(uploadedFile.getValue().toPath());
+        byte[] a2 = getContent(uploadedFile.getValue());
         assertArrayEquals(getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray(), a2);
         assertEquals(createdPackage, cbamPackage);
     }
@@ -106,7 +103,7 @@ public class TestCbamCatalogManager extends TestBase {
             public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable {
                 //this is done by an other thread
                 existingVnfPackages.add(createdPackage);
-                when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(createdPackage);
+                when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(buildObservable(createdPackage));
                 throw can_not_upload_package;
             }
         });
@@ -115,7 +112,7 @@ public class TestCbamCatalogManager extends TestBase {
         //verify
         //the correct portion of the package is extracted and uploaded to CBAM
         byte[] expectedContentToUpload = getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray();
-        assertTrue(Arrays.equals(expectedContentToUpload, Files.readAllBytes(uploadedFile.getValue().toPath())));
+        assertTrue(Arrays.equals(expectedContentToUpload, getContent(uploadedFile.getValue())));
         assertEquals(createdPackage, cbamPackage);
         verify(logger).debug("Probably concurrent package uploads", can_not_upload_package);
     }
@@ -137,7 +134,7 @@ public class TestCbamCatalogManager extends TestBase {
         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
         existingPackage.setVnfdId(CBAM_VNFD_ID);
         existingVnfPackages.add(existingPackage);
-        when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(existingPackage);
+        when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(buildObservable(existingPackage));
         //when
         CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
         //verify
@@ -152,7 +149,7 @@ public class TestCbamCatalogManager extends TestBase {
     @Test
     public void testFailureToListVnfPackagesInCbam() throws Exception {
         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
-        com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException();
+        RuntimeException expectedException = new RuntimeException();
         when(cbamCatalogApi.list()).thenThrow(expectedException);
         //when
         try {
@@ -173,7 +170,7 @@ public class TestCbamCatalogManager extends TestBase {
         CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage();
         existingPackage.setVnfdId(CBAM_VNFD_ID);
         existingVnfPackages.add(existingPackage);
-        com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException();
+        RuntimeException expectedException = new RuntimeException();
         when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenThrow(expectedException);
         //when
         try {
@@ -196,7 +193,7 @@ public class TestCbamCatalogManager extends TestBase {
         when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID);
         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
         when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
-        com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException();
+        RuntimeException expectedException = new RuntimeException();
         when(cbamCatalogApi.create(Mockito.any())).thenThrow(expectedException);
         try {
             cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID);
@@ -212,9 +209,7 @@ public class TestCbamCatalogManager extends TestBase {
      */
     @Test
     public void testExtractVnfdFromPackage() throws Exception {
-        Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar");
-        Files.write(csar, TestUtil.loadFile("unittests/cbam.package.zip"));
-        when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile());
+        when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(buildObservable(buildResponse(TestUtil.loadFile("unittests/cbam.package.zip"))));
         //when
         String content = cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
         //verify
@@ -226,9 +221,7 @@ public class TestCbamCatalogManager extends TestBase {
      */
     @Test
     public void testEmptyCbamPackage() throws Exception {
-        Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar");
-        Files.write(csar, TestUtil.loadFile("unittests/empty.zip"));
-        when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile());
+        when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(buildObservable(buildResponse(TestUtil.loadFile("unittests/empty.zip"))));
         //when
         try {
             cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
@@ -244,9 +237,8 @@ public class TestCbamCatalogManager extends TestBase {
      */
     @Test
     public void testMissingVnfdCbamPackage() throws Exception {
-        Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar");
-        Files.write(csar, TestUtil.loadFile("unittests/missing.vnfd.zip"));
-        when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile());
+        byte[] bytes = TestUtil.loadFile("unittests/missing.vnfd.zip");
+        when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(buildObservable(buildResponse(bytes)));
         //when
         try {
             cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID);
@@ -258,4 +250,45 @@ public class TestCbamCatalogManager extends TestBase {
             );
         }
     }
+
+    /**
+     * ETSI configuration extraction from the package
+     */
+    @Test
+    public void testEtsiConfigurationDownload() throws Exception{
+        //given
+        byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
+        when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
+        //when
+        String etsiConfiguration = cbamCatalogManager.getEtsiConfiguration(CSAR_ID);
+
+        assertEquals("{ \"a\" : \"b\" }\n", etsiConfiguration);
+    }
+
+    /**
+     * ETSI configuration extraction from the package
+     */
+    @Test
+    public void testEtsiConfigurationMissing() throws Exception{
+        //given
+        byte[] onapPackageContent = TestUtil.loadFile("unittests/missing.vnfd.zip");
+        when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent);
+        //when
+        try {
+            cbamCatalogManager.getEtsiConfiguration(CSAR_ID);
+            fail();
+        }
+        catch (Exception e){
+            assertEquals("Unable to download the ETSI configuration file", e.getMessage());
+            verify(logger).error("Unable to download the ETSI configuration file");
+        }
+    }
+
+    private ResponseBody buildResponse(byte[] content) throws IOException {
+        Headers headers = new Headers.Builder().build();
+        Buffer buffer = new Buffer();
+        buffer.write(content);
+        BufferedSource response = buffer;
+        return new RealResponseBody(headers, response);
+    }
 }