331bb39d2a6ecc502db98281454e019b9e4c08ff
[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.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertThrows;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
14
15 import org.junit.AfterClass;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
27 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30
31 public class CaseAugmentTest {
32     private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
33     private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
34     private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
35     private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
36
37     private static EffectiveModelContext SCHEMA_CONTEXT;
38
39     @BeforeClass
40     public static void beforeClass() {
41         SCHEMA_CONTEXT = YangParserTestUtils.parseYang("""
42             module case-augment-test {
43               yang-version 1;
44               namespace "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test";
45               prefix "store-test";
46
47               revision "2014-03-13" {
48                 description "Initial revision.";
49               }
50
51               container test {
52                 choice choice1 {
53                   case case1 {
54                     leaf case1-leaf1 {
55                       type string;
56                     }
57                   }
58                   case case2 {
59                     leaf case2-leaf1 {
60                       type string;
61                     }
62                   }
63                 }
64               }
65
66               augment "/test/choice1/case1" {
67                 leaf case1-leaf2 {
68                   type string;
69                 }
70                 leaf case1-leaf3 {
71                   type string;
72                 }
73               }
74             }""");
75     }
76
77     @AfterClass
78     public static void afterClass() {
79         SCHEMA_CONTEXT = null;
80     }
81
82     private static DataTree initDataTree() {
83         DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
84             SCHEMA_CONTEXT);
85         return inMemoryDataTree;
86     }
87
88     @Test
89     public void testWriteAugment() throws DataValidationFailedException {
90         final DataTree inMemoryDataTree = initDataTree();
91
92         final ContainerNode container = Builders.containerBuilder()
93             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
94             .withChild(Builders.choiceBuilder()
95                 .withNodeIdentifier(CHOICE_ID)
96                 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
97                 .build())
98             .build();
99         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
100         modificationTree.write(TestModel.TEST_PATH, container);
101         modificationTree.ready();
102
103         inMemoryDataTree.validate(modificationTree);
104         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
105         inMemoryDataTree.commit(prepare);
106     }
107
108     @Test
109     public void testWriteCase1All() throws DataValidationFailedException {
110         final DataTree inMemoryDataTree = initDataTree();
111
112         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
113         modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
114             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
115             .withChild(Builders.choiceBuilder()
116                 .withNodeIdentifier(CHOICE_ID)
117                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
118                 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
119                 .withChild(leafNode(C1L3_QNAME, "leaf-value"))
120                 .build())
121             .build());
122         modificationTree.ready();
123
124         inMemoryDataTree.validate(modificationTree);
125         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
126         inMemoryDataTree.commit(prepare);
127     }
128
129     @Test
130     public void testWriteConflict() throws DataValidationFailedException {
131         final DataTreeModification modificationTree = initDataTree().takeSnapshot().newModification();
132         modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
133             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
134             .withChild(Builders.choiceBuilder()
135                 .withNodeIdentifier(CHOICE_ID)
136                 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
137                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
138                 .build())
139             .build());
140
141         final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, modificationTree::ready);
142         assertThat(e.getMessage(), containsString(" implies non-presence of child "));
143     }
144 }