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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
24 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 public class MandatoryLeafTest {
30 private SchemaContext schemaContext;
33 public void prepare() throws ReactorException {
34 schemaContext = TestModel.createTestContext("/mandatory-leaf-test.yang");
35 assertNotNull("Schema context must not be null.", schemaContext);
38 private InMemoryDataTree initDataTree(final boolean enableValidation) {
39 final InMemoryDataTree inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(
40 new DataTreeConfiguration.Builder(TreeType.CONFIGURATION).setMandatoryNodesValidation(enableValidation)
42 inMemoryDataTree.setSchemaContext(schemaContext);
43 return inMemoryDataTree;
47 public void testCorrectMandatoryLeafWrite() throws DataValidationFailedException {
48 final InMemoryDataTree inMemoryDataTree = initDataTree(true);
49 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
51 final ContainerNode container = Builders
53 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
55 Builders.choiceBuilder()
56 .withNodeIdentifier(choice1Id)
58 Builders.containerBuilder()
60 new NodeIdentifier(QName.create(TestModel.TEST_QNAME,
63 leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"),
66 leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"),
67 "leaf-value2")).build()).build()).build();
69 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
70 modificationTree.write(TestModel.TEST_PATH, container);
71 modificationTree.ready();
73 inMemoryDataTree.validate(modificationTree);
74 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
75 inMemoryDataTree.commit(prepare);
79 public void testCorrectMandatoryLeafChoiceWrite() throws DataValidationFailedException {
80 final InMemoryDataTree inMemoryDataTree = initDataTree(true);
82 final ContainerNode container = Builders.containerBuilder()
83 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
85 final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
86 modificationTree1.write(TestModel.TEST_PATH, container);
87 modificationTree1.ready();
89 inMemoryDataTree.validate(modificationTree1);
90 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
91 inMemoryDataTree.commit(prepare1);
94 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
95 final ChoiceNode choice = Builders
97 .withNodeIdentifier(choice1Id)
99 Builders.containerBuilder()
101 new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
102 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
103 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
106 final InMemoryDataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
107 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
108 modificationTree2.ready();
110 inMemoryDataTree.validate(modificationTree2);
111 final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
112 inMemoryDataTree.commit(prepare2);
115 @Test(expected = IllegalArgumentException.class)
116 public void testMandatoryLeafViolation() throws DataValidationFailedException {
117 final InMemoryDataTree inMemoryDataTree = initDataTree(true);
118 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
120 final ContainerNode container = Builders
122 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
124 Builders.choiceBuilder()
125 .withNodeIdentifier(choice1Id)
127 Builders.containerBuilder()
129 new NodeIdentifier(QName.create(TestModel.TEST_QNAME,
132 leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"),
133 "leaf-value2")).build()).build()).build();
135 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
136 modificationTree.write(TestModel.TEST_PATH, container);
137 modificationTree.ready();
139 inMemoryDataTree.validate(modificationTree);
140 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
141 inMemoryDataTree.commit(prepare);
142 } catch (final IllegalArgumentException e) {
143 assertEquals("Node (urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?"
144 + "revision=2014-03-13)choice1 is missing mandatory descendant /(urn:opendaylight:params:xml:ns:"
145 + "yang:controller:md:sal:dom:store:test?revision=2014-03-13)case2-cont/case2-leaf1",
152 public void testDisabledValidation() throws DataValidationFailedException {
153 final InMemoryDataTree inMemoryDataTree = initDataTree(false);
154 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
156 final ContainerNode container = Builders
158 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
160 Builders.choiceBuilder()
161 .withNodeIdentifier(choice1Id)
163 Builders.containerBuilder()
165 new NodeIdentifier(QName.create(TestModel.TEST_QNAME,
168 leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"),
169 "leaf-value2")).build()).build()).build();
170 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
171 modificationTree.write(TestModel.TEST_PATH, container);
172 modificationTree.ready();
174 inMemoryDataTree.validate(modificationTree);
175 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
176 inMemoryDataTree.commit(prepare);
179 @Test(expected = IllegalArgumentException.class)
180 public void testMandatoryLeafViolationChoiceWrite() throws DataValidationFailedException {
181 final InMemoryDataTree inMemoryDataTree = initDataTree(true);
183 final ContainerNode container = Builders.containerBuilder()
184 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
186 final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
187 modificationTree1.write(TestModel.TEST_PATH, container);
188 modificationTree1.ready();
190 inMemoryDataTree.validate(modificationTree1);
191 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
192 inMemoryDataTree.commit(prepare1);
195 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
196 final ChoiceNode choice = Builders
198 .withNodeIdentifier(choice1Id)
200 Builders.containerBuilder()
202 new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
203 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
207 final InMemoryDataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
208 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
209 modificationTree2.ready();
210 inMemoryDataTree.validate(modificationTree2);
211 final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
212 inMemoryDataTree.commit(prepare2);
213 } catch (final IllegalArgumentException e) {
214 assertEquals("Node (urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?"
215 + "revision=2014-03-13)choice1 is missing mandatory descendant /(urn:opendaylight:params:xml:ns:"
216 + "yang:controller:md:sal:dom:store:test?revision=2014-03-13)case2-cont/case2-leaf1",
223 public void testDisabledValidationChoiceWrite() throws DataValidationFailedException {
224 final InMemoryDataTree inMemoryDataTree = initDataTree(false);
226 final ContainerNode container = Builders.containerBuilder()
227 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
229 final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
230 modificationTree1.write(TestModel.TEST_PATH, container);
231 modificationTree1.ready();
233 inMemoryDataTree.validate(modificationTree1);
234 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
235 inMemoryDataTree.commit(prepare1);
238 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
239 final ChoiceNode choice = Builders
241 .withNodeIdentifier(choice1Id)
243 Builders.containerBuilder()
245 new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
246 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
249 final InMemoryDataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
250 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
251 modificationTree2.ready();
252 inMemoryDataTree.validate(modificationTree2);
253 final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
254 inMemoryDataTree.commit(prepare2);