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.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;
46 public class Retest_ListConstraintsValidation {
47 private static final Logger LOG = LoggerFactory.getLogger(Retest_ListConstraintsValidation.class);
49 private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
50 private SchemaContext schemaContext;
52 private InMemoryDataTree inMemoryDataTree;
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",
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");
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();
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();
88 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
89 modificationTree.ready();
90 inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
93 public static InputStream getDatastoreTestInputStream() {
94 return getInputStream(CONSTRAINTS_VALIDATION_TEST_YANG);
97 private static InputStream getInputStream(final String resourceName) {
98 return TestModel.class.getResourceAsStream(CONSTRAINTS_VALIDATION_TEST_YANG);
101 public static SchemaContext createTestContext() throws ReactorException {
102 return RetestUtils.parseYangStreams(Collections.singletonList(getDatastoreTestInputStream()));
106 public void minMaxListTestPass() throws DataValidationFailedException {
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();
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();
123 inMemoryDataTree.validate(modificationTree);
124 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
125 inMemoryDataTree.commit(prepare);
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);
133 @Test(expected=DataValidationFailedException.class)
134 public void minMaxListFail() throws DataValidationFailedException {
135 InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
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();
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());
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();
154 inMemoryDataTree.validate(modificationTree);
155 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
156 inMemoryDataTree.commit(prepare1);
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);
163 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
164 modificationTree.write(gooPath, gooEntryNode);
165 modificationTree.ready();
167 inMemoryDataTree.validate(modificationTree);
168 prepare1 = inMemoryDataTree.prepare(modificationTree);
169 inMemoryDataTree.commit(prepare1);
171 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
172 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
173 assertTrue(minMaxListRead.isPresent());
174 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
176 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
178 modificationTree.delete(gooPath);
179 modificationTree.delete(fooPath);
180 modificationTree.ready();
182 inMemoryDataTree.validate(modificationTree);
186 public void minMaxLeafListPass() throws DataValidationFailedException {
187 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
189 final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
190 final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
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();
199 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
200 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
201 .withChildValue("foo").build();
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();
209 inMemoryDataTree.validate(modificationTree);
210 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
211 inMemoryDataTree.commit(prepare1);
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);
222 @Test(expected=DataValidationFailedException.class)
223 public void minMaxLeafListFail() throws DataValidationFailedException {
224 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
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");
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();
241 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
242 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
243 .withChildValue("foo").build();
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();
251 inMemoryDataTree.validate(modificationTree);
255 public void unkeyedListTestPass() throws DataValidationFailedException {
256 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
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();
267 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
268 modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
269 modificationTree.ready();
271 inMemoryDataTree.validate(modificationTree);
272 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
273 inMemoryDataTree.commit(prepare1);
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);
281 @Test(expected=DataValidationFailedException.class)
282 public void unkeyedListTestFail() throws DataValidationFailedException {
283 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
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();
298 modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
299 modificationTree.ready();
301 inMemoryDataTree.validate(modificationTree);