From 04061f9c4b04f43c55ef190a6e4afc9b3dd203d7 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Fri, 10 Jan 2020 20:27:22 +0000 Subject: 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 --- .../common/utils/resources/ResourceUtilsTest.java | 49 ++++++++++++++++++---- 1 file changed, 42 insertions(+), 7 deletions(-) (limited to 'utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java') 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 4b2b007b..2991009f 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; @@ -75,6 +78,15 @@ public class ResourceUtilsTest { } } + /** + * Cleandown resource utils test. + */ + @After + public void cleandownResourceUtilsTest() { + tmpEmptyFile.delete(); + tmpUsedFile.delete(); + } + /** * Test get url resource. */ @@ -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 resultD0 = ResourceUtils.getDirectoryContents("testdir"); + assertEquals(1, resultD0.size()); + assertEquals("testdir/testfile.xml", resultD0.iterator().next()); + + Set resultD1 = ResourceUtils.getDirectoryContents("org/onap/policy/common"); + assertTrue(resultD1.size() > 0); + assertEquals("org/onap/policy/common/utils/", resultD1.iterator().next()); + + Set 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 resultJ0 = ResourceUtils.getDirectoryContents("com"); + assertEquals(189, resultJ0.size()); + assertEquals("com/google/", resultJ0.iterator().next()); + + Set 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()); + } } -- cgit 1.2.3-korg