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 com.google.common.collect.ImmutableSet;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
21 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
29 public class CaseAugmentTest {
31 private SchemaContext schemaContext;
32 private final QName CHOICE1_QNAME = QName.create(TestModel.TEST_QNAME, "choice1");
33 private final QName C1L1_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf1");
34 private final QName C1L2_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf2");
35 private final QName C1L3_QNAME = QName.create(TestModel.TEST_QNAME, "case1-leaf3");
36 private final QName C2L1_QNAME = QName.create(TestModel.TEST_QNAME, "case2-leaf1");
37 private final NodeIdentifier CHOICE_ID = new NodeIdentifier(CHOICE1_QNAME);
38 private final AugmentationIdentifier AUGMENT_ID =
39 new AugmentationIdentifier(ImmutableSet.<QName>builder().add(C1L2_QNAME).add(C1L3_QNAME).build());
43 public void prepare() throws ReactorException {
44 schemaContext = TestModel.createTestContext("/case-augment-test.yang");
45 assertNotNull("Schema context must not be null.", schemaContext);
48 private InMemoryDataTree initDataTree() {
49 InMemoryDataTree inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(
50 TreeType.CONFIGURATION);
51 inMemoryDataTree.setSchemaContext(schemaContext);
52 return inMemoryDataTree;
56 public void testWriteAugment() throws DataValidationFailedException {
57 final InMemoryDataTree inMemoryDataTree = initDataTree();
59 AugmentationNode augmentationNode = Builders.augmentationBuilder()
60 .withNodeIdentifier(AUGMENT_ID)
61 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
64 final ContainerNode container = Builders.containerBuilder()
65 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
67 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
68 .withChild(augmentationNode)
70 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
71 modificationTree.write(TestModel.TEST_PATH, container);
72 modificationTree.ready();
74 inMemoryDataTree.validate(modificationTree);
75 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
76 inMemoryDataTree.commit(prepare);
80 public void testWriteCase1All() throws DataValidationFailedException {
81 final InMemoryDataTree inMemoryDataTree = initDataTree();
83 AugmentationNode augmentationNode = Builders.augmentationBuilder()
84 .withNodeIdentifier(AUGMENT_ID)
85 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
86 .withChild(leafNode(C1L3_QNAME, "leaf-value"))
89 final ContainerNode container = Builders
91 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
93 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
94 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
95 .withChild(augmentationNode)
97 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
98 modificationTree.write(TestModel.TEST_PATH, container);
99 modificationTree.ready();
101 inMemoryDataTree.validate(modificationTree);
102 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
103 inMemoryDataTree.commit(prepare);
106 @Test(expected = IllegalArgumentException.class)
107 public void testWriteConflict() throws DataValidationFailedException {
108 final InMemoryDataTree inMemoryDataTree = initDataTree();
110 AugmentationNode augmentationNode = Builders.augmentationBuilder()
111 .withNodeIdentifier(AUGMENT_ID)
112 .withChild(leafNode(C1L2_QNAME, "leaf-value"))
115 final ContainerNode container = Builders.containerBuilder()
116 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
118 Builders.choiceBuilder().withNodeIdentifier(CHOICE_ID)
119 .withChild(augmentationNode)
120 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value"))
124 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
125 modificationTree.write(TestModel.TEST_PATH, container);
126 modificationTree.ready();
128 inMemoryDataTree.validate(modificationTree);
129 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
130 inMemoryDataTree.commit(prepare);
131 } catch (IllegalArgumentException e) {
132 assertTrue(e.getMessage().contains("implies non-presence of child"));