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.tree.impl;
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.impl.schema.Builders;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 public class CaseExclusionTest {
32 private static EffectiveModelContext SCHEMA_CONTEXT;
34 private DataTree inMemoryDataTree;
37 public static void beforeClass() {
38 SCHEMA_CONTEXT = TestModel.createTestContext("/case-exclusion-test.yang");
42 public static void afterClass() {
43 SCHEMA_CONTEXT = null;
47 public void before() {
48 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
53 public void testCorrectCaseWrite() throws DataValidationFailedException {
54 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
56 final ContainerNode container = Builders
58 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
60 Builders.choiceBuilder().withNodeIdentifier(choice1Id)
61 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
63 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
64 modificationTree.write(TestModel.TEST_PATH, container);
65 modificationTree.ready();
67 inMemoryDataTree.validate(modificationTree);
68 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
69 inMemoryDataTree.commit(prepare);
72 @Test(expected = IllegalArgumentException.class)
73 public void testCaseExclusion() throws DataValidationFailedException {
74 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
76 final ContainerNode container = Builders
78 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
80 Builders.choiceBuilder()
81 .withNodeIdentifier(choice1Id)
82 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
84 ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont")))
87 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
88 modificationTree.write(TestModel.TEST_PATH, container);
89 modificationTree.ready();
91 inMemoryDataTree.validate(modificationTree);
92 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
93 inMemoryDataTree.commit(prepare);
94 } catch (IllegalArgumentException e) {
95 assertTrue(e.getMessage().contains("implies non-presence of child"));
100 @Test(expected = IllegalArgumentException.class)
101 public void testCaseExclusionOnChoiceWrite() throws DataValidationFailedException {
103 final ContainerNode container = Builders.containerBuilder()
104 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
106 final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
107 modificationTree1.write(TestModel.TEST_PATH, container);
108 modificationTree1.ready();
110 inMemoryDataTree.validate(modificationTree1);
111 final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree1);
112 inMemoryDataTree.commit(prepare1);
115 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
116 final ChoiceNode choice = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
117 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
118 .withChild(ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont"))).build();
121 final DataTreeModification modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
122 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
123 modificationTree2.ready();
125 inMemoryDataTree.validate(modificationTree2);
127 final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(modificationTree2);
128 inMemoryDataTree.commit(prepare2);
129 } catch (IllegalArgumentException e) {
130 assertTrue(e.getMessage().contains("implies non-presence of child"));