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