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