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.tree.impl;
10 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
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.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.YangInstanceIdentifier.NodeIdentifier;
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.impl.schema.Builders;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.tree.api.SchemaValidationFailedException;
29 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
31 // TODO: expand these tests to catch some more obscure cases
32 public class ConfigStatementValidationTest extends AbstractTestModelTest {
33 private static final Short ONE_ID = 1;
34 private static final Short TWO_ID = 2;
36 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier
37 .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
40 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier
41 .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
44 private static final MapEntryNode INNER_FOO_ENTRY_NODE = mapEntry(TestModel.INNER_LIST_QNAME,
45 TestModel.NAME_QNAME, "foo");
47 private static final MapEntryNode INNER_BAR_ENTRY_NODE =
48 mapEntryBuilder(QName.create(TestModel.TEST_QNAME, "inner-list2"), TestModel.NAME_QNAME, "foo")
49 .withChild(leafNode(TestModel.VALUE_QNAME, "value")).build();
51 private static final MapEntryNode FOO_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
52 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_FOO_ENTRY_NODE)
56 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
57 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_BAR_ENTRY_NODE)
61 private static ContainerNode createFooTestContainerNode() {
62 return Builders.containerBuilder()
63 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
64 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(FOO_NODE).build()).build();
67 private static ContainerNode createBarTestContainerNode() {
68 return Builders.containerBuilder()
69 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
70 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(BAR_NODE).build()).build();
73 @Test(expected = SchemaValidationFailedException.class)
74 public void testOnPathFail() throws DataValidationFailedException {
75 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
76 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
77 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
78 final YangInstanceIdentifier ii = OUTER_LIST_1_PATH.node(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
79 .node(INNER_FOO_ENTRY_NODE.name());
80 modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
82 inMemoryDataTree.validate(modificationTree);
83 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
84 inMemoryDataTree.commit(prepare);
87 @Test(expected = SchemaValidationFailedException.class)
88 public void testOnDataFail() throws DataValidationFailedException {
89 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
90 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
91 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
92 modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
93 modificationTree.ready();
94 inMemoryDataTree.validate(modificationTree);
95 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
96 inMemoryDataTree.commit(prepare);
99 @Test(expected = SchemaValidationFailedException.class)
100 public void testOnDataLeafFail() throws DataValidationFailedException {
101 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
102 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
103 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
104 modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
105 modificationTree.ready();
106 inMemoryDataTree.validate(modificationTree);
107 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
108 inMemoryDataTree.commit(prepare);
111 @Test(expected = SchemaValidationFailedException.class)
112 public void testOnPathCaseLeafFail() throws DataValidationFailedException {
113 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
114 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
115 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
116 final NodeIdentifier case2ContId = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont"));
117 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
118 final ContainerNode case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
119 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
121 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
122 modificationTree.write(ii, case2Cont);
123 modificationTree.ready();
126 @Test(expected = SchemaValidationFailedException.class)
127 public void testOnDataCaseLeafFail() throws DataValidationFailedException {
128 final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
129 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
130 final NodeIdentifier choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
131 final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id);
132 final ChoiceNode choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
133 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
135 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
136 modificationTree.write(ii, choice1);
138 modificationTree.ready();