diff options
author | Dmitry Puzikov <d.puzikov2@partner.samsung.com> | 2019-11-19 09:55:27 +0100 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2019-11-25 08:50:03 +0000 |
commit | 1d3f7e44c9773db172694e8abe0c1ffea1538e37 (patch) | |
tree | abface10322ef57211f016621b84c2090eef8394 | |
parent | 60be4e21f1ada02fbbdb39cc47ff1c69c098a00a (diff) |
Fixing sonar issues
Simplified method ConfigurationUtils::getProperty.
Added unit tests for method.
Change-Id: I521661b464e7f60101ee51ccbc9d915bf5030f31
Issue-ID: SDC-2654
Signed-off-by: Dmitry Puzikov <d.puzikov2@partner.samsung.com>
2 files changed, 96 insertions, 43 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java index e7a4c458dc..df382143c6 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java @@ -19,12 +19,30 @@ package org.onap.config; -import static java.util.Optional.ofNullable; -import static org.onap.config.api.Hint.EXTERNAL_LOOKUP; -import static org.onap.config.api.Hint.LATEST_LOOKUP; -import static org.onap.config.api.Hint.NODE_SPECIFIC; - import com.virtlink.commons.configuration2.jackson.JsonConfiguration; +import net.sf.corn.cps.CPScanner; +import net.sf.corn.cps.ResourceFilter; +import org.apache.commons.configuration2.Configuration; +import org.apache.commons.configuration2.CompositeConfiguration; +import org.apache.commons.configuration2.FileBasedConfiguration; +import org.apache.commons.configuration2.PropertiesConfiguration; +import org.apache.commons.configuration2.XMLConfiguration; +import org.apache.commons.configuration2.builder.BasicConfigurationBuilder; +import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; +import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder; +import org.apache.commons.configuration2.builder.fluent.Configurations; +import org.apache.commons.configuration2.builder.fluent.Parameters; +import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler; +import org.apache.commons.configuration2.ex.ConfigurationException; +import org.apache.commons.io.IOUtils; +import org.onap.config.api.Config; +import org.onap.config.api.ConfigurationManager; +import org.onap.config.impl.YamlConfiguration; +import org.onap.config.type.ConfigurationMode; +import org.onap.config.type.ConfigurationType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; @@ -58,28 +76,11 @@ import java.util.concurrent.TransferQueue; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import net.sf.corn.cps.CPScanner; -import net.sf.corn.cps.ResourceFilter; -import org.apache.commons.configuration2.CompositeConfiguration; -import org.apache.commons.configuration2.Configuration; -import org.apache.commons.configuration2.FileBasedConfiguration; -import org.apache.commons.configuration2.PropertiesConfiguration; -import org.apache.commons.configuration2.XMLConfiguration; -import org.apache.commons.configuration2.builder.BasicConfigurationBuilder; -import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; -import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder; -import org.apache.commons.configuration2.builder.fluent.Configurations; -import org.apache.commons.configuration2.builder.fluent.Parameters; -import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler; -import org.apache.commons.configuration2.ex.ConfigurationException; -import org.apache.commons.io.IOUtils; -import org.onap.config.api.Config; -import org.onap.config.api.ConfigurationManager; -import org.onap.config.impl.YamlConfiguration; -import org.onap.config.type.ConfigurationMode; -import org.onap.config.type.ConfigurationType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + +import static java.util.Optional.ofNullable; +import static org.onap.config.api.Hint.EXTERNAL_LOOKUP; +import static org.onap.config.api.Hint.LATEST_LOOKUP; +import static org.onap.config.api.Hint.NODE_SPECIFIC; public class ConfigurationUtils { @@ -141,7 +142,7 @@ public class ConfigurationUtils { } return list.stream().filter(o -> o != null && !o.toString().trim().isEmpty()) - .map(o -> o.toString().trim()).collect(Collectors.joining(",")); + .map(o -> o.toString().trim()).collect(Collectors.joining(",")); } public static boolean isConfig(URL url) { @@ -179,12 +180,12 @@ public class ConfigurationUtils { private static Optional<String> readNamespace(Configuration config) { return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.NAMESPACE_KEY))) - .map(String::toUpperCase); + .map(String::toUpperCase); } private static Optional<String> readMergeStrategy(Configuration config) { return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.MODE_KEY))) - .map(String::toUpperCase); + .map(String::toUpperCase); } public static ConfigurationMode getMergeStrategy(File file) { @@ -313,7 +314,7 @@ public class ConfigurationUtils { public static boolean isWrapperClass(Class clazz) { return clazz == String.class || clazz == Boolean.class || clazz == Character.class - || Number.class.isAssignableFrom(clazz); + || Number.class.isAssignableFrom(clazz); } public static Class getArrayClass(Class clazz) { @@ -329,7 +330,7 @@ public class ConfigurationUtils { ConfigurationType configType = ConfigurationUtils.getConfigType(file); builder = getFileBasedConfigurationBuilder(configType); builder.configure(new Parameters().fileBased().setFile(file) - .setListDelimiterHandler(new DefaultListDelimiterHandler(','))); + .setListDelimiterHandler(new DefaultListDelimiterHandler(','))); return builder; } @@ -573,7 +574,7 @@ public class ConfigurationUtils { String moduleName = matcher.group(1).substring(1); return moduleName.equalsIgnoreCase(ConfigurationMode.OVERRIDE.name()) || moduleName.equalsIgnoreCase( ConfigurationMode.UNION.name()) || moduleName.equalsIgnoreCase(ConfigurationMode.MERGE.name()) - ? Constants.DEFAULT_NAMESPACE : moduleName; + ? Constants.DEFAULT_NAMESPACE : moduleName; } else { return Constants.DEFAULT_NAMESPACE; } @@ -593,21 +594,15 @@ public class ConfigurationUtils { } public static Object getProperty(Configuration config, String key, int processingHints) { - - if (!isDirectLookup(processingHints) && (config instanceof CompositeConfiguration)) { - + if (!isDirectLookup(processingHints) && isNodeSpecific(processingHints) && (config instanceof CompositeConfiguration)) { CompositeConfiguration conf = (CompositeConfiguration) config; for (int i = 0; i < conf.getNumberOfConfigurations(); i++) { - - if (isNodeSpecific(processingHints)) { - Object obj = conf.getConfiguration(i).getProperty(key); - if (obj != null) { - return obj; - } + Object obj = conf.getConfiguration(i).getProperty(key); + if (obj != null) { + return obj; } } } - return config.getProperty(key); } 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 b32c1d81df..d0b0a12297 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 @@ -49,10 +49,19 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; +import org.apache.commons.configuration2.BaseConfiguration; +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.api.Hint; +import org.onap.config.impl.ConfigurationRepository; public class ConfigurationUtilsTest { + private static final String TEST_NAME_SPACE = "testNameSpaceOne"; + private static final String TEST_COMPOSITE_NAMESPACE = "testCOmpositeConfig"; + @Test public void testCommaList() { List<?> list = Arrays.asList("1", "2", 3); @@ -123,4 +132,53 @@ public class ConfigurationUtilsTest { ConfigurationUtils.getCompatibleCollectionForAbstractDef(Collection.class); } + + @Test + public void testGetConfigPropertyBaseConfig() throws Exception { + ConfigurationRepository repo = populateTestBaseConfig(); + Configuration config = repo.getConfigurationFor(Constants.DEFAULT_TENANT, TEST_NAME_SPACE); + assertEquals(TEST_NAME_SPACE, ConfigurationUtils.getProperty(config, Constants.NAMESPACE_KEY, Hint.DEFAULT.value())); + } + + @Test + public void testGetCompositeConfigPropertyDefaultHints() throws Exception { + ConfigurationRepository repo = populateTestCompositeConfig(); + Configuration config = repo.getConfigurationFor(Constants.DEFAULT_TENANT, TEST_COMPOSITE_NAMESPACE); + assertEquals(TEST_NAME_SPACE, ConfigurationUtils.getProperty(config, Constants.NAMESPACE_KEY, Hint.DEFAULT.value())); + } + + @Test + public void testGetCompositeConfigPropertyNodeSpecificHints() throws Exception { + ConfigurationRepository repo = populateTestCompositeConfig(); + Configuration config = repo.getConfigurationFor(Constants.DEFAULT_TENANT, TEST_COMPOSITE_NAMESPACE); + assertEquals(TEST_NAME_SPACE, ConfigurationUtils.getProperty(config, Constants.NAMESPACE_KEY, Hint.NODE_SPECIFIC.value())); + } + + private ConfigurationRepository populateTestBaseConfig() { + BaseConfiguration inputConfig = new PropertiesConfiguration(); + inputConfig.setProperty(Constants.NAMESPACE_KEY, TEST_NAME_SPACE); + ConfigurationRepository repo = ConfigurationRepository.lookup(); + + repo.populateConfiguration( + Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + TEST_NAME_SPACE, inputConfig); + + return repo; + } + + private ConfigurationRepository populateTestCompositeConfig() { + CompositeConfiguration inputCompositeConfig = new CompositeConfiguration(); + Configuration inputConfig1= new BaseConfiguration(); + Configuration inputConfig2= new BaseConfiguration(); + inputConfig1.setProperty(Constants.NAMESPACE_KEY, TEST_NAME_SPACE); + inputCompositeConfig.addConfiguration(inputConfig1); + inputCompositeConfig.addConfigurationFirst(inputConfig2); + + ConfigurationRepository repo = ConfigurationRepository.lookup(); + repo.populateConfiguration( + Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + TEST_COMPOSITE_NAMESPACE, + inputCompositeConfig + ); + + return repo; + } } |