2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.assertEquals;
11 import static org.junit.Assert.assertThrows;
14 import java.util.function.Consumer;
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;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
25 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30 public class YT1276Test {
31 private static final QName FOO = QName.create("foo", "foo");
32 private static final QName BAR = QName.create(FOO, "bar");
33 private static final QName BAZ = QName.create(FOO, "baz");
34 private static final QName XYZZY_LEAF = QName.create(FOO, "xyzzy-leaf");
35 private static final QName XYZZY_AUGMENT = QName.create(FOO, "xyzzy-augment");
37 private static EffectiveModelContext MODEL;
39 private final DataTree tree = new InMemoryDataTreeFactory()
40 .create(DataTreeConfiguration.DEFAULT_CONFIGURATION, MODEL);
43 public static void beforeClass() {
44 MODEL = YangParserTestUtils.parseYangResource("/yt1276.yang");
48 public void testFooWithBar() throws DataValidationFailedException {
49 applyOperation(mod -> {
50 mod.write(YangInstanceIdentifier.of(FOO), Builders.containerBuilder()
51 .withNodeIdentifier(new NodeIdentifier(FOO))
52 .withChild(Builders.augmentationBuilder()
53 .withNodeIdentifier(new AugmentationIdentifier(Set.of(BAR)))
54 .withChild(ImmutableNodes.leafNode(BAR, "xyzzy"))
62 public void testFooWithBarLegacy() throws DataValidationFailedException {
63 applyOperation(mod -> {
64 mod.write(YangInstanceIdentifier.of(FOO), Builders.containerBuilder()
65 .withNodeIdentifier(new NodeIdentifier(FOO))
66 .withChild(ImmutableNodes.leafNode(BAR, "xyzzy"))
72 public void testFooWithoutBar() {
73 final IllegalArgumentException ex = assertFailsReady(mod -> {
74 mod.write(YangInstanceIdentifier.of(FOO), Builders.containerBuilder()
75 .withNodeIdentifier(new NodeIdentifier(FOO))
79 "Node (foo)foo is missing mandatory descendant /AugmentationIdentifier{childNames=[(foo)bar]}/(foo)bar",
84 public void testBarWithXyzzy() throws DataValidationFailedException {
85 applyOperation(mod -> {
86 mod.write(YangInstanceIdentifier.of(BAR), Builders.containerBuilder()
87 .withNodeIdentifier(new NodeIdentifier(BAR))
88 .withChild(Builders.choiceBuilder()
89 .withNodeIdentifier(new NodeIdentifier(BAZ))
90 .withChild(ImmutableNodes.leafNode(XYZZY_LEAF, "xyzzy"))
91 .withChild(Builders.augmentationBuilder()
92 .withNodeIdentifier(new AugmentationIdentifier(Set.of(XYZZY_AUGMENT)))
93 .withChild(ImmutableNodes.leafNode(XYZZY_AUGMENT, "xyzzy"))
102 public void testBarWithXyzzyLegacy() throws DataValidationFailedException {
103 applyOperation(mod -> {
104 mod.write(YangInstanceIdentifier.of(BAR), Builders.containerBuilder()
105 .withNodeIdentifier(new NodeIdentifier(BAR))
106 .withChild(Builders.choiceBuilder()
107 .withNodeIdentifier(new NodeIdentifier(BAZ))
108 .withChild(ImmutableNodes.leafNode(XYZZY_LEAF, "xyzzy"))
109 .withChild(ImmutableNodes.leafNode(XYZZY_AUGMENT, "xyzzy"))
116 public void testBarWithoutXyzzyLeaf() {
117 final IllegalArgumentException ex = assertFailsReady(mod -> {
118 mod.write(YangInstanceIdentifier.of(BAR), Builders.containerBuilder()
119 .withNodeIdentifier(new NodeIdentifier(BAR))
120 .withChild(Builders.choiceBuilder()
121 .withNodeIdentifier(new NodeIdentifier(BAZ))
122 .withChild(Builders.augmentationBuilder()
123 .withNodeIdentifier(new AugmentationIdentifier(Set.of(XYZZY_AUGMENT)))
124 .withChild(ImmutableNodes.leafNode(XYZZY_AUGMENT, "xyzzy"))
130 "Node (foo)baz is missing mandatory descendant /(foo)xyzzy-leaf",
135 public void testBarWithoutXyzzyAugment() {
136 final IllegalArgumentException ex = assertFailsReady(mod -> {
137 mod.write(YangInstanceIdentifier.of(BAR), Builders.containerBuilder()
138 .withNodeIdentifier(new NodeIdentifier(BAR))
139 .withChild(Builders.choiceBuilder()
140 .withNodeIdentifier(new NodeIdentifier(BAZ))
141 .withChild(ImmutableNodes.leafNode(XYZZY_LEAF, "xyzzy"))
145 assertEquals("Node (foo)baz is missing mandatory descendant "
146 + "/AugmentationIdentifier{childNames=[(foo)xyzzy-augment]}/(foo)xyzzy-augment",
151 private IllegalArgumentException assertFailsReady(final Consumer<DataTreeModification> operation) {
152 return assertThrows(IllegalArgumentException.class, () -> applyOperation(operation));
155 private void applyOperation(final Consumer<DataTreeModification> operation)
156 throws DataValidationFailedException {
157 final DataTreeModification mod = tree.takeSnapshot().newModification();
158 operation.accept(mod);
160 tree.commit(tree.prepare(mod));