Split out yang-data-tree-impl
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / MandatoryLeafTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.tree.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
12
13 import org.junit.AfterClass;
14 import org.junit.BeforeClass;
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.impl.schema.Builders;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
26 import org.opendaylight.yangtools.yang.data.tree.api.TreeType;
27 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29
30 public class MandatoryLeafTest {
31     private static EffectiveModelContext SCHEMA_CONTEXT;
32
33     @BeforeClass
34     public static void beforeClass() {
35         SCHEMA_CONTEXT = TestModel.createTestContext("/mandatory-leaf-test.yang");
36     }
37
38     @AfterClass
39     public static void afterClass() {
40         SCHEMA_CONTEXT = null;
41     }
42
43     private static DataTree initDataTree(final boolean enableValidation) {
44         return new InMemoryDataTreeFactory().create(new DataTreeConfiguration.Builder(TreeType.CONFIGURATION)
45             .setMandatoryNodesValidation(enableValidation)
46             .build(), SCHEMA_CONTEXT);
47     }
48
49     @Test
50     public void testCorrectMandatoryLeafWrite() throws DataValidationFailedException {
51         final DataTree inMemoryDataTree = initDataTree(true);
52         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
53
54         final ContainerNode container = Builders.containerBuilder()
55             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
56             .withChild(Builders.choiceBuilder()
57                 .withNodeIdentifier(choice1Id)
58                 .withChild(Builders.containerBuilder()
59                     .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
60                     .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
61                     .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
62                     .build())
63                 .build())
64             .build();
65
66         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
67         modificationTree.write(TestModel.TEST_PATH, container);
68         modificationTree.ready();
69
70         inMemoryDataTree.validate(modificationTree);
71         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
72         inMemoryDataTree.commit(prepare);
73     }
74
75     @Test
76     public void testCorrectMandatoryLeafChoiceWrite() throws DataValidationFailedException {
77         final DataTree inMemoryDataTree = initDataTree(true);
78         // Container write
79         final ContainerNode container = Builders.containerBuilder()
80             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
81             .build();
82
83         final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
84         modificationTree1.write(TestModel.TEST_PATH, container);
85         modificationTree1.ready();
86
87         inMemoryDataTree.validate(modificationTree1);
88         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
89         inMemoryDataTree.commit(prepare1);
90
91         // Choice write
92         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
93         final ChoiceNode choice = Builders.choiceBuilder()
94             .withNodeIdentifier(choice1Id)
95             .withChild(Builders.containerBuilder()
96                 .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
97                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
98                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
99                 .build())
100             .build();
101
102         final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
103         modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
104         modificationTree2.ready();
105
106         inMemoryDataTree.validate(modificationTree2);
107         final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
108         inMemoryDataTree.commit(prepare2);
109     }
110
111     @Test(expected = IllegalArgumentException.class)
112     public void testMandatoryLeafViolation() throws DataValidationFailedException {
113         final DataTree inMemoryDataTree = initDataTree(true);
114         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
115
116         final ContainerNode container = Builders.containerBuilder()
117             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
118             .withChild(Builders.choiceBuilder()
119                 .withNodeIdentifier(choice1Id)
120                 .withChild(Builders.containerBuilder()
121                     .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
122                     .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
123                     .build())
124                 .build())
125             .build();
126         try {
127             final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
128             modificationTree.write(TestModel.TEST_PATH, container);
129             modificationTree.ready();
130
131             inMemoryDataTree.validate(modificationTree);
132             final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
133             inMemoryDataTree.commit(prepare);
134         } catch (final IllegalArgumentException e) {
135             assertEquals("Node (urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?"
136                     + "revision=2014-03-13)choice1 is missing mandatory descendant /(urn:opendaylight:params:xml:ns:"
137                     + "yang:controller:md:sal:dom:store:test?revision=2014-03-13)case2-cont/case2-leaf1",
138                     e.getMessage());
139             throw e;
140         }
141     }
142
143     @Test
144     public void testDisabledValidation() throws DataValidationFailedException {
145         final DataTree inMemoryDataTree = initDataTree(false);
146         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
147
148         final ContainerNode container = Builders.containerBuilder()
149             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
150             .withChild(Builders.choiceBuilder()
151                 .withNodeIdentifier(choice1Id)
152                 .withChild(Builders.containerBuilder()
153                     .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
154                     .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
155                     .build())
156                 .build())
157             .build();
158         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
159         modificationTree.write(TestModel.TEST_PATH, container);
160         modificationTree.ready();
161
162         inMemoryDataTree.validate(modificationTree);
163         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
164         inMemoryDataTree.commit(prepare);
165     }
166
167     @Test(expected = IllegalArgumentException.class)
168     public void testMandatoryLeafViolationChoiceWrite() throws DataValidationFailedException {
169         final DataTree inMemoryDataTree = initDataTree(true);
170         // Container write
171         final ContainerNode container = Builders.containerBuilder()
172                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
173
174         final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
175         modificationTree1.write(TestModel.TEST_PATH, container);
176         modificationTree1.ready();
177
178         inMemoryDataTree.validate(modificationTree1);
179         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
180         inMemoryDataTree.commit(prepare1);
181
182         // Choice write
183         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
184         final ChoiceNode choice = Builders.choiceBuilder()
185             .withNodeIdentifier(choice1Id)
186             .withChild(Builders.containerBuilder()
187                 .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
188                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
189                 .build())
190             .build();
191
192         try {
193             final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
194             modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
195             modificationTree2.ready();
196             inMemoryDataTree.validate(modificationTree2);
197             final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
198             inMemoryDataTree.commit(prepare2);
199         } catch (final IllegalArgumentException e) {
200             assertEquals("Node (urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?"
201                     + "revision=2014-03-13)choice1 is missing mandatory descendant /(urn:opendaylight:params:xml:ns:"
202                     + "yang:controller:md:sal:dom:store:test?revision=2014-03-13)case2-cont/case2-leaf1",
203                     e.getMessage());
204             throw e;
205         }
206     }
207
208     @Test
209     public void testDisabledValidationChoiceWrite() throws DataValidationFailedException {
210         final DataTree inMemoryDataTree = initDataTree(false);
211         // Container write
212         final ContainerNode container = Builders.containerBuilder()
213                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
214
215         final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
216         modificationTree1.write(TestModel.TEST_PATH, container);
217         modificationTree1.ready();
218
219         inMemoryDataTree.validate(modificationTree1);
220         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
221         inMemoryDataTree.commit(prepare1);
222
223         // Choice write
224         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
225         final ChoiceNode choice = Builders.choiceBuilder()
226             .withNodeIdentifier(choice1Id)
227             .withChild(Builders.containerBuilder()
228                 .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
229                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
230                 .build())
231             .build();
232
233         final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
234         modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
235         modificationTree2.ready();
236         inMemoryDataTree.validate(modificationTree2);
237         final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
238         inMemoryDataTree.commit(prepare2);
239     }
240 }