aboutsummaryrefslogtreecommitdiffstats
path: root/common/onap-common-configuration-management/onap-configuration-management-core/src
diff options
context:
space:
mode:
authorDmitry Puzikov <d.puzikov2@partner.samsung.com>2019-11-13 12:51:41 +0100
committerOfir Sonsino <ofir.sonsino@intl.att.com>2019-12-01 15:25:43 +0000
commit4a9ddb2618b8dde8820c08d934c708216b0ebca9 (patch)
tree1dba2999d87ef1717b9f8211b137188e1cefb08a /common/onap-common-configuration-management/onap-configuration-management-core/src
parent5b1f9a16f823c31d26f9306ea1ee540120f6780e (diff)
Fixing sonar issue
Getting rid of nested ifs-fors Change-Id: I71ef686683b7070614c27aabbf6cd058b5d35ffd Issue-ID: SDC-2654 Signed-off-by: Dmitry Puzikov <d.puzikov2@partner.samsung.com>
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src')
-rw-r--r--common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java37
-rw-r--r--common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/ConfigurationUtilsTest.java53
-rw-r--r--common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/util/TestUtil.java58
3 files changed, 137 insertions, 11 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java
index df382143c6..17e9ef048f 100644
--- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java
+++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java
@@ -44,6 +44,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
+import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
@@ -51,7 +52,10 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
+import java.nio.file.FileVisitResult;
import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
@@ -113,20 +117,31 @@ public class ConfigurationUtils {
}
public static Collection<File> getAllFiles(File file, boolean recursive, boolean onlyDirectory) {
-
ArrayList<File> collection = new ArrayList<>();
- if (file.isDirectory() && file.exists()) {
- File[] files = file.listFiles();
- for (File innerFile : Objects.requireNonNull(files)) {
- if (innerFile.isFile() && !onlyDirectory) {
- collection.add(innerFile);
- } else if (innerFile.isDirectory()) {
- collection.add(innerFile);
- if (recursive) {
- collection.addAll(getAllFiles(innerFile, true, onlyDirectory));
+ Path rootPath = file.toPath();
+ try {
+ Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
+ super.preVisitDirectory(dir,attrs);
+ if (rootPath.equals(dir)) {
+ return FileVisitResult.CONTINUE;
}
+ collection.add(dir.toFile());
+ return recursive? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE;
}
- }
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ super.visitFile(file, attrs);
+ if (!onlyDirectory) {
+ collection.add(file.toFile());
+ }
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ } catch (IOException e) {
+ LOGGER.error("Failed to walk through directories starting from: {}.", file.toString(), e);
}
return collection;
}
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/ConfigurationUtilsTest.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/ConfigurationUtilsTest.java
index d0b0a12297..dab8946883 100644
--- a/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/ConfigurationUtilsTest.java
+++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/ConfigurationUtilsTest.java
@@ -26,8 +26,12 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
+import java.io.File;
+import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
@@ -54,11 +58,13 @@ import org.apache.commons.configuration2.CompositeConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.junit.Test;
+import org.onap.config.util.TestUtil;
import org.onap.config.api.Hint;
import org.onap.config.impl.ConfigurationRepository;
public class ConfigurationUtilsTest {
+ public static final String TMP_DIR_PREFIX = "sdc-testing-";
private static final String TEST_NAME_SPACE = "testNameSpaceOne";
private static final String TEST_COMPOSITE_NAMESPACE = "testCOmpositeConfig";
@@ -134,6 +140,53 @@ public class ConfigurationUtilsTest {
}
@Test
+ public void testGetAllFilesRecursiveIncludeAll() throws IOException {
+ Path tmpRoot = TestUtil.createTestDirsStructure(TMP_DIR_PREFIX);
+ Collection<File> allFiles = ConfigurationUtils.getAllFiles(tmpRoot.toFile(), true, false);
+ assertEquals(7, allFiles.size());
+ TestUtil.deleteTestDirsStrucuture(tmpRoot);
+ }
+
+ @Test
+ public void testGetAllFilesRecursiveIncludeDirsOnly() throws IOException {
+ Path tmpRoot = TestUtil.createTestDirsStructure(TMP_DIR_PREFIX);
+ Collection<File> allFiles = ConfigurationUtils.getAllFiles(tmpRoot.toFile(), true, true);
+ assertEquals(3, allFiles.size());
+ TestUtil.deleteTestDirsStrucuture(tmpRoot);
+ }
+
+ @Test
+ public void testGetAllFilesNonRecursiveIncludeAll() throws IOException {
+ Path tmpRoot = TestUtil.createTestDirsStructure(TMP_DIR_PREFIX);
+ Collection<File> allFiles = ConfigurationUtils.getAllFiles(tmpRoot.toFile(), false, false);
+ assertEquals(2, allFiles.size());
+ TestUtil.deleteTestDirsStrucuture(tmpRoot);
+ }
+
+ @Test
+ public void testGetAllFilesNonRecursiveIncludeDirsOnly() throws IOException {
+ Path tmpRoot = TestUtil.createTestDirsStructure(TMP_DIR_PREFIX);
+ Collection<File> allFiles = ConfigurationUtils.getAllFiles(tmpRoot.toFile(), false, true);
+ assertEquals(1, allFiles.size());
+ TestUtil.deleteTestDirsStrucuture(tmpRoot);
+ }
+
+ @Test
+ public void testGetAllFilesEmptyDir() throws IOException {
+ Path tmpRoot = TestUtil.createEmptyTmpDir(TMP_DIR_PREFIX);
+ Collection<File> allFiles = ConfigurationUtils.getAllFiles(tmpRoot.toFile(), true, true);
+ assertEquals(0, allFiles.size());
+ TestUtil.deleteTestDirsStrucuture(tmpRoot);
+ }
+
+ @Test
+ public void testGetAllFilesNonExistentDir() throws IOException {
+ Path nonExistentDir = Paths.get("/tmp/nonexistentdir");
+ Collection<File> allFiles = ConfigurationUtils.getAllFiles(nonExistentDir.toFile(), false, true);
+ assertEquals(0, allFiles.size());
+ }
+
+ @Test
public void testGetConfigPropertyBaseConfig() throws Exception {
ConfigurationRepository repo = populateTestBaseConfig();
Configuration config = repo.getConfigurationFor(Constants.DEFAULT_TENANT, TEST_NAME_SPACE);
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/util/TestUtil.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/util/TestUtil.java
index 87ff4ba394..28611d44c8 100644
--- a/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/util/TestUtil.java
+++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/util/TestUtil.java
@@ -19,6 +19,12 @@ package org.onap.config.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
@@ -104,4 +110,56 @@ public class TestUtil {
String value = System.getenv(name);
return value == null ? "" : value;
}
+
+ /**
+ * Creates temporary directories structure with files inside every directory
+ *
+ * @param tmpDirPrefix
+ * @return
+ * @throws IOException
+ */
+ public static Path createTestDirsStructure(String tmpDirPrefix) throws IOException {
+ Path tmpPath = Files.createTempDirectory(tmpDirPrefix);
+ Path dir0 = Paths.get(tmpPath.toString(), "dir0", "dir1", "dir2");
+ Files.createDirectories(dir0);
+ Path[] files= {
+ Paths.get(tmpPath.toString(), "file001"),
+ Paths.get(tmpPath.toString(), "dir0", "file002"),
+ Paths.get(tmpPath.toString(), "dir0", "dir1", "file003"),
+ Paths.get(tmpPath.toString(), "dir0", "dir1", "dir2", "file004"),
+ };
+ for (Path file : files ) {
+ Files.createFile(file);
+ }
+ return tmpPath;
+ }
+
+ public static Path createEmptyTmpDir(String prefix) throws IOException {
+ return Files.createTempDirectory(prefix);
+ }
+
+ /**
+ * Delete all tmp directories and files created for testing
+ *
+ * @param rootPath
+ */
+ public static void deleteTestDirsStrucuture(Path rootPath) {
+ try {
+ Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+ Files.delete(dir);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ Files.delete(file);
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}