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