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