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