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