2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Optional;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
28 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
36 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 public class ListConstraintsValidation {
45 private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
46 private static final QName MASTER_CONTAINER_QNAME = QName.create(
47 "urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
49 private static final QName MIN_MAX_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-list");
50 private static final QName MIN_MAX_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-key-leaf");
51 private static final QName UNBOUNDED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-list");
52 private static final QName UNBOUNDED_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-key-leaf");
53 private static final QName MIN_MAX_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-leaf-list");
54 private static final QName UNBOUNDED_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-leaf-list");
55 private static final QName UNKEYED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-list");
56 private static final QName UNKEYED_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-leaf");
58 private static final YangInstanceIdentifier MASTER_CONTAINER_PATH = YangInstanceIdentifier
59 .of(MASTER_CONTAINER_QNAME);
60 private static final YangInstanceIdentifier MIN_MAX_LIST_PATH = YangInstanceIdentifier
61 .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LIST_QNAME).build();
62 private static final YangInstanceIdentifier UNBOUNDED_LIST_PATH = YangInstanceIdentifier
63 .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LIST_QNAME).build();
64 private static final YangInstanceIdentifier MIN_MAX_LEAF_LIST_PATH = YangInstanceIdentifier
65 .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LEAF_LIST_QNAME).build();
66 private static final YangInstanceIdentifier UNBOUNDED_LEAF_LIST_PATH = YangInstanceIdentifier
67 .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LEAF_LIST_QNAME).build();
68 private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier
69 .builder(MASTER_CONTAINER_PATH).node(UNKEYED_LIST_QNAME).build();
71 private SchemaContext schemaContext;
72 private DataTree inMemoryDataTree;
75 public void prepare() {
76 schemaContext = createTestContext();
77 assertNotNull("Schema context must not be null.", schemaContext);
78 inMemoryDataTree = InMemoryDataTreeFactory.getInstance().create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
79 inMemoryDataTree.setSchemaContext(schemaContext);
80 final DataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
81 final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
83 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
84 modificationTree.ready();
85 inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
88 public static SchemaContext createTestContext() {
89 return YangParserTestUtils.parseYangResource(CONSTRAINTS_VALIDATION_TEST_YANG);
93 public void minMaxListTestPass() throws DataValidationFailedException {
95 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
96 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
97 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
98 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
99 .withChild(fooEntryNode).build();
100 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
101 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
102 .withChild(barEntryNode).build();
104 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
105 modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
106 modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
107 // TODO: check why write and then merge on list commits only "bar" child
108 modificationTree.ready();
110 inMemoryDataTree.validate(modificationTree);
111 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
112 inMemoryDataTree.commit(prepare);
114 final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
115 final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
116 assertTrue(minMaxListRead.isPresent());
117 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
120 @Test(expected = DataValidationFailedException.class)
121 public void minMaxListFail() throws DataValidationFailedException {
122 DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
124 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
125 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
126 final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
127 final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
128 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
129 .withChild(fooEntryNode).build();
131 final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
132 final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
133 final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
135 modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
136 modificationTree.merge(barPath, barEntryNode);
137 modificationTree.write(gooPath, gooEntryNode);
138 modificationTree.delete(gooPath);
139 modificationTree.ready();
141 inMemoryDataTree.validate(modificationTree);
142 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
143 inMemoryDataTree.commit(prepare1);
145 DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
146 Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
147 assertTrue(minMaxListRead.isPresent());
148 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
150 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
151 modificationTree.write(gooPath, gooEntryNode);
152 modificationTree.ready();
154 inMemoryDataTree.validate(modificationTree);
155 prepare1 = inMemoryDataTree.prepare(modificationTree);
156 inMemoryDataTree.commit(prepare1);
158 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
159 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
160 assertTrue(minMaxListRead.isPresent());
161 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
163 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
165 modificationTree.delete(gooPath);
166 modificationTree.delete(fooPath);
167 modificationTree.ready();
169 inMemoryDataTree.validate(modificationTree);
173 public void minMaxLeafListPass() throws DataValidationFailedException {
174 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
176 final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
177 final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
179 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
180 .withNodeIdentifier(barPath)
181 .withValue("bar").build();
182 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
183 .withNodeIdentifier(gooPath)
184 .withValue("goo").build();
186 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
187 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
188 .withChildValue("foo").build();
190 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
191 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
192 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
193 modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
194 modificationTree.ready();
196 inMemoryDataTree.validate(modificationTree);
197 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
198 inMemoryDataTree.commit(prepare1);
200 final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
201 final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
202 assertTrue(masterContainer.isPresent());
203 final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get())
204 .getChild(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
205 assertTrue(leafList.isPresent());
206 assertTrue(leafList.get().getValue().size() == 2);
209 @Test(expected = DataValidationFailedException.class)
210 public void minMaxLeafListFail() throws DataValidationFailedException {
211 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
213 final NodeWithValue<Object> fooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "foo");
214 final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
215 final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
216 final NodeWithValue<Object> fuuPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "fuu");
218 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
219 .withNodeIdentifier(barPath)
220 .withValue("bar").build();
221 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
222 .withNodeIdentifier(gooPath)
223 .withValue("goo").build();
224 final LeafSetEntryNode<Object> fuuLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
225 .withNodeIdentifier(fuuPath)
226 .withValue("fuu").build();
228 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
229 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
230 .withChildValue("foo").build();
232 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
233 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
234 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
235 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry);
236 modificationTree.ready();
238 inMemoryDataTree.validate(modificationTree);
242 public void unkeyedListTestPass() throws DataValidationFailedException {
243 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
245 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
246 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
247 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
248 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
249 unkeyedEntries.add(foo);
250 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
251 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
252 .withValue(unkeyedEntries).build();
254 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
255 modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
256 modificationTree.ready();
258 inMemoryDataTree.validate(modificationTree);
259 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
260 inMemoryDataTree.commit(prepare1);
262 final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
263 final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
264 assertTrue(unkeyedListRead.isPresent());
265 assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
268 @Test(expected = DataValidationFailedException.class)
269 public void unkeyedListTestFail() throws DataValidationFailedException {
270 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
272 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
273 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
274 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
275 final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
276 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
277 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
278 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
279 unkeyedEntries.add(foo);
280 unkeyedEntries.add(bar);
281 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
282 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
283 .withValue(unkeyedEntries).build();
285 modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
286 modificationTree.ready();
288 inMemoryDataTree.validate(modificationTree);