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