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.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.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
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;
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() throws IOException, YangSyntaxErrorException {
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));
91 private static ByteSource getInputStream() {
92 return Resources.asByteSource(TestModel.class.getResource(CONSTRAINTS_VALIDATION_TEST_YANG));
95 public static SchemaContext createTestContext() throws IOException, YangSyntaxErrorException {
96 final YangParserImpl parser = new YangParserImpl();
97 return parser.parseSources(Collections.singletonList(getInputStream()));
101 public void minMaxListTestPass() throws DataValidationFailedException {
103 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
104 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
105 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
106 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
107 .withChild(fooEntryNode).build();
108 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
109 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
110 .withChild(barEntryNode).build();
112 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
113 modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
114 modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
115 // TODO: check why write and then merge on list commits only "bar" child
116 modificationTree.ready();
118 inMemoryDataTree.validate(modificationTree);
119 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
120 inMemoryDataTree.commit(prepare);
122 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
123 final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
124 assertTrue(minMaxListRead.isPresent());
125 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
128 @Test(expected=DataValidationFailedException.class)
129 public void minMaxListFail() throws DataValidationFailedException {
130 InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
132 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
133 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
134 final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
135 final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
136 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LIST_QNAME))
137 .withChild(fooEntryNode).build();
139 final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
140 final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
141 final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
143 modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
144 modificationTree.merge(barPath, barEntryNode);
145 modificationTree.write(gooPath, gooEntryNode);
146 modificationTree.delete(gooPath);
147 modificationTree.ready();
149 inMemoryDataTree.validate(modificationTree);
150 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
151 inMemoryDataTree.commit(prepare1);
153 InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
154 Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
155 assertTrue(minMaxListRead.isPresent());
156 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
158 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
159 modificationTree.write(gooPath, gooEntryNode);
160 modificationTree.ready();
162 inMemoryDataTree.validate(modificationTree);
163 prepare1 = inMemoryDataTree.prepare(modificationTree);
164 inMemoryDataTree.commit(prepare1);
166 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
167 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
168 assertTrue(minMaxListRead.isPresent());
169 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
171 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
173 modificationTree.delete(gooPath);
174 modificationTree.delete(fooPath);
175 modificationTree.ready();
177 inMemoryDataTree.validate(modificationTree);
181 public void minMaxLeafListPass() throws DataValidationFailedException {
182 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
184 final YangInstanceIdentifier.NodeWithValue barPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "bar");
185 final YangInstanceIdentifier.NodeWithValue gooPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "goo");
187 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
188 .withNodeIdentifier(barPath)
189 .withValue("bar").build();
190 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
191 .withNodeIdentifier(gooPath)
192 .withValue("goo").build();
194 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
195 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
196 .withChildValue("foo").build();
198 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
199 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
200 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
201 modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
202 modificationTree.ready();
204 inMemoryDataTree.validate(modificationTree);
205 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
206 inMemoryDataTree.commit(prepare1);
208 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
209 final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
210 assertTrue(masterContainer.isPresent());
211 final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get()).getChild(
212 new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
213 assertTrue(leafList.isPresent());
214 assertTrue(leafList.get().getValue().size() == 2);
217 @Test(expected=DataValidationFailedException.class)
218 public void minMaxLeafListFail() throws DataValidationFailedException {
219 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
221 final YangInstanceIdentifier.NodeWithValue fooPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "foo");
222 final YangInstanceIdentifier.NodeWithValue barPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "bar");
223 final YangInstanceIdentifier.NodeWithValue gooPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "goo");
224 final YangInstanceIdentifier.NodeWithValue fuuPath = new YangInstanceIdentifier.NodeWithValue(MIN_MAX_LIST_QNAME, "fuu");
226 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
227 .withNodeIdentifier(barPath)
228 .withValue("bar").build();
229 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
230 .withNodeIdentifier(gooPath)
231 .withValue("goo").build();
232 final LeafSetEntryNode<Object> fuuLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
233 .withNodeIdentifier(fuuPath)
234 .withValue("fuu").build();
236 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
237 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
238 .withChildValue("foo").build();
240 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
241 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
242 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
243 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry);
244 modificationTree.ready();
246 inMemoryDataTree.validate(modificationTree);
250 public void unkeyedListTestPass() throws DataValidationFailedException {
251 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
253 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
254 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LEAF_QNAME))
255 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
256 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
257 unkeyedEntries.add(foo);
258 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
259 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(UNKEYED_LIST_QNAME))
260 .withValue(unkeyedEntries).build();
262 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
263 modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
264 modificationTree.ready();
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);
294 modificationTree.ready();
296 inMemoryDataTree.validate(modificationTree);