diff options
author | Dmitry Puzikov <d.puzikov2@partner.samsung.com> | 2019-11-29 09:57:27 +0100 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-01-29 09:48:59 +0000 |
commit | fe4623d1cddefe53df417d18115b69cc9265ec6e (patch) | |
tree | 0cb613d83bbbb0b1013cc05b98c3aad7dcd8e440 /common/onap-common-configuration-management/onap-configuration-management-core/src/test | |
parent | ceb5ab575aa49f4d082fe1e52cc1a3f8ff3ffd24 (diff) |
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 <d.puzikov2@partner.samsung.com>
Change-Id: Ifb5887401be4b397267b2f922cc9ad3f01c757f6
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/test')
-rw-r--r-- | common/onap-common-configuration-management/onap-configuration-management-core/src/test/java/org/onap/config/ConfigurationUtilsTest.java | 113 |
1 files changed, 102 insertions, 11 deletions
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<String> 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<Class, Class> testClasses = Stream.of(new Class[][] { + final Map<Class<?>, Class<?>> testClasses = Stream.of(new Class<?>[][]{ {BlockingQueue.class, LinkedBlockingQueue.class}, {TransferQueue.class, LinkedTransferQueue.class}, {Deque.class, ArrayDeque.class}, @@ -130,9 +133,9 @@ 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); @@ -140,6 +143,54 @@ public class ConfigurationUtilsTest { } @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<Integer> 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<Integer> 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); Collection<File> allFiles = ConfigurationUtils.getAllFiles(tmpRoot.toFile(), true, false); @@ -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); |