Bug 5947: Increasing code coverage - binding-generator 45/42145/3
authorPeter Nosal <peter.nosal@pantheon.tech>
Wed, 20 Jul 2016 12:22:52 +0000 (14:22 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 21 Jul 2016 14:01:32 +0000 (14:01 +0000)
Change-Id: I7d89a0bb9950776712b57bf6aad0a3425d531c73
Signed-off-by: Peter Nosal <peter.nosal@pantheon.tech>
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/CodecTypeUtilsTest.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTextTemplateTest.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/JavassistUtilsTest.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/SourceCodeGeneratorFactoryTest.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/XtendHelperTest.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/YangSchemaUtilsTest.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderModel.java

diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/CodecTypeUtilsTest.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/CodecTypeUtilsTest.java
new file mode 100644 (file)
index 0000000..196dceb
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.binding.generator.impl;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+
+import java.lang.reflect.Constructor;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.binding.Identifiable;
+import org.opendaylight.yangtools.yang.binding.Identifier;
+
+public class CodecTypeUtilsTest {
+
+    @Test
+    public void newIdentifiableItem() throws Exception {
+        assertNotNull(CodecTypeUtils.newIdentifiableItem(Identifiable.class, mock(Identifier.class)));
+    }
+
+    @Test(expected = UnsupportedOperationException.class)
+    public void privateConstructTest() throws Throwable {
+        final Constructor constructor = CodecTypeUtils.class.getDeclaredConstructor();
+        constructor.setAccessible(true);
+        try {
+            constructor.newInstance();
+        } catch (Exception e) {
+            throw e.getCause();
+        }
+    }
+}
\ No newline at end of file
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTextTemplateTest.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTextTemplateTest.java
new file mode 100644 (file)
index 0000000..1b92e02
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.binding.generator.impl;
+
+import java.lang.reflect.Constructor;
+import org.junit.Test;
+
+public class YangTextTemplateTest {
+
+    @Test(expected = UnsupportedOperationException.class)
+    public void privateConstructTest() throws Throwable {
+        final Constructor constructor = YangTextTemplate.class.getDeclaredConstructor();
+        constructor.setAccessible(true);
+        try {
+            constructor.newInstance();
+        } catch (Exception e) {
+            throw e.getCause();
+        }
+    }
+}
\ No newline at end of file
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/JavassistUtilsTest.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/JavassistUtilsTest.java
new file mode 100644 (file)
index 0000000..695d785
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.binding.generator.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+
+import com.google.common.collect.ImmutableList;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtField;
+import javassist.bytecode.AccessFlag;
+import org.junit.Test;
+
+public class JavassistUtilsTest {
+
+    @Test
+    public void forClassPool() throws Exception {
+        final JavassistUtils javassistUtils = JavassistUtils.forClassPool(ClassPool.getDefault());
+        final ClassGenerator classGenerator = mock(ClassGenerator.class);
+        doNothing().when(classGenerator).process(any());
+        final CtClass ctInterface = javassistUtils.createClass("TestInterface", classGenerator);
+        ctInterface.setModifiers(AccessFlag.INTERFACE);
+        final CtClass ctClass = javassistUtils.createClass("TestClass", ctInterface, classGenerator);
+        javassistUtils.ensureClassLoader(ctClass.getClass());
+        assertNotNull(ctClass);
+        assertNotNull(javassistUtils.get(ClassPool.getDefault(), ctClass.getClass()));
+
+        CtField ctField = javassistUtils.field(ctClass, "testField", Object.class);
+        assertEquals(ctField, ctClass.getField("testField"));
+        ctField = javassistUtils.staticField(ctClass, "testStaticField", Object.class);
+        assertEquals(ctField, ctClass.getField("testStaticField"));
+
+        final MethodGenerator methodGenerator = mock(MethodGenerator.class);
+        doNothing().when(methodGenerator).process(any());
+        javassistUtils.method(ctClass, Void.class, "testMethod", Object.class, methodGenerator);
+        javassistUtils.method(ctInterface, Void.class, "testInterfaceMethod", Object.class, methodGenerator);
+        javassistUtils.method(ctClass, Void.class, "testMethod2", ImmutableList.of(Object.class), methodGenerator);
+        javassistUtils.staticMethod(ctClass, Void.class, "testStaticMethod", Object.class, methodGenerator);
+        javassistUtils.implementMethodsFrom(ctClass, ctInterface, methodGenerator);
+        assertNotNull(ctClass.getDeclaredMethod("testMethod"));
+        assertNotNull(ctClass.getDeclaredMethod("testMethod2"));
+        assertNotNull(ctClass.getDeclaredMethod("testStaticMethod"));
+        assertNotNull(ctClass.getDeclaredMethod("testInterfaceMethod"));
+
+        final ClassCustomizer classCustomizer = mock(ClassCustomizer.class);
+        doNothing().when(classCustomizer).customizeClass(any());
+        assertNotNull(javassistUtils.instantiatePrototype("javassist.CtNewClass", "leWut", classCustomizer));
+    }
+
+    @Test
+    public void privateConstructTest() throws Exception {
+        assertFalse(JavassistUtils.class.getDeclaredConstructor(ClassPool.class).isAccessible());
+    }
+}
\ No newline at end of file
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/SourceCodeGeneratorFactoryTest.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/SourceCodeGeneratorFactoryTest.java
new file mode 100644 (file)
index 0000000..44a19fa
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.binding.generator.util;
+
+import static java.util.Arrays.stream;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+import com.google.common.collect.ImmutableList;
+import java.io.File;
+import java.lang.reflect.Field;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtField;
+import javassist.CtMethod;
+import javassist.bytecode.AccessFlag;
+import javassist.bytecode.ClassFile;
+import org.junit.Test;
+
+public class SourceCodeGeneratorFactoryTest {
+
+    private static final SourceCodeGeneratorFactory FACTORY = new SourceCodeGeneratorFactory();
+    private SourceCodeGenerator generator;
+    private String propKey;
+
+    @Test
+    public void basicFactoryTest() throws Exception {
+        final Field propField = SourceCodeGeneratorFactory.class.getDeclaredField("GENERATE_CODEC_SOURCE_PROP");
+        propField.setAccessible(true);
+        propKey = (String) propField.get(FACTORY);
+        final String propValue;
+        final boolean present;
+
+        propValue = System.getProperty(propKey, null);
+        present = propValue != null;
+
+        testWithPropertyPresent();
+        testWithoutPropertyPresent();
+
+        if (present) {
+            System.setProperty(propKey, propValue);
+        } else {
+            System.clearProperty(propKey);
+        }
+    }
+
+    private void testWithPropertyPresent() throws Exception {
+        System.clearProperty(propKey);
+        System.setProperty(propKey, "true");
+        generator = FACTORY.getInstance(null);
+        assertTrue(generator instanceof DefaultSourceCodeGenerator);
+    }
+
+    private void testWithoutPropertyPresent() throws Exception {
+        System.clearProperty(propKey);
+        generator = FACTORY.getInstance(null);
+        assertTrue(generator instanceof NullSourceCodeGenerator);
+    }
+
+    @Test
+    public void nullSourceCodeGenTest() throws Exception {
+        generator = new NullSourceCodeGenerator();
+        generator.appendField(null, null);
+    }
+
+    @Test
+    public void defaultSourceCodeGenTest() throws Exception {
+        final File dir = new File("testDir");
+        assertTrue(cleanup(dir));
+
+        generator = new DefaultSourceCodeGenerator(dir.getName());
+        final CtClass ctClass = mock(CtClass.class, CALLS_REAL_METHODS);
+        doReturn(false).when(ctClass).isFrozen();
+        ctClass.setName("TestClass");
+        final ClassPool classPool = mock(ClassPool.class);
+        doReturn(ctClass).when(classPool).get((String) any());
+        doNothing().when(ctClass).setName("TestClass");
+        doReturn(ctClass).when(ctClass).getSuperclass();
+        doReturn(new CtClass[] {ctClass,ctClass}).when(ctClass).getInterfaces();
+        doReturn(classPool).when(ctClass).getClassPool();
+        doReturn(false).when(ctClass).isArray();
+        doReturn(false).when(ctClass).isPrimitive();
+        doReturn(AccessFlag.toModifier(AccessFlag.PUBLIC)).when(ctClass).getModifiers();
+        final ClassFile classFile = new ClassFile(false,"test", null);
+        doReturn(classFile).when(ctClass).getClassFile2();
+        doReturn(false).when(ctClass).isFrozen();
+        doReturn("testClass").when(ctClass).getName();
+        final CtField ctField = mock(CtField.class);
+        doReturn(AccessFlag.toModifier(AccessFlag.PUBLIC)).when(ctField).getModifiers();
+        doReturn(ctClass).when(ctField).getType();
+        doReturn("testField").when(ctField).getName();
+        final CtMethod ctMethod = new CtMethod(ctClass,"method", new CtClass[] { ctClass , ctClass }, ctClass);
+
+        generator.appendField(ctField, "b");
+        generator.appendMethod(ctMethod, "c");
+        generator.outputGeneratedSource(ctClass);
+
+        assertTrue(dir.exists());
+        assertTrue(dir.isDirectory());
+        assertFalse(ImmutableList.of(dir.listFiles()).isEmpty());
+
+        assertTrue(cleanup(dir));
+    }
+
+    private boolean cleanup(File dir) {
+        if (!dir.exists()) return true;
+
+        stream(dir.listFiles()).forEach(file -> file.delete());
+        return dir.delete();
+    }
+}
\ No newline at end of file
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/XtendHelperTest.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/XtendHelperTest.java
new file mode 100644 (file)
index 0000000..991d4e4
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.binding.generator.util;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import java.lang.reflect.Constructor;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+
+public class XtendHelperTest {
+
+    @Test
+    public void getTypesTest() throws Exception {
+        final UnionTypeDefinition unionTypeDefinition = mock(UnionTypeDefinition.class);
+        doReturn(null).when(unionTypeDefinition).getTypes();
+        XtendHelper.getTypes(unionTypeDefinition);
+        verify(unionTypeDefinition).getTypes();
+    }
+
+    @Test(expected = UnsupportedOperationException.class)
+    public void privateConstructTest() throws Throwable {
+        final Constructor constructor = XtendHelper.class.getDeclaredConstructor();
+        constructor.setAccessible(true);
+        try {
+            constructor.newInstance();
+        } catch (Exception e) {
+            throw e.getCause();
+        }
+    }
+}
\ No newline at end of file
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/YangSchemaUtilsTest.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/generator/util/YangSchemaUtilsTest.java
new file mode 100644 (file)
index 0000000..f8ca2a2
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.binding.generator.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.withSettings;
+import static org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderModel.createTestContext;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import java.lang.reflect.Constructor;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
+
+public class YangSchemaUtilsTest {
+
+    private static final AugmentationSchema AUGMENTATION_SCHEMA = mock(AugmentationSchema.class);
+    private static final UnknownSchemaNode UNKNOWN_SCHEMA_NODE = mock(UnknownSchemaNode.class);
+    private static final QName Q_NAME =
+            QName.create(URI.create("testUri"), new Date(System.currentTimeMillis()), "foo_augment");
+
+    @Before
+    public void setUp() throws Exception {
+        doReturn(ImmutableList.of(UNKNOWN_SCHEMA_NODE)).when(AUGMENTATION_SCHEMA).getUnknownSchemaNodes();
+        doReturn(QName.create(YangSchemaUtils.AUGMENT_IDENTIFIER)).when(UNKNOWN_SCHEMA_NODE).getNodeType();
+        doReturn(Q_NAME).when(UNKNOWN_SCHEMA_NODE).getQName();
+    }
+
+    @Test
+    public void getAugmentationQName() throws Exception {
+        assertEquals(Q_NAME, YangSchemaUtils.getAugmentationQName(AUGMENTATION_SCHEMA));
+        final DataSchemaNode dataSchemaNode = mock(DataSchemaNode.class);
+        doReturn(Q_NAME).when(UNKNOWN_SCHEMA_NODE).getNodeType();
+        doReturn(ImmutableList.of(dataSchemaNode)).when(AUGMENTATION_SCHEMA).getChildNodes();
+        doReturn(false).when(dataSchemaNode).isAugmenting();
+        doReturn(Q_NAME).when(dataSchemaNode).getQName();
+        doReturn(ImmutableList.of(dataSchemaNode)).when(AUGMENTATION_SCHEMA).getChildNodes();
+        assertEquals(Q_NAME, YangSchemaUtils.getAugmentationQName(AUGMENTATION_SCHEMA));
+    }
+
+    @Test
+    public void getAugmentationIdentifier() throws Exception {
+        assertEquals(Q_NAME, YangSchemaUtils.getAugmentationIdentifier(AUGMENTATION_SCHEMA));
+        doReturn(Q_NAME).when(UNKNOWN_SCHEMA_NODE).getNodeType();
+        assertNull(YangSchemaUtils.getAugmentationIdentifier(AUGMENTATION_SCHEMA));
+    }
+
+    @Test
+    public void findTypeDefinition() throws Exception {
+        SchemaContext context = createTestContext();
+        assertNotNull(context);
+        final QName qName = QName.create(context.getModules().iterator().next().getNamespace(),
+                context.getModules().iterator().next().getRevision(), context.getModules().iterator().next().getName());
+        assertNull(YangSchemaUtils.findTypeDefinition(context, SchemaPath.create(ImmutableList.of(qName), false)));
+        final List qNames = new ArrayList();
+        context.getTypeDefinitions().forEach(typeDefinition -> qNames.add(typeDefinition.getQName()));
+        assertNull(YangSchemaUtils.findTypeDefinition(context, SchemaPath.create(qNames, false)));
+
+        context = mock(SchemaContext.class);
+        final Module container = mock(Module.class);
+        doReturn(null).when(context).findModuleByNamespaceAndRevision(any(), any());
+        assertNull(YangSchemaUtils.findTypeDefinition(context, SchemaPath.create(qNames, false)));
+        doReturn(container).when(context).findModuleByNamespaceAndRevision(any(), any());
+
+        final DataSchemaNode node = mock(DataSchemaNode.class);
+        doReturn(node).when(container).getDataChildByName((QName) any());
+        final TypeDefinition typeDefinition = mock(TypeDefinition.class);
+        doReturn(Q_NAME).when(typeDefinition).getQName();
+        doReturn(ImmutableSet.of(typeDefinition)).when(container).getTypeDefinitions();
+        assertEquals(typeDefinition,
+                YangSchemaUtils.findTypeDefinition(context, SchemaPath.create(ImmutableList.of(Q_NAME), false)));
+
+        final GroupingDefinition grouping = mock(GroupingDefinition.class);
+        doReturn(Q_NAME).when(grouping).getQName();
+        doReturn(ImmutableSet.of(grouping)).when(container).getGroupings();
+        doReturn(ImmutableSet.of(typeDefinition)).when(grouping).getTypeDefinitions();
+        assertEquals(typeDefinition,
+                YangSchemaUtils.findTypeDefinition(context, SchemaPath.create(ImmutableList.of(Q_NAME, Q_NAME), false)));
+
+        final DataNodeContainer dataNode =
+                mock(DataNodeContainer.class, withSettings().extraInterfaces(DataSchemaNode.class));
+        doReturn(dataNode).when(container).getDataChildByName((QName) any());
+        doReturn(ImmutableSet.of(typeDefinition)).when(dataNode).getTypeDefinitions();
+        assertEquals(typeDefinition,
+                YangSchemaUtils.findTypeDefinition(context, SchemaPath.create(ImmutableList.of(Q_NAME, Q_NAME), false)));
+
+        final ChoiceSchemaNode choiceNode =
+                mock(ChoiceSchemaNode.class, withSettings().extraInterfaces(DataSchemaNode.class));
+        doReturn(choiceNode).when(container).getDataChildByName((QName) any());
+        final ChoiceCaseNode caseNode = mock(ChoiceCaseNode.class);
+        doReturn(caseNode).when(choiceNode).getCaseNodeByName((QName) any());
+        doReturn(ImmutableSet.of(typeDefinition)).when(caseNode).getTypeDefinitions();
+        assertEquals(typeDefinition,
+                YangSchemaUtils.findTypeDefinition(context,
+                        SchemaPath.create(ImmutableList.of(Q_NAME, Q_NAME, Q_NAME), false)));
+    }
+
+    @Test(expected = UnsupportedOperationException.class)
+    public void privateConstructTest() throws Throwable {
+        final Constructor constructor = YangSchemaUtils.class.getDeclaredConstructor();
+        constructor.setAccessible(true);
+        try {
+            constructor.newInstance();
+        } catch (Exception e) {
+            throw e.getCause();
+        }
+    }
+}
\ No newline at end of file
index 2c56181200cc855d08240fa5f49e8428a18e09f6..16fa6d2b70c8aa27bf710937a4234af331dddcc4 100644 (file)
@@ -18,7 +18,7 @@ import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
  * Test Model Provider designated to load test resources and provide Schema Context
  * for testing of TypeProviderImpl
  */
-final class TypeProviderModel {
+public final class TypeProviderModel {
 
     public static final String TEST_TYPE_PROVIDER_MODULE_NAME = "test-type-provider";