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;
30 public class CaseAugmentTest {
31 private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
32 private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
33 private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
34 private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
36 private static EffectiveModelContext SCHEMA_CONTEXT;
39 public static void beforeClass() {
40 SCHEMA_CONTEXT = TestModel.createTestContext("/case-augment-test.yang");
44 public static void afterClass() {
45 SCHEMA_CONTEXT = null;
48 private static DataTree initDataTree() {
49 DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
51 return inMemoryDataTree;
55 public void testWriteAugment() throws DataValidationFailedException {
56 final DataTree inMemoryDataTree = initDataTree();
58 final ContainerNode container = Builders.containerBuilder()
59 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
60 .withChild(Builders.choiceBuilder()
61 .withNodeIdentifier(CHOICE_ID)
62 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
65 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
66 modificationTree.write(TestModel.TEST_PATH, container);
67 modificationTree.ready();
69 inMemoryDataTree.validate(modificationTree);
70 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
71 inMemoryDataTree.commit(prepare);
75 public void testWriteCase1All() throws DataValidationFailedException {
76 final DataTree inMemoryDataTree = initDataTree();
78 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
79 modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
80 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
81 .withChild(Builders.choiceBuilder()
82 .withNodeIdentifier(CHOICE_ID)
83 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
84 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
85 .withChild(leafNode(C1L3_QNAME, "leaf-value"))
88 modificationTree.ready();
90 inMemoryDataTree.validate(modificationTree);
91 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
92 inMemoryDataTree.commit(prepare);
96 public void testWriteConflict() throws DataValidationFailedException {
97 final DataTreeModification modificationTree = initDataTree().takeSnapshot().newModification();
98 modificationTree.write(TestModel.TEST_PATH, Builders.containerBuilder()
99 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
100 .withChild(Builders.choiceBuilder()
101 .withNodeIdentifier(CHOICE_ID)
102 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
103 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
107 final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, modificationTree::ready);
108 assertThat(e.getMessage(), containsString(" implies non-presence of child "));