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.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
14 import com.google.common.collect.ImmutableSet;
15 import org.junit.Before;
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 {
32 private SchemaContext schemaContext;
33 private static final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
34 private static final QName C1L1_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf1");
35 private static final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
36 private static final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
37 private static final QName C2L1_QNAME = QName.create(TestModel.TEST_QNAME, "case2-leaf1");
38 private static final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
39 private static final AugmentationIdentifier AUGMENT_ID = new AugmentationIdentifier(
40 ImmutableSet.of(C1L2_QNAME, C1L3_QNAME));
43 public void prepare() {
44 schemaContext = TestModel.createTestContext("/case-augment-test.yang");
45 assertNotNull("Schema context must not be null.", schemaContext);
48 private DataTree initDataTree() {
49 DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
50 DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
51 return inMemoryDataTree;
55 public void testWriteAugment() throws DataValidationFailedException {
56 final DataTree inMemoryDataTree = initDataTree();
58 AugmentationNode augmentationNode = Builders.augmentationBuilder()
59 .withNodeIdentifier(AUGMENT_ID)
60 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
63 final ContainerNode container = Builders.containerBuilder()
64 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
66 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
67 .withChild(augmentationNode)
69 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
70 modificationTree.write(TestModel.TEST_PATH, container);
71 modificationTree.ready();
73 inMemoryDataTree.validate(modificationTree);
74 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
75 inMemoryDataTree.commit(prepare);
79 public void testWriteCase1All() throws DataValidationFailedException {
80 final DataTree inMemoryDataTree = initDataTree();
82 AugmentationNode augmentationNode = Builders.augmentationBuilder()
83 .withNodeIdentifier(AUGMENT_ID)
84 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
85 .withChild(leafNode(C1L3_QNAME, "leaf-value"))
88 final ContainerNode container = Builders
90 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
92 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
93 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
94 .withChild(augmentationNode)
96 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
97 modificationTree.write(TestModel.TEST_PATH, container);
98 modificationTree.ready();
100 inMemoryDataTree.validate(modificationTree);
101 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
102 inMemoryDataTree.commit(prepare);
105 @Test(expected = IllegalArgumentException.class)
106 public void testWriteConflict() throws DataValidationFailedException {
107 final DataTree inMemoryDataTree = initDataTree();
109 AugmentationNode augmentationNode = Builders.augmentationBuilder()
110 .withNodeIdentifier(AUGMENT_ID)
111 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
114 final ContainerNode container = Builders.containerBuilder()
115 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
117 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
118 .withChild(augmentationNode)
119 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
123 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
124 modificationTree.write(TestModel.TEST_PATH, container);
125 modificationTree.ready();
127 inMemoryDataTree.validate(modificationTree);
128 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
129 inMemoryDataTree.commit(prepare);
130 } catch (IllegalArgumentException e) {
131 assertTrue(e.getMessage().contains("implies non-presence of child"));