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.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.BeforeEach;
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.spi.node.ImmutableNodes;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
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;
26 class CaseExclusionTest {
27 private static EffectiveModelContext SCHEMA_CONTEXT;
29 private DataTree inMemoryDataTree;
32 static void beforeClass() {
33 SCHEMA_CONTEXT = TestModel.createTestContext("/case-exclusion-test.yang");
37 static void afterClass() {
38 SCHEMA_CONTEXT = null;
43 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
48 void testCorrectCaseWrite() throws DataValidationFailedException {
49 final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
51 final var container = ImmutableNodes.newContainerBuilder()
52 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
53 .withChild(ImmutableNodes.newChoiceBuilder()
54 .withNodeIdentifier(choice1Id)
55 .withChild(ImmutableNodes.leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
58 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
59 modificationTree.write(TestModel.TEST_PATH, container);
60 modificationTree.ready();
62 inMemoryDataTree.validate(modificationTree);
63 final var prepare = inMemoryDataTree.prepare(modificationTree);
64 inMemoryDataTree.commit(prepare);
68 void testCaseExclusion() {
69 assertThrows(IllegalArgumentException.class, () -> {
70 final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
71 final var container = ImmutableNodes.newContainerBuilder()
72 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
73 .withChild(ImmutableNodes.newChoiceBuilder()
74 .withNodeIdentifier(choice1Id)
75 .withChild(ImmutableNodes.leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
76 .withChild(ImmutableNodes.newContainerBuilder()
77 .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
83 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
84 modificationTree.write(TestModel.TEST_PATH, container);
85 modificationTree.ready();
87 inMemoryDataTree.validate(modificationTree);
88 final var prepare = inMemoryDataTree.prepare(modificationTree);
89 inMemoryDataTree.commit(prepare);
90 } catch (IllegalArgumentException e) {
91 assertTrue(e.getMessage().contains("implies non-presence of child"));
98 void testCaseExclusionOnChoiceWrite() {
99 assertThrows(IllegalArgumentException.class, () -> {
101 final var container = ImmutableNodes.newContainerBuilder()
102 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
104 final var modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
105 modificationTree1.write(TestModel.TEST_PATH, container);
106 modificationTree1.ready();
108 inMemoryDataTree.validate(modificationTree1);
109 final var prepare1 = inMemoryDataTree.prepare(modificationTree1);
110 inMemoryDataTree.commit(prepare1);
113 final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
114 final var choice = ImmutableNodes.newChoiceBuilder().withNodeIdentifier(choice1Id)
115 .withChild(ImmutableNodes.leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
116 .withChild(ImmutableNodes.newContainerBuilder()
117 .withNodeIdentifier(new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont")))
122 final var modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
123 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
124 modificationTree2.ready();
126 inMemoryDataTree.validate(modificationTree2);
128 final var prepare2 = inMemoryDataTree.prepare(modificationTree2);
129 inMemoryDataTree.commit(prepare2);
130 } catch (IllegalArgumentException e) {
131 assertTrue(e.getMessage().contains("implies non-presence of child"));