diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib')
3 files changed, 103 insertions, 1 deletions
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 e70ad923a2..f510febbcf 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 @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,13 +22,20 @@ import org.openecomp.core.utilities.json.JsonUtil; import org.openecomp.sdc.tosca.services.YamlUtil; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; import java.util.Enumeration; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.function.Function; import java.util.zip.ZipEntry; @@ -277,4 +284,44 @@ public class FileUtils { } + /** + * Write files and folders map to disk in the given path + * + * @param fileContentHandler the file content handler + * @param dir the dir + * @return a map containing file names and their absolute paths + * @throws IOException the io exception + */ + public static Map<String, String> writeFilesFromFileContentHandler(FileContentHandler + fileContentHandler, + Path dir) + throws IOException { + + File file; + File dirFile = dir.toFile(); + Map<String, String> filePaths = new HashMap<>(); + for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) { + file = new File(dirFile, fileEntry.getKey()); + file.getParentFile().mkdirs(); + filePaths.put(fileEntry.getKey(), file.getAbsolutePath()); + try (FileOutputStream fop = new FileOutputStream(file.getAbsolutePath());) { + fop.write(fileEntry.getValue()); + fop.flush(); + } + } + + return filePaths; + } + + /** + * Verify whether the provided extension is valid Yaml/Yml extension or not. + * + * @param fileExtension the file extension + * @return the boolean + */ + public static boolean isValidYamlExtension(String fileExtension){ + return fileExtension.equalsIgnoreCase(FileExtension.YML.getDisplayName()) || + fileExtension.equalsIgnoreCase(FileExtension.YAML.getDisplayName()); + } + } 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 e32aa39904..3c00d40a6b 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 @@ -1,12 +1,36 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.openecomp.core.utilities.file; +import org.apache.commons.io.IOUtils; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.testng.annotations.Test; +import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; import java.util.function.Function; import static org.testng.Assert.assertTrue; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertEquals; /** * @author EVITALIY @@ -45,4 +69,35 @@ public class FileUtilsTest { public void testReadViaInputStreamNotFound() throws Exception { FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION); } + + @Test + public void testWriteFilesFromFileContentHandler() throws IOException { + Path dir = Files.createTempDirectory("CSAR_" + System.currentTimeMillis()); + try { + byte[] uploadedFileData = IOUtils.toByteArray( + FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE" + + ".csar")); + FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData); + Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap, + dir); + + assertFalse(filePaths.isEmpty()); + assertEquals(filePaths.size(), 18); + for (Map.Entry<String, String> fileEntry : filePaths.entrySet()) { + File f = new File(fileEntry.getValue()); + assertTrue(f.exists()); + } + } + finally { + org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile()); + } + } + + @Test + public void testIsValidYamlExtension() throws IOException { + assertTrue(FileUtils.isValidYamlExtension("yaml")); + assertTrue(FileUtils.isValidYamlExtension("yml")); + assertFalse(FileUtils.isValidYamlExtension("yml1")); + assertFalse(FileUtils.isValidYamlExtension("zip")); + } }
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/resource-Spgw-csar-ZTE.csar b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/resource-Spgw-csar-ZTE.csar Binary files differnew file mode 100644 index 0000000000..58c3ddd074 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/resource-Spgw-csar-ZTE.csar |