716c3f45313c9af6de1d886dad575fb8cfbb78a8
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / ConfigStatementValidationTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.tree.impl;
9
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;
14
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;
30
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;
35
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)
38             .build();
39
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)
42             .build();
43
44     private static final MapEntryNode INNER_FOO_ENTRY_NODE = mapEntry(TestModel.INNER_LIST_QNAME,
45             TestModel.NAME_QNAME, "foo");
46
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();
50
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)
53                     .build())
54             .build();
55
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)
58                     .build())
59             .build();
60
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();
65     }
66
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();
71     }
72
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);
81
82         inMemoryDataTree.validate(modificationTree);
83         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
84         inMemoryDataTree.commit(prepare);
85     }
86
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);
97     }
98
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);
109     }
110
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();
120
121         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
122         modificationTree.write(ii, case2Cont);
123         modificationTree.ready();
124     }
125
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();
134
135         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
136         modificationTree.write(ii, choice1);
137
138         modificationTree.ready();
139     }
140 }