From fe4623d1cddefe53df417d18115b69cc9265ec6e Mon Sep 17 00:00:00 2001 From: Dmitry Puzikov Date: Fri, 29 Nov 2019 09:57:27 +0100 Subject: Fixed sonar issues Refactored overcomplicated methods by splitting into the more simple. Remove redundant conditional checks. Move common checks into the utils class. Fixed raw types usage where it is possible. Add some tests. Fixed typos in test names. Fixed lost switch branch. Issue-ID: SDC-2698 Signed-off-by: Dmitry Puzikov Change-Id: Ifb5887401be4b397267b2f922cc9ad3f01c757f6 --- .../org/onap/config/ConfigurationUtilsTest.java | 113 +++++++++++++++++++-- 1 file changed, 102 insertions(+), 11 deletions(-) (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/test') 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 dab8946883..cfb27d53db 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 @@ -20,6 +20,7 @@ package org.onap.config; import static java.util.stream.Collectors.toList; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNotNull; @@ -36,7 +37,9 @@ import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Deque; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -77,7 +80,7 @@ public class ConfigurationUtilsTest { @Test public void testCommaListWithNullAndEmptyStrings() { - List list = Arrays.asList(null, "", " "); + List list = Arrays.asList(null, "", " "); String commaSeparatedList = ConfigurationUtils.getCommaSeparatedList(list); assertTrue(commaSeparatedList.isEmpty()); } @@ -101,12 +104,12 @@ public class ConfigurationUtilsTest { @Test public void testCastingArray() { int arraySize = 2; - final Class[] primitiveType = new Class[]{boolean.class, byte.class, + 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 -> + 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); @@ -119,7 +122,7 @@ public class ConfigurationUtilsTest { @Test(expected = IllegalArgumentException.class) public void testGetCompatibleCollection() { - final Map testClasses = Stream.of(new Class[][] { + final Map, Class> testClasses = Stream.of(new Class[][]{ {BlockingQueue.class, LinkedBlockingQueue.class}, {TransferQueue.class, LinkedTransferQueue.class}, {Deque.class, ArrayDeque.class}, @@ -130,15 +133,63 @@ public class ConfigurationUtilsTest { }).collect(Collectors.toMap(data -> data[0], data -> data[1])); testClasses.forEach((entryClass, expResultClass) -> { - Class resultClass = ConfigurationUtils.getCompatibleCollectionForAbstractDef(entryClass).getClass(); - assertEquals(expResultClass, resultClass); - } + Class resultClass = ConfigurationUtils.getCompatibleCollectionForAbstractDef(entryClass).getClass(); + assertEquals(expResultClass, resultClass); + } ); ConfigurationUtils.getCompatibleCollectionForAbstractDef(Collection.class); } + @Test + public void testGetPrimitivesArrayZeroLength() { + assertArrayEquals(new int[0], (int[]) ConfigurationUtils.getPrimitiveArray(Collections.emptyList(), int.class)); + assertArrayEquals(new byte[0], (byte[]) ConfigurationUtils.getPrimitiveArray(Collections.emptyList(), byte.class)); + assertArrayEquals(new short[0], (short[]) ConfigurationUtils.getPrimitiveArray(Collections.emptyList(), short.class)); + assertArrayEquals(new long[0], (long[]) ConfigurationUtils.getPrimitiveArray(Collections.emptyList(), long.class)); + assertArrayEquals(new double[0], (double[]) ConfigurationUtils.getPrimitiveArray(Collections.emptyList(), double.class), 0); + assertArrayEquals(new float[0], (float[]) ConfigurationUtils.getPrimitiveArray(Collections.emptyList(), float.class), 0); + assertArrayEquals(new boolean[0], (boolean[]) ConfigurationUtils.getPrimitiveArray(Collections.emptyList(), boolean.class)); + assertArrayEquals(new char[0], (char[]) ConfigurationUtils.getPrimitiveArray(Collections.emptyList(), char.class)); + assertNull(ConfigurationUtils.getPrimitiveArray(Collections.emptyList(), Integer.class)); + } + + @Test + public void testGetWrappersArrayZeroLength() { + assertArrayEquals(new Integer[0], (Integer[]) ConfigurationUtils.getWrappersArray(Collections.emptyList(), Integer.class)); + assertArrayEquals(new Byte[0], (Byte[]) ConfigurationUtils.getWrappersArray(Collections.emptyList(), Byte.class)); + assertArrayEquals(new Short[0], (Short[]) ConfigurationUtils.getWrappersArray(Collections.emptyList(), Short.class)); + assertArrayEquals(new Long[0], (Long[]) ConfigurationUtils.getWrappersArray(Collections.emptyList(), Long.class)); + assertArrayEquals(new Double[0], (Double[]) ConfigurationUtils.getWrappersArray(Collections.emptyList(), Double.class)); + assertArrayEquals(new Float[0], (Float[]) ConfigurationUtils.getWrappersArray(Collections.emptyList(), Float.class)); + assertArrayEquals(new Boolean[0], (Boolean[]) ConfigurationUtils.getWrappersArray(Collections.emptyList(), Boolean.class)); + assertArrayEquals(new Character[0], (Character[]) ConfigurationUtils.getWrappersArray(Collections.emptyList(), Character.class)); + assertNull(ConfigurationUtils.getWrappersArray(Collections.emptyList(), boolean.class)); + } + + @Test + public void testPrimitivesArrayNonZeroLength() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + assertArrayEquals(new int[]{1, 2, 3, 4, 5}, (int[]) ConfigurationUtils.getPrimitiveArray(list, int.class)); + } + + @Test + public void testWrappersArrayNonZeroLength() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + assertArrayEquals(new Integer[]{1, 2, 3, 4, 5}, (Integer[]) ConfigurationUtils.getWrappersArray(list, Integer.class)); + } + @Test public void testGetAllFilesRecursiveIncludeAll() throws IOException { Path tmpRoot = TestUtil.createTestDirsStructure(TMP_DIR_PREFIX); @@ -207,6 +258,46 @@ public class ConfigurationUtilsTest { assertEquals(TEST_NAME_SPACE, ConfigurationUtils.getProperty(config, Constants.NAMESPACE_KEY, Hint.NODE_SPECIFIC.value())); } + @Test + public void testIsAMap() { + assertTrue(ConfigurationUtils.isAMap(HashMap.class)); + assertFalse(ConfigurationUtils.isAMap(ArrayList.class)); + } + + @Test + public void testIsACollection() { + assertTrue(ConfigurationUtils.isACollection(ArrayList.class)); + assertFalse(ConfigurationUtils.isACollection(HashMap.class)); + } + + @Test + public void testIsAPrimitiveOrWrapper() { + assertTrue(ConfigurationUtils.isAPrimitiveOrWrapper(int.class)); + assertTrue(ConfigurationUtils.isAPrimitiveOrWrapper(Integer.class)); + assertFalse(ConfigurationUtils.isAPrimitiveOrWrapper(HashMap.class)); + } + + @Test + public void testIsAPrimitivesArray() { + assertTrue(ConfigurationUtils.isAPrimitivesArray(int[].class)); + assertFalse(ConfigurationUtils.isAPrimitivesArray(Integer[].class)); + assertFalse(ConfigurationUtils.isAPrimitivesArray(HashMap[].class)); + } + + @Test + public void testIsAWrapperArray() { + assertTrue(ConfigurationUtils.isAWrappersArray(Integer[].class)); + assertFalse(ConfigurationUtils.isAWrappersArray(int[].class)); + assertFalse(ConfigurationUtils.isAWrappersArray(HashMap[].class)); + } + + @Test + public void testIsAPrimitivesOrWrapperArray() { + assertTrue(ConfigurationUtils.isAPrimitivesOrWrappersArray(int[].class)); + assertTrue(ConfigurationUtils.isAPrimitivesOrWrappersArray(Integer[].class)); + assertFalse(ConfigurationUtils.isAPrimitivesOrWrappersArray(HashMap[].class)); + } + private ConfigurationRepository populateTestBaseConfig() { BaseConfiguration inputConfig = new PropertiesConfiguration(); inputConfig.setProperty(Constants.NAMESPACE_KEY, TEST_NAME_SPACE); @@ -220,8 +311,8 @@ public class ConfigurationUtilsTest { private ConfigurationRepository populateTestCompositeConfig() { CompositeConfiguration inputCompositeConfig = new CompositeConfiguration(); - Configuration inputConfig1= new BaseConfiguration(); - Configuration inputConfig2= new BaseConfiguration(); + Configuration inputConfig1 = new BaseConfiguration(); + Configuration inputConfig2 = new BaseConfiguration(); inputConfig1.setProperty(Constants.NAMESPACE_KEY, TEST_NAME_SPACE); inputCompositeConfig.addConfiguration(inputConfig1); inputCompositeConfig.addConfigurationFirst(inputConfig2); -- cgit 1.2.3-korg