From 6c32cbc7343bd56f62007451071c4b26c875382f Mon Sep 17 00:00:00 2001 From: vasraz Date: Fri, 26 Nov 2021 14:40:29 +0000 Subject: Add VNFD to NSD when using S3 storage Change-Id: I8eb810fadfe54b335647e71e21115514963b4be7 Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3792 --- .../openecomp/core/utilities/file/FileUtils.java | 28 ++++++++++++---------- .../core/utilities/file/FileUtilsTest.java | 23 ++++++++---------- 2 files changed, 26 insertions(+), 25 deletions(-) (limited to 'openecomp-be/lib/openecomp-core-lib') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java index 60ff1c172a..d6df24e4c2 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java @@ -30,12 +30,12 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Function; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.core.utilities.json.JsonUtil; -import org.openecomp.sdc.common.zip.ZipUtils; -import org.openecomp.sdc.common.zip.exception.ZipException; /** * The type File utils. @@ -206,20 +206,24 @@ public class FileUtils { /** * Gets file content map from zip. * - * @param zipData the zip data + * @param inputStream the zip data * @return the file content map from zip - * @throws ZipException when an error occurs while extracting zip files + * @throws IOException when an error occurs while extracting zip files */ - public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws ZipException { - final Map zipFileAndByteMap = ZipUtils.readZip(zipData, true); - final FileContentHandler fileContentHandler = new FileContentHandler(); - zipFileAndByteMap.forEach((path, bytes) -> { - if (bytes == null) { - fileContentHandler.addFolder(path); + public static FileContentHandler getFileContentMapFromZip(final InputStream inputStream) throws IOException { + + final var zipInputStream = new ZipInputStream(inputStream); + ZipEntry zipEntry; + final var fileContentHandler = new FileContentHandler(); + while ((zipEntry = zipInputStream.getNextEntry()) != null) { + final var entryName = zipEntry.getName(); + if (zipEntry.isDirectory()) { + fileContentHandler.addFolder(entryName); } else { - fileContentHandler.addFile(path, bytes); + fileContentHandler.addFile(entryName, zipInputStream.readAllBytes()); } - }); + + } return fileContentHandler; } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java index facfe57622..05ec5a478d 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java @@ -35,7 +35,6 @@ import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.apache.commons.io.IOUtils; import org.junit.Assert; import org.junit.Test; import org.openecomp.sdc.common.zip.exception.ZipException; @@ -47,7 +46,7 @@ import org.openecomp.sdc.common.zip.exception.ZipException; public class FileUtilsTest { private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName() - .replace('.', '/') + "/test-resource.txt"; + .replace('.', '/') + "/test-resource.txt"; private static final Function TEST_FUNCTION = (s) -> { @@ -82,13 +81,11 @@ public class FileUtilsTest { public void testWriteFilesFromFileContentHandler() throws IOException, ZipException { final Path tempDirectory = Files.createTempDirectory("CSAR_" + System.currentTimeMillis()); try { - byte[] uploadedFileData = - IOUtils.toByteArray(FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE.csar")); - final FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData); + final var contentMap = FileUtils.getFileContentMapFromZip(FileUtilsTest.class.getResourceAsStream("resource-Spgw-csar-ZTE.csar")); final Map filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap, tempDirectory); assertThat("The file map should not be empty", filePaths, is(not(anEmptyMap()))); - assertThat("The file map should have size 20", filePaths, is(aMapWithSize(20))); + assertThat("The file map should have size 20", filePaths, is(aMapWithSize(18))); for (final Map.Entry fileEntry : filePaths.entrySet()) { final File f = new File(fileEntry.getValue()); assertThat(String.format("The file '%s' is expected to", f.getAbsolutePath()), f.exists(), is(true)); @@ -151,7 +148,7 @@ public class FileUtilsTest { @Test public void testConvertToBytes() { byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()), - FileUtils.FileExtension.YAML); + FileUtils.FileExtension.YAML); Assert.assertNotNull(bytesArray); } @@ -159,7 +156,7 @@ public class FileUtilsTest { @Test public void testConvertToBytesNotYaml() { byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()), - FileUtils.FileExtension.JSON); + FileUtils.FileExtension.JSON); Assert.assertNotNull(bytesArray); } @@ -172,7 +169,7 @@ public class FileUtilsTest { @Test public void testConvertToInputStream() { InputStream inputStream = FileUtils.convertToInputStream(Stream.of("Json", "Util", "Test") - .collect(Collectors.toList()), FileUtils.FileExtension.YAML); + .collect(Collectors.toList()), FileUtils.FileExtension.YAML); Assert.assertNotNull(inputStream); } @@ -183,13 +180,13 @@ public class FileUtilsTest { } @Test - public void testLoadFileToInputStream() throws IOException{ + public void testLoadFileToInputStream() throws IOException { int i; StringBuilder builder = new StringBuilder(20); InputStream inputStream = FileUtils.loadFileToInputStream( - "org/openecomp/core/utilities/file/testFileUtils.txt"); - while((i = inputStream.read())!=-1) { - builder.append((char)i); + "org/openecomp/core/utilities/file/testFileUtils.txt"); + while ((i = inputStream.read()) != -1) { + builder.append((char) i); } Assert.assertNotNull(inputStream); -- cgit 1.2.3-korg