2 * Copyright (c) 2014 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;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
28 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
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.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class ListConstraintsValidationTest {
45 private static final Logger LOG = LoggerFactory.getLogger(ListConstraintsValidationTest.class);
47 private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
48 private SchemaContext schemaContext;
49 private RootModificationApplyOperation rootOper;
51 private InMemoryDataTree inMemoryDataTree;
53 private static final QName MASTER_CONTAINER_QNAME = QName.create("urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
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");
64 private static final YangInstanceIdentifier MASTER_CONTAINER_PATH = YangInstanceIdentifier.of(MASTER_CONTAINER_QNAME);
65 private static final YangInstanceIdentifier MIN_MAX_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
66 .node(MIN_MAX_LIST_QNAME).build();
67 private static final YangInstanceIdentifier UNBOUNDED_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
68 .node(UNBOUNDED_LIST_QNAME).build();
69 private static final YangInstanceIdentifier MIN_MAX_LEAF_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
70 .node(MIN_MAX_LEAF_LIST_QNAME).build();
71 private static final YangInstanceIdentifier UNBOUNDED_LEAF_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
72 .node(UNBOUNDED_LEAF_LIST_QNAME).build();
73 private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
74 .node(UNKEYED_LIST_QNAME).build();
77 public void prepare() {
78 schemaContext = createTestContext();
79 assertNotNull("Schema context must not be null.", schemaContext);
80 rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext));
81 inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create();
82 inMemoryDataTree.setSchemaContext(schemaContext);
83 final InMemoryDataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
84 final DataTreeModification modificationTree = new InMemoryDataTreeModification(initialDataTreeSnapshot,
87 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
88 inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
92 public static final InputStream getDatastoreTestInputStream() {
93 return getInputStream(CONSTRAINTS_VALIDATION_TEST_YANG);
96 private static InputStream getInputStream(final String resourceName) {
97 return TestModel.class.getResourceAsStream(CONSTRAINTS_VALIDATION_TEST_YANG);
100 public static SchemaContext createTestContext() {
101 final YangParserImpl parser = new YangParserImpl();
102 final Set<Module> modules = parser.parseYangModelsFromStreams(Collections.singletonList(getDatastoreTestInputStream()));
103 return parser.resolveSchemaContext(modules);
107 public void minMaxListTestPass() throws DataValidationFailedException {
109 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
110 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
111 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
112 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
113 .withChild(fooEntryNode).build();
114 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
115 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
116 .withChild(barEntryNode).build();
118 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
119 modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
120 modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
121 // TODO: check why write and then merge on list commits only "bar" child
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 YangInstanceIdentifier.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);
153 inMemoryDataTree.validate(modificationTree);
154 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
155 inMemoryDataTree.commit(prepare1);
157 InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
158 Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
159 assertTrue(minMaxListRead.isPresent());
160 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
162 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
163 modificationTree.write(gooPath, gooEntryNode);
165 inMemoryDataTree.validate(modificationTree);
166 prepare1 = inMemoryDataTree.prepare(modificationTree);
167 inMemoryDataTree.commit(prepare1);
169 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
170 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
171 assertTrue(minMaxListRead.isPresent());
172 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
174 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
176 modificationTree.delete(gooPath);
177 modificationTree.delete(fooPath);
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));
205 inMemoryDataTree.validate(modificationTree);
206 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
207 inMemoryDataTree.commit(prepare1);
209 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
210 final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
211 assertTrue(masterContainer.isPresent());
212 final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get()).getChild(
213 new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
214 assertTrue(leafList.isPresent());
215 assertTrue(leafList.get().getValue().size() == 2);
218 @Test(expected=DataValidationFailedException.class)
219 public void minMaxLeafListFail() throws DataValidationFailedException {
220 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);
247 inMemoryDataTree.validate(modificationTree);
251 public void unkeyedListTestPass() throws DataValidationFailedException {
252 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
254 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
255 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LEAF_QNAME))
256 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
257 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
258 unkeyedEntries.add(foo);
259 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
260 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LIST_QNAME))
261 .withValue(unkeyedEntries).build();
263 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
264 modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
266 inMemoryDataTree.validate(modificationTree);
267 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
268 inMemoryDataTree.commit(prepare1);
270 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
271 final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
272 assertTrue(unkeyedListRead.isPresent());
273 assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
276 @Test(expected=DataValidationFailedException.class)
277 public void unkeyedListTestFail() throws DataValidationFailedException {
278 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
280 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
281 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LEAF_QNAME))
282 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
283 final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
284 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LEAF_QNAME))
285 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
286 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
287 unkeyedEntries.add(foo);
288 unkeyedEntries.add(bar);
289 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
290 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LIST_QNAME))
291 .withValue(unkeyedEntries).build();
293 modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
295 inMemoryDataTree.validate(modificationTree);