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.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
31 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
44 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class ListConstraintsValidationTest {
49 private static final Logger LOG = LoggerFactory.getLogger(ListConstraintsValidationTest.class);
51 private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
52 private SchemaContext schemaContext;
54 private InMemoryDataTree inMemoryDataTree;
56 private static final QName MASTER_CONTAINER_QNAME = QName.create("urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
58 private static final QName MIN_MAX_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-list");
59 private static final QName MIN_MAX_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-key-leaf");
60 private static final QName UNBOUNDED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-list");
61 private static final QName UNBOUNDED_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-key-leaf");
62 private static final QName MIN_MAX_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-leaf-list");
63 private static final QName UNBOUNDED_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-leaf-list");
64 private static final QName UNKEYED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-list");
65 private static final QName UNKEYED_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-leaf");
67 private static final YangInstanceIdentifier MASTER_CONTAINER_PATH = YangInstanceIdentifier.of(MASTER_CONTAINER_QNAME);
68 private static final YangInstanceIdentifier MIN_MAX_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
69 .node(MIN_MAX_LIST_QNAME).build();
70 private static final YangInstanceIdentifier UNBOUNDED_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
71 .node(UNBOUNDED_LIST_QNAME).build();
72 private static final YangInstanceIdentifier MIN_MAX_LEAF_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
73 .node(MIN_MAX_LEAF_LIST_QNAME).build();
74 private static final YangInstanceIdentifier UNBOUNDED_LEAF_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
75 .node(UNBOUNDED_LEAF_LIST_QNAME).build();
76 private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
77 .node(UNKEYED_LIST_QNAME).build();
80 public void prepare() throws IOException, YangSyntaxErrorException {
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));
94 private static ByteSource getInputStream() {
95 return Resources.asByteSource(TestModel.class.getResource(CONSTRAINTS_VALIDATION_TEST_YANG));
98 public static SchemaContext createTestContext() throws IOException, YangSyntaxErrorException {
99 final YangParserImpl parser = new YangParserImpl();
100 return parser.parseSources(Collections.singletonList(getInputStream()));
104 public void minMaxListTestPass() throws DataValidationFailedException {
106 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
107 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
108 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
109 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
110 .withChild(fooEntryNode).build();
111 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
112 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
113 .withChild(barEntryNode).build();
115 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
116 modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
117 modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
118 // TODO: check why write and then merge on list commits only "bar" child
119 modificationTree.ready();
121 inMemoryDataTree.validate(modificationTree);
122 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
123 inMemoryDataTree.commit(prepare);
125 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
126 final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
127 assertTrue(minMaxListRead.isPresent());
128 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
131 @Test(expected=DataValidationFailedException.class)
132 public void minMaxListFail() throws DataValidationFailedException {
133 InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
135 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
136 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
137 final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
138 final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
139 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
140 .withChild(fooEntryNode).build();
142 final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
143 final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
144 final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
146 modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
147 modificationTree.merge(barPath, barEntryNode);
148 modificationTree.write(gooPath, gooEntryNode);
149 modificationTree.delete(gooPath);
150 modificationTree.ready();
152 inMemoryDataTree.validate(modificationTree);
153 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
154 inMemoryDataTree.commit(prepare1);
156 InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
157 Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
158 assertTrue(minMaxListRead.isPresent());
159 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
161 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
162 modificationTree.write(gooPath, gooEntryNode);
163 modificationTree.ready();
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);
178 modificationTree.ready();
180 inMemoryDataTree.validate(modificationTree);
184 public void minMaxLeafListPass() throws DataValidationFailedException {
185 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
187 final NodeWithValue<?> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
188 final NodeWithValue<?> gooPath = new 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 NodeIdentifier(MIN_MAX_LEAF_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));
205 modificationTree.ready();
207 inMemoryDataTree.validate(modificationTree);
208 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
209 inMemoryDataTree.commit(prepare1);
211 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
212 final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
213 assertTrue(masterContainer.isPresent());
214 final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get()).getChild(
215 new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
216 assertTrue(leafList.isPresent());
217 assertTrue(leafList.get().getValue().size() == 2);
220 @Test(expected=DataValidationFailedException.class)
221 public void minMaxLeafListFail() throws DataValidationFailedException {
222 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
224 final NodeWithValue<?> fooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "foo");
225 final NodeWithValue<?> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
226 final NodeWithValue<?> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
227 final NodeWithValue<?> fuuPath = new 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 NodeIdentifier(MIN_MAX_LEAF_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);
247 modificationTree.ready();
249 inMemoryDataTree.validate(modificationTree);
253 public void unkeyedListTestPass() throws DataValidationFailedException {
254 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
256 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
257 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
258 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
259 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
260 unkeyedEntries.add(foo);
261 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
262 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
263 .withValue(unkeyedEntries).build();
265 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
266 modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
267 modificationTree.ready();
269 inMemoryDataTree.validate(modificationTree);
270 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
271 inMemoryDataTree.commit(prepare1);
273 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
274 final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
275 assertTrue(unkeyedListRead.isPresent());
276 assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
279 @Test(expected=DataValidationFailedException.class)
280 public void unkeyedListTestFail() throws DataValidationFailedException {
281 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
283 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
284 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
285 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
286 final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
287 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
288 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
289 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
290 unkeyedEntries.add(foo);
291 unkeyedEntries.add(bar);
292 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
293 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
294 .withValue(unkeyedEntries).build();
296 modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
297 modificationTree.ready();
299 inMemoryDataTree.validate(modificationTree);