Binding2-runtime JUnit code coverage increase 35/60235/4
authorMartin Ciglan <martin.ciglan@pantheon.tech>
Mon, 10 Jul 2017 15:30:20 +0000 (17:30 +0200)
committerMartin Ciglan <martin.ciglan@pantheon.tech>
Mon, 17 Jul 2017 12:26:38 +0000 (12:26 +0000)
- JUnit tests
- code clean-up

Change-Id: Id2dbf490e41a6cd7eddf024208765ac3381325f6
Signed-off-by: Martin Ciglan <martin.ciglan@pantheon.tech>
(cherry picked from commit a5bdef7fdfea23bc27cc7600e3fa34232bc27e71)

binding2/mdsal-binding2-runtime/pom.xml
binding2/mdsal-binding2-runtime/src/main/java/org/opendaylight/mdsal/binding/javav2/runtime/context/util/BindingSchemaContextUtils.java
binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/context/BindingRuntimeContextTest.java [new file with mode: 0644]
binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/context/ModuleInfoBackedContextTest.java [new file with mode: 0644]
binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/context/util/BindingSchemaContextUtilsTest.java [new file with mode: 0644]
binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/javassist/JavassistUtilsTest.java [new file with mode: 0644]
binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/reflection/BindingReflectionsTest.java
binding2/mdsal-binding2-runtime/src/test/resources/yang/test-runtime.yang [new file with mode: 0644]
binding2/mdsal-binding2-test-model/src/main/yang/test-runtime.yang [new file with mode: 0644]

index 8323a6bfce0ca336d5186d5e67b58810c208d1f4..a221ab7ce98e73fac3a61e66152a6c249138186f 100644 (file)
             <artifactId>mockito-core</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>yang-test-util</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.opendaylight.mdsal</groupId>
