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