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.impl.schema.tree;
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;
29 // TODO: expand these tests to catch some more obscure cases
30 public class ConfigStatementValidationTest extends AbstractTestModelTest {
31 private static final Short ONE_ID = 1;
32 private static final Short TWO_ID = 2;
34 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier
35 .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
38 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier
39 .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
42 private static final MapEntryNode INNER_FOO_ENTRY_NODE = ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME,
43 TestModel.NAME_QNAME, "foo");
45 private static final MapEntryNode INNER_BAR_ENTRY_NODE = ImmutableNodes
46 .mapEntryBuilder(QName.create(TestModel.TEST_QNAME, "inner-list2"), TestModel.NAME_QNAME, "foo")
47 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value")).build();
49 private static final MapEntryNode FOO_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
50 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_FOO_ENTRY_NODE)
54 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
55 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_BAR_ENTRY_NODE)
59 private static ContainerNode createFooTestContainerNode() {
60 return ImmutableContainerNodeBuilder.create()
61 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
62 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(FOO_NODE).build()).build();
65 private static ContainerNode createBarTestContainerNode() {
66 return ImmutableContainerNodeBuilder.create()
67 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
68 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(BAR_NODE).build()).build();
71 @Test(expected = SchemaValidationFailedException.class)
72 public void testOnPathFail() throws DataValidationFailedException {
73 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
74 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
75 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
76 final YangInstanceIdentifier ii = OUTER_LIST_1_PATH.node(
77 new YangInstanceIdentifier.NodeIdentifier(TestModel.INNER_LIST_QNAME)).node(
78 INNER_FOO_ENTRY_NODE.getIdentifier());
79 modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
81 inMemoryDataTree.validate(modificationTree);
82 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
83 inMemoryDataTree.commit(prepare);
86 @Test(expected = SchemaValidationFailedException.class)
87 public void testOnDataFail() throws DataValidationFailedException {
88 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
89 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
90 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
91 modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
92 modificationTree.ready();
93 inMemoryDataTree.validate(modificationTree);
94 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
95 inMemoryDataTree.commit(prepare);
98 @Test(expected = SchemaValidationFailedException.class)
99 public void testOnDataLeafFail() throws DataValidationFailedException {
100 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
101 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
102 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
103 modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
104 modificationTree.ready();
105 inMemoryDataTree.validate(modificationTree);
106 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
107 inMemoryDataTree.commit(prepare);
110 @Test(expected = SchemaValidationFailedException.class)
111 public void testOnPathCaseLeafFail() throws DataValidationFailedException {
112 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
113 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
114 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(QName.create(
115 TestModel.TEST_QNAME, "choice1"));
116 final YangInstanceIdentifier.NodeIdentifier case2ContId = new YangInstanceIdentifier.NodeIdentifier(
117 QName.create(TestModel.TEST_QNAME, "case2-cont"));
118 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
119 final ContainerNode case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
120 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
122 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
123 modificationTree.write(ii, case2Cont);
124 modificationTree.ready();
127 @Test(expected = SchemaValidationFailedException.class)
128 public void testOnDataCaseLeafFail() throws DataValidationFailedException {
129 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
130 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
131 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(QName.create(
132 TestModel.TEST_QNAME, "choice1"));
133 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id);
134 final ChoiceNode choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
135 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
137 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
138 modificationTree.write(ii, choice1);
140 modificationTree.ready();