Populate data/ hierarchy
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ListConstraintsValidation.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18 import org.junit.AfterClass;
19 import org.junit.Before;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
33 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
46 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
47 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
48
49 public class ListConstraintsValidation {
50     private static final QName MASTER_CONTAINER_QNAME = QName.create(
51             "urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
52             "master-container");
53     private static final QName MIN_MAX_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-list");
54     private static final QName MIN_MAX_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-key-leaf");
55     private static final QName UNBOUNDED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-list");
56     private static final QName UNBOUNDED_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-key-leaf");
57     private static final QName MIN_MAX_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-leaf-list");
58     private static final QName UNBOUNDED_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-leaf-list");
59     private static final QName UNKEYED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-list");
60     private static final QName UNKEYED_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-leaf");
61
62     private static final YangInstanceIdentifier MASTER_CONTAINER_PATH = YangInstanceIdentifier
63             .of(MASTER_CONTAINER_QNAME);
64     private static final YangInstanceIdentifier MIN_MAX_LIST_PATH = YangInstanceIdentifier
65             .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LIST_QNAME).build();
66     private static final YangInstanceIdentifier UNBOUNDED_LIST_PATH = YangInstanceIdentifier
67             .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LIST_QNAME).build();
68     private static final YangInstanceIdentifier MIN_MAX_LEAF_LIST_PATH = YangInstanceIdentifier
69             .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LEAF_LIST_QNAME).build();
70     private static final YangInstanceIdentifier UNBOUNDED_LEAF_LIST_PATH = YangInstanceIdentifier
71             .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LEAF_LIST_QNAME).build();
72     private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier
73             .builder(MASTER_CONTAINER_PATH).node(UNKEYED_LIST_QNAME).build();
74
75     private static EffectiveModelContext schemaContext;
76
77     private DataTree inMemoryDataTree;
78
79     @BeforeClass
80     public static void beforeClass() {
81         schemaContext = YangParserTestUtils.parseYangResource("/list-constraints-validation-test-model.yang");
82     }
83
84     @AfterClass
85     public static void afterClass() {
86         schemaContext = null;
87     }
88
89     @Before
90     public void prepare() throws DataValidationFailedException {
91         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
92             schemaContext);
93         final DataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
94         final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
95
96         modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
97         modificationTree.ready();
98         inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
99     }
100
101     @Test
102     public void minMaxListTestPass() throws DataValidationFailedException {
103
104         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
105         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
106         final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
107                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
108                 .withChild(fooEntryNode).build();
109         final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
110                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
111                 .withChild(barEntryNode).build();
112
113         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
114         modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
115         modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
116         modificationTree.ready();
117
118         inMemoryDataTree.validate(modificationTree);
119         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
120         inMemoryDataTree.commit(prepare);
121
122         final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
123         final Optional<NormalizedNode> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
124         assertTrue(minMaxListRead.isPresent());
125         assertEquals(2, ((NormalizedNodeContainer<?>) minMaxListRead.get()).size());
126     }
127
128     @Test(expected = DataValidationFailedException.class)
129     public void minMaxListFail() throws DataValidationFailedException {
130         DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
131
132         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
133         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
134         final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
135         final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
136                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
137                 .withChild(fooEntryNode).build();
138
139         final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
140         final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
141         final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
142
143         modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
144         modificationTree.merge(barPath, barEntryNode);
145         modificationTree.write(gooPath, gooEntryNode);
146         modificationTree.delete(gooPath);
147         modificationTree.ready();
148
149         inMemoryDataTree.validate(modificationTree);
150         DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
151         inMemoryDataTree.commit(prepare1);
152
153         DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
154         Optional<NormalizedNode> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
155         assertTrue(minMaxListRead.isPresent());
156         assertEquals(2, ((NormalizedNodeContainer<?>) minMaxListRead.get()).size());
157
158         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
159         modificationTree.write(gooPath, gooEntryNode);
160         modificationTree.ready();
161
162         inMemoryDataTree.validate(modificationTree);
163         prepare1 = inMemoryDataTree.prepare(modificationTree);
164         inMemoryDataTree.commit(prepare1);
165
166         snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
167         minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
168         assertTrue(minMaxListRead.isPresent());
169         assertEquals(3, ((NormalizedNodeContainer<?>) minMaxListRead.get()).size());
170
171         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
172
173         modificationTree.delete(gooPath);
174         modificationTree.delete(fooPath);
175         modificationTree.ready();
176
177         inMemoryDataTree.validate(modificationTree);
178     }
179
180     @Test
181     public void minMaxLeafListPass() throws DataValidationFailedException {
182         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
183
184         final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
185         final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
186
187         final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
188                 .withNodeIdentifier(barPath)
189                 .withValue("bar").build();
190         final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
191                 .withNodeIdentifier(gooPath)
192                 .withValue("goo").build();
193
194         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
195                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
196                 .withChildValue("foo").build();
197
198         modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
199         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
200         modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
201         modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
202         modificationTree.ready();
203
204         inMemoryDataTree.validate(modificationTree);
205         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
206         inMemoryDataTree.commit(prepare1);
207
208         final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
209         final Optional<NormalizedNode> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
210         assertTrue(masterContainer.isPresent());
211         final NormalizedNodeContainer<?> leafList =
212             (NormalizedNodeContainer<?>) ((DistinctNodeContainer) masterContainer.get())
213                 .childByArg(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
214         assertNotNull(leafList);
215         assertEquals(2, leafList.size());
216     }
217
218     @Test
219     public void minMaxLeafListFail() {
220         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
221
222         final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
223         final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
224         final NodeWithValue<Object> fuuPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "fuu");
225
226         final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
227                 .withNodeIdentifier(barPath)
228                 .withValue("bar").build();
229         final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
230                 .withNodeIdentifier(gooPath)
231                 .withValue("goo").build();
232         final LeafSetEntryNode<Object> fuuLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
233                 .withNodeIdentifier(fuuPath)
234                 .withValue("fuu").build();
235
236         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
237                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
238                 .withChildValue("foo").build();
239
240         modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
241         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
242         modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
243         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry);
244
245         assertEquals("(urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
246             + "revision=2015-02-02)min-max-leaf-list has too many elements (4), can have at most 3",
247             assertThrows(MinMaxElementsValidationFailedException.class, () -> modificationTree.ready()).getMessage());
248     }
249
250     @Test
251     public void unkeyedListTestPass() throws DataValidationFailedException {
252         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
253
254         final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
255                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
256                 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
257         final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
258         unkeyedEntries.add(foo);
259         final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
260                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
261                 .withValue(unkeyedEntries).build();
262
263         modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
264         modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
265         modificationTree.ready();
266
267         inMemoryDataTree.validate(modificationTree);
268         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
269         inMemoryDataTree.commit(prepare1);
270
271         final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
272         final Optional<NormalizedNode> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
273         assertTrue(unkeyedListRead.isPresent());
274         assertTrue(((UnkeyedListNode) unkeyedListRead.get()).size() == 1);
275     }
276
277     @Test
278     public void unkeyedListTestFail() {
279         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
280
281         final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
282                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
283                 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
284         final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
285                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
286                 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
287         final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
288         unkeyedEntries.add(foo);
289         unkeyedEntries.add(bar);
290         final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
291                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
292                 .withValue(unkeyedEntries).build();
293
294         modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
295         assertEquals("(urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
296             + "revision=2015-02-02)unkeyed-list has too many elements (2), can have at most 1",
297             assertThrows(MinMaxElementsValidationFailedException.class, () -> modificationTree.ready()).getMessage());
298     }
299 }