Remove Augmentation{Identifier,Node}
[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
30 public class CaseAugmentTest {
31     private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
32     private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
33     private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
34     private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
35
36     private static EffectiveModelContext SCHEMA_CONTEXT;
37
38     @BeforeClass
39     public static void beforeClass() {
40         SCHEMA_CONTEXT = TestModel.createTestContext("/case-augment-test.yang");
41     }
42
43     @AfterClass
44     public static void afterClass() {
45         SCHEMA_CONTEXT = null;
46     }
47
48     private static DataTree initDataTree() {
49         DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
50             SCHEMA_CONTEXT);
51         return inMemoryDataTree;
52     }
53
54     @Test
55     public void testWriteAugment() throws DataValidationFailedException {
56         final DataTree inMemoryDataTree = initDataTree();
57
58         final ContainerNode container = Builders.containerBuilder()
59             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
60             .withChild(Builders.choiceBuilder()
61                 .withNodeIdentifier(CHOICE_ID)
62                 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
63                 .build())
64             .build();
65         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
66         modificationTree.write(TestModel.TEST_PATH, container);
67         modificationTree.ready();
68
69         inMemoryDataTree.validate(modificationTree);
70         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
71         inMemoryDataTree.commit(prepare);
72     }
73
74     @Test
75     public void testWriteCase1All() throws DataValidationFailedException {
76         final DataTree inMemoryDataTree = initDataTree();
77
78         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
79         modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
80             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
81             .withChild(Builders.choiceBuilder()
82                 .withNodeIdentifier(CHOICE_ID)
83                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
84                 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
85                 .withChild(leafNode(C1L3_QNAME, "leaf-value"))
86                 .build())
87             .build());
88         modificationTree.ready();
89
90         inMemoryDataTree.validate(modificationTree);
91         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
92         inMemoryDataTree.commit(prepare);
93     }
94
95     @Test
96     public void testWriteConflict() throws DataValidationFailedException {
97         final DataTreeModification modificationTree = initDataTree().takeSnapshot().newModification();
98         modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
99             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
100             .withChild(Builders.choiceBuilder()
101                 .withNodeIdentifier(CHOICE_ID)
102                 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
103                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
104                 .build())
105             .build());
106
107         final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, modificationTree::ready);
108         assertThat(e.getMessage(), containsString(" implies non-presence of child "));
109     }
110 }