From 7c62480edb52c829102cddd030384176bf16780b Mon Sep 17 00:00:00 2001 From: "k.kedron" Date: Fri, 24 May 2019 11:06:02 +0200 Subject: Improve unit test for ConfigurationUtils and sonar fixes Add more test for ConfigurationUtils. Fix error in getCompatibleCollectionForAbstractDef method bad return value for TransferQueue.class. Sonar fixes: - use not deprecated method IOUtils.toString. - remove code duplication. - return empty collection instead of null. Issue-ID: SDC-2327 Signed-off-by: Krystian Kedron Change-Id: I5abb10bca0a1c409ec20daf6c22066836a0b76a3 --- .../java/org/onap/config/ConfigurationUtils.java | 35 +++++----- .../org/onap/config/ConfigurationUtilsTest.java | 78 ++++++++++++++++++++++ 2 files changed, 97 insertions(+), 16 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 bdba27a420..b18649181d 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 @@ -12,6 +12,9 @@ * 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. + * + * Modifications Copyright (c) 2019 Samsung + * */ package org.onap.config; @@ -28,6 +31,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.MalformedURLException; import java.net.URL; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayDeque; @@ -108,7 +112,7 @@ public class ConfigurationUtils { ArrayList collection = new ArrayList<>(); if (file.isDirectory() && file.exists()) { File[] files = file.listFiles(); - for (File innerFile : files) { + for (File innerFile : Objects.requireNonNull(files)) { if (innerFile.isFile() && !onlyDirectory) { collection.add(innerFile); } else if (innerFile.isDirectory()) { @@ -430,20 +434,17 @@ public class ConfigurationUtils { Matcher matcher = pattern.matcher(data); if (matcher.matches()) { String key = matcher.group(1); + String value; if (key.toUpperCase().startsWith("ENV:")) { - String envValue = System.getenv(key.substring(4)); - return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "\\}", - envValue == null ? "" : envValue.replace("\\", "\\\\"))); + value = System.getenv(key.substring(4)); } else if (key.toUpperCase().startsWith("SYS:")) { - String sysValue = System.getProperty(key.substring(4)); - return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "\\}", - sysValue == null ? "" : sysValue.replace("\\", "\\\\"))); + value = System.getProperty(key.substring(4)); } else { - String propertyValue = ConfigurationUtils.getCollectionString( + value = ConfigurationUtils.getCollectionString( ConfigurationManager.lookup().getAsStringValues(tenant, namespace, key).toString()); - return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "\\}", - propertyValue == null ? "" : propertyValue.replace("\\", "\\\\"))); } + return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "}", + value == null ? "" : value.replace("\\", "\\\\"))); } else { return data; } @@ -452,7 +453,7 @@ public class ConfigurationUtils { public static String getFileContents(String path) { try { if (path != null) { - return IOUtils.toString(new URL(path)); + return IOUtils.toString(new URL(path), Charset.defaultCharset()); } } catch (Exception exception) { exception.printStackTrace(); @@ -491,12 +492,12 @@ public class ConfigurationUtils { } public static Collection getCompatibleCollectionForAbstractDef(Class clazz) { - if (BlockingQueue.class.isAssignableFrom(clazz)) { - return getConcreteCollection(BlockingQueue.class); - } if (TransferQueue.class.isAssignableFrom(clazz)) { return getConcreteCollection(TransferQueue.class); } + if (BlockingQueue.class.isAssignableFrom(clazz)) { + return getConcreteCollection(BlockingQueue.class); + } if (Deque.class.isAssignableFrom(clazz)) { return getConcreteCollection(Deque.class); } @@ -512,7 +513,8 @@ public class ConfigurationUtils { if (List.class.isAssignableFrom(clazz)) { return getConcreteCollection(List.class); } - return null; + throw new IllegalArgumentException("Only corresponding array classes and any are allowed as argument." + + "assignable from TransferQueue, BlockingQueue, Deque, Queue, SortedSet, Set, List class"); } public static Collection getConcreteCollection(Class clazz) { @@ -533,7 +535,8 @@ public class ConfigurationUtils { case "java.util.concurrent.BlockingQueue": return new LinkedBlockingQueue<>(); default: - return null; + throw new IllegalArgumentException("Only corresponding array classes and any are allowed as argument." + + "assignable from TransferQueue, BlockingQueue, Deque, Queue, SortedSet, Set, List class"); } } 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 94ac71d1f2..728fc0589d 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 @@ -12,16 +12,43 @@ * 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. + * + * Modifications Copyright (c) 2019 Samsung + * */ package org.onap.config; +import static java.util.stream.Collectors.toList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.util.ArrayDeque; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.Deque; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.LinkedTransferQueue; +import java.util.concurrent.TransferQueue; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + import org.junit.Test; public class ConfigurationUtilsTest { @@ -45,4 +72,55 @@ public class ConfigurationUtilsTest { assertEquals(String[].class, ConfigurationUtils.getArrayClass(String.class)); assertNull(ConfigurationUtils.getArrayClass(ConfigurationUtilsTest.class)); } + + @Test + public void testGetCollectionGenericType() throws NoSuchFieldException { + + class DummyClass { + public Map testParameterizedTypeField; + } + Field field = DummyClass.class.getField("testParameterizedTypeField"); + assertEquals(String.class, ConfigurationUtils.getCollectionGenericType(field)); + } + + @Test + public void testCastingArray() { + int arraySize = 2; + final Class[] primitiveType = new Class[]{boolean.class, byte.class, + double.class, float.class, int.class, long.class, short.class}; + + for (Class clazz: primitiveType) { + Class expectedResultClass = Array.newInstance(clazz, 0).getClass(); + List defaultCollection = IntStream.range(0, arraySize).mapToObj(i -> + ConfigurationUtils.getDefaultFor(clazz)).collect(toList()); + + Object resultArray = ConfigurationUtils.getPrimitiveArray(defaultCollection, clazz); + + assertNotNull(resultArray); + assertFalse(ConfigurationUtils.isZeroLengthArray(expectedResultClass, resultArray)); + assertNotNull(expectedResultClass.cast(resultArray)); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testGetCompatibleCollection() { + final Map testClasses = Stream.of(new Class[][] { + {BlockingQueue.class, LinkedBlockingQueue.class}, + {TransferQueue.class, LinkedTransferQueue.class}, + {Deque.class, ArrayDeque.class}, + {Queue.class, ConcurrentLinkedQueue.class}, + {SortedSet.class, TreeSet.class}, + {Set.class, HashSet.class}, + {List.class, ArrayList.class} + }).collect(Collectors.toMap(data -> data[0], data -> data[1])); + + testClasses.forEach((entryClass, expResultClass) -> { + Class resultClass = ConfigurationUtils.getCompatibleCollectionForAbstractDef(entryClass).getClass(); + assertEquals(expResultClass, resultClass); + } + ); + + ConfigurationUtils.getCompatibleCollectionForAbstractDef(Collection.class); + + } } \ No newline at end of file -- cgit 1.2.3-korg