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.Assert.assertTrue;
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
13 import com.google.common.collect.ImmutableSet;
14 import org.junit.AfterClass;
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.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 public class CaseAugmentTest {
32 private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
33 private static final QName C1L1_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf1");
34 private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
35 private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
36 private static final QName C2L1_QNAME = QName.create(TestModel.TEST_QNAME, "case2-leaf1");
37 private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
38 private static final AugmentationIdentifier AUGMENT_ID = new AugmentationIdentifier(
39 ImmutableSet.of(C1L2_QNAME, C1L3_QNAME));
41 private static EffectiveModelContext SCHEMA_CONTEXT;
44 public static void beforeClass() {
45 SCHEMA_CONTEXT = TestModel.createTestContext("/case-augment-test.yang");
49 public static void afterClass() {
50 SCHEMA_CONTEXT = null;
53 private static DataTree initDataTree() {
54 DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
56 return inMemoryDataTree;
60 public void testWriteAugment() throws DataValidationFailedException {
61 final DataTree inMemoryDataTree = initDataTree();
63 AugmentationNode augmentationNode = Builders.augmentationBuilder()
64 .withNodeIdentifier(AUGMENT_ID)
65 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
68 final ContainerNode container = Builders.containerBuilder()
69 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
71 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
72 .withChild(augmentationNode)
74 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
75 modificationTree.write(TestModel.TEST_PATH, container);
76 modificationTree.ready();
78 inMemoryDataTree.validate(modificationTree);
79 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
80 inMemoryDataTree.commit(prepare);
84 public void testWriteCase1All() throws DataValidationFailedException {
85 final DataTree inMemoryDataTree = initDataTree();
87 AugmentationNode augmentationNode = Builders.augmentationBuilder()
88 .withNodeIdentifier(AUGMENT_ID)
89 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
90 .withChild(leafNode(C1L3_QNAME, "leaf-value"))
93 final ContainerNode container = Builders
95 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
97 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
98 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
99 .withChild(augmentationNode)
101 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
102 modificationTree.write(TestModel.TEST_PATH, container);
103 modificationTree.ready();
105 inMemoryDataTree.validate(modificationTree);
106 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
107 inMemoryDataTree.commit(prepare);
110 @Test(expected = IllegalArgumentException.class)
111 public void testWriteConflict() throws DataValidationFailedException {
112 final DataTree inMemoryDataTree = initDataTree();
114 AugmentationNode augmentationNode = Builders.augmentationBuilder()
115 .withNodeIdentifier(AUGMENT_ID)
116 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
119 final ContainerNode container = Builders.containerBuilder()
120 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
122 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
123 .withChild(augmentationNode)
124 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
128 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
129 modificationTree.write(TestModel.TEST_PATH, container);
130 modificationTree.ready();
132 inMemoryDataTree.validate(modificationTree);
133 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
134 inMemoryDataTree.commit(prepare);
135 } catch (IllegalArgumentException e) {
136 assertTrue(e.getMessage().contains("implies non-presence of child"));