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.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;
28 public class CaseExclusionTest {
30 private SchemaContext schemaContext;
33 public void prepare() throws ReactorException {
34 schemaContext = TestModel.createTestContext("/case-exclusion-test.yang");
35 assertNotNull("Schema context must not be null.", schemaContext);
38 private InMemoryDataTree initDataTree() {
39 InMemoryDataTree inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(
40 TreeType.CONFIGURATION);
41 inMemoryDataTree.setSchemaContext(schemaContext);
42 return inMemoryDataTree;
46 public void testCorrectCaseWrite() throws DataValidationFailedException {
47 final InMemoryDataTree inMemoryDataTree = initDataTree();
48 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
50 final ContainerNode container = Builders
52 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
54 Builders.choiceBuilder().withNodeIdentifier(choice1Id)
55 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
57 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
58 modificationTree.write(TestModel.TEST_PATH, container);
59 modificationTree.ready();
61 inMemoryDataTree.validate(modificationTree);
62 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
63 inMemoryDataTree.commit(prepare);
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"));
71 final ContainerNode container = Builders
73 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
75 Builders.choiceBuilder()
76 .withNodeIdentifier(choice1Id)
77 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
79 ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont")))
82 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
83 modificationTree.write(TestModel.TEST_PATH, container);
84 modificationTree.ready();
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"));
95 @Test(expected = IllegalArgumentException.class)
96 public void testCaseExclusionOnChoiceWrite() throws DataValidationFailedException {
97 final InMemoryDataTree inMemoryDataTree = initDataTree();
99 final ContainerNode container = Builders.containerBuilder()
100 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
102 final InMemoryDataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
103 modificationTree1.write(TestModel.TEST_PATH, container);
104 modificationTree1.ready();
106 inMemoryDataTree.validate(modificationTree1);
107 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
108 inMemoryDataTree.commit(prepare1);
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();
117 final InMemoryDataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
118 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
119 modificationTree2.ready();
121 inMemoryDataTree.validate(modificationTree2);
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"));