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.impl.schema.tree;
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.api.schema.tree.DataTree;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 public class CaseAugmentTest {
31 private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
32 private static final QName C1L1_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf1");
33 private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
34 private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
35 private static final QName C2L1_QNAME = QName.create(TestModel.TEST_QNAME, "case2-leaf1");
36 private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
37 private static final AugmentationIdentifier AUGMENT_ID = new AugmentationIdentifier(
38 ImmutableSet.of(C1L2_QNAME, C1L3_QNAME));
40 private static SchemaContext SCHEMA_CONTEXT;
43 public static void beforeClass() {
44 SCHEMA_CONTEXT = TestModel.createTestContext("/case-augment-test.yang");
48 public static void afterClass() {
49 SCHEMA_CONTEXT = null;
52 private static DataTree initDataTree() {
53 DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
55 return inMemoryDataTree;
59 public void testWriteAugment() throws DataValidationFailedException {
60 final DataTree inMemoryDataTree = initDataTree();
62 AugmentationNode augmentationNode = Builders.augmentationBuilder()
63 .withNodeIdentifier(AUGMENT_ID)
64 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
67 final ContainerNode container = Builders.containerBuilder()
68 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
70 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
71 .withChild(augmentationNode)
73 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
74 modificationTree.write(TestModel.TEST_PATH, container);
75 modificationTree.ready();
77 inMemoryDataTree.validate(modificationTree);
78 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
79 inMemoryDataTree.commit(prepare);
83 public void testWriteCase1All() throws DataValidationFailedException {
84 final DataTree inMemoryDataTree = initDataTree();
86 AugmentationNode augmentationNode = Builders.augmentationBuilder()
87 .withNodeIdentifier(AUGMENT_ID)
88 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
89 .withChild(leafNode(C1L3_QNAME, "leaf-value"))
92 final ContainerNode container = Builders
94 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
96 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
97 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
98 .withChild(augmentationNode)
100 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
101 modificationTree.write(TestModel.TEST_PATH, container);
102 modificationTree.ready();
104 inMemoryDataTree.validate(modificationTree);
105 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
106 inMemoryDataTree.commit(prepare);
109 @Test(expected = IllegalArgumentException.class)
110 public void testWriteConflict() throws DataValidationFailedException {
111 final DataTree inMemoryDataTree = initDataTree();
113 AugmentationNode augmentationNode = Builders.augmentationBuilder()
114 .withNodeIdentifier(AUGMENT_ID)
115 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
118 final ContainerNode container = Builders.containerBuilder()
119 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
121 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
122 .withChild(augmentationNode)
123 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
127 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
128 modificationTree.write(TestModel.TEST_PATH, container);
129 modificationTree.ready();
131 inMemoryDataTree.validate(modificationTree);
132 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
133 inMemoryDataTree.commit(prepare);
134 } catch (IllegalArgumentException e) {
135 assertTrue(e.getMessage().contains("implies non-presence of child"));