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