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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
30 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
46 public class ListConstraintsValidation {
47 private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
48 private static final QName MASTER_CONTAINER_QNAME = QName.create(
49 "urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
51 private static final QName MIN_MAX_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-list");
52 private static final QName MIN_MAX_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-key-leaf");
53 private static final QName UNBOUNDED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-list");
54 private static final QName UNBOUNDED_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-key-leaf");
55 private static final QName MIN_MAX_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-leaf-list");
56 private static final QName UNBOUNDED_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-leaf-list");
57 private static final QName UNKEYED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-list");
58 private static final QName UNKEYED_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-leaf");
60 private static final YangInstanceIdentifier MASTER_CONTAINER_PATH = YangInstanceIdentifier
61 .of(MASTER_CONTAINER_QNAME);
62 private static final YangInstanceIdentifier MIN_MAX_LIST_PATH = YangInstanceIdentifier
63 .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LIST_QNAME).build();
64 private static final YangInstanceIdentifier UNBOUNDED_LIST_PATH = YangInstanceIdentifier
65 .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LIST_QNAME).build();
66 private static final YangInstanceIdentifier MIN_MAX_LEAF_LIST_PATH = YangInstanceIdentifier
67 .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LEAF_LIST_QNAME).build();
68 private static final YangInstanceIdentifier UNBOUNDED_LEAF_LIST_PATH = YangInstanceIdentifier
69 .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LEAF_LIST_QNAME).build();
70 private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier
71 .builder(MASTER_CONTAINER_PATH).node(UNKEYED_LIST_QNAME).build();
73 private SchemaContext schemaContext;
74 private DataTree inMemoryDataTree;
77 public void prepare() {
78 schemaContext = createTestContext();
79 assertNotNull("Schema context must not be null.", schemaContext);
80 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
82 final DataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
83 final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
85 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
86 modificationTree.ready();
87 inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
90 public static SchemaContext createTestContext() {
91 return YangParserTestUtils.parseYangResource(CONSTRAINTS_VALIDATION_TEST_YANG);
95 public void minMaxListTestPass() throws DataValidationFailedException {
97 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
98 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
99 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
100 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
101 .withChild(fooEntryNode).build();
102 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
103 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
104 .withChild(barEntryNode).build();
106 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
107 modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
108 modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
109 modificationTree.ready();
111 inMemoryDataTree.validate(modificationTree);
112 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
113 inMemoryDataTree.commit(prepare);
115 final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
116 final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
117 assertTrue(minMaxListRead.isPresent());
118 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
121 @Test(expected = DataValidationFailedException.class)
122 public void minMaxListFail() throws DataValidationFailedException {
123 DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
125 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
126 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
127 final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
128 final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
129 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
130 .withChild(fooEntryNode).build();
132 final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
133 final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
134 final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
136 modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
137 modificationTree.merge(barPath, barEntryNode);
138 modificationTree.write(gooPath, gooEntryNode);
139 modificationTree.delete(gooPath);
140 modificationTree.ready();
142 inMemoryDataTree.validate(modificationTree);
143 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
144 inMemoryDataTree.commit(prepare1);
146 DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
147 Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
148 assertTrue(minMaxListRead.isPresent());
149 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
151 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
152 modificationTree.write(gooPath, gooEntryNode);
153 modificationTree.ready();
155 inMemoryDataTree.validate(modificationTree);
156 prepare1 = inMemoryDataTree.prepare(modificationTree);
157 inMemoryDataTree.commit(prepare1);
159 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
160 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
161 assertTrue(minMaxListRead.isPresent());
162 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
164 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
166 modificationTree.delete(gooPath);
167 modificationTree.delete(fooPath);
168 modificationTree.ready();
170 inMemoryDataTree.validate(modificationTree);
174 public void minMaxLeafListPass() throws DataValidationFailedException {
175 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
177 final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
178 final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
180 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
181 .withNodeIdentifier(barPath)
182 .withValue("bar").build();
183 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
184 .withNodeIdentifier(gooPath)
185 .withValue("goo").build();
187 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
188 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
189 .withChildValue("foo").build();
191 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
192 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
193 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
194 modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
195 modificationTree.ready();
197 inMemoryDataTree.validate(modificationTree);
198 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
199 inMemoryDataTree.commit(prepare1);
201 final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
202 final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
203 assertTrue(masterContainer.isPresent());
204 final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get())
205 .getChild(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
206 assertTrue(leafList.isPresent());
207 assertTrue(leafList.get().getValue().size() == 2);
211 public void minMaxLeafListFail() {
212 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
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);
238 modificationTree.ready();
239 fail("Should have failed with IAE");
240 } catch (IllegalArgumentException e) {
241 assertEquals("Node (urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
242 + "revision=2015-02-02)min-max-leaf-list has too many elements (4), can have at most 3",
248 public void unkeyedListTestPass() throws DataValidationFailedException {
249 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
251 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
252 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
253 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
254 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
255 unkeyedEntries.add(foo);
256 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
257 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
258 .withValue(unkeyedEntries).build();
260 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
261 modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
262 modificationTree.ready();
264 inMemoryDataTree.validate(modificationTree);
265 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
266 inMemoryDataTree.commit(prepare1);
268 final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
269 final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
270 assertTrue(unkeyedListRead.isPresent());
271 assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
275 public void unkeyedListTestFail() {
276 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
278 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
279 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
280 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
281 final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
282 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
283 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
284 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
285 unkeyedEntries.add(foo);
286 unkeyedEntries.add(bar);
287 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
288 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
289 .withValue(unkeyedEntries).build();
291 modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
293 modificationTree.ready();
294 fail("Should have failed with IAE");
295 } catch (IllegalArgumentException e) {
296 assertEquals("Node (urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
297 + "revision=2015-02-02)unkeyed-list has too many elements (2), can have at most 1", e.getMessage());