Rename opendaylight.mdsal.binding.runtime.spi
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / CaseAugmentTest.java
1 /*
2  * Copyright (c) 2017 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.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.spi.node.ImmutableNodes;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
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 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 class CaseAugmentTest {
28     private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
29     private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
30     private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
31     private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
32
33     private static EffectiveModelContext SCHEMA_CONTEXT;
34
35     @BeforeAll
36     static void beforeClass() {
37         SCHEMA_CONTEXT = YangParserTestUtils.parseYang("""
38             module case-augment-test {
39               yang-version 1;
40               namespace "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test";
41               prefix "store-test";
42
43               revision "2014-03-13" {
44                 description "Initial revision.";
45               }
46
47               container test {
48                 choice choice1 {
49                   case case1 {
50                     leaf case1-leaf1 {
51                       type string;
52                     }
53                   }
54                   case case2 {
55                     leaf case2-leaf1 {
56                       type string;
57                     }
58                   }
59                 }
60               }
61
62               augment "/test/choice1/case1" {
63                 leaf case1-leaf2 {
64                   type string;
65                 }
66                 leaf case1-leaf3 {
67                   type string;
68                 }
69               }
70             }""");
71     }
72
73     @AfterAll
74     static void afterClass() {
75         SCHEMA_CONTEXT = null;
76     }
77
78     private static DataTree initDataTree() {
79         return new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
80             SCHEMA_CONTEXT);
81     }
82
83     @Test
84     void testWriteAugment() throws DataValidationFailedException {
85         final var inMemoryDataTree = initDataTree();
86
87         final var container = ImmutableNodes.newContainerBuilder()
88             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
89             .withChild(ImmutableNodes.newChoiceBuilder()
90                 .withNodeIdentifier(CHOICE_ID)
91                 .withChild(ImmutableNodes.leafNode(C1L2_QNAME, "leaf-value"))
92                 .build())
93             .build();
94         final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
95         modificationTree.write(TestModel.TEST_PATH, container);
96         modificationTree.ready();
97
98         inMemoryDataTree.validate(modificationTree);
99         final var prepare = inMemoryDataTree.prepare(modificationTree);
100         inMemoryDataTree.commit(prepare);
101     }
102
103     @Test
104     void testWriteCase1All() throws DataValidationFailedException {
105         final var inMemoryDataTree = initDataTree();
106
107         final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
108         modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
109             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
110             .withChild(ImmutableNodes.newChoiceBuilder()
111                 .withNodeIdentifier(CHOICE_ID)
112                 .withChild(ImmutableNodes.leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
113                 .withChild(ImmutableNodes.leafNode(C1L2_QNAME, "leaf-value"))
114                 .withChild(ImmutableNodes.leafNode(C1L3_QNAME, "leaf-value"))
115                 .build())
116             .build());
117         modificationTree.ready();
118
119         inMemoryDataTree.validate(modificationTree);
120         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
121         inMemoryDataTree.commit(prepare);
122     }
123
124     @Test
125     void testWriteConflict() throws DataValidationFailedException {
126         final var modificationTree = initDataTree().takeSnapshot().newModification();
127         modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
128             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
129             .withChild(ImmutableNodes.newChoiceBuilder()
130                 .withNodeIdentifier(CHOICE_ID)
131                 .withChild(ImmutableNodes.leafNode(C1L2_QNAME, "leaf-value"))
132                 .withChild(ImmutableNodes.leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
133                 .build())
134             .build());
135
136         final var e = assertThrows(IllegalArgumentException.class, modificationTree::ready);
137         assertTrue(e.getMessage().contains(" implies non-presence of child "));
138     }
139 }