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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
11 import static org.junit.Assert.assertNotNull;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
14 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
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.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 // TODO: expand these tests to catch some more obscure cases
35 public class Retest_ConfigStatementValidationTest {
36 private static final Logger LOG = LoggerFactory.getLogger(Retest_ConfigStatementValidationTest.class);
38 private static final Short ONE_ID = 1;
39 private static final Short TWO_ID = 2;
41 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
42 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
45 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
46 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
49 private static final MapEntryNode INNER_FOO_ENTRY_NODE =
50 ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "foo");
52 private static final MapEntryNode INNER_BAR_ENTRY_NODE =
53 ImmutableNodes.mapEntryBuilder(QName.create(TestModel.TEST_QNAME, "inner-list2"), TestModel.NAME_QNAME, "foo")
54 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value")).build();
56 private static final MapEntryNode FOO_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
57 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_FOO_ENTRY_NODE) //
61 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
62 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_BAR_ENTRY_NODE) //
66 private SchemaContext schemaContext;
68 private static ContainerNode createFooTestContainerNode() {
69 return ImmutableContainerNodeBuilder
71 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
73 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
74 .withChild(FOO_NODE).build()).build();
77 private static ContainerNode createBarTestContainerNode() {
78 return ImmutableContainerNodeBuilder
80 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
82 mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
83 .withChild(BAR_NODE).build()).build();
87 public void prepare() throws ReactorException {
88 schemaContext = RetestModel.createTestContext();
89 assertNotNull("Schema context must not be null.", schemaContext);
92 @Test(expected=SchemaValidationFailedException.class)
93 public void testOnPathFail() throws DataValidationFailedException {
94 final InMemoryDataTree inMemoryDataTree =
95 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
96 inMemoryDataTree.setSchemaContext(schemaContext);
97 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
98 final YangInstanceIdentifier ii = OUTER_LIST_1_PATH.node(new YangInstanceIdentifier.NodeIdentifier(TestModel.INNER_LIST_QNAME))
99 .node(INNER_FOO_ENTRY_NODE.getIdentifier());
100 modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
102 inMemoryDataTree.validate(modificationTree);
103 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
104 inMemoryDataTree.commit(prepare);
107 @Test(expected=SchemaValidationFailedException.class)
108 public void testOnDataFail() throws DataValidationFailedException {
109 final InMemoryDataTree inMemoryDataTree =
110 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
111 inMemoryDataTree.setSchemaContext(schemaContext);
112 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
113 modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
114 modificationTree.ready();
115 inMemoryDataTree.validate(modificationTree);
116 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
117 inMemoryDataTree.commit(prepare);
120 @Test(expected=SchemaValidationFailedException.class)
121 public void testOnDataLeafFail() throws DataValidationFailedException {
122 final InMemoryDataTree inMemoryDataTree =
123 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
124 inMemoryDataTree.setSchemaContext(schemaContext);
125 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
126 modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
127 modificationTree.ready();
128 inMemoryDataTree.validate(modificationTree);
129 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
130 inMemoryDataTree.commit(prepare);
133 @Test(expected=SchemaValidationFailedException.class)
134 public void testOnPathCaseLeafFail() throws DataValidationFailedException {
135 final InMemoryDataTree inMemoryDataTree =
136 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
137 inMemoryDataTree.setSchemaContext(schemaContext);
138 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(
139 QName.create(TestModel.TEST_QNAME, "choice1"));
140 final YangInstanceIdentifier.NodeIdentifier case2ContId = new YangInstanceIdentifier.NodeIdentifier(
141 QName.create(TestModel.TEST_QNAME, "case2-cont"));
142 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
143 final ContainerNode case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
144 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
146 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
147 modificationTree.write(ii, case2Cont);
148 modificationTree.ready();
149 inMemoryDataTree.validate(modificationTree);
150 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
151 inMemoryDataTree.commit(prepare);
154 @Test(expected=SchemaValidationFailedException.class)
155 public void testOnDataCaseLeafFail() throws DataValidationFailedException {
156 final InMemoryDataTree inMemoryDataTree =
157 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
158 inMemoryDataTree.setSchemaContext(schemaContext);
159 final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(
160 QName.create(TestModel.TEST_QNAME, "choice1"));
161 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id);
162 final ChoiceNode choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
163 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
165 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
166 modificationTree.write(ii, choice1);
167 modificationTree.ready();
168 inMemoryDataTree.validate(modificationTree);
169 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
170 inMemoryDataTree.commit(prepare);