summaryrefslogtreecommitdiffstats
path: root/common/onap-common-configuration-management/onap-configuration-management-core/src/test
diff options
context:
space:
mode:
authork.kedron <k.kedron@partner.samsung.com>2019-05-24 11:06:02 +0200
committerAvi Gaffa <avi.gaffa@amdocs.com>2019-05-30 13:13:12 +0000
commit7c62480edb52c829102cddd030384176bf16780b (patch)
tree7bb20d3ad28d3ce30f68e209f6159e23a2374d85 /common/onap-common-configuration-management/onap-configuration-management-core/src/test
parent937803bdd4b6e459093e42d784195a60556a7021 (diff)
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 <k.kedron@partner.samsung.com> Change-Id: I5abb10bca0a1c409ec20daf6c22066836a0b76a3
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.java78
1 files changed, 78 insertions, 0 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 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<String, String> 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<Class, Class> 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