From 3a98ad373d3f8a8cccce36f78e24bd24278043d6 Mon Sep 17 00:00:00 2001 From: "k.kedron" Date: Tue, 4 Jun 2019 16:21:03 +0200 Subject: Improve unit test in the NonConfigResourceTest and various sonar fixes. Improve the NonConfigResourceTest. Remove the throws the Exception in CliConfigurationImp constructor. ConfigurationImpl class: - Replace the string to chart in indexOf method. - Extract the assigment out from expression Issue-ID: SDC-2327 Signed-off-by: Krystian Kedron Change-Id: If72a75e6a2d81a9ea74e3ae1af94b16a8489a29a --- .../org/onap/config/impl/CliConfigurationImpl.java | 2 +- .../org/onap/config/impl/ConfigurationImpl.java | 10 ++++--- .../org/onap/config/NonConfigResourceTest.java | 33 ++++++++++++++++------ 3 files changed, 31 insertions(+), 14 deletions(-) (limited to 'common/onap-common-configuration-management') diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java index 03b2075bac..a2440b9e94 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java @@ -41,7 +41,7 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con private static final List KEYS_TO_FILTER = Arrays.asList(NAMESPACE_KEY, MODE_KEY, LOAD_ORDER_KEY); - public CliConfigurationImpl() throws Exception { + public CliConfigurationImpl() { super(); } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java index c51c8621a9..6cad62ef03 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java @@ -207,7 +207,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { String k = keys.next(); if (k.startsWith(key + ".")) { k = k.substring(key.length() + 1); - String subkey = k.substring(0, k.indexOf(".")); + String subkey = k.substring(0, k.indexOf('.')); if (!map.containsKey(subkey)) { map.put(subkey, get(tenantId, namespace, key + "." + subkey, clazz)); } @@ -248,10 +248,12 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { while (k.contains(".")) { if (k.contains(".")) { - String subkey = k.substring(0, k.indexOf(".")); - k = k.substring(k.indexOf(".") + 1); + String subkey = k.substring(0, k.indexOf('.')); + k = k.substring(k.indexOf('.') + 1); if (!map.containsKey(subkey)) { - map.put(subkey, map = new HashMap<>()); + Map tmp = new HashMap(); + map.put(subkey, tmp); + map = tmp; } else { map = (Map) map.get(subkey); } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/NonConfigResourceTest.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/NonConfigResourceTest.java index 7566a29749..f2f3f9b6f8 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/NonConfigResourceTest.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/NonConfigResourceTest.java @@ -27,6 +27,7 @@ import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import org.junit.Test; @@ -55,15 +56,19 @@ public class NonConfigResourceTest { @Test public void testShouldLocateResourceWhenNodeConfigPropertyIsSet2() { + final String path = new File(sampleResourcePath).getParentFile().getPath(); + Map properties = Collections.singletonMap(NODE_CONFIG_LOCATION, path); - Map properties = Collections.singletonMap( - NODE_CONFIG_LOCATION, new File(sampleResourcePath).getParentFile().getPath()); - - NonConfigResource testedNonConfigResource = new NonConfigResource(properties::get); - System.getProperties().setProperty(NODE_CONFIG_LOCATION, - new File(sampleResourcePath).getParentFile().getPath()); + NonConfigResource testedNonConfigResource = new NonConfigResource(properties::get); Path thisFilePath = testedNonConfigResource.locate(RESOURCE_NAME); - assertEquals(0, thisFilePath.compareTo(new File(sampleResourcePath).toPath())); + + NonConfigResource systemNonConfigResource = new NonConfigResource(System.getProperties()::getProperty); + System.setProperty(NODE_CONFIG_LOCATION, path); + Path systemFilePath = systemNonConfigResource.locate(RESOURCE_NAME); + + Path testFilePath = sampleResourceFile.toPath(); + assertEquals(0, thisFilePath.compareTo(testFilePath)); + assertEquals(0, systemFilePath.compareTo(testFilePath)); } @Test @@ -88,9 +93,19 @@ public class NonConfigResourceTest { @Test public void testShouldNotLocateResource2() { - Map properties = Collections.emptyMap(); + String badResource = "noexistingresource"; + String badPath = "noexistingpath"; + + Map properties = new HashMap<>(); NonConfigResource testedObject = new NonConfigResource(properties::get); - Path thisFilePath = testedObject.locate("nonexistingresource"); + // empty properties and bad resource + Path thisFilePath = testedObject.locate(badResource); + + properties.put(NODE_CONFIG_LOCATION, badPath); + // bad path in properties and bad resource + Path thisFilePath2 = testedObject.locate(badResource); + assertNull(thisFilePath); + assertNull(thisFilePath2); } } \ No newline at end of file -- cgit 1.2.3-korg