$YangModuleInfoImpl not generated for only extensions model
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / CompilationTest.java
index 39687816dab5c65b056d145968fd8faa1b533aac..753ab98bdc06dc6643320659e2aefb3ba6ecaae4 100644 (file)
@@ -33,10 +33,10 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.stream.Collectors;
-import javax.annotation.Nullable;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.binding.ChildOf;
 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
+import org.opendaylight.yangtools.yang.common.Empty;
 
 /**
  * Test correct code generation.
@@ -90,7 +90,7 @@ public class CompilationTest extends BaseCompilationTest {
         assertTrue(nodeList.exists());
         assertTrue(nodeListBuilder.exists());
         assertTrue(nodesType.exists());
-        CompilationTestUtils.assertFilesCount(parent, 7);
+        CompilationTestUtils.assertFilesCount(parent, 8);
 
         // Test if sources are compilable
         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
@@ -107,13 +107,12 @@ public class CompilationTest extends BaseCompilationTest {
         assertTrue(keyArgsClass.isInterface());
         CompilationTestUtils.assertContainsMethod(keyArgsClass, String.class, "getName");
         CompilationTestUtils.assertContainsMethod(keyArgsClass, Integer.class, "getSize");
-        assertEquals(2, abstractMethods(keyArgsClass).size());
+        assertEquals(3, abstractMethods(keyArgsClass).size());
 
         // Test generated 'list links'
         assertTrue(linksClass.isInterface());
         CompilationTestUtils.assertImplementsIfc(linksClass, keyArgsClass);
-        // TODO: anyxml
-        assertEquals(6, abstractMethods(linksClass).size());
+        assertEquals(7, abstractMethods(linksClass).size());
 
         // Test list key constructor arguments ordering
         CompilationTestUtils.assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
@@ -392,7 +391,7 @@ public class CompilationTest extends BaseCompilationTest {
         CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
         CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "isIdBoolean");
         CompilationTestUtils.assertContainsMethod(nodesClass, BigDecimal.class, "getIdDecimal64");
-        CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "isIdEmpty");
+        CompilationTestUtils.assertContainsMethod(nodesClass, Empty.class, "getIdEmpty");
         CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
         testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
         testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
@@ -410,20 +409,22 @@ public class CompilationTest extends BaseCompilationTest {
 
         final Object builderObj = builderClass.getDeclaredConstructor().newInstance();
 
-        Method m = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary", b.getClass());
+        Method method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary",
+            b.getClass());
         final List<Range<Integer>> lengthConstraints = new ArrayList<>();
         lengthConstraints.add(Range.closed(1, 10));
         byte[] arg = new byte[] {};
         String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg),
             lengthConstraints);
-        CompilationTestUtils.assertContainsRestrictionCheck(builderObj, m, expectedMsg, arg);
+        CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg);
 
-        m = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64", BigDecimal.class);
+        method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64",
+            BigDecimal.class);
         final List<Range<BigDecimal>> rangeConstraints = new ArrayList<>();
         rangeConstraints.add(Range.closed(new BigDecimal("1.5"), new BigDecimal("5.5")));
         Object arg1 = new BigDecimal("1.4");
         expectedMsg = String.format("Invalid range: %s, expected: %s.", arg1, rangeConstraints);
-        CompilationTestUtils.assertContainsRestrictionCheck(builderObj, m, expectedMsg, arg1);
+        CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg1);
 
         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
     }
@@ -468,7 +469,7 @@ public class CompilationTest extends BaseCompilationTest {
             throw new AssertionError("Method getId() not found", e);
         }
 
-        assertEquals(ImmutableSet.of(RoutingContext.class, Nullable.class), Arrays.stream(getId.getAnnotations())
+        assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
             .map(Annotation::annotationType).collect(Collectors.toSet()));
         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
     }
@@ -511,8 +512,6 @@ public class CompilationTest extends BaseCompilationTest {
 
     /**
      * Test handling nested uses-augmentations.
-     *
-     * @throws Exception
      */
     @Test
     public void testBug1172() throws Exception {
@@ -552,49 +551,48 @@ public class CompilationTest extends BaseCompilationTest {
         assertTrue(new File(parent, "TypedefCurrent.java").exists());
         assertTrue(new File(parent, "TypedefDeprecated.java").exists());
 
-        final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
-        final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
-        final Class<?> cls = loader.loadClass(pkg + ".FooData");
-        final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
-        final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
-        final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
-        final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
-        final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
-        final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
-        final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
-        final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
-        assertTrue(clsTypedefDepr.getAnnotations()[0].toString().contains("Deprecated"));
-        assertTrue(clsTypedefCur.getAnnotations().length == 0);
-        assertTrue(clsGroupingDepr.getAnnotations()[0].toString().contains("Deprecated"));
-        assertTrue(clsGroupingCur.getAnnotations().length == 0);
-        assertTrue(clsTypeDef1.getAnnotations().length == 0);
-        assertTrue(clsTypeDef3.getAnnotations().length == 0);
-        assertTrue(clsTypeDef2.getAnnotations()[0].toString().contains("Deprecated"));
-
-        /*methods inside container*/
-        assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
-        assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
-        assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
-        assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
-        assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
-        assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
-
-        /*methods inside module*/
-        assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
-        assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
-        assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
-        assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
-        assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
-        assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
-        assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
+        try (URLClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() })) {
+            final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
+            final Class<?> cls = loader.loadClass(pkg + ".FooData");
+            final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
+            final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
+            final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
+            final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
+            final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
+            final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
+            final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
+            final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
+            assertTrue(clsTypedefDepr.getAnnotations()[0].toString().contains("Deprecated"));
+            assertTrue(clsTypedefCur.getAnnotations().length == 0);
+            assertTrue(clsGroupingDepr.getAnnotations()[0].toString().contains("Deprecated"));
+            assertTrue(clsGroupingCur.getAnnotations().length == 0);
+            assertTrue(clsTypeDef1.getAnnotations().length == 0);
+            assertTrue(clsTypeDef3.getAnnotations().length == 0);
+            assertTrue(clsTypeDef2.getAnnotations()[0].toString().contains("Deprecated"));
+
+            /*methods inside container*/
+            assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
+            assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
+            assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
+            assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
+            assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
+            assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
+
+            /*methods inside module*/
+            assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
+            assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
+            assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
+            assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
+            assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
+            assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
+            assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
+        }
 
         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
     }
 
     /**
      * Test if class generated for node from grouping implements ChildOf.
-     *
-     * @throws Exception
      */
     @Test
     public void testBug1377() throws Exception {
@@ -638,6 +636,15 @@ public class CompilationTest extends BaseCompilationTest {
         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
     }
 
+    @Test
+    public void testMdsal395() throws Exception {
+        final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal395");
+        final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal395");
+        generateTestSources("/compilation/mdsal395", sourcesOutputDir);
+        CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
+        CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
     @Test
     public void classNamesColisionTest() throws Exception {
         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("class-name-collision");
@@ -665,6 +672,33 @@ public class CompilationTest extends BaseCompilationTest {
         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
     }
 
+    @Test
+    public void testMdsal425() throws Exception {
+        final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal425");
+        final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal425");
+        generateTestSources("/compilation/mdsal425", sourcesOutputDir);
+        CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
+        CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+    @Test
+    public void testMdsal529() throws Exception {
+        final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal529");
+        final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal529");
+        generateTestSources("/compilation/mdsal529", sourcesOutputDir);
+        CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
+        CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+    @Test
+    public void testMdsal589() throws Exception {
+        final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal589");
+        final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal589");
+        generateTestSources("/compilation/mdsal589", sourcesOutputDir);
+        CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
+        CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
     private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
             final String returnTypeStr) throws NoSuchMethodException {
         Method method = clazz.getMethod(methodName);
@@ -682,13 +716,11 @@ public class CompilationTest extends BaseCompilationTest {
 
     private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz,
             final String methodName) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
-        Method method;
-        Class<?> rawReturnType;
-        java.lang.reflect.Type returnType;
-        method = clazz.getMethod(methodName);
-        rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true, loader);
+        final Method method = clazz.getMethod(methodName);
+        final Class<?> rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true,
+            loader);
         assertEquals(rawReturnType, method.getReturnType());
-        returnType = method.getGenericReturnType();
+        final java.lang.reflect.Type returnType = method.getGenericReturnType();
         assertTrue(returnType instanceof ParameterizedType);
         final ParameterizedType pt = (ParameterizedType) returnType;
         final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();