Bug 8575 - IllegalArgumentException thrown when uses a grouping which name is not... 95/58495/1
authorJie Han <han.jie@zte.com.cn>
Thu, 1 Jun 2017 02:43:29 +0000 (10:43 +0800)
committerMartin Ciglan <martin.ciglan@pantheon.tech>
Thu, 8 Jun 2017 11:44:47 +0000 (11:44 +0000)
- find target uses grouping which name is not unique
- add JUnit Test and Yang file
Change-Id: Idc0d782a2443ed2cd3339f4f7ed4c712a04a60fe
Signed-off-by: Jie Han <han.jie@zte.com.cn>
(cherry picked from commit e28261b237a2bd06144dc30da814671698c8b48a)

binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AugmentToGenType.java
binding2/mdsal-binding2-generator-impl/src/test/java/org/opendaylight/mdsal/binding/javav2/generator/impl/Bug8575Test.java [new file with mode: 0644]
binding2/mdsal-binding2-generator-impl/src/test/resources/bug-8575/foo.yang [new file with mode: 0644]

index 91a5fa15cfc775a262627595ad740b8e931842f3..1abdb1375829174da8a94287f73c6f15a2524ef5 100644 (file)
@@ -270,10 +270,21 @@ final class AugmentToGenType {
      */
     private static DataSchemaNode findOriginalTargetFromGrouping(final SchemaContext schemaContext, final SchemaPath targetPath,
                                           final UsesNode parentUsesNode) {
-        final SchemaNode targetGrouping = SchemaContextUtil.findNodeInSchemaContext(schemaContext, parentUsesNode
-                .getGroupingPath()
-                .getPathFromRoot());
-        if (!(targetGrouping instanceof GroupingDefinition)) {
+        SchemaNode targetGrouping = null;
+        QName current = parentUsesNode.getGroupingPath().getPathFromRoot().iterator().next();
+        Module module = schemaContext.findModuleByNamespaceAndRevision(current.getNamespace(), current.getRevision());
+        if (module == null) {
+            throw new IllegalArgumentException("Fialed to find module for grouping in: " + parentUsesNode);
+        } else {
+            for (GroupingDefinition group : module.getGroupings()) {
+                if (group.getQName().equals(current)) {
+                    targetGrouping = group;
+                    break;
+                }
+            }
+        }
+
+        if (targetGrouping == null) {
             throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
         }
 
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/java/org/opendaylight/mdsal/binding/javav2/generator/impl/Bug8575Test.java b/binding2/mdsal-binding2-generator-impl/src/test/java/org/opendaylight/mdsal/binding/javav2/generator/impl/Bug8575Test.java
new file mode 100644 (file)
index 0000000..c94f933
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2017 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.mdsal.binding.javav2.generator.impl;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.UsesNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+import java.io.File;
+import java.lang.reflect.Method;
+
+
+public class Bug8575Test {
+    @Test
+    public void bug8575Test() throws Exception {
+        final Class[] parameterTypes = { SchemaContext.class, SchemaPath.class, UsesNode.class };
+        final Method generate =
+                AugmentToGenType.class.getDeclaredMethod("findOriginalTargetFromGrouping", parameterTypes);
+        assertNotNull(generate);
+        generate.setAccessible(true);
+
+        File foo = new File(getClass().getResource(
+                "/bug-8575/foo.yang").toURI());
+
+        SchemaContext context = YangParserTestUtils.parseYangSources(foo);
+
+        final QName groupingQname = QName.create("foo", "2017-05-15", "A");
+        final QName containerQname = QName.create("foo", "2017-05-15", "A1");
+        final SchemaPath groupingPath = SchemaPath.create(true, groupingQname);
+        final SchemaPath targetPath = SchemaPath.create(true, containerQname);
+
+        final UsesNode usesNode = mock(UsesNode.class);
+        when(usesNode.getGroupingPath()).thenReturn(groupingPath);
+
+        final Object[] args = { context, targetPath, usesNode };
+        final DataSchemaNode result = (DataSchemaNode) generate.invoke(AugmentToGenType.class, args);
+        assertNotNull(result);
+        assertTrue(result instanceof ContainerSchemaNode);
+    }
+}
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/bug-8575/foo.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/bug-8575/foo.yang
new file mode 100644 (file)
index 0000000..8e53633
--- /dev/null
@@ -0,0 +1,21 @@
+module foo {
+
+    namespace "foo";
+    prefix foo;
+
+    revision 2017-05-15;
+
+    grouping A{
+      container A1 {
+        leaf leaf-A-A1 {
+            type string;
+        }
+      }
+    }
+
+    container A{
+        leaf leaf-A {
+            type string;
+        }
+    }
+}
\ No newline at end of file