Fix mandatory enforcer failure on augmented nodes
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / YT1276Test.java
index 7e138cc92914b22e60f121d8a37f07b05d832b3d..55223f854e5b0e299269919af672268e6187c115 100644 (file)
@@ -24,6 +24,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
@@ -33,6 +34,14 @@ public class YT1276Test {
     private static final QName BAZ = QName.create(FOO, "baz");
     private static final QName XYZZY_LEAF = QName.create(FOO, "xyzzy-leaf");
     private static final QName XYZZY_AUGMENT = QName.create(FOO, "xyzzy-augment");
+    private static final QName XYZZY_AUGMENT_CONT = QName.create(FOO, "xyzzy-augment-container");
+    private static final QName XYZZY_AUGMENT_CONT_INNER = QName.create(FOO, "xyzzy-augment-container-inner");
+    private static final QName XYZZY_AUGMENT_CONT_LEAF = QName.create(FOO, "xyzzy-augment-container-leaf");
+    private static final QName BAZ_AUG_CASE_MANDAT_LEAF = QName.create(FOO, "augmented-case-mandatory");
+    private static final QName BAZ_AUG_CASE_NON_MANDAT_LEAF = QName.create(FOO, "augmented-case-non-mandatory");
+    private static final QName NESTED_BAZ_CHOICE = QName.create(FOO, "nested-baz");
+    private static final QName NESTED_BAZ_XYZ_CASE_MANDATORY = QName.create(FOO, "nested-xyz-mandatory");
+    private static final QName NESTED_BAZ_XYZ_CASE_NON_MANDATORY = QName.create(FOO, "nested-xyz-non-mandatory");
 
     private static EffectiveModelContext MODEL;
 
@@ -81,7 +90,7 @@ public class YT1276Test {
     }
 
     @Test
-    public void testBarWithXyzzy() throws DataValidationFailedException {
+    public void testBarWithXyzzyWithSubtree() throws DataValidationFailedException {
         applyOperation(mod -> {
             mod.write(YangInstanceIdentifier.of(BAR), Builders.containerBuilder()
                 .withNodeIdentifier(new NodeIdentifier(BAR))
@@ -89,8 +98,75 @@ public class YT1276Test {
                     .withNodeIdentifier(new NodeIdentifier(BAZ))
                     .withChild(ImmutableNodes.leafNode(XYZZY_LEAF, "xyzzy"))
                     .withChild(Builders.augmentationBuilder()
-                        .withNodeIdentifier(new AugmentationIdentifier(Set.of(XYZZY_AUGMENT)))
+                        .withNodeIdentifier(new AugmentationIdentifier(Set.of(XYZZY_AUGMENT, XYZZY_AUGMENT_CONT)))
                         .withChild(ImmutableNodes.leafNode(XYZZY_AUGMENT, "xyzzy"))
+                        .withChild(ImmutableContainerNodeBuilder.create()
+                            .withNodeIdentifier(new NodeIdentifier(XYZZY_AUGMENT_CONT))
+                            .withChild(ImmutableContainerNodeBuilder.create()
+                                .withNodeIdentifier(new NodeIdentifier(XYZZY_AUGMENT_CONT_INNER))
+                                .withChild(ImmutableNodes.leafNode(XYZZY_AUGMENT_CONT_LEAF, "aug-cont-leaf"))
+                                .build())
+                            .build())
+                        .build())
+                    .build())
+                .build());
+        });
+    }
+
+    @Test
+    public void testBazWithAugmentedCaseWithMandatoryLeaf() throws DataValidationFailedException {
+        applyOperation(mod -> {
+            mod.write(YangInstanceIdentifier.of(BAR), Builders.containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(BAR))
+                .withChild(Builders.choiceBuilder()
+                    .withNodeIdentifier(new NodeIdentifier(BAZ))
+                    .withChild(ImmutableNodes.leafNode(BAZ_AUG_CASE_MANDAT_LEAF, "augmentedCaseMandatory"))
+                    .withChild(ImmutableNodes.leafNode(BAZ_AUG_CASE_NON_MANDAT_LEAF, "augmentedCaseNonMandatory"))
+                    .build())
+                .build());
+        });
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testBazWithAugmentedCaseWithoutMandatoryLeaf() throws DataValidationFailedException {
+        applyOperation(mod -> {
+            mod.write(YangInstanceIdentifier.of(BAR), Builders.containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(BAR))
+                .withChild(Builders.choiceBuilder()
+                    .withNodeIdentifier(new NodeIdentifier(BAZ))
+                    .withChild(ImmutableNodes.leafNode(BAZ_AUG_CASE_NON_MANDAT_LEAF, "augmentedCaseNonMandatory"))
+                    .build())
+                .build());
+        });
+    }
+
+    @Test
+    public void testWithAugmentedNestedBazWithMandatoryLeaf() throws DataValidationFailedException {
+        applyOperation(mod -> {
+            mod.write(YangInstanceIdentifier.of(BAR), Builders.containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(BAR))
+                .withChild(Builders.choiceBuilder()
+                    .withNodeIdentifier(new NodeIdentifier(BAZ))
+                    .withChild(Builders.choiceBuilder()
+                        .withNodeIdentifier(new NodeIdentifier(NESTED_BAZ_CHOICE))
+                        .withChild(ImmutableNodes.leafNode(NESTED_BAZ_XYZ_CASE_MANDATORY, "nestedMandatory"))
+                        .withChild(ImmutableNodes.leafNode(NESTED_BAZ_XYZ_CASE_NON_MANDATORY, "nestedNonMandatory"))
+                    .build())
+                    .build())
+                .build());
+        });
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWithAugmentedNestedBazWithhoutMandatoryLeaf() throws DataValidationFailedException {
+        applyOperation(mod -> {
+            mod.write(YangInstanceIdentifier.of(BAR), Builders.containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(BAR))
+                .withChild(Builders.choiceBuilder()
+                    .withNodeIdentifier(new NodeIdentifier(BAZ))
+                    .withChild(Builders.choiceBuilder()
+                        .withNodeIdentifier(new NodeIdentifier(NESTED_BAZ_CHOICE))
+                        .withChild(ImmutableNodes.leafNode(NESTED_BAZ_XYZ_CASE_NON_MANDATORY, "nestedNonMandatory"))
                         .build())
                     .build())
                 .build());
@@ -107,6 +183,13 @@ public class YT1276Test {
                     .withNodeIdentifier(new NodeIdentifier(BAZ))
                     .withChild(ImmutableNodes.leafNode(XYZZY_LEAF, "xyzzy"))
                     .withChild(ImmutableNodes.leafNode(XYZZY_AUGMENT, "xyzzy"))
+                    .withChild(ImmutableContainerNodeBuilder.create()
+                        .withNodeIdentifier(NodeIdentifier.create(XYZZY_AUGMENT_CONT))
+                        .withChild(ImmutableContainerNodeBuilder.create()
+                            .withNodeIdentifier(NodeIdentifier.create(XYZZY_AUGMENT_CONT_INNER))
+                            .withChild(ImmutableNodes.leafNode(XYZZY_AUGMENT_CONT_LEAF, "aug-cont-leaf"))
+                            .build())
+                        .build())
                     .build())
                 .build());
         });
