2 * Copyright (c) 2017 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.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertThrows;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
15 import org.junit.AfterClass;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
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.tree.api.DataTree;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
27 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 public class CaseAugmentTest {
32 private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
33 private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
34 private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
35 private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
37 private static EffectiveModelContext SCHEMA_CONTEXT;
40 public static void beforeClass() {
41 SCHEMA_CONTEXT = YangParserTestUtils.parseYang("""
42 module case-augment-test {
44 namespace "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test";
47 revision "2014-03-13" {
48 description "Initial revision.";
66 augment "/test/choice1/case1" {
78 public static void afterClass() {
79 SCHEMA_CONTEXT = null;
82 private static DataTree initDataTree() {
83 DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
85 return inMemoryDataTree;
89 public void testWriteAugment() throws DataValidationFailedException {
90 final DataTree inMemoryDataTree = initDataTree();
92 final ContainerNode container = Builders.containerBuilder()
93 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
94 .withChild(Builders.choiceBuilder()
95 .withNodeIdentifier(CHOICE_ID)
96 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
99 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
100 modificationTree.write(TestModel.TEST_PATH, container);
101 modificationTree.ready();
103 inMemoryDataTree.validate(modificationTree);
104 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
105 inMemoryDataTree.commit(prepare);
109 public void testWriteCase1All() throws DataValidationFailedException {
110 final DataTree inMemoryDataTree = initDataTree();
112 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
113 modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
114 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
115 .withChild(Builders.choiceBuilder()
116 .withNodeIdentifier(CHOICE_ID)
117 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
118 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
119 .withChild(leafNode(C1L3_QNAME, "leaf-value"))
122 modificationTree.ready();
124 inMemoryDataTree.validate(modificationTree);
125 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
126 inMemoryDataTree.commit(prepare);
130 public void testWriteConflict() throws DataValidationFailedException {
131 final DataTreeModification modificationTree = initDataTree().takeSnapshot().newModification();
132 modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
133 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
134 .withChild(Builders.choiceBuilder()
135 .withNodeIdentifier(CHOICE_ID)
136 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
137 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
141 final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, modificationTree::ready);
142 assertThat(e.getMessage(), containsString(" implies non-presence of child "));