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.junit.Assert.assertNotNull;
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
15 import com.google.common.base.VerifyException;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
26 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 // TODO: expand these tests to catch some more obscure cases
34 public class ConfigStatementValidationTest {
35 private static final Logger LOG = LoggerFactory.getLogger(ConfigStatementValidationTest.class);
37 private static final Short ONE_ID = 1;
38 private static final Short TWO_ID = 2;
40 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
41 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
44 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
45 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
48 private static final MapEntryNode INNER_FOO_ENTRY_NODE =
49 ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "foo");
51 private static final MapEntryNode INNER_BAR_ENTRY_NODE =
52 ImmutableNodes.mapEntryBuilder(QName.create(TestModel.TEST_QNAME, "inner-list2"), TestModel.NAME_QNAME, "foo")
53 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value")).build();
55 private static final MapEntryNode FOO_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
56 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_FOO_ENTRY_NODE) //
60 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
61 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_BAR_ENTRY_NODE) //
65 private SchemaContext schemaContext;
67 private static ContainerNode createFooTestContainerNode() {
68 return ImmutableContainerNodeBuilder
70 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
72 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
73 .withChild(FOO_NODE).build()).build();
76 private static ContainerNode createBarTestContainerNode() {
77 return ImmutableContainerNodeBuilder
79 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
81 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
82 .withChild(BAR_NODE).build()).build();
86 public void prepare() {
87 schemaContext = TestModel.createTestContext();
88 assertNotNull("Schema context must not be null.", schemaContext);
91 @Test(expected=SchemaValidationFailedException.class)
92 public void testOnPathFail() throws DataValidationFailedException {
93 final InMemoryDataTree inMemoryDataTree =
94 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
95 inMemoryDataTree.setSchemaContext(schemaContext);
96 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
97 final YangInstanceIdentifier ii = OUTER_LIST_1_PATH.node(new YangInstanceIdentifier.NodeIdentifier(TestModel.INNER_LIST_QNAME))
98 .node(INNER_FOO_ENTRY_NODE.getIdentifier());
99 modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
101 inMemoryDataTree.validate(modificationTree);
102 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
103 inMemoryDataTree.commit(prepare);
106 @Test(expected=SchemaValidationFailedException.class)
107 public void testOnDataFail() throws DataValidationFailedException {
108 final InMemoryDataTree inMemoryDataTree =
109 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
110 inMemoryDataTree.setSchemaContext(schemaContext);
111 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
112 modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
113 modificationTree.ready();
114 inMemoryDataTree.validate(modificationTree);
115 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
116 inMemoryDataTree.commit(prepare);
119 @Test(expected=SchemaValidationFailedException.class)
120 public void testOnDataLeafFail() throws DataValidationFailedException {
121 final InMemoryDataTree inMemoryDataTree =
122 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
123 inMemoryDataTree.setSchemaContext(schemaContext);
124 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
125 modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
126 modificationTree.ready();
127 inMemoryDataTree.validate(modificationTree);
128 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
129 inMemoryDataTree.commit(prepare);
132 @Test(expected=SchemaValidationFailedException.class)
133 public void testOnPathCaseLeafFail() throws DataValidationFailedException {
134 final InMemoryDataTree inMemoryDataTree =
135 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
136 inMemoryDataTree.setSchemaContext(schemaContext);
137 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(
138 QName.create(TestModel.TEST_QNAME, "choice1"));
139 final YangInstanceIdentifier.NodeIdentifier case2ContId = new YangInstanceIdentifier.NodeIdentifier(
140 QName.create(TestModel.TEST_QNAME, "case2-cont"));
141 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
142 final ContainerNode case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
143 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
145 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
146 modificationTree.write(ii, case2Cont);
148 inMemoryDataTree.validate(modificationTree);
149 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
150 inMemoryDataTree.commit(prepare);
153 @Test(expected=VerifyException.class)
154 public void testOnDataCaseLeafFail() throws DataValidationFailedException {
155 final InMemoryDataTree inMemoryDataTree =
156 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
157 inMemoryDataTree.setSchemaContext(schemaContext);
158 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(
159 QName.create(TestModel.TEST_QNAME, "choice1"));
160 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id);
161 final ChoiceNode choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
162 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
164 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
165 modificationTree.write(ii, choice1);
166 modificationTree.ready();
167 inMemoryDataTree.validate(modificationTree);
168 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
169 inMemoryDataTree.commit(prepare);