Fix min/max validation inside written value
[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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
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;
45
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",
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 SchemaContext schemaContext;
74     private DataTree inMemoryDataTree;
75
76     @Before
77     public void prepare() {
78         schemaContext = createTestContext();
79         assertNotNull("Schema context must not be null.", schemaContext);
80         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
81             schemaContext);
82         final DataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
83         final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
84
85         modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
86         modificationTree.ready();
87         inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
88     }
89
90     public static SchemaContext createTestContext() {
91         return YangParserTestUtils.parseYangResource(CONSTRAINTS_VALIDATION_TEST_YANG);
92     }
93
94     @Test
95     public void minMaxListTestPass() throws DataValidationFailedException {
96
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();
105
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();
110
111         inMemoryDataTree.validate(modificationTree);
112         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
113         inMemoryDataTree.commit(prepare);
114
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);
119     }
120
121     @Test(expected = DataValidationFailedException.class)
122     public void minMaxListFail() throws DataValidationFailedException {
123         DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
124
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();
131
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());
135
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();
141
142         inMemoryDataTree.validate(modificationTree);
143         DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
144         inMemoryDataTree.commit(prepare1);
145
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);
150
151         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
152         modificationTree.write(gooPath, gooEntryNode);
153         modificationTree.ready();
154
155         inMemoryDataTree.validate(modificationTree);
156         prepare1 = inMemoryDataTree.prepare(modificationTree);
157         inMemoryDataTree.commit(prepare1);
158
159         snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
160         minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
161         assertTrue(minMaxListRead.isPresent());
162         assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
163
164         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
165
166         modificationTree.delete(gooPath);
167         modificationTree.delete(fooPath);
168         modificationTree.ready();
169
170         inMemoryDataTree.validate(modificationTree);
171     }
172
173     @Test
174     public void minMaxLeafListPass() throws DataValidationFailedException {
175         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
176
177         final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
178         final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
179
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();
186
187         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
188                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
189                 .withChildValue("foo").build();
190
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();
196
197         inMemoryDataTree.validate(modificationTree);
198         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
199         inMemoryDataTree.commit(prepare1);
200
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);
208     }
209
210     @Test
211     public void minMaxLeafListFail() {
212         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
213
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");
217
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();
227
228         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
229                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
230                 .withChildValue("foo").build();
231
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
237         try {
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",
243                     e.getMessage());
244         }
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         try {
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());
298         }
299     }
300 }