aboutsummaryrefslogtreecommitdiffstats
path: root/utils/src/test/java/org/onap/policy/common/utils/resources
diff options
context:
space:
mode:
Diffstat (limited to 'utils/src/test/java/org/onap/policy/common/utils/resources')
-rw-r--r--utils/src/test/java/org/onap/policy/common/utils/resources/DirectoryUtilsTest.java42
-rw-r--r--utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java109
-rw-r--r--utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java73
3 files changed, 152 insertions, 72 deletions
diff --git a/utils/src/test/java/org/onap/policy/common/utils/resources/DirectoryUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/resources/DirectoryUtilsTest.java
new file mode 100644
index 00000000..c12ef9f8
--- /dev/null
+++ b/utils/src/test/java/org/onap/policy/common/utils/resources/DirectoryUtilsTest.java
@@ -0,0 +1,42 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.utils.resources;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import org.apache.commons.io.FileUtils;
+import org.junit.Test;
+
+public class DirectoryUtilsTest {
+
+ @Test
+ public void testCreateTempDirectory() throws IOException {
+ Path path = DirectoryUtils.createTempDirectory("directoryUtilsTest");
+
+ var file = path.toFile();
+ FileUtils.forceDeleteOnExit(file);
+
+ assertThat(file).canRead().canWrite().isDirectory();
+ assertThat(file.canExecute()).isTrue();
+ }
+}
diff --git a/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java
index 2991009f..c56409ee 100644
--- a/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java
+++ b/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
- * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020-2021, 2023-2024 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
package org.onap.policy.common.utils.resources;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -30,11 +31,9 @@ import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
-import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
-
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -79,12 +78,12 @@ public class ResourceUtilsTest {
}
/**
- * Cleandown resource utils test.
+ * Clean resource utils test.
*/
@After
- public void cleandownResourceUtilsTest() {
- tmpEmptyFile.delete();
- tmpUsedFile.delete();
+ public void cleanDownResourceUtilsTest() {
+ assertTrue(tmpEmptyFile.delete());
+ assertTrue(tmpUsedFile.delete());
}
/**
@@ -184,45 +183,26 @@ public class ResourceUtilsTest {
* Test get resource as stream.
*/
@Test
- public void testGetResourceAsStream() {
- InputStream theStream = ResourceUtils.getResourceAsStream(tmpDir.getAbsolutePath());
- assertNotNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(tmpEmptyFile.getAbsolutePath());
- assertNotNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(tmpUsedFile.getAbsolutePath());
- assertNotNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(jarDirResource);
- assertNotNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(jarFileResource);
- assertNotNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(PATH_DIR_RESOURCE);
- assertNotNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(PATH_FILE_RESOURCE);
- assertNotNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(RESOURCES_PATH + PATH_DIR_RESOURCE);
- assertNotNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(RESOURCES_PATH + PATH_FILE_RESOURCE);
- assertNotNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(NON_EXISTENT_RESOURCE);
- assertNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(INVALID_RESOURCE);
- assertNull(theStream);
-
- theStream = ResourceUtils.getResourceAsStream(null);
- assertNull(theStream);
+ public void testGetResourceAsStream() throws IOException {
+ verifyStream(tmpDir.getAbsolutePath());
+ verifyStream(tmpEmptyFile.getAbsolutePath());
+ verifyStream(tmpUsedFile.getAbsolutePath());
+ verifyStream(jarDirResource);
+ verifyStream(jarFileResource);
+ verifyStream(PATH_DIR_RESOURCE);
+ verifyStream(PATH_FILE_RESOURCE);
+ verifyStream(RESOURCES_PATH + PATH_DIR_RESOURCE);
+ verifyStream(RESOURCES_PATH + PATH_FILE_RESOURCE);
+ assertNull(ResourceUtils.getResourceAsStream(NON_EXISTENT_RESOURCE));
+ assertNull(ResourceUtils.getResourceAsStream(INVALID_RESOURCE));
+ assertNull(ResourceUtils.getResourceAsStream(null));
+ verifyStream("");
+ }
- theStream = ResourceUtils.getResourceAsStream("");
- assertNotNull(theStream);
+ private void verifyStream(String path) throws IOException {
+ try (var theStream = ResourceUtils.getResourceAsStream(path)) {
+ assertNotNull(theStream);
+ }
}
/**
@@ -234,10 +214,10 @@ public class ResourceUtilsTest {
assertNotNull(theString);
theString = ResourceUtils.getResourceAsString(tmpEmptyFile.getAbsolutePath());
- assertTrue(theString.equals(""));
+ assertEquals("", theString);
theString = ResourceUtils.getResourceAsString(tmpUsedFile.getAbsolutePath());
- assertTrue(theString.equals("Bluebirds fly over the rainbow"));
+ assertEquals("Bluebirds fly over the rainbow", theString);
theString = ResourceUtils.getResourceAsString(jarFileResource);
assertNotNull(theString);
@@ -265,7 +245,7 @@ public class ResourceUtilsTest {
theString = ResourceUtils.getResourceAsString("");
- assertEquals("logback-test.xml\nMETA-INF\norg\ntestdir\n", theString);
+ assertEquals("cmdFiles\nlogback-test.xml\nMETA-INF\norg\ntestdir\nversion.txt\n", theString);
}
@@ -310,7 +290,7 @@ public class ResourceUtilsTest {
assertNull(ResourceUtils.getFilePath4Resource(null));
assertEquals("/something/else", ResourceUtils.getFilePath4Resource("/something/else"));
assertTrue(ResourceUtils.getFilePath4Resource("xml/example.xml").endsWith("xml/example.xml"));
- assertTrue(ResourceUtils.getFilePath4Resource("com/google").endsWith("com/google"));
+ assertTrue(ResourceUtils.getFilePath4Resource("com/google").contains("com/google"));
}
@Test
@@ -321,26 +301,37 @@ public class ResourceUtilsTest {
Set<String> resultD0 = ResourceUtils.getDirectoryContents("testdir");
assertEquals(1, resultD0.size());
- assertEquals("testdir/testfile.xml", resultD0.iterator().next());
+ assertEquals("testdir/testfile.xml", normalizePath(resultD0.iterator().next()));
Set<String> resultD1 = ResourceUtils.getDirectoryContents("org/onap/policy/common");
- assertTrue(resultD1.size() > 0);
- assertEquals("org/onap/policy/common/utils/", resultD1.iterator().next());
+ assertFalse(resultD1.isEmpty());
+ assertEquals("org/onap/policy/common/utils/", normalizePath(resultD1.iterator().next()));
Set<String> resultD2 = ResourceUtils.getDirectoryContents("org/onap/policy/common/utils/coder");
assertTrue(resultD2.size() >= 15);
- assertEquals("org/onap/policy/common/utils/coder/CoderExceptionTest.class", resultD2.iterator().next());
+ assertEquals("org/onap/policy/common/utils/coder/CoderExceptionTest.class",
+ normalizePath(resultD2.iterator().next()));
Set<String> resultJ0 = ResourceUtils.getDirectoryContents("com");
- assertEquals(189, resultJ0.size());
- assertEquals("com/google/", resultJ0.iterator().next());
+ assertTrue(resultJ0.contains("com/google/gson/"));
+ assertEquals("com/google/", normalizePath(resultJ0.iterator().next()));
- Set<String> resultJ1 = ResourceUtils.getDirectoryContents("com/google/gson/util");
- assertEquals(1, resultJ1.size());
- assertEquals("com/google/gson/util/VersionUtils.class", resultJ1.iterator().next());
+ Set<String> resultJ1 = ResourceUtils.getDirectoryContents("com/google/gson");
+ assertTrue(resultJ1.size() > 1);
+ assertTrue(resultJ1.contains("com/google/gson/JsonElement.class"));
URL dummyUrl = new URL("http://even/worse");
assertTrue(ResourceUtils.getDirectoryContentsJar(dummyUrl, "nonexistantdirectory").isEmpty());
}
+
+ /**
+ * Normalizes a path name, replacing OS-specific separators with "/".
+ *
+ * @param pathName path name to be normalized
+ * @return the normalized path name
+ */
+ private String normalizePath(String pathName) {
+ return pathName.replace(File.separator, "/");
+ }
}
diff --git a/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java
index 0952b168..91268979 100644
--- a/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java
+++ b/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,12 +21,13 @@
package org.onap.policy.common.utils.resources;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
-
+import org.apache.commons.io.FileUtils;
import org.junit.Test;
/**
@@ -39,35 +41,80 @@ public class TextFileUtilsTest {
@Test
public void testPutToFile() throws IOException {
- final File tempTextFile = File.createTempFile("Test", "txt");
+ final File tempTextFile = File.createTempFile("Test", ".txt");
+ tempTextFile.deleteOnExit();
TextFileUtils.putStringAsTextFile(FILE_CONTENT, tempTextFile.getAbsolutePath());
final String textFileString0 = TextFileUtils.getTextFileAsString(tempTextFile.getAbsolutePath());
assertEquals(FILE_CONTENT, textFileString0);
- final FileInputStream fis = new FileInputStream(tempTextFile);
- final String textFileString1 = TextFileUtils.getStreamAsString(fis);
- assertEquals(textFileString0, textFileString1);
+ try (final FileInputStream fis = new FileInputStream(tempTextFile)) {
+ final String textFileString1 = TextFileUtils.getStreamAsString(fis);
+ assertEquals(textFileString0, textFileString1);
+ }
}
@Test
public void testPutToFileWithNewPath() throws IOException {
String tempDirAndFileName = System.getProperty("java.io.tmpdir") + "/non/existant/path/Test.txt";
+ FileUtils.forceDeleteOnExit(new File(tempDirAndFileName));
TextFileUtils.putStringAsTextFile(FILE_CONTENT, tempDirAndFileName);
final String textFileString0 = TextFileUtils.getTextFileAsString(tempDirAndFileName);
assertEquals(FILE_CONTENT, textFileString0);
- final FileInputStream fis = new FileInputStream(tempDirAndFileName);
- final String textFileString1 = TextFileUtils.getStreamAsString(fis);
- assertEquals(textFileString0, textFileString1);
+ try (final FileInputStream fis = new FileInputStream(tempDirAndFileName)) {
+ final String textFileString1 = TextFileUtils.getStreamAsString(fis);
+ assertEquals(textFileString0, textFileString1);
+ }
+ }
+
+ @Test
+ public void testCreateTempFile() throws IOException {
+ var file = TextFileUtils.createTempFile("textFileUtilsTest", ".txt");
+ file.deleteOnExit();
+
+ verifyDefaultPermissions(file);
+ }
+
+ @Test
+ public void testSetDefaultPermissions() throws IOException {
+ var file = new File("target/tempfile.txt");
+ file.deleteOnExit();
+
+ // ensure it doesn't exist before we create it
+ file.delete();
+ assertThat(file.createNewFile()).isTrue();
+
+ // check using whatever permissions it comes with
+
+ TextFileUtils.setDefaultPermissions(file);
+
+ verifyDefaultPermissions(file);
+
+ // prevent read-write-execute by anyone
+ file.setReadable(false);
+ file.setWritable(false);
+ file.setExecutable(false);
+
+ TextFileUtils.setDefaultPermissions(file);
+
+ verifyDefaultPermissions(file);
+
+ // make it read-write-execute by everyone
+ file.setReadable(true);
+ file.setWritable(true);
+ file.setExecutable(true);
+
+ TextFileUtils.setDefaultPermissions(file);
+
+ verifyDefaultPermissions(file);
+ }
- File tempDirAndFile = new File(tempDirAndFileName);
- tempDirAndFile.delete();
- tempDirAndFile.getParentFile().delete();
- tempDirAndFile.getParentFile().getParentFile().delete();
- tempDirAndFile.getParentFile().getParentFile().getParentFile().delete();
+ private void verifyDefaultPermissions(File file) {
+ assertThat(file).canRead().canWrite();
+ assertThat(file.canExecute()).isTrue();
}
}