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