2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
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.impl.RetestUtils;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class Retest_ListConstraintsValidation {
44 private static final Logger LOG = LoggerFactory.getLogger(Retest_ListConstraintsValidation.class);
46 private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
47 private SchemaContext schemaContext;
49 private InMemoryDataTree inMemoryDataTree;
51 private static final QName MASTER_CONTAINER_QNAME = QName.create(
52 "urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
54 private static final QName MIN_MAX_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-list");
55 private static final QName MIN_MAX_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-key-leaf");
56 private static final QName UNBOUNDED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-list");
57 private static final QName UNBOUNDED_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-key-leaf");
58 private static final QName MIN_MAX_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-leaf-list");
59 private static final QName UNBOUNDED_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-leaf-list");
60 private static final QName UNKEYED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-list");
61 private static final QName UNKEYED_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-leaf");
63 private static final YangInstanceIdentifier MASTER_CONTAINER_PATH = YangInstanceIdentifier
64 .of(MASTER_CONTAINER_QNAME);
65 private static final YangInstanceIdentifier MIN_MAX_LIST_PATH = YangInstanceIdentifier
66 .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LIST_QNAME).build();
67 private static final YangInstanceIdentifier UNBOUNDED_LIST_PATH = YangInstanceIdentifier
68 .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LIST_QNAME).build();
69 private static final YangInstanceIdentifier MIN_MAX_LEAF_LIST_PATH = YangInstanceIdentifier
70 .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LEAF_LIST_QNAME).build();
71 private static final YangInstanceIdentifier UNBOUNDED_LEAF_LIST_PATH = YangInstanceIdentifier
72 .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LEAF_LIST_QNAME).build();
73 private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier
74 .builder(MASTER_CONTAINER_PATH).node(UNKEYED_LIST_QNAME).build();
77 public void prepare() throws ReactorException {
78 schemaContext = createTestContext();
79 assertNotNull("Schema context must not be null.", schemaContext);
80 inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create();
81 inMemoryDataTree.setSchemaContext(schemaContext);
82 final InMemoryDataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
83 final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
85 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
86 modificationTree.ready();
87 inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
90 public static final InputStream getDatastoreTestInputStream() {
91 return getInputStream(CONSTRAINTS_VALIDATION_TEST_YANG);
94 private static InputStream getInputStream(final String resourceName) {
95 return TestModel.class.getResourceAsStream(CONSTRAINTS_VALIDATION_TEST_YANG);
98 public static SchemaContext createTestContext() throws ReactorException {
99 return RetestUtils.parseYangStreams(Collections.singletonList(getDatastoreTestInputStream()));
103 public void minMaxListTestPass() throws DataValidationFailedException {
105 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
106 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
107 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
108 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
109 .withChild(fooEntryNode).build();
110 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
111 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
112 .withChild(barEntryNode).build();
114 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
115 modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
116 modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
117 // TODO: check why write and then merge on list commits only "bar" child
118 modificationTree.ready();
120 inMemoryDataTree.validate(modificationTree);
121 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
122 inMemoryDataTree.commit(prepare);
124 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
125 final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
126 assertTrue(minMaxListRead.isPresent());
127 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
130 @Test(expected=DataValidationFailedException.class)
131 public void minMaxListFail() throws DataValidationFailedException {
132 InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
134 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
135 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
136 final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
137 final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
138 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
139 .withChild(fooEntryNode).build();
141 final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
142 final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
143 final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
145 modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
146 modificationTree.merge(barPath, barEntryNode);
147 modificationTree.write(gooPath, gooEntryNode);
148 modificationTree.delete(gooPath);
149 modificationTree.ready();
151 inMemoryDataTree.validate(modificationTree);
152 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
153 inMemoryDataTree.commit(prepare1);
155 InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
156 Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
157 assertTrue(minMaxListRead.isPresent());
158 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
160 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
161 modificationTree.write(gooPath, gooEntryNode);
162 modificationTree.ready();
164 inMemoryDataTree.validate(modificationTree);
165 prepare1 = inMemoryDataTree.prepare(modificationTree);
166 inMemoryDataTree.commit(prepare1);
168 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
169 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
170 assertTrue(minMaxListRead.isPresent());
171 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
173 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
175 modificationTree.delete(gooPath);
176 modificationTree.delete(fooPath);
177 modificationTree.ready();
179 inMemoryDataTree.validate(modificationTree);
183 public void minMaxLeafListPass() throws DataValidationFailedException {
184 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
186 final YangInstanceIdentifier.NodeWithValue barPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "bar");
187 final YangInstanceIdentifier.NodeWithValue gooPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "goo");
189 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
190 .withNodeIdentifier(barPath)
191 .withValue("bar").build();
192 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
193 .withNodeIdentifier(gooPath)
194 .withValue("goo").build();
196 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
197 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
198 .withChildValue("foo").build();
200 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
201 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
202 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
203 modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
204 modificationTree.ready();
206 inMemoryDataTree.validate(modificationTree);
207 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
208 inMemoryDataTree.commit(prepare1);
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);
219 @Test(expected=DataValidationFailedException.class)
220 public void minMaxLeafListFail() throws DataValidationFailedException {
221 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
223 final YangInstanceIdentifier.NodeWithValue fooPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "foo");
224 final YangInstanceIdentifier.NodeWithValue barPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "bar");
225 final YangInstanceIdentifier.NodeWithValue gooPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "goo");
226 final YangInstanceIdentifier.NodeWithValue fuuPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "fuu");
228 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
229 .withNodeIdentifier(barPath)
230 .withValue("bar").build();
231 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
232 .withNodeIdentifier(gooPath)
233 .withValue("goo").build();
234 final LeafSetEntryNode<Object> fuuLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
235 .withNodeIdentifier(fuuPath)
236 .withValue("fuu").build();
238 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
239 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
240 .withChildValue("foo").build();
242 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
243 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
244 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
245 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry);
246 modificationTree.ready();
248 inMemoryDataTree.validate(modificationTree);
252 public void unkeyedListTestPass() throws DataValidationFailedException {
253 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
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();
264 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
265 modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
266 modificationTree.ready();
268 inMemoryDataTree.validate(modificationTree);
269 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
270 inMemoryDataTree.commit(prepare1);
272 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
273 final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
274 assertTrue(unkeyedListRead.isPresent());
275 assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
278 @Test(expected=DataValidationFailedException.class)
279 public void unkeyedListTestFail() throws DataValidationFailedException {
280 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
282 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
283 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LEAF_QNAME))
284 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
285 final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
286 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LEAF_QNAME))
287 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
288 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
289 unkeyedEntries.add(foo);
290 unkeyedEntries.add(bar);
291 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
292 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LIST_QNAME))
293 .withValue(unkeyedEntries).build();
295 modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
296 modificationTree.ready();
298 inMemoryDataTree.validate(modificationTree);