BUG-4688: align Optional/nullable Date usage
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / 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.impl.schema.tree;
9
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;
13
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;
27
28 public class MandatoryLeafTest {
29
30     private SchemaContext schemaContext;
31
32     @Before
33     public void prepare() throws ReactorException {
34         schemaContext = TestModel.createTestContext("/mandatory-leaf-test.yang");
35         assertNotNull("Schema context must not be null.", schemaContext);
36     }
37
38     private InMemoryDataTree initDataTree(final boolean enableValidation) {
39         final InMemoryDataTree inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(
40                 new DataTreeConfiguration.Builder(TreeType.CONFIGURATION).setMandatoryNodesValidation(enableValidation)
41                         .build());
42         inMemoryDataTree.setSchemaContext(schemaContext);
43         return inMemoryDataTree;
44     }
45
46     @Test
47     public void testCorrectMandatoryLeafWrite() throws DataValidationFailedException {
48         final InMemoryDataTree inMemoryDataTree = initDataTree(true);
49         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
50
51         final ContainerNode container = Builders
52                 .containerBuilder()
53                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
54                 .withChild(
55                         Builders.choiceBuilder()
56                                 .withNodeIdentifier(choice1Id)
57                                 .withChild(
58                                         Builders.containerBuilder()
59                                                 .withNodeIdentifier(
60                                                         new NodeIdentifier(QName.create(TestModel.TEST_QNAME,
61                                                                 "case2-cont")))
62                                                 .withChild(
63                                                         leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"),
64                                                                 "leaf-value"))
65                                                 .withChild(
66                                                         leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"),
67                                                                 "leaf-value2")).build()).build()).build();
68
69         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
70         modificationTree.write(TestModel.TEST_PATH, container);
71         modificationTree.ready();
72
73         inMemoryDataTree.validate(modificationTree);
74         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
75         inMemoryDataTree.commit(prepare);
76     }
77
78     @Test
79     public void testCorrectMandatoryLeafChoiceWrite() throws DataValidationFailedException {
80         final InMemoryDataTree inMemoryDataTree = initDataTree(true);
81         // Container write
82         final ContainerNode container = Builders.containerBuilder()
83                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
84
85         final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
86         modificationTree1.write(TestModel.TEST_PATH, container);
87         modificationTree1.ready();
88
89         inMemoryDataTree.validate(modificationTree1);
90         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
91         inMemoryDataTree.commit(prepare1);
92
93         // Choice write
94         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
95         final ChoiceNode choice = Builders
96                 .choiceBuilder()
97                 .withNodeIdentifier(choice1Id)
98                 .withChild(
99                         Builders.containerBuilder()
100                                 .withNodeIdentifier(
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"))
104                                 .build()).build();
105
106         final InMemoryDataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
107         modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
108         modificationTree2.ready();
109
110         inMemoryDataTree.validate(modificationTree2);
111         final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
112         inMemoryDataTree.commit(prepare2);
113     }
114
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"));
119
120         final ContainerNode container = Builders
121                 .containerBuilder()
122                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
123                 .withChild(
124                         Builders.choiceBuilder()
125                                 .withNodeIdentifier(choice1Id)
126                                 .withChild(
127                                         Builders.containerBuilder()
128                                                 .withNodeIdentifier(
129                                                         new NodeIdentifier(QName.create(TestModel.TEST_QNAME,
130                                                                 "case2-cont")))
131                                                 .withChild(
132                                                         leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"),
133                                                                 "leaf-value2")).build()).build()).build();
134         try {
135             final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
136             modificationTree.write(TestModel.TEST_PATH, container);
137             modificationTree.ready();
138
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",
146                     e.getMessage());
147             throw e;
148         }
149     }
150
151     @Test
152     public void testDisabledValidation() throws DataValidationFailedException {
153         final InMemoryDataTree inMemoryDataTree = initDataTree(false);
154         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
155
156         final ContainerNode container = Builders
157                 .containerBuilder()
158                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
159                 .withChild(
160                         Builders.choiceBuilder()
161                                 .withNodeIdentifier(choice1Id)
162                                 .withChild(
163                                         Builders.containerBuilder()
164                                                 .withNodeIdentifier(
165                                                         new NodeIdentifier(QName.create(TestModel.TEST_QNAME,
166                                                                 "case2-cont")))
167                                                 .withChild(
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();
173
174         inMemoryDataTree.validate(modificationTree);
175         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
176         inMemoryDataTree.commit(prepare);
177     }
178
179     @Test(expected = IllegalArgumentException.class)
180     public void testMandatoryLeafViolationChoiceWrite() throws DataValidationFailedException {
181         final InMemoryDataTree inMemoryDataTree = initDataTree(true);
182         // Container write
183         final ContainerNode container = Builders.containerBuilder()
184                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
185
186         final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
187         modificationTree1.write(TestModel.TEST_PATH, container);
188         modificationTree1.ready();
189
190         inMemoryDataTree.validate(modificationTree1);
191         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
192         inMemoryDataTree.commit(prepare1);
193
194         // Choice write
195         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
196         final ChoiceNode choice = Builders
197                 .choiceBuilder()
198                 .withNodeIdentifier(choice1Id)
199                 .withChild(
200                         Builders.containerBuilder()
201                                 .withNodeIdentifier(
202                                         new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
203                                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
204                                 .build()).build();
205
206         try {
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",
217                     e.getMessage());
218             throw e;
219         }
220     }
221
222     @Test
223     public void testDisabledValidationChoiceWrite() throws DataValidationFailedException {
224         final InMemoryDataTree inMemoryDataTree = initDataTree(false);
225         // Container write
226         final ContainerNode container = Builders.containerBuilder()
227                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
228
229         final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
230         modificationTree1.write(TestModel.TEST_PATH, container);
231         modificationTree1.ready();
232
233         inMemoryDataTree.validate(modificationTree1);
234         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
235         inMemoryDataTree.commit(prepare1);
236
237         // Choice write
238         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
239         final ChoiceNode choice = Builders
240                 .choiceBuilder()
241                 .withNodeIdentifier(choice1Id)
242                 .withChild(
243                         Builders.containerBuilder()
244                                 .withNodeIdentifier(
245                                         new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
246                                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf2"), "leaf-value2"))
247                                 .build()).build();
248
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);
255     }
256 }