Eliminate use of DataTreeFactory.create(TreeType)
[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 = InMemoryDataTreeFactory.getInstance().create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
79         inMemoryDataTree.setSchemaContext(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         // TODO: check why write and then merge on list commits only "bar" child
108         modificationTree.ready();
109
110         inMemoryDataTree.validate(modificationTree);
111         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
112         inMemoryDataTree.commit(prepare);
113
114         final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
115         final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
116         assertTrue(minMaxListRead.isPresent());
117         assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
118     }
119
120     @Test(expected = DataValidationFailedException.class)
121     public void minMaxListFail() throws DataValidationFailedException {
122         DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
123
124         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
125         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
126         final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
127         final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
128                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
129                 .withChild(fooEntryNode).build();
130
131         final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
132         final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
133         final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
134
135         modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
136         modificationTree.merge(barPath, barEntryNode);
137         modificationTree.write(gooPath, gooEntryNode);
138         modificationTree.delete(gooPath);
139         modificationTree.ready();
140
141         inMemoryDataTree.validate(modificationTree);
142         DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
143         inMemoryDataTree.commit(prepare1);
144
145         DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
146         Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
147         assertTrue(minMaxListRead.isPresent());
148         assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
149
150         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
151         modificationTree.write(gooPath, gooEntryNode);
152         modificationTree.ready();
153
154         inMemoryDataTree.validate(modificationTree);
155         prepare1 = inMemoryDataTree.prepare(modificationTree);
156         inMemoryDataTree.commit(prepare1);
157
158         snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
159         minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
160         assertTrue(minMaxListRead.isPresent());
161         assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
162
163         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
164
165         modificationTree.delete(gooPath);
166         modificationTree.delete(fooPath);
167         modificationTree.ready();
168
169         inMemoryDataTree.validate(modificationTree);
170     }
171
172     @Test
173     public void minMaxLeafListPass() throws DataValidationFailedException {
174         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
175
176         final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
177         final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
178
179         final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
180                 .withNodeIdentifier(barPath)
181                 .withValue("bar").build();
182         final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
183                 .withNodeIdentifier(gooPath)
184                 .withValue("goo").build();
185
186         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
187                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
188                 .withChildValue("foo").build();
189
190         modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
191         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
192         modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
193         modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
194         modificationTree.ready();
195
196         inMemoryDataTree.validate(modificationTree);
197         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
198         inMemoryDataTree.commit(prepare1);
199
200         final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
201         final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
202         assertTrue(masterContainer.isPresent());
203         final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get())
204                 .getChild(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
205         assertTrue(leafList.isPresent());
206         assertTrue(leafList.get().getValue().size() == 2);
207     }
208
209     @Test(expected = DataValidationFailedException.class)
210     public void minMaxLeafListFail() throws DataValidationFailedException {
211         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
212
213         final NodeWithValue<Object> fooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "foo");
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         modificationTree.ready();
237
238         inMemoryDataTree.validate(modificationTree);
239     }
240
241     @Test
242     public void unkeyedListTestPass() throws DataValidationFailedException {
243         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
244
245         final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
246                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
247                 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
248         final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
249         unkeyedEntries.add(foo);
250         final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
251                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
252                 .withValue(unkeyedEntries).build();
253
254         modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
255         modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
256         modificationTree.ready();
257
258         inMemoryDataTree.validate(modificationTree);
259         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
260         inMemoryDataTree.commit(prepare1);
261
262         final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
263         final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
264         assertTrue(unkeyedListRead.isPresent());
265         assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
266     }
267
268     @Test(expected = DataValidationFailedException.class)
269     public void unkeyedListTestFail() throws DataValidationFailedException {
270         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
271
272         final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
273                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
274                 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
275         final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
276                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
277                 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
278         final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
279         unkeyedEntries.add(foo);
280         unkeyedEntries.add(bar);
281         final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
282                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
283                 .withValue(unkeyedEntries).build();
284
285         modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
286         modificationTree.ready();
287
288         inMemoryDataTree.validate(modificationTree);
289     }
290 }