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;
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;
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;
45 public class ListConstraintsValidationTest {
46 private static final Logger LOG = LoggerFactory.getLogger(ListConstraintsValidationTest.class);
48 private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
49 private SchemaContext schemaContext;
50 private RootModificationApplyOperation rootOper;
52 private InMemoryDataTree inMemoryDataTree;
54 private static final QName MASTER_CONTAINER_QNAME = QName.create("urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
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");
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();
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,
88 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
89 inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
93 public static final 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() {
102 final YangParserImpl parser = new YangParserImpl();
103 final Set<Module> modules = parser.parseYangModelsFromStreams(Collections.singletonList(getDatastoreTestInputStream()));
104 return parser.resolveSchemaContext(modules);
108 public void minMaxListTestPass() throws DataValidationFailedException {
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();
119 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
120 modificationTree.merge(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
124 inMemoryDataTree.validate(modificationTree);
125 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
126 inMemoryDataTree.commit(prepare);
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);
134 @Test(expected=DataValidationFailedException.class)
135 public void minMaxListFail() throws DataValidationFailedException {
136 InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
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();
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());
149 modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
150 modificationTree.merge(barPath, barEntryNode);
151 modificationTree.write(gooPath, gooEntryNode);
152 modificationTree.delete(gooPath);
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);
166 inMemoryDataTree.validate(modificationTree);
167 prepare1 = inMemoryDataTree.prepare(modificationTree);
168 inMemoryDataTree.commit(prepare1);
170 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
171 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
172 assertTrue(minMaxListRead.isPresent());
173 assertTrue(((NormalizedNodeContainer) minMaxListRead.get()).getValue().size() == 3);
175 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
177 modificationTree.delete(gooPath);
178 modificationTree.delete(fooPath);
180 inMemoryDataTree.validate(modificationTree);
184 public void minMaxLeafListPass() throws DataValidationFailedException {
185 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
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");
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();
197 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
198 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
199 .withChildValue("foo").build();
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));
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_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();
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");
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();
239 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
240 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
241 .withChildValue("foo").build();
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);
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);
267 inMemoryDataTree.validate(modificationTree);
268 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
269 inMemoryDataTree.commit(prepare1);
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);
277 @Test(expected=DataValidationFailedException.class)
278 public void unkeyedListTestFail() throws DataValidationFailedException {
279 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
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();
294 modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
296 inMemoryDataTree.validate(modificationTree);