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