Enforce case uniqueness
[yangtools.git] / yang / 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.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
23 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27
28 public class CaseExclusionTest {
29
30     private SchemaContext schemaContext;
31
32     @Before
33     public void prepare() throws ReactorException {
34         schemaContext = RetestModel.createTestContext("/case-exclusion-test.yang");
35         assertNotNull("Schema context must not be null.", schemaContext);
36     }
37
38     private InMemoryDataTree initDataTree() {
39         InMemoryDataTree inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(
40                 TreeType.CONFIGURATION);
41         inMemoryDataTree.setSchemaContext(schemaContext);
42         return inMemoryDataTree;
43     }
44
45     @Test
46     public void testCorrectCaseWrite() throws DataValidationFailedException {
47         final InMemoryDataTree inMemoryDataTree = initDataTree();
48         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
49
50         final ContainerNode container = Builders
51                 .containerBuilder()
52                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
53                 .withChild(
54                         Builders.choiceBuilder().withNodeIdentifier(choice1Id)
55                                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
56                                 .build()).build();
57         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
58         modificationTree.write(TestModel.TEST_PATH, container);
59         modificationTree.ready();
60
61         inMemoryDataTree.validate(modificationTree);
62         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
63         inMemoryDataTree.commit(prepare);
64     }
65
66     @Test(expected = IllegalArgumentException.class)
67     public void testCaseExclusion() throws DataValidationFailedException {
68         final InMemoryDataTree inMemoryDataTree = initDataTree();
69         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
70
71         final ContainerNode container = Builders
72                 .containerBuilder()
73                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
74                 .withChild(
75                         Builders.choiceBuilder()
76                                 .withNodeIdentifier(choice1Id)
77                                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
78                                 .withChild(
79                                         ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont")))
80                                 .build()).build();
81         try {
82             final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
83             modificationTree.write(TestModel.TEST_PATH, container);
84             modificationTree.ready();
85
86             inMemoryDataTree.validate(modificationTree);
87             final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
88             inMemoryDataTree.commit(prepare);
89         } catch (IllegalArgumentException e) {
90             assertTrue(e.getMessage().contains("implies non-presence of child"));
91             throw e;
92         }
93     }
94
95     @Test(expected = IllegalArgumentException.class)
96     public void testCaseExclusionOnChoiceWrite() throws DataValidationFailedException {
97         final InMemoryDataTree inMemoryDataTree = initDataTree();
98         // Container write
99         final ContainerNode container = Builders.containerBuilder()
100                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
101
102         final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
103         modificationTree1.write(TestModel.TEST_PATH, container);
104         modificationTree1.ready();
105
106         inMemoryDataTree.validate(modificationTree1);
107         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
108         inMemoryDataTree.commit(prepare1);
109
110         // Choice write
111         final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
112         final ChoiceNode choice = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
113                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
114                 .withChild(ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont"))).build();
115
116         try {
117             final InMemoryDataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
118             modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
119             modificationTree2.ready();
120
121             inMemoryDataTree.validate(modificationTree2);
122
123             final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
124             inMemoryDataTree.commit(prepare2);
125         } catch (IllegalArgumentException e) {
126             assertTrue(e.getMessage().contains("implies non-presence of child"));
127             throw e;
128         }
129     }
130 }