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;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
24 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 // TODO: expand these tests to catch some more obscure cases
32 public class ConfigStatementValidationTest {
33 private static final Logger LOG = LoggerFactory.getLogger(ConfigStatementValidationTest.class);
35 private static final Short ONE_ID = 1;
36 private static final Short TWO_ID = 2;
38 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
39 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
42 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
43 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
46 private static final MapEntryNode INNER_FOO_ENTRY_NODE =
47 ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "foo");
49 private static final MapEntryNode INNER_BAR_ENTRY_NODE =
50 ImmutableNodes.mapEntryBuilder(QName.create(TestModel.TEST_QNAME, "inner-list2"), TestModel.NAME_QNAME, "foo")
51 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value")).build();
53 private static final MapEntryNode FOO_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
54 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_FOO_ENTRY_NODE) //
58 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
59 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_BAR_ENTRY_NODE) //
63 private SchemaContext schemaContext;
65 private static ContainerNode createFooTestContainerNode() {
66 return ImmutableContainerNodeBuilder
68 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
70 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
71 .withChild(FOO_NODE).build()).build();
74 private static ContainerNode createBarTestContainerNode() {
75 return ImmutableContainerNodeBuilder
77 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
79 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
80 .withChild(BAR_NODE).build()).build();
84 public void prepare() {
85 schemaContext = TestModel.createTestContext();
86 assertNotNull("Schema context must not be null.", schemaContext);
89 @Test(expected=SchemaValidationFailedException.class)
90 public void testOnPathFail() throws DataValidationFailedException {
91 final InMemoryDataTree inMemoryDataTree =
92 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
93 inMemoryDataTree.setSchemaContext(schemaContext);
94 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
95 final YangInstanceIdentifier ii = OUTER_LIST_1_PATH.node(new YangInstanceIdentifier.NodeIdentifier(TestModel.INNER_LIST_QNAME))
96 .node(INNER_FOO_ENTRY_NODE.getIdentifier());
97 modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
99 inMemoryDataTree.validate(modificationTree);
100 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
101 inMemoryDataTree.commit(prepare);
104 @Test(expected=SchemaValidationFailedException.class)
105 public void testOnDataFail() throws DataValidationFailedException {
106 final InMemoryDataTree inMemoryDataTree =
107 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
108 inMemoryDataTree.setSchemaContext(schemaContext);
109 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
110 modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
111 modificationTree.ready();
112 inMemoryDataTree.validate(modificationTree);
113 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
114 inMemoryDataTree.commit(prepare);
117 @Test(expected=SchemaValidationFailedException.class)
118 public void testOnDataLeafFail() throws DataValidationFailedException {
119 final InMemoryDataTree inMemoryDataTree =
120 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
121 inMemoryDataTree.setSchemaContext(schemaContext);
122 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
123 modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
124 modificationTree.ready();
125 inMemoryDataTree.validate(modificationTree);
126 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
127 inMemoryDataTree.commit(prepare);
130 @Test(expected=SchemaValidationFailedException.class)
131 public void testOnPathCaseLeafFail() throws DataValidationFailedException {
132 final InMemoryDataTree inMemoryDataTree =
133 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
134 inMemoryDataTree.setSchemaContext(schemaContext);
135 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(
136 QName.create(TestModel.TEST_QNAME, "choice1"));
137 final YangInstanceIdentifier.NodeIdentifier case2ContId = new YangInstanceIdentifier.NodeIdentifier(
138 QName.create(TestModel.TEST_QNAME, "case2-cont"));
139 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
140 final ContainerNode case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
141 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
143 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
144 modificationTree.write(ii, case2Cont);
146 inMemoryDataTree.validate(modificationTree);
147 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
148 inMemoryDataTree.commit(prepare);
151 @Test(expected=SchemaValidationFailedException.class)
152 public void testOnDataCaseLeafFail() throws DataValidationFailedException {
153 final InMemoryDataTree inMemoryDataTree =
154 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
155 inMemoryDataTree.setSchemaContext(schemaContext);
156 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(
157 QName.create(TestModel.TEST_QNAME, "choice1"));
158 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id);
159 final ChoiceNode choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
160 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
162 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
163 modificationTree.write(ii, choice1);
164 modificationTree.ready();
165 inMemoryDataTree.validate(modificationTree);
166 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
167 inMemoryDataTree.commit(prepare);