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