X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2Ftree%2FListConstraintsValidation.java;h=b60b3747c8ba6a315036fbf199fce5e39b57ea2f;hb=ec2ef458dbc67b4caad24ab2baab047f53921312;hp=ef99fefa19cac2822a3b22c27ec454fe7e3e35db;hpb=a6ea70c09b13489918c387d54cde8f1095721acc;p=yangtools.git diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ListConstraintsValidation.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ListConstraintsValidation.java index ef99fefa19..b60b3747c8 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ListConstraintsValidation.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ListConstraintsValidation.java @@ -7,13 +7,16 @@ */ package org.opendaylight.yangtools.yang.data.impl.schema.tree; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import com.google.common.base.Optional; import java.util.ArrayList; import java.util.List; +import java.util.Optional; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -27,20 +30,21 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer; import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; -import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; public class ListConstraintsValidation { - private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang"; private static final QName MASTER_CONTAINER_QNAME = QName.create( "urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02", "master-container"); @@ -66,16 +70,25 @@ public class ListConstraintsValidation { private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier .builder(MASTER_CONTAINER_PATH).node(UNKEYED_LIST_QNAME).build(); - private SchemaContext schemaContext; - private InMemoryDataTree inMemoryDataTree; + private static EffectiveModelContext schemaContext; + + private DataTree inMemoryDataTree; + + @BeforeClass + public static void beforeClass() { + schemaContext = YangParserTestUtils.parseYangResource("/list-constraints-validation-test-model.yang"); + } + + @AfterClass + public static void afterClass() { + schemaContext = null; + } @Before - public void prepare() { - schemaContext = createTestContext(); - assertNotNull("Schema context must not be null.", schemaContext); - inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL); - inMemoryDataTree.setSchemaContext(schemaContext); - final InMemoryDataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot(); + public void prepare() throws DataValidationFailedException { + inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, + schemaContext); + final DataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot(); final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification(); modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME)); @@ -83,10 +96,6 @@ public class ListConstraintsValidation { inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree)); } - public static SchemaContext createTestContext() { - return YangParserTestUtils.parseYangResource(CONSTRAINTS_VALIDATION_TEST_YANG); - } - @Test public void minMaxListTestPass() throws DataValidationFailedException { @@ -99,25 +108,24 @@ public class ListConstraintsValidation { .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME)) .withChild(barEntryNode).build(); - final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification(); + final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification(); modificationTree.write(MIN_MAX_LIST_PATH, mapNode1); modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2); - // TODO: check why write and then merge on list commits only "bar" child modificationTree.ready(); inMemoryDataTree.validate(modificationTree); final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree); inMemoryDataTree.commit(prepare); - final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot(); + final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot(); final Optional> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH); assertTrue(minMaxListRead.isPresent()); - assertTrue(((NormalizedNodeContainer) minMaxListRead.get()).getValue().size() == 2); + assertEquals(2, ((NormalizedNodeContainer) minMaxListRead.get()).size()); } @Test(expected = DataValidationFailedException.class) public void minMaxListFail() throws DataValidationFailedException { - InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification(); + DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification(); final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo"); final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar"); @@ -140,10 +148,10 @@ public class ListConstraintsValidation { DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree); inMemoryDataTree.commit(prepare1); - InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot(); + DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot(); Optional> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH); assertTrue(minMaxListRead.isPresent()); - assertTrue(((NormalizedNodeContainer) minMaxListRead.get()).getValue().size() == 2); + assertEquals(2, ((NormalizedNodeContainer) minMaxListRead.get()).size()); modificationTree = inMemoryDataTree.takeSnapshot().newModification(); modificationTree.write(gooPath, gooEntryNode); @@ -156,7 +164,7 @@ public class ListConstraintsValidation { snapshotAfterCommit = inMemoryDataTree.takeSnapshot(); minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH); assertTrue(minMaxListRead.isPresent()); - assertTrue(((NormalizedNodeContainer) minMaxListRead.get()).getValue().size() == 3); + assertEquals(3, ((NormalizedNodeContainer) minMaxListRead.get()).size()); modificationTree = inMemoryDataTree.takeSnapshot().newModification(); @@ -195,20 +203,19 @@ public class ListConstraintsValidation { final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree); inMemoryDataTree.commit(prepare1); - final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot(); + final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot(); final Optional> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH); assertTrue(masterContainer.isPresent()); final Optional> leafList = ((NormalizedNodeContainer) masterContainer.get()) .getChild(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME)); assertTrue(leafList.isPresent()); - assertTrue(leafList.get().getValue().size() == 2); + assertEquals(2, leafList.get().size()); } - @Test(expected = DataValidationFailedException.class) - public void minMaxLeafListFail() throws DataValidationFailedException { + @Test + public void minMaxLeafListFail() { final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification(); - final NodeWithValue fooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "foo"); final NodeWithValue barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar"); final NodeWithValue gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo"); final NodeWithValue fuuPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "fuu"); @@ -231,9 +238,10 @@ public class ListConstraintsValidation { modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry); modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry); modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry); - modificationTree.ready(); - inMemoryDataTree.validate(modificationTree); + assertEquals("(urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?" + + "revision=2015-02-02)min-max-leaf-list has too many elements (4), can have at most 3", + assertThrows(MinMaxElementsValidationFailedException.class, () -> modificationTree.ready()).getMessage()); } @Test @@ -257,14 +265,14 @@ public class ListConstraintsValidation { final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree); inMemoryDataTree.commit(prepare1); - final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot(); + final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot(); final Optional> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH); assertTrue(unkeyedListRead.isPresent()); assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1); } - @Test(expected = DataValidationFailedException.class) - public void unkeyedListTestFail() throws DataValidationFailedException { + @Test + public void unkeyedListTestFail() { final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification(); final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create() @@ -281,8 +289,8 @@ public class ListConstraintsValidation { .withValue(unkeyedEntries).build(); modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode); - modificationTree.ready(); - - inMemoryDataTree.validate(modificationTree); + assertEquals("(urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?" + + "revision=2015-02-02)unkeyed-list has too many elements (2), can have at most 1", + assertThrows(MinMaxElementsValidationFailedException.class, () -> modificationTree.ready()).getMessage()); } }