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