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;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
14 import org.junit.jupiter.api.AfterAll;
15 import org.junit.jupiter.api.BeforeAll;
16 import org.junit.jupiter.api.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.impl.schema.Builders;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
24 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 class CaseAugmentTest {
29 private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
30 private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
31 private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
32 private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
34 private static EffectiveModelContext SCHEMA_CONTEXT;
37 static void beforeClass() {
38 SCHEMA_CONTEXT = YangParserTestUtils.parseYang("""
39 module case-augment-test {
41 namespace "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test";
44 revision "2014-03-13" {
45 description "Initial revision.";
63 augment "/test/choice1/case1" {
75 static void afterClass() {
76 SCHEMA_CONTEXT = null;
79 private static DataTree initDataTree() {
80 return new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
85 void testWriteAugment() throws DataValidationFailedException {
86 final var inMemoryDataTree = initDataTree();
88 final var container = Builders.containerBuilder()
89 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
90 .withChild(Builders.choiceBuilder()
91 .withNodeIdentifier(CHOICE_ID)
92 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
95 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
96 modificationTree.write(TestModel.TEST_PATH, container);
97 modificationTree.ready();
99 inMemoryDataTree.validate(modificationTree);
100 final var prepare = inMemoryDataTree.prepare(modificationTree);
101 inMemoryDataTree.commit(prepare);
105 void testWriteCase1All() throws DataValidationFailedException {
106 final var inMemoryDataTree = initDataTree();
108 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
109 modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
110 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
111 .withChild(Builders.choiceBuilder()
112 .withNodeIdentifier(CHOICE_ID)
113 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
114 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
115 .withChild(leafNode(C1L3_QNAME, "leaf-value"))
118 modificationTree.ready();
120 inMemoryDataTree.validate(modificationTree);
121 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
122 inMemoryDataTree.commit(prepare);
126 void testWriteConflict() throws DataValidationFailedException {
127 final var modificationTree = initDataTree().takeSnapshot().newModification();
128 modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
129 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
130 .withChild(Builders.choiceBuilder()
131 .withNodeIdentifier(CHOICE_ID)
132 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
133 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
137 final var e = assertThrows(IllegalArgumentException.class, modificationTree::ready);
138 assertTrue(e.getMessage().contains(" implies non-presence of child "));