2 * Copyright (c) 2015 Cisco Systems, Inc. and others. 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.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
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;
30 // TODO: expand these tests to catch some more obscure cases
31 public class ConfigStatementValidationTest extends AbstractTestModelTest {
32 private static final Short ONE_ID = 1;
33 private static final Short TWO_ID = 2;
35 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier
36 .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
39 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier
40 .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
43 private static final MapEntryNode INNER_FOO_ENTRY_NODE = ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME,
44 TestModel.NAME_QNAME, "foo");
46 private static final MapEntryNode INNER_BAR_ENTRY_NODE = ImmutableNodes
47 .mapEntryBuilder(QName.create(TestModel.TEST_QNAME, "inner-list2"), TestModel.NAME_QNAME, "foo")
48 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value")).build();
50 private static final MapEntryNode FOO_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
51 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_FOO_ENTRY_NODE)
55 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
56 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_BAR_ENTRY_NODE)
60 private static ContainerNode createFooTestContainerNode() {
61 return ImmutableContainerNodeBuilder.create()
62 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
63 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(FOO_NODE).build()).build();
66 private static ContainerNode createBarTestContainerNode() {
67 return ImmutableContainerNodeBuilder.create()
68 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
69 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(BAR_NODE).build()).build();
72 @Test(expected = SchemaValidationFailedException.class)
73 public void testOnPathFail() throws DataValidationFailedException {
74 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
75 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
76 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
77 final YangInstanceIdentifier ii = OUTER_LIST_1_PATH.node(
78 new YangInstanceIdentifier.NodeIdentifier(TestModel.INNER_LIST_QNAME)).node(
79 INNER_FOO_ENTRY_NODE.getIdentifier());
80 modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
82 inMemoryDataTree.validate(modificationTree);
83 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
84 inMemoryDataTree.commit(prepare);
87 @Test(expected = SchemaValidationFailedException.class)
88 public void testOnDataFail() throws DataValidationFailedException {
89 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
90 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
91 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
92 modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
93 modificationTree.ready();
94 inMemoryDataTree.validate(modificationTree);
95 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
96 inMemoryDataTree.commit(prepare);
99 @Test(expected = SchemaValidationFailedException.class)
100 public void testOnDataLeafFail() throws DataValidationFailedException {
101 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
102 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
103 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
104 modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
105 modificationTree.ready();
106 inMemoryDataTree.validate(modificationTree);
107 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
108 inMemoryDataTree.commit(prepare);
111 @Test(expected = SchemaValidationFailedException.class)
112 public void testOnPathCaseLeafFail() throws DataValidationFailedException {
113 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
114 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
115 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(QName.create(
116 TestModel.TEST_QNAME, "choice1"));
117 final YangInstanceIdentifier.NodeIdentifier case2ContId = new YangInstanceIdentifier.NodeIdentifier(
118 QName.create(TestModel.TEST_QNAME, "case2-cont"));
119 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
120 final ContainerNode case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
121 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
123 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
124 modificationTree.write(ii, case2Cont);
125 modificationTree.ready();
128 @Test(expected = SchemaValidationFailedException.class)
129 public void testOnDataCaseLeafFail() throws DataValidationFailedException {
130 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
131 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
132 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(QName.create(
133 TestModel.TEST_QNAME, "choice1"));
134 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id);
135 final ChoiceNode choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
136 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
138 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
139 modificationTree.write(ii, choice1);
141 modificationTree.ready();