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