2 * Copyright (c) 2015 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.util.ArrayList;
15 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
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.api.schema.tree.TreeType;
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.test.util.YangParserTestUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class ListConstraintsValidation {
45 private static final Logger LOG = LoggerFactory.getLogger(ListConstraintsValidation.class);
47 private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
48 private SchemaContext schemaContext;
50 private InMemoryDataTree inMemoryDataTree;
52 private static final QName MASTER_CONTAINER_QNAME = QName.create(
53 "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
65 .of(MASTER_CONTAINER_QNAME);
66 private static final YangInstanceIdentifier MIN_MAX_LIST_PATH = YangInstanceIdentifier
67 .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LIST_QNAME).build();
68 private static final YangInstanceIdentifier UNBOUNDED_LIST_PATH = YangInstanceIdentifier
69 .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LIST_QNAME).build();
70 private static final YangInstanceIdentifier MIN_MAX_LEAF_LIST_PATH = YangInstanceIdentifier
71 .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LEAF_LIST_QNAME).build();
72 private static final YangInstanceIdentifier UNBOUNDED_LEAF_LIST_PATH = YangInstanceIdentifier
73 .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LEAF_LIST_QNAME).build();
74 private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier
75 .builder(MASTER_CONTAINER_PATH).node(UNKEYED_LIST_QNAME).build();
78 public void prepare() {
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));
91 public static SchemaContext createTestContext() {
92 return YangParserTestUtils.parseYangResource(CONSTRAINTS_VALIDATION_TEST_YANG);
96 public void minMaxListTestPass() throws DataValidationFailedException {
98 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
99 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
100 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
101 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
102 .withChild(fooEntryNode).build();
103 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
104 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
105 .withChild(barEntryNode).build();
107 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
108 modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
109 modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
110 // TODO: check why write and then merge on list commits only "bar" child
111 modificationTree.ready();
113 inMemoryDataTree.validate(modificationTree);
114 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
115 inMemoryDataTree.commit(prepare);
117 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
118 final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
119 assertTrue(minMaxListRead.isPresent());
120 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
123 @Test(expected=DataValidationFailedException.class)
124 public void minMaxListFail() throws DataValidationFailedException {
125 InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
127 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
128 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
129 final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
130 final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
131 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
132 .withChild(fooEntryNode).build();
134 final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
135 final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
136 final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
138 modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
139 modificationTree.merge(barPath, barEntryNode);
140 modificationTree.write(gooPath, gooEntryNode);
141 modificationTree.delete(gooPath);
142 modificationTree.ready();
144 inMemoryDataTree.validate(modificationTree);
145 DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
146 inMemoryDataTree.commit(prepare1);
148 InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
149 Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
150 assertTrue(minMaxListRead.isPresent());
151 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
153 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
154 modificationTree.write(gooPath, gooEntryNode);
155 modificationTree.ready();
157 inMemoryDataTree.validate(modificationTree);
158 prepare1 = inMemoryDataTree.prepare(modificationTree);
159 inMemoryDataTree.commit(prepare1);
161 snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
162 minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
163 assertTrue(minMaxListRead.isPresent());
164 assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
166 modificationTree = inMemoryDataTree.takeSnapshot().newModification();
168 modificationTree.delete(gooPath);
169 modificationTree.delete(fooPath);
170 modificationTree.ready();
172 inMemoryDataTree.validate(modificationTree);
176 public void minMaxLeafListPass() throws DataValidationFailedException {
177 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
179 final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
180 final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
182 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
183 .withNodeIdentifier(barPath)
184 .withValue("bar").build();
185 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
186 .withNodeIdentifier(gooPath)
187 .withValue("goo").build();
189 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
190 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
191 .withChildValue("foo").build();
193 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
194 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
195 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
196 modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
197 modificationTree.ready();
199 inMemoryDataTree.validate(modificationTree);
200 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
201 inMemoryDataTree.commit(prepare1);
203 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
204 final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
205 assertTrue(masterContainer.isPresent());
206 final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get()).getChild(
207 new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
208 assertTrue(leafList.isPresent());
209 assertTrue(leafList.get().getValue().size() == 2);
212 @Test(expected=DataValidationFailedException.class)
213 public void minMaxLeafListFail() throws DataValidationFailedException {
214 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
216 final NodeWithValue<Object> fooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "foo");
217 final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
218 final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
219 final NodeWithValue<Object> fuuPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "fuu");
221 final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
222 .withNodeIdentifier(barPath)
223 .withValue("bar").build();
224 final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
225 .withNodeIdentifier(gooPath)
226 .withValue("goo").build();
227 final LeafSetEntryNode<Object> fuuLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
228 .withNodeIdentifier(fuuPath)
229 .withValue("fuu").build();
231 final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
232 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
233 .withChildValue("foo").build();
235 modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
236 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
237 modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
238 modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry);
239 modificationTree.ready();
241 inMemoryDataTree.validate(modificationTree);
245 public void unkeyedListTestPass() throws DataValidationFailedException {
246 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
248 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
249 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
250 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
251 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
252 unkeyedEntries.add(foo);
253 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
254 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
255 .withValue(unkeyedEntries).build();
257 modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
258 modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
259 modificationTree.ready();
261 inMemoryDataTree.validate(modificationTree);
262 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
263 inMemoryDataTree.commit(prepare1);
265 final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
266 final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
267 assertTrue(unkeyedListRead.isPresent());
268 assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
271 @Test(expected=DataValidationFailedException.class)
272 public void unkeyedListTestFail() throws DataValidationFailedException {
273 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
275 final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
276 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
277 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
278 final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
279 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
280 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
281 final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
282 unkeyedEntries.add(foo);
283 unkeyedEntries.add(bar);
284 final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
285 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
286 .withValue(unkeyedEntries).build();
288 modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
289 modificationTree.ready();
291 inMemoryDataTree.validate(modificationTree);