Add MinMaxElementsValidationFailedException
[yangtools.git] / yang / 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.assertThrows;
12 import static org.junit.Assert.assertTrue;
13
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.EffectiveModelContext;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
46
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",
50             "master-container");
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");
59
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();
72
73     private static EffectiveModelContext schemaContext;
74
75     private DataTree inMemoryDataTree;
76
77     @BeforeClass
78     public static void beforeClass() {
79         schemaContext = YangParserTestUtils.parseYangResource("/list-constraints-validation-test-model.yang");
80     }
81
82     @AfterClass
83     public static void afterClass() {
84         schemaContext = null;
85     }
86
87     @Before
88     public void prepare() throws DataValidationFailedException {
89         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
90             schemaContext);
91         final DataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
92         final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
93
94         modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
95         modificationTree.ready();
96         inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
97     }
98
99     @Test
100     public void minMaxListTestPass() throws DataValidationFailedException {
101
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();
110
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();
115
116         inMemoryDataTree.validate(modificationTree);
117         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
118         inMemoryDataTree.commit(prepare);
119
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());
124     }
125
126     @Test(expected = DataValidationFailedException.class)
127     public void minMaxListFail() throws DataValidationFailedException {
128         DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
129
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();
136
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());
140
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();
146
147         inMemoryDataTree.validate(modificationTree);
148         DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
149         inMemoryDataTree.commit(prepare1);
150
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());
155
156         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
157         modificationTree.write(gooPath, gooEntryNode);
158         modificationTree.ready();
159
160         inMemoryDataTree.validate(modificationTree);
161         prepare1 = inMemoryDataTree.prepare(modificationTree);
162         inMemoryDataTree.commit(prepare1);
163
164         snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
165         minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
166         assertTrue(minMaxListRead.isPresent());
167         assertEquals(3, ((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).size());
168
169         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
170
171         modificationTree.delete(gooPath);
172         modificationTree.delete(fooPath);
173         modificationTree.ready();
174
175         inMemoryDataTree.validate(modificationTree);
176     }
177
178     @Test
179     public void minMaxLeafListPass() throws DataValidationFailedException {
180         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
181
182         final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
183         final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
184
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();
191
192         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
193                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
194                 .withChildValue("foo").build();
195
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();
201
202         inMemoryDataTree.validate(modificationTree);
203         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
204         inMemoryDataTree.commit(prepare1);
205
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());
213     }
214
215     @Test
216     public void minMaxLeafListFail() {
217         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
218
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");
222
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();
232
233         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
234                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
235                 .withChildValue("foo").build();
236
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);
241
242         assertEquals("(urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
243             + "revision=2015-02-02)min-max-leaf-list has too many elements (4), can have at most 3",
244             assertThrows(MinMaxElementsValidationFailedException.class, () -> modificationTree.ready()).getMessage());
245     }
246
247     @Test
248     public void unkeyedListTestPass() throws DataValidationFailedException {
249         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
250
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();
259
260         modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
261         modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
262         modificationTree.ready();
263
264         inMemoryDataTree.validate(modificationTree);
265         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
266         inMemoryDataTree.commit(prepare1);
267
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);
272     }
273
274     @Test
275     public void unkeyedListTestFail() {
276         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
277
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();
290
291         modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
292         assertEquals("(urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
293             + "revision=2015-02-02)unkeyed-list has too many elements (2), can have at most 1",
294             assertThrows(MinMaxElementsValidationFailedException.class, () -> modificationTree.ready()).getMessage());
295     }
296 }