aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2020-01-10 20:27:22 +0000
committerJim Hahn <jrh3@att.com>2020-01-13 14:56:13 +0000
commit04061f9c4b04f43c55ef190a6e4afc9b3dd203d7 (patch)
tree47663b94715ccf29602e7b7a05d0f5d484336a28
parent8cfa59063447343d51aba60399f6279d24589777 (diff)
Add method to return resource directory contents
In order to avoid hard coding the policy types and policies that are in the example directories into unit test cases, and in order to automatically pick up added and removed policy types and policies, it would be good to read the contents of resource directories at run time in unit tests. This change brings in that functionality into ResourceUtils. Issue-ID: POLICY-2315 Change-Id: I601718828aad0f065dbbaa1f5af8d0a0f133f44d Signed-off-by: liamfallon <liam.fallon@est.tech>
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java105
-rw-r--r--utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java49
2 files changed, 144 insertions, 10 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java
index cca6e62..365efab 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java
@@ -1,19 +1,20 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2020 Nordix Foundation.
* ================================================================================
* 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.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
@@ -25,6 +26,12 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -40,6 +47,10 @@ public abstract class ResourceUtils {
// The length of byte buffers used to read resources into strings
private static final int BYTE_BUFFER_LENGH = 1024;
+ // Resource types
+ private static final String FILE_PROTOCOL = "file";
+ private static final String JAR_PROTOCOL = "jar";
+
/**
* Private constructor used to prevent sub class instantiation.
*/
@@ -211,4 +222,92 @@ public abstract class ResourceUtils {
return resource;
}
}
+
+ /**
+ * Read the list of entries in a resource directory.
+ *
+ * @param resourceDirectoryName the name of the resource directory
+ * @return the list of entries
+ */
+ public static Set<String> getDirectoryContents(final String resourceDirectoryName) {
+ // Find the location of the resource, is it in a Jar or on the local file system?
+ URL directoryUrl = ResourceUtils.getUrl4Resource(resourceDirectoryName);
+
+ if (directoryUrl == null) {
+ LOGGER.debug("resource \"{}\" was not found", resourceDirectoryName);
+ return Collections.emptySet();
+ }
+
+ if (FILE_PROTOCOL.equals(directoryUrl.getProtocol())) {
+ return getDirectoryContentsLocal(directoryUrl, resourceDirectoryName);
+ } else if (JAR_PROTOCOL.equals(directoryUrl.getProtocol())) {
+ // Examine the Jar
+ return getDirectoryContentsJar(directoryUrl, resourceDirectoryName);
+ } else {
+ LOGGER.debug("resource \"{}\" has an unsupported protocol {}", resourceDirectoryName,
+ directoryUrl.getProtocol());
+ return Collections.emptySet();
+ }
+ }
+
+ /**
+ * Get a list of the contents of a local resource directory.
+ *
+ * @param localResourceDirectoryUrl the local resource file URL
+ * @param resourceDirectoryName the name of the resource directory
+ * @return a list of the directory contents
+ */
+ public static Set<String> getDirectoryContentsLocal(final URL localResourceDirectoryUrl,
+ final String resourceDirectoryName) {
+ File localDirectory = new File(localResourceDirectoryUrl.getFile());
+
+ if (!localDirectory.isDirectory()) {
+ LOGGER.debug("resource \"{}\" is not a directory", resourceDirectoryName);
+ return Collections.emptySet();
+ }
+
+ Set<String> localDirectorySet = new TreeSet<>();
+ for (File localDirectoryEntry : localDirectory.listFiles()) {
+ if (localDirectoryEntry.isDirectory()) {
+ localDirectorySet
+ .add(resourceDirectoryName + File.separator + localDirectoryEntry.getName() + File.separator);
+ } else {
+ localDirectorySet.add(resourceDirectoryName + File.separator + localDirectoryEntry.getName());
+ }
+ }
+
+ return localDirectorySet;
+ }
+
+ /**
+ * Get a list of the contents of a local resource directory.
+ *
+ * @param jarResourceDirectoryUrl the name of the resource directory in the jar
+ * @param resourceDirectoryName the name of the resource directory
+ * @return a list of the directory contents
+ */
+ public static Set<String> getDirectoryContentsJar(final URL jarResourceDirectoryUrl,
+ final String resourceDirectoryName) {
+ File jarResourceDirectory = new File(jarResourceDirectoryUrl.getPath());
+ String jarFileName = jarResourceDirectory.getParent().replaceFirst("^file:", "").replaceFirst("!.*$", "");
+
+ Set<String> localDirectorySet = new TreeSet<>();
+
+ try (JarFile jarFile = new JarFile(jarFileName)) {
+ Enumeration<JarEntry> entries = jarFile.entries();
+
+ while (entries.hasMoreElements()) {
+ JarEntry je = entries.nextElement();
+
+ if (je.getName().matches("^" + resourceDirectoryName + "\\/.+")) {
+ localDirectorySet.add(je.getName());
+ }
+ }
+ } catch (IOException ioe) {
+ LOGGER.debug("error opening jar file {}", jarResourceDirectoryUrl.getPath());
+ return Collections.emptySet();
+ }
+
+ return localDirectorySet;
+ }
}
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 4b2b007..2991009 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
@@ -2,6 +2,7 @@
* ============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.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +31,9 @@ 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;
@@ -76,6 +79,15 @@ public class ResourceUtilsTest {
}
/**
+ * Cleandown resource utils test.
+ */
+ @After
+ public void cleandownResourceUtilsTest() {
+ tmpEmptyFile.delete();
+ tmpUsedFile.delete();
+ }
+
+ /**
* Test get url resource.
*/
@Test
@@ -298,14 +310,37 @@ 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"));
}
- /**
- * Cleandown resource utils test.
- */
- @After
- public void cleandownResourceUtilsTest() {
- tmpEmptyFile.delete();
- tmpUsedFile.delete();
+ @Test
+ public void testGetDirectoryContents() throws MalformedURLException {
+ assertTrue(ResourceUtils.getDirectoryContents(null).isEmpty());
+ assertTrue(ResourceUtils.getDirectoryContents("idontexist").isEmpty());
+ assertTrue(ResourceUtils.getDirectoryContents("logback-test.xml").isEmpty());
+
+ Set<String> resultD0 = ResourceUtils.getDirectoryContents("testdir");
+ assertEquals(1, resultD0.size());
+ assertEquals("testdir/testfile.xml", 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());
+
+ 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());
+
+ Set<String> resultJ0 = ResourceUtils.getDirectoryContents("com");
+ assertEquals(189, resultJ0.size());
+ assertEquals("com/google/", 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());
+
+ URL dummyUrl = new URL("http://even/worse");
+ assertTrue(ResourceUtils.getDirectoryContentsJar(dummyUrl, "nonexistantdirectory").isEmpty());
+
}
}