diff options
author | Dmitry Puzikov <d.puzikov2@partner.samsung.com> | 2019-11-13 12:51:41 +0100 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2019-12-01 15:25:43 +0000 |
commit | 4a9ddb2618b8dde8820c08d934c708216b0ebca9 (patch) | |
tree | 1dba2999d87ef1717b9f8211b137188e1cefb08a /common/onap-common-configuration-management/onap-configuration-management-core/src/test | |
parent | 5b1f9a16f823c31d26f9306ea1ee540120f6780e (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/test')
2 files changed, 111 insertions, 0 deletions
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(); + } + } } |