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;
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.*;
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);
}
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);
}
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;
}
});
//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);
}
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
@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 {
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 {
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);
*/
@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
*/
@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);
*/
@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);
);
}
}
+
+ /**
+ * 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);
+ }
}