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.assertTrue;
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
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;
30 public class CaseExclusionTest {
31 private static EffectiveModelContext SCHEMA_CONTEXT;
33 private DataTree inMemoryDataTree;
36 public static void beforeClass() {
37 SCHEMA_CONTEXT = TestModel.createTestContext("/case-exclusion-test.yang");
41 public static void afterClass() {
42 SCHEMA_CONTEXT = null;
46 public void before() {
47 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
52 public void testCorrectCaseWrite() throws DataValidationFailedException {
53 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
55 final ContainerNode container = Builders
57 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
59 Builders.choiceBuilder().withNodeIdentifier(choice1Id)
60 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
62 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
63 modificationTree.write(TestModel.TEST_PATH, container);
64 modificationTree.ready();
66 inMemoryDataTree.validate(modificationTree);
67 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
68 inMemoryDataTree.commit(prepare);
71 @Test(expected = IllegalArgumentException.class)
72 public void testCaseExclusion() throws DataValidationFailedException {
73 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
75 final ContainerNode container = Builders
77 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
79 Builders.choiceBuilder()
80 .withNodeIdentifier(choice1Id)
81 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
83 ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont")))
86 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
87 modificationTree.write(TestModel.TEST_PATH, container);
88 modificationTree.ready();
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"));
99 @Test(expected = IllegalArgumentException.class)
100 public void testCaseExclusionOnChoiceWrite() throws DataValidationFailedException {
102 final ContainerNode container = Builders.containerBuilder()
103 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
105 final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
106 modificationTree1.write(TestModel.TEST_PATH, container);
107 modificationTree1.ready();
109 inMemoryDataTree.validate(modificationTree1);
110 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
111 inMemoryDataTree.commit(prepare1);
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();
120 final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
121 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
122 modificationTree2.ready();
124 inMemoryDataTree.validate(modificationTree2);
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"));