+            <artifactId>mdsal-binding2-test-model</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <!--
index 3050d60d85166aef4afbd6776467403f4a36f8e3..bbe9d4f8f0d2247f96692bdfb54405c24054fa6e 100644 (file)
@@ -60,7 +60,7 @@ public final class BindingSchemaContextUtils {
         Preconditions.checkArgument(currentArg != null);
         QName currentQName = BindingReflections.findQName(currentArg.getType());
 
-        Optional<DataNodeContainer> currentContainer = Optional.absent();
+        Optional<DataNodeContainer> currentContainer;
         if (BindingReflections.isNotification(currentArg.getType())) {
             currentContainer = findNotification(ctx, currentQName);
         } else if (BindingReflections.isOperationType(currentArg.getType())) {
@@ -101,7 +101,7 @@ public final class BindingSchemaContextUtils {
     private static Optional<DataNodeContainer> findNotification(final SchemaContext ctx, final QName notificationQName) {
         for (final NotificationDefinition notification : ctx.getNotifications()) {
             if (notification.getQName().equals(notificationQName)) {
-                return Optional.<DataNodeContainer> of(notification);
+                return Optional.of(notification);
             }
         }
         return Optional.absent();
@@ -182,9 +182,9 @@ public final class BindingSchemaContextUtils {
         final String actionOutputName =
                 new StringBuilder(operationName).append(BindingMapping.RPC_OUTPUT_SUFFIX).toString();
         if (targetType.equals(actionInputName)) {
-            return Optional.<DataNodeContainer> of(operation.getInput());
+            return Optional.of(operation.getInput());
         } else if (targetType.equals(actionOutputName)) {
-            return Optional.<DataNodeContainer> of(operation.getOutput());
+            return Optional.of(operation.getOutput());
         }
        return Optional.absent();
     }
diff --git a/binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/context/BindingRuntimeContextTest.java b/binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/context/BindingRuntimeContextTest.java
new file mode 100644 (file)
index 0000000..5df2959
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.runtime.context;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.javav2.generator.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class BindingRuntimeContextTest {
+
+    private SchemaContext schemaContext;
+    private BindingRuntimeContext brc;
+    private DataNodeContainer myCont;
+
+    @Before
+    public void setup() throws URISyntaxException, FileNotFoundException, ReactorException {
+        schemaContext = YangParserTestUtils.parseYangSources(
+                 new File(getClass().getResource("/yang/test-runtime.yang").toURI()));
+        myCont = (DataNodeContainer) schemaContext.getChildNodes().iterator().next();
+        brc = BindingRuntimeContext.create(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(), schemaContext);
+    }
+
+    @Test
+    public void basicTest() {
+        assertNotNull(brc.getSchemaContext());
+        assertNotNull(brc.getStrategy());
+        assertNotNull(brc.getChoiceCaseChildren(myCont));
+    }
+}
diff --git a/binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/context/ModuleInfoBackedContextTest.java b/binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/context/ModuleInfoBackedContextTest.java
new file mode 100644 (file)
index 0000000..f2b2005
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.runtime.context;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.javav2.generator.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+
+public class ModuleInfoBackedContextTest {
+
+    private ModuleInfoBackedContext moduleInfoBackedContext;
+
+    @Before
+    public void setup() throws ReactorException, FileNotFoundException, URISyntaxException {
+        moduleInfoBackedContext = ModuleInfoBackedContext.create();
+    }
+
+    @Test
+    public void createTestWithStrategy() {
+        assertNotNull(ModuleInfoBackedContext.create(
+                GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy()));
+    }
+
+    @Test(expected = ClassNotFoundException.class)
+    public void loadClassTest() throws ClassNotFoundException {
+        moduleInfoBackedContext.loadClass("org.opendaylight.mdsal.gen.javav2.test.rev990939.Dummy");
+    }
+}
diff --git a/binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/context/util/BindingSchemaContextUtilsTest.java b/binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/context/util/BindingSchemaContextUtilsTest.java
new file mode 100644 (file)
index 0000000..d87b568
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.runtime.context.util;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.javav2.spec.base.InstanceIdentifier;
+import org.opendaylight.mdsal.gen.javav2.org.test.runtime.rev170710.data.MyCont;
+import org.opendaylight.mdsal.gen.javav2.org.test.runtime.rev170710.data.my_cont.MyChoice;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class BindingSchemaContextUtilsTest {
+
+    private SchemaContext schemaContext;
+    private DataNodeContainer myCont;
+    private ChoiceSchemaNode myChoice;
+
+    private static final InstanceIdentifier<MyCont> MY_CONT_NODE_PATH
+            = InstanceIdentifier.create(MyCont.class);
+
+
+    @Before
+    public void setup() throws URISyntaxException, FileNotFoundException, ReactorException {
+        schemaContext = YangParserTestUtils.parseYangSources(
+                new File(getClass().getResource("/yang/test-runtime.yang").toURI()));
+        myCont = (DataNodeContainer) schemaContext.getChildNodes().iterator().next();
+        myChoice = (ChoiceSchemaNode) myCont.getChildNodes().iterator().next();
+    }
+
+    @Test
+    public void utilTest() {
+        assertNotNull(BindingSchemaContextUtils.findDataNodeContainer(schemaContext, MY_CONT_NODE_PATH));
+        assertNotNull(BindingSchemaContextUtils.findInstantiatedChoice(myCont, MyChoice.class));
+        assertNotNull(BindingSchemaContextUtils.findInstantiatedCase(myChoice, myChoice.getCaseNodeByName("one")));
+    }
+}
diff --git a/binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/javassist/JavassistUtilsTest.java b/binding2/mdsal-binding2-runtime/src/test/java/org/opendaylight/mdsal/binding/javav2/runtime/javassist/JavassistUtilsTest.java
new file mode 100644 (file)
index 0000000..65fc1d7
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.runtime.javassist;
+
+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());
+    }
+}
index 36146d55a4dac89ca0f7641689371456dd587107..ba76dd09414680c09da7af5aec944195be15f654 100644 (file)
@@ -9,12 +9,12 @@ package org.opendaylight.mdsal.binding.javav2.runtime.reflection;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 
 import java.lang.reflect.Constructor;
-import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.Future;
 import org.junit.Test;
@@ -37,7 +37,6 @@ public class BindingReflectionsTest {
                 BindingReflections.getModelRootPackageName("org.opendaylight.mdsal.gen.javav2.test.rev990939"));
         assertEquals("ModuleInfoClassName should be equal to string", "test.$YangModuleInfoImpl",
                 BindingReflections.getModuleInfoClassName("test"));
-        assertEquals("Module info should be empty Set", Collections.EMPTY_SET, BindingReflections.loadModuleInfos());
         assertFalse("Should not be RpcType", BindingReflections.isOperationType(TreeNode.class));
         assertFalse("Should not be AugmentationChild", BindingReflections.isAugmentationChild(TreeNode.class));
         assertTrue("Should be BindingClass", BindingReflections.isBindingClass(TreeNode.class));
@@ -56,6 +55,7 @@ public class BindingReflectionsTest {
                 .resolveOperationOutputClass(TestImplementation.class.getDeclaredMethod("rpcMethodTest2")).isPresent());
 
         assertTrue(BindingReflections.getQName(TestImplementation.class).toString().equals("test"));
+        assertNotNull(BindingReflections.getQNameModule(TestImplementation.class));
     }
 
     @SuppressWarnings("rawtypes")
diff --git a/binding2/mdsal-binding2-runtime/src/test/resources/yang/test-runtime.yang b/binding2/mdsal-binding2-runtime/src/test/resources/yang/test-runtime.yang
new file mode 100644 (file)
index 0000000..36526bd
--- /dev/null
@@ -0,0 +1,22 @@
+module test-runtime {
+   namespace "org.test.runtime";
+   prefix "tst";
+   revision "2017-07-10" {
+   }
+
+   container my-cont {
+    choice my-choice {
+       case one {
+           leaf one {
+               type string;
+           }
+       }
+       case two {
+           leaf two {
+               type string;
+           }
+       }
+
+      }
+   }
+}
\ No newline at end of file
diff --git a/binding2/mdsal-binding2-test-model/src/main/yang/test-runtime.yang b/binding2/mdsal-binding2-test-model/src/main/yang/test-runtime.yang
new file mode 100644 (file)
index 0000000..36526bd
--- /dev/null
@@ -0,0 +1,22 @@
+module test-runtime {
+   namespace "org.test.runtime";
+   prefix "tst";
+   revision "2017-07-10" {
+   }
+
+   container my-cont {
+    choice my-choice {
+       case one {
+           leaf one {
+               type string;
+           }
+       }
+       case two {
+           leaf two {
+               type string;
+           }
+       }
+
+      }
+   }
+}
\ No newline at end of file