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.junit.jupiter.api.Assertions.assertThrows;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
13 import org.junit.jupiter.api.AfterAll;
14 import org.junit.jupiter.api.BeforeAll;
15 import org.junit.jupiter.api.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.spi.node.ImmutableNodes;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
23 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27 class CaseAugmentTest {
28 private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
29 private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
30 private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
31 private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
33 private static EffectiveModelContext SCHEMA_CONTEXT;
36 static void beforeClass() {
37 SCHEMA_CONTEXT = YangParserTestUtils.parseYang("""
38 module case-augment-test {
40 namespace "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test";
43 revision "2014-03-13" {
44 description "Initial revision.";
62 augment "/test/choice1/case1" {
74 static void afterClass() {
75 SCHEMA_CONTEXT = null;
78 private static DataTree initDataTree() {
79 return new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
84 void testWriteAugment() throws DataValidationFailedException {
85 final var inMemoryDataTree = initDataTree();
87 final var container = ImmutableNodes.newContainerBuilder()
88 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
89 .withChild(ImmutableNodes.newChoiceBuilder()
90 .withNodeIdentifier(CHOICE_ID)
91 .withChild(ImmutableNodes.leafNode(C1L2_QNAME, "leaf-value"))
94 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
95 modificationTree.write(TestModel.TEST_PATH, container);
96 modificationTree.ready();
98 inMemoryDataTree.validate(modificationTree);
99 final var prepare = inMemoryDataTree.prepare(modificationTree);
100 inMemoryDataTree.commit(prepare);
104 void testWriteCase1All() throws DataValidationFailedException {
105 final var inMemoryDataTree = initDataTree();
107 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
108 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
109 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
110 .withChild(ImmutableNodes.newChoiceBuilder()
111 .withNodeIdentifier(CHOICE_ID)
112 .withChild(ImmutableNodes.leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
113 .withChild(ImmutableNodes.leafNode(C1L2_QNAME, "leaf-value"))
114 .withChild(ImmutableNodes.leafNode(C1L3_QNAME, "leaf-value"))
117 modificationTree.ready();
119 inMemoryDataTree.validate(modificationTree);
120 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
121 inMemoryDataTree.commit(prepare);
125 void testWriteConflict() throws DataValidationFailedException {
126 final var modificationTree = initDataTree().takeSnapshot().newModification();
127 modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
128 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
129 .withChild(ImmutableNodes.newChoiceBuilder()
130 .withNodeIdentifier(CHOICE_ID)
131 .withChild(ImmutableNodes.leafNode(C1L2_QNAME, "leaf-value"))
132 .withChild(ImmutableNodes.leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
136 final var e = assertThrows(IllegalArgumentException.class, modificationTree::ready);
137 assertTrue(e.getMessage().contains(" implies non-presence of child "));