X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=data%2Fyang-data-tree-ri%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Ftree%2Fimpl%2FYT776Test.java;h=5784e28da4f88a9d960124696e8f5d9ea5c9fa3b;hb=HEAD;hp=375de0fce77a89d413f9111e0941544e90a029f4;hpb=b97e568159de1d0cb724d8b2a4e7827a45afad40;p=yangtools.git diff --git a/data/yang-data-tree-ri/src/test/java/org/opendaylight/yangtools/yang/data/tree/impl/YT776Test.java b/data/yang-data-tree-ri/src/test/java/org/opendaylight/yangtools/yang/data/tree/impl/YT776Test.java index 375de0fce7..4c6e152c57 100644 --- a/data/yang-data-tree-ri/src/test/java/org/opendaylight/yangtools/yang/data/tree/impl/YT776Test.java +++ b/data/yang-data-tree-ri/src/test/java/org/opendaylight/yangtools/yang/data/tree/impl/YT776Test.java @@ -7,36 +7,29 @@ */ package org.opendaylight.yangtools.yang.data.tree.impl; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; -import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.choiceBuilder; -import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.containerBuilder; -import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.leafBuilder; -import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.leafSetBuilder; -import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.mapBuilder; -import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.mapEntryBuilder; -import static org.opendaylight.yangtools.yang.data.tree.impl.ListConstraintsValidation.assertTooFewElements; -import static org.opendaylight.yangtools.yang.data.tree.impl.ListConstraintsValidation.assertTooManyElements; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import com.google.common.collect.ImmutableMap; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; +import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes; import org.opendaylight.yangtools.yang.data.tree.api.DataTree; import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration; import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification; -import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException; import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; -public class YT776Test { +class YT776Test { private static final QName MODULE = QName.create("yt776", "yt776"); private static final NodeIdentifier BOX = new NodeIdentifier(QName.create(MODULE, "box")); private static final QName OBJECT = QName.create(MODULE, "object"); @@ -44,8 +37,7 @@ public class YT776Test { private static final NodeIdentifier OBJECT_LIST = new NodeIdentifier(OBJECT); private static final NodeIdentifierWithPredicates OBJECT_ITEM = NodeIdentifierWithPredicates.of(OBJECT, ImmutableMap.of(OBJECT_ID, "1")); - private static final LeafNode OBJECT_ID_LEAF = leafBuilder().withNodeIdentifier(new NodeIdentifier(OBJECT_ID)) - .withValue("1").build(); + private static final LeafNode OBJECT_ID_LEAF = ImmutableNodes.leafNode(OBJECT_ID, "1"); private static final NodeIdentifier ATTRIBUTES = new NodeIdentifier(QName.create(MODULE, "attributes")); private static final QName NESTED = QName.create(MODULE, "nested"); @@ -65,33 +57,78 @@ public class YT776Test { private DataTree dataTree; - @BeforeClass - public static void beforeClass() { - SCHEMA_CONTEXT = TestModel.createTestContext("/yt776.yang"); + @BeforeAll + static void beforeClass() { + SCHEMA_CONTEXT = YangParserTestUtils.parseYang(""" + module yt776 { + namespace yt776; + prefix yt776; + + container box { + list object { + key object-id; + + leaf object-id { + type string; + } + + leaf-list attributes { + type string; + min-elements 1; + max-elements 2; + } + + list nested { + key nested-attribute; + max-elements 1; + leaf nested-attribute { + type string; + } + } + } + + choice any-of { + leaf some-leaf { + type string; + } + list some-list { + key some-leaf; + min-elements 1; + + leaf some-leaf { + type string; + } + } + } + } + }"""); } - @AfterClass - public static void afterClass() { + @AfterAll + static void afterClass() { SCHEMA_CONTEXT = null; } - @Before - public void init() { + @BeforeEach + void init() { dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT); } @Test - public void testNoAttributes() { - final DataTreeModification mod = dataTree.takeSnapshot().newModification(); - mod.write(YangInstanceIdentifier.of(BOX), containerBuilder().withNodeIdentifier(BOX) - .withChild(mapBuilder().withNodeIdentifier(OBJECT_LIST) - .addChild(mapEntryBuilder().withNodeIdentifier(OBJECT_ITEM) + void testNoAttributes() { + final var mod = dataTree.takeSnapshot().newModification(); + mod.write(YangInstanceIdentifier.of(BOX), ImmutableNodes.newContainerBuilder() + .withNodeIdentifier(BOX) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(OBJECT_LIST) + .addChild(ImmutableNodes.newMapEntryBuilder() + .withNodeIdentifier(OBJECT_ITEM) .withChild(OBJECT_ID_LEAF) .build()) .build()) .build()); - final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, mod::ready); + final var ex = assertThrows(IllegalArgumentException.class, mod::ready); // FIXME: This is actually mandatory leaf enforcer kicking in: attributes have to be present. This is // most probably not what we want. assertEquals("Node (yt776)object[{(yt776)object-id=1}] is missing mandatory descendant /(yt776)attributes", @@ -99,28 +136,35 @@ public class YT776Test { } @Test - public void testEmptyAttributes() throws DataValidationFailedException { - final DataTreeModification mod = write(containerBuilder().withNodeIdentifier(BOX) - .withChild(mapBuilder().withNodeIdentifier(OBJECT_LIST) - .addChild(mapEntryBuilder().withNodeIdentifier(OBJECT_ITEM) + void testEmptyAttributes() { + final var mod = write(ImmutableNodes.newContainerBuilder() + .withNodeIdentifier(BOX) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(OBJECT_LIST) + .addChild(ImmutableNodes.newMapEntryBuilder() + .withNodeIdentifier(OBJECT_ITEM) .withChild(OBJECT_ID_LEAF) - .withChild(leafSetBuilder().withNodeIdentifier(ATTRIBUTES).build()) + .withChild(ImmutableNodes.newSystemLeafSetBuilder().withNodeIdentifier(ATTRIBUTES).build()) .build()) .build()) .build()); - final IllegalArgumentException ex = assertThrows(MinMaxElementsValidationFailedException.class, mod::ready); + final var ex = assertThrows(MinMaxElementsValidationFailedException.class, mod::ready); assertEquals("(yt776)attributes does not have enough elements (0), needs at least 1", ex.getMessage()); - assertTooFewElements(ex); + ListConstraintsValidation.assertTooFewElements(ex); } @Test - public void testOneAttribute() throws DataValidationFailedException { - writeAndCommit(containerBuilder().withNodeIdentifier(BOX) - .withChild(mapBuilder().withNodeIdentifier(OBJECT_LIST) - .addChild(mapEntryBuilder().withNodeIdentifier(OBJECT_ITEM) + void testOneAttribute() throws Exception { + writeAndCommit(ImmutableNodes.newContainerBuilder() + .withNodeIdentifier(BOX) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(OBJECT_LIST) + .addChild(ImmutableNodes.newMapEntryBuilder() + .withNodeIdentifier(OBJECT_ITEM) .withChild(OBJECT_ID_LEAF) - .withChild(leafSetBuilder().withNodeIdentifier(ATTRIBUTES) + .withChild(ImmutableNodes.newSystemLeafSetBuilder() + .withNodeIdentifier(ATTRIBUTES) .withChildValue("object1") .build()) .build()) @@ -129,12 +173,16 @@ public class YT776Test { } @Test - public void testTwoAttributes() throws DataValidationFailedException { - writeAndCommit(containerBuilder().withNodeIdentifier(BOX) - .withChild(mapBuilder().withNodeIdentifier(OBJECT_LIST) - .addChild(mapEntryBuilder().withNodeIdentifier(OBJECT_ITEM) + void testTwoAttributes() throws Exception { + writeAndCommit(ImmutableNodes.newContainerBuilder() + .withNodeIdentifier(BOX) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(OBJECT_LIST) + .addChild(ImmutableNodes.newMapEntryBuilder() + .withNodeIdentifier(OBJECT_ITEM) .withChild(OBJECT_ID_LEAF) - .withChild(leafSetBuilder().withNodeIdentifier(ATTRIBUTES) + .withChild(ImmutableNodes.newSystemLeafSetBuilder() + .withNodeIdentifier(ATTRIBUTES) .withChildValue("object1") .withChildValue("object2") .build()) @@ -144,12 +192,16 @@ public class YT776Test { } @Test - public void testThreeAttributes() throws DataValidationFailedException { - final DataTreeModification mod = write(containerBuilder().withNodeIdentifier(BOX) - .withChild(mapBuilder().withNodeIdentifier(OBJECT_LIST) - .addChild(mapEntryBuilder().withNodeIdentifier(OBJECT_ITEM) + void testThreeAttributes() { + final var mod = write(ImmutableNodes.newContainerBuilder() + .withNodeIdentifier(BOX) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(OBJECT_LIST) + .addChild(ImmutableNodes.newMapEntryBuilder() + .withNodeIdentifier(OBJECT_ITEM) .withChild(OBJECT_ID_LEAF) - .withChild(leafSetBuilder().withNodeIdentifier(ATTRIBUTES) + .withChild(ImmutableNodes.newSystemLeafSetBuilder() + .withNodeIdentifier(ATTRIBUTES) .withChildValue("object1") .withChildValue("object2") .withChildValue("object3") @@ -158,26 +210,33 @@ public class YT776Test { .build()) .build()); - final IllegalArgumentException ex = assertThrows(MinMaxElementsValidationFailedException.class, mod::ready); + final var ex = assertThrows(MinMaxElementsValidationFailedException.class, mod::ready); assertEquals("(yt776)attributes has too many elements (3), can have at most 2", ex.getMessage()); - assertTooManyElements(ex); + ListConstraintsValidation.assertTooManyElements(ex); } @Test - public void testEmptyAndMergeOne() throws DataValidationFailedException { - final DataTreeModification mod = dataTree.takeSnapshot().newModification(); - mod.write(YangInstanceIdentifier.of(BOX), containerBuilder().withNodeIdentifier(BOX) - .withChild(mapBuilder().withNodeIdentifier(OBJECT_LIST) - .addChild(mapEntryBuilder().withNodeIdentifier(OBJECT_ITEM) + void testEmptyAndMergeOne() throws Exception { + final var mod = dataTree.takeSnapshot().newModification(); + mod.write(YangInstanceIdentifier.of(BOX), ImmutableNodes.newContainerBuilder() + .withNodeIdentifier(BOX) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(OBJECT_LIST) + .addChild(ImmutableNodes.newMapEntryBuilder() + .withNodeIdentifier(OBJECT_ITEM) .withChild(OBJECT_ID_LEAF) .build()) .build()) .build()); - mod.merge(YangInstanceIdentifier.of(BOX), containerBuilder().withNodeIdentifier(BOX) - .withChild(mapBuilder().withNodeIdentifier(OBJECT_LIST) - .addChild(mapEntryBuilder().withNodeIdentifier(OBJECT_ITEM) + mod.merge(YangInstanceIdentifier.of(BOX), ImmutableNodes.newContainerBuilder() + .withNodeIdentifier(BOX) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(OBJECT_LIST) + .addChild(ImmutableNodes.newMapEntryBuilder() + .withNodeIdentifier(OBJECT_ITEM) .withChild(OBJECT_ID_LEAF) - .withChild(leafSetBuilder().withNodeIdentifier(ATTRIBUTES) + .withChild(ImmutableNodes.newSystemLeafSetBuilder() + .withNodeIdentifier(ATTRIBUTES) .withChildValue("object1") .build()) .build()) @@ -188,20 +247,27 @@ public class YT776Test { } @Test - public void testEmptyAndMergeOneWithListTouched() throws DataValidationFailedException { - final DataTreeModification mod = dataTree.takeSnapshot().newModification(); - mod.write(YangInstanceIdentifier.of(BOX), containerBuilder().withNodeIdentifier(BOX) - .withChild(mapBuilder().withNodeIdentifier(OBJECT_LIST) - .addChild(mapEntryBuilder().withNodeIdentifier(OBJECT_ITEM) + void testEmptyAndMergeOneWithListTouched() throws Exception { + final var mod = dataTree.takeSnapshot().newModification(); + mod.write(YangInstanceIdentifier.of(BOX), ImmutableNodes.newContainerBuilder() + .withNodeIdentifier(BOX) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(OBJECT_LIST) + .addChild(ImmutableNodes.newMapEntryBuilder() + .withNodeIdentifier(OBJECT_ITEM) .withChild(OBJECT_ID_LEAF) .build()) .build()) .build()); - mod.merge(YangInstanceIdentifier.of(BOX), containerBuilder().withNodeIdentifier(BOX) - .withChild(mapBuilder().withNodeIdentifier(OBJECT_LIST) - .addChild(mapEntryBuilder().withNodeIdentifier(OBJECT_ITEM) + mod.merge(YangInstanceIdentifier.of(BOX), ImmutableNodes.newContainerBuilder() + .withNodeIdentifier(BOX) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(OBJECT_LIST) + .addChild(ImmutableNodes.newMapEntryBuilder() + .withNodeIdentifier(OBJECT_ITEM) .withChild(OBJECT_ID_LEAF) - .withChild(leafSetBuilder().withNodeIdentifier(ATTRIBUTES) + .withChild(ImmutableNodes.newSystemLeafSetBuilder() + .withNodeIdentifier(ATTRIBUTES) .withChildValue("object1") .build()) .build()) @@ -214,16 +280,18 @@ public class YT776Test { } @Test - public void testDisappearInChoice() throws DataValidationFailedException { - DataTreeModification mod = dataTree.takeSnapshot().newModification(); + void testDisappearInChoice() throws Exception { + var mod = dataTree.takeSnapshot().newModification(); // Initialize choice with list - mod.write(YangInstanceIdentifier.of(BOX), containerBuilder() + mod.write(YangInstanceIdentifier.of(BOX), ImmutableNodes.newContainerBuilder() .withNodeIdentifier(BOX) - .withChild(choiceBuilder().withNodeIdentifier(ANY_OF) - .withChild(mapBuilder().withNodeIdentifier(SOME_LIST_ID) - .withChild(mapEntryBuilder() + .withChild(ImmutableNodes.newChoiceBuilder() + .withNodeIdentifier(ANY_OF) + .withChild(ImmutableNodes.newSystemMapBuilder() + .withNodeIdentifier(SOME_LIST_ID) + .withChild(ImmutableNodes.newMapEntryBuilder() .withNodeIdentifier(SOME_LIST_ITEM) - .withChild(leafBuilder().withNodeIdentifier(SOME_LEAF_ID).withValue("foo").build()) + .withChild(ImmutableNodes.leafNode(SOME_LEAF_ID, "foo")) .build()) .build()) .build()) @@ -233,25 +301,24 @@ public class YT776Test { // Now delete the single item, causing the list to fizzle, while creating the alterinative case mod = dataTree.takeSnapshot().newModification(); mod.delete(YangInstanceIdentifier.of(BOX, ANY_OF, SOME_LIST_ID, SOME_LIST_ITEM)); - mod.write(YangInstanceIdentifier.of(BOX, ANY_OF, SOME_LEAF_ID), - leafBuilder().withNodeIdentifier(SOME_LEAF_ID).withValue("foo").build()); + mod.write(YangInstanceIdentifier.of(BOX, ANY_OF, SOME_LEAF_ID), ImmutableNodes.leafNode(SOME_LEAF_ID, "foo")); commit(mod); } - private DataTreeModification write(final ContainerNode data) throws DataValidationFailedException { - final DataTreeModification mod = dataTree.takeSnapshot().newModification(); + private DataTreeModification write(final ContainerNode data) { + final var mod = dataTree.takeSnapshot().newModification(); mod.write(YangInstanceIdentifier.of(BOX), data); return mod; } - private void writeAndCommit(final ContainerNode data) throws DataValidationFailedException { - final DataTreeModification mod = dataTree.takeSnapshot().newModification(); + private void writeAndCommit(final ContainerNode data) throws Exception { + final var mod = dataTree.takeSnapshot().newModification(); mod.write(YangInstanceIdentifier.of(BOX), data); commit(mod); } - private void commit(final DataTreeModification mod) throws DataValidationFailedException { + private void commit(final DataTreeModification mod) throws Exception { mod.ready(); dataTree.validate(mod); dataTree.commit(dataTree.prepare(mod));