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 com.google.common.io.ByteSource;
14 import com.google.common.io.Resources;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 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.api.schema.tree.TreeType;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
42 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class ListConstraintsValidationTest {
47 private static final Logger LOG = LoggerFactory.getLogger(ListConstraintsValidationTest.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("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() throws IOException, YangSyntaxErrorException {
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();
86 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
87 modificationTree.ready();
88 inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
92 private static ByteSource getInputStream() {
93 return Resources.asByteSource(TestModel.class.getResource(CONSTRAINTS_VALIDATION_TEST_YANG));
96 public static SchemaContext createTestContext() throws IOException, YangSyntaxErrorException {
97 final YangParserImpl parser = new YangParserImpl();
98 return parser.parseSources(Collections.singletonList(getInputStream()));
102 public void minMaxListTestPass() throws DataValidationFailedException {
104 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
105 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
106 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
107 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
108 .withChild(fooEntryNode).build();
109 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
110 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
111 .withChild(barEntryNode).build();
113 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
114 modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
115 modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
116 // TODO: check why write and then merge on list commits only "bar" child
117 modificationTree.ready();
119 inMemoryDataTree.validate(modificationTree);
120 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
121 inMemoryDataTree.commit(prepare);
123 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
124 final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
125 assertTrue(minMaxListRead.isPresent());
126 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
129 @Test(expected=DataValidationFailedException.class)
130 public void minMaxListFail() throws DataValidationFailedException {
131 InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
133 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
134 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
135 final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
136 final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
137 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
138 .withChild(fooEntryNode).build();
140 final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
141 final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
142 final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
144 modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
145 modificationTree.merge(barPath, barEntryNode);
146 modificationTree.write(gooPath, gooEntryNode);
147 modificationTree.delete(gooPath);
148 modificationTree.ready();
150 inMemoryDataTree.validate(modificationTree);
151 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
152 inMemoryDataTree.commit(prepare1);
154 InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
155 Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
156 assertTrue(minMaxListRead.isPresent());
157 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
159 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
160 modificationTree.write(gooPath, gooEntryNode);
161 modificationTree.ready();
163 inMemoryDataTree.validate(modificationTree);
164 prepare1 = inMemoryDataTree.prepare(modificationTree);
165 inMemoryDataTree.commit(prepare1);
167 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
168 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
169 assertTrue(minMaxListRead.isPresent());
170 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
172 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
174 modificationTree.delete(gooPath);
175 modificationTree.delete(fooPath);
176 modificationTree.ready();
178 inMemoryDataTree.validate(modificationTree);
182 public void minMaxLeafListPass() throws DataValidationFailedException {
183 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
185 final YangInstanceIdentifier.NodeWithValue barPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "bar");
186 final YangInstanceIdentifier.NodeWithValue gooPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "goo");
188 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
189 .withNodeIdentifier(barPath)
190 .withValue("bar").build();
191 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
192 .withNodeIdentifier(gooPath)
193 .withValue("goo").build();
195 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
196 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
197 .withChildValue("foo").build();
199 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
200 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
201 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
202 modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
203 modificationTree.ready();
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();
222 final YangInstanceIdentifier.NodeWithValue fooPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "foo");
223 final YangInstanceIdentifier.NodeWithValue barPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "bar");
224 final YangInstanceIdentifier.NodeWithValue gooPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "goo");
225 final YangInstanceIdentifier.NodeWithValue fuuPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "fuu");
227 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
228 .withNodeIdentifier(barPath)
229 .withValue("bar").build();
230 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
231 .withNodeIdentifier(gooPath)
232 .withValue("goo").build();
233 final LeafSetEntryNode<Object> fuuLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
234 .withNodeIdentifier(fuuPath)
235 .withValue("fuu").build();
237 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
238 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
239 .withChildValue("foo").build();
241 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
242 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
243 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
244 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry);
245 modificationTree.ready();
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);
265 modificationTree.ready();
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);
295 modificationTree.ready();
297 inMemoryDataTree.validate(modificationTree);