2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
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;
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;
29 public class CaseExclusionTest {
31 private SchemaContext schemaContext;
34 public void prepare() {
35 schemaContext = TestModel.createTestContext("/case-exclusion-test.yang");
36 assertNotNull("Schema context must not be null.", schemaContext);
39 private DataTree initDataTree() {
40 return new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
44 public void testCorrectCaseWrite() throws DataValidationFailedException {
45 final DataTree inMemoryDataTree = initDataTree();
46 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
48 final ContainerNode container = Builders
50 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
52 Builders.choiceBuilder().withNodeIdentifier(choice1Id)
53 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
55 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
56 modificationTree.write(TestModel.TEST_PATH, container);
57 modificationTree.ready();
59 inMemoryDataTree.validate(modificationTree);
60 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
61 inMemoryDataTree.commit(prepare);
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"));
69 final ContainerNode container = Builders
71 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
73 Builders.choiceBuilder()
74 .withNodeIdentifier(choice1Id)
75 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
77 ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont")))
80 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
81 modificationTree.write(TestModel.TEST_PATH, container);
82 modificationTree.ready();
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"));
93 @Test(expected = IllegalArgumentException.class)
94 public void testCaseExclusionOnChoiceWrite() throws DataValidationFailedException {
95 final DataTree inMemoryDataTree = initDataTree();
97 final ContainerNode container = Builders.containerBuilder()
98 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
100 final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
101 modificationTree1.write(TestModel.TEST_PATH, container);
102 modificationTree1.ready();
104 inMemoryDataTree.validate(modificationTree1);
105 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
106 inMemoryDataTree.commit(prepare1);
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();
115 final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
116 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
117 modificationTree2.ready();
119 inMemoryDataTree.validate(modificationTree2);
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"));