@@ -120,8 +203,15 @@ public class YT1276Test {
                 .withChild(Builders.choiceBuilder()
                     .withNodeIdentifier(new NodeIdentifier(BAZ))
                     .withChild(Builders.augmentationBuilder()
-                        .withNodeIdentifier(new AugmentationIdentifier(Set.of(XYZZY_AUGMENT)))
+                        .withNodeIdentifier(new AugmentationIdentifier(Set.of(XYZZY_AUGMENT, XYZZY_AUGMENT_CONT)))
                         .withChild(ImmutableNodes.leafNode(XYZZY_AUGMENT, "xyzzy"))
+                        .withChild(ImmutableContainerNodeBuilder.create()
+                            .withNodeIdentifier(NodeIdentifier.create(XYZZY_AUGMENT_CONT))
+                            .withChild(ImmutableContainerNodeBuilder.create()
+                                .withNodeIdentifier(NodeIdentifier.create(XYZZY_AUGMENT_CONT_INNER))
+                                .withChild(ImmutableNodes.leafNode(XYZZY_AUGMENT_CONT_LEAF, "aug-cont-leaf"))
+                                .build())
+                            .build())
                         .build())
                     .build())
                 .build());
@@ -142,9 +232,8 @@ public class YT1276Test {
                     .build())
                 .build());
         });
-        assertEquals("Node (foo)baz is missing mandatory descendant "
-            + "/AugmentationIdentifier{childNames=[(foo)xyzzy-augment]}/(foo)xyzzy-augment",
-            ex.getMessage());
+        assertEquals("Node (foo)baz is missing mandatory descendant /AugmentationIdentifier{childNames="
+                + "[(foo)xyzzy-augment, (foo)xyzzy-augment-container]}/(foo)xyzzy-augment", ex.getMessage());
 
     }