b0218a832dfabfd7c95571720d083a4e49ffea77
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / CaseExclusionTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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 org.junit.AfterClass;
14 import org.junit.Before;
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.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
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.impl.schema.ImmutableNodes;
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 CaseExclusionTest {
32     private static EffectiveModelContext SCHEMA_CONTEXT;
33
34     private DataTree inMemoryDataTree;
35
36     @BeforeClass
37     public static void beforeClass() {
38         SCHEMA_CONTEXT = TestModel.createTestContext("/case-exclusion-test.yang");
39     }
40
41     @AfterClass
42     public static void afterClass() {
43         SCHEMA_CONTEXT = null;
44     }
45
46     @Before
47     public void before() {
48         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
49             SCHEMA_CONTEXT);
50     }
51
52     @Test
53     public void testCorrectCaseWrite() throws DataValidationFailedException {
54         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
55
56         final ContainerNode container = Builders
57                 .containerBuilder()
58                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
59                 .withChild(
60                         Builders.choiceBuilder().withNodeIdentifier(choice1Id)
61                                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
62                                 .build()).build();
63         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
64         modificationTree.write(TestModel.TEST_PATH, container);
65         modificationTree.ready();
66
67         inMemoryDataTree.validate(modificationTree);
68         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
69         inMemoryDataTree.commit(prepare);
70     }
71
72     @Test(expected = IllegalArgumentException.class)
73     public void testCaseExclusion() throws DataValidationFailedException {
74         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
75
76         final ContainerNode container = Builders
77                 .containerBuilder()
78                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
79                 .withChild(
80                         Builders.choiceBuilder()
81                                 .withNodeIdentifier(choice1Id)
82                                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
83                                 .withChild(
84                                         ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont")))
85                                 .build()).build();
86         try {
87             final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
88             modificationTree.write(TestModel.TEST_PATH, container);
89             modificationTree.ready();
90
91             inMemoryDataTree.validate(modificationTree);
92             final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
93             inMemoryDataTree.commit(prepare);
94         } catch (IllegalArgumentException e) {
95             assertTrue(e.getMessage().contains("implies non-presence of child"));
96             throw e;
97         }
98     }
99
100     @Test(expected = IllegalArgumentException.class)
101     public void testCaseExclusionOnChoiceWrite() throws DataValidationFailedException {
102         // Container write
103         final ContainerNode container = Builders.containerBuilder()
104                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
105
106         final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
107         modificationTree1.write(TestModel.TEST_PATH, container);
108         modificationTree1.ready();
109
110         inMemoryDataTree.validate(modificationTree1);
111         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
112         inMemoryDataTree.commit(prepare1);
113
114         // Choice write
115         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
116         final ChoiceNode choice = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
117                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
118                 .withChild(ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont"))).build();
119
120         try {
121             final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
122             modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
123             modificationTree2.ready();
124
125             inMemoryDataTree.validate(modificationTree2);
126
127             final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
128             inMemoryDataTree.commit(prepare2);
129         } catch (IllegalArgumentException e) {
130             assertTrue(e.getMessage().contains("implies non-presence of child"));
131             throw e;
132         }
133     }
134 }