Optimize IMDT tests
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / 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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10
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;
14
15 import com.google.common.base.VerifyException;
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.DataTree;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
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
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 = ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME,
45             TestModel.NAME_QNAME, "foo");
46
47     private static final MapEntryNode INNER_BAR_ENTRY_NODE = ImmutableNodes
48             .mapEntryBuilder(QName.create(TestModel.TEST_QNAME, "inner-list2"), TestModel.NAME_QNAME, "foo")
49             .withChild(ImmutableNodes.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 ImmutableContainerNodeBuilder.create()
63                 .withNodeIdentifier(new YangInstanceIdentifier.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 ImmutableContainerNodeBuilder.create()
69                 .withNodeIdentifier(new YangInstanceIdentifier.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(
79                 new YangInstanceIdentifier.NodeIdentifier(TestModel.INNER_LIST_QNAME)).node(
80                 INNER_FOO_ENTRY_NODE.getIdentifier());
81         modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
82
83         inMemoryDataTree.validate(modificationTree);
84         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
85         inMemoryDataTree.commit(prepare);
86     }
87
88     @Test(expected = SchemaValidationFailedException.class)
89     public void testOnDataFail() throws DataValidationFailedException {
90         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
91             DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
92         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
93         modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
94         modificationTree.ready();
95         inMemoryDataTree.validate(modificationTree);
96         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
97         inMemoryDataTree.commit(prepare);
98     }
99
100     @Test(expected = SchemaValidationFailedException.class)
101     public void testOnDataLeafFail() throws DataValidationFailedException {
102         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
103             DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
104         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
105         modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
106         modificationTree.ready();
107         inMemoryDataTree.validate(modificationTree);
108         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
109         inMemoryDataTree.commit(prepare);
110     }
111
112     @Test(expected = SchemaValidationFailedException.class)
113     public void testOnPathCaseLeafFail() throws DataValidationFailedException {
114         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
115             DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
116         final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(QName.create(
117                 TestModel.TEST_QNAME, "choice1"));
118         final YangInstanceIdentifier.NodeIdentifier case2ContId = new YangInstanceIdentifier.NodeIdentifier(
119                 QName.create(TestModel.TEST_QNAME, "case2-cont"));
120         final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
121         final ContainerNode case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
122                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
123
124         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
125         modificationTree.write(ii, case2Cont);
126         modificationTree.ready();
127         inMemoryDataTree.validate(modificationTree);
128         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
129         inMemoryDataTree.commit(prepare);
130     }
131
132     @Test(expected = VerifyException.class)
133     public void testOnDataCaseLeafFail() throws DataValidationFailedException {
134         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
135             DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
136         final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(QName.create(
137                 TestModel.TEST_QNAME, "choice1"));
138         final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id);
139         final ChoiceNode choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
140                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
141
142         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
143         modificationTree.write(ii, choice1);
144         modificationTree.ready();
145         inMemoryDataTree.validate(modificationTree);
146         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
147         inMemoryDataTree.commit(prepare);
148     }
149 }