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.assertTrue;
12 import static org.junit.Assert.fail;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Optional;
17 import org.junit.AfterClass;
18 import org.junit.Before;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
31 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 public class ListConstraintsValidation {
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 static SchemaContext schemaContext;
75 private DataTree inMemoryDataTree;
78 public static void beforeClass() {
79 schemaContext = YangParserTestUtils.parseYangResource("/list-constraints-validation-test-model.yang");
83 public static void afterClass() {
88 public void prepare() throws DataValidationFailedException {
89 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
91 final DataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
92 final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
94 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
95 modificationTree.ready();
96 inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
100 public void minMaxListTestPass() throws DataValidationFailedException {
102 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
103 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
104 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
105 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
106 .withChild(fooEntryNode).build();
107 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
108 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
109 .withChild(barEntryNode).build();
111 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
112 modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
113 modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
114 modificationTree.ready();
116 inMemoryDataTree.validate(modificationTree);
117 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
118 inMemoryDataTree.commit(prepare);
120 final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
121 final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
122 assertTrue(minMaxListRead.isPresent());
123 assertEquals(2, ((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).size());
126 @Test(expected = DataValidationFailedException.class)
127 public void minMaxListFail() throws DataValidationFailedException {
128 DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
130 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
131 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
132 final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
133 final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
134 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
135 .withChild(fooEntryNode).build();
137 final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
138 final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
139 final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
141 modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
142 modificationTree.merge(barPath, barEntryNode);
143 modificationTree.write(gooPath, gooEntryNode);
144 modificationTree.delete(gooPath);
145 modificationTree.ready();
147 inMemoryDataTree.validate(modificationTree);
148 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
149 inMemoryDataTree.commit(prepare1);
151 DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
152 Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
153 assertTrue(minMaxListRead.isPresent());
154 assertEquals(2, ((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).size());
156 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
157 modificationTree.write(gooPath, gooEntryNode);
158 modificationTree.ready();
160 inMemoryDataTree.validate(modificationTree);
161 prepare1 = inMemoryDataTree.prepare(modificationTree);
162 inMemoryDataTree.commit(prepare1);
164 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
165 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
166 assertTrue(minMaxListRead.isPresent());
167 assertEquals(3, ((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).size());
169 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
171 modificationTree.delete(gooPath);
172 modificationTree.delete(fooPath);
173 modificationTree.ready();
175 inMemoryDataTree.validate(modificationTree);
179 public void minMaxLeafListPass() throws DataValidationFailedException {
180 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
182 final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
183 final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
185 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
186 .withNodeIdentifier(barPath)
187 .withValue("bar").build();
188 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
189 .withNodeIdentifier(gooPath)
190 .withValue("goo").build();
192 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
193 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
194 .withChildValue("foo").build();
196 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
197 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
198 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
199 modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
200 modificationTree.ready();
202 inMemoryDataTree.validate(modificationTree);
203 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
204 inMemoryDataTree.commit(prepare1);
206 final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
207 final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
208 assertTrue(masterContainer.isPresent());
209 final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get())
210 .getChild(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
211 assertTrue(leafList.isPresent());
212 assertEquals(2, leafList.get().size());
216 public void minMaxLeafListFail() {
217 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
219 final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
220 final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
221 final NodeWithValue<Object> fuuPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "fuu");
223 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
224 .withNodeIdentifier(barPath)
225 .withValue("bar").build();
226 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
227 .withNodeIdentifier(gooPath)
228 .withValue("goo").build();
229 final LeafSetEntryNode<Object> fuuLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
230 .withNodeIdentifier(fuuPath)
231 .withValue("fuu").build();
233 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
234 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
235 .withChildValue("foo").build();
237 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
238 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
239 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
240 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry);
243 modificationTree.ready();
244 fail("Should have failed with IAE");
245 } catch (IllegalArgumentException e) {
246 assertEquals("Node (urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
247 + "revision=2015-02-02)min-max-leaf-list has too many elements (4), can have at most 3",
253 public void unkeyedListTestPass() throws DataValidationFailedException {
254 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
256 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
257 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
258 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
259 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
260 unkeyedEntries.add(foo);
261 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
262 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
263 .withValue(unkeyedEntries).build();
265 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
266 modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
267 modificationTree.ready();
269 inMemoryDataTree.validate(modificationTree);
270 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
271 inMemoryDataTree.commit(prepare1);
273 final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
274 final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
275 assertTrue(unkeyedListRead.isPresent());
276 assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
280 public void unkeyedListTestFail() {
281 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
283 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
284 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
285 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
286 final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
287 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
288 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
289 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
290 unkeyedEntries.add(foo);
291 unkeyedEntries.add(bar);
292 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
293 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
294 .withValue(unkeyedEntries).build();
296 modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
298 modificationTree.ready();
299 fail("Should have failed with IAE");
300 } catch (IllegalArgumentException e) {
301 assertEquals("Node (urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
302 + "revision=2015-02-02)unkeyed-list has too many elements (2), can have at most 1", e.getMessage());