Clean up TreeNode API
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / CaseExclusionTest.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.assertThrows;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12
13 import org.junit.jupiter.api.AfterAll;
14 import org.junit.jupiter.api.BeforeAll;
15 import org.junit.jupiter.api.BeforeEach;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
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.impl.di.InMemoryDataTreeFactory;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25
26 class CaseExclusionTest {
27     private static EffectiveModelContext SCHEMA_CONTEXT;
28
29     private DataTree inMemoryDataTree;
30
31     @BeforeAll
32     static void beforeClass() {
33         SCHEMA_CONTEXT = TestModel.createTestContext("/case-exclusion-test.yang");
34     }
35
36     @AfterAll
37     static void afterClass() {
38         SCHEMA_CONTEXT = null;
39     }
40
41     @BeforeEach
42     void before() {
43         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
44             SCHEMA_CONTEXT);
45     }
46
47     @Test
48     void testCorrectCaseWrite() throws DataValidationFailedException {
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.leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
56                 .build())
57             .build();
58         final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
59         modificationTree.write(TestModel.TEST_PATH, container);
60         modificationTree.ready();
61
62         inMemoryDataTree.validate(modificationTree);
63         final var prepare = inMemoryDataTree.prepare(modificationTree);
64         inMemoryDataTree.commit(prepare);
65     }
66
67     @Test
68     void testCaseExclusion() {
69         assertThrows(IllegalArgumentException.class, () -> {
70             final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
71             final var container = ImmutableNodes.newContainerBuilder()
72                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
73                 .withChild(ImmutableNodes.newChoiceBuilder()
74                     .withNodeIdentifier(choice1Id)
75                     .withChild(ImmutableNodes.leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
76                     .withChild(ImmutableNodes.newContainerBuilder()
77                         .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
78                         .build())
79                     .build())
80                 .build();
81
82             try {
83                 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
84                 modificationTree.write(TestModel.TEST_PATH, container);
85                 modificationTree.ready();
86
87                 inMemoryDataTree.validate(modificationTree);
88                 final var prepare = inMemoryDataTree.prepare(modificationTree);
89                 inMemoryDataTree.commit(prepare);
90             } catch (IllegalArgumentException e) {
91                 assertTrue(e.getMessage().contains("implies non-presence of child"));
92                 throw e;
93             }
94         });
95     }
96
97     @Test
98     void testCaseExclusionOnChoiceWrite() {
99         assertThrows(IllegalArgumentException.class, () -> {
100             // Container write
101             final var container = ImmutableNodes.newContainerBuilder()
102                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
103
104             final var modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
105             modificationTree1.write(TestModel.TEST_PATH, container);
106             modificationTree1.ready();
107
108             inMemoryDataTree.validate(modificationTree1);
109             final var prepare1 = inMemoryDataTree.prepare(modificationTree1);
110             inMemoryDataTree.commit(prepare1);
111
112             // Choice write
113             final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
114             final var choice = ImmutableNodes.newChoiceBuilder().withNodeIdentifier(choice1Id)
115                 .withChild(ImmutableNodes.leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
116                 .withChild(ImmutableNodes.newContainerBuilder()
117                     .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
118                     .build())
119                 .build();
120
121             try {
122                 final var modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
123                 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
124                 modificationTree2.ready();
125
126                 inMemoryDataTree.validate(modificationTree2);
127
128                 final var prepare2 = inMemoryDataTree.prepare(modificationTree2);
129                 inMemoryDataTree.commit(prepare2);
130             } catch (IllegalArgumentException e) {
131                 assertTrue(e.getMessage().contains("implies non-presence of child"));
132                 throw e;
133             }
134         });
135     }
136 }