BUG-1014: Moved recursive verify of written data to ready()
[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 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static org.junit.Assert.assertNotNull;
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 org.junit.Before;
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.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
25 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 // TODO: expand these tests to catch some more obscure cases
33 public class ConfigStatementValidationTest {
34     private static final Logger LOG = LoggerFactory.getLogger(ConfigStatementValidationTest.class);
35
36     private static final Short ONE_ID = 1;
37     private static final Short TWO_ID = 2;
38
39     private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
40             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
41             .build();
42
43     private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
44             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
45             .build();
46
47     private static final MapEntryNode INNER_FOO_ENTRY_NODE =
48             ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "foo");
49
50     private static final MapEntryNode INNER_BAR_ENTRY_NODE =
51             ImmutableNodes.mapEntryBuilder(QName.create(TestModel.TEST_QNAME, "inner-list2"), TestModel.NAME_QNAME, "foo")
52                     .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value")).build();
53
54     private static final MapEntryNode FOO_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID) //
55             .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_FOO_ENTRY_NODE) //
56                     .build()) //
57             .build();
58
59     private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID) //
60             .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_BAR_ENTRY_NODE) //
61                     .build()) //
62             .build();
63
64     private SchemaContext schemaContext;
65
66     private ContainerNode createFooTestContainerNode() {
67         return ImmutableContainerNodeBuilder
68                 .create()
69                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
70                 .withChild(
71                         mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
72                                 .withChild(FOO_NODE).build()).build();
73     }
74
75     private ContainerNode createBarTestContainerNode() {
76         return ImmutableContainerNodeBuilder
77                 .create()
78                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
79                 .withChild(
80                         mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
81                                 .withChild(BAR_NODE).build()).build();
82     }
83
84     @Before
85     public void prepare() {
86         schemaContext = TestModel.createTestContext();
87         assertNotNull("Schema context must not be null.", schemaContext);
88     }
89
90     @Test(expected=SchemaValidationFailedException.class)
91     public void testOnPathFail() throws DataValidationFailedException {
92         final InMemoryDataTree inMemoryDataTree =
93                 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
94         inMemoryDataTree.setSchemaContext(schemaContext);
95         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
96         final YangInstanceIdentifier ii = OUTER_LIST_1_PATH.node(new YangInstanceIdentifier.NodeIdentifier(TestModel.INNER_LIST_QNAME))
97                 .node(INNER_FOO_ENTRY_NODE.getIdentifier());
98         modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
99
100         inMemoryDataTree.validate(modificationTree);
101         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
102         inMemoryDataTree.commit(prepare);
103     }
104
105     @Test(expected=SchemaValidationFailedException.class)
106     public void testOnDataFail() throws DataValidationFailedException {
107         final InMemoryDataTree inMemoryDataTree =
108                 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
109         inMemoryDataTree.setSchemaContext(schemaContext);
110         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
111         modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
112         modificationTree.ready();
113         inMemoryDataTree.validate(modificationTree);
114         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
115         inMemoryDataTree.commit(prepare);
116     }
117
118     @Test(expected=SchemaValidationFailedException.class)
119     public void testOnDataLeafFail() throws DataValidationFailedException {
120         final InMemoryDataTree inMemoryDataTree =
121                 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
122         inMemoryDataTree.setSchemaContext(schemaContext);
123         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
124         modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
125         modificationTree.ready();
126         inMemoryDataTree.validate(modificationTree);
127         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
128         inMemoryDataTree.commit(prepare);
129     }
130
131     @Test(expected=SchemaValidationFailedException.class)
132     public void testOnPathCaseLeafFail() throws DataValidationFailedException {
133         final InMemoryDataTree inMemoryDataTree =
134                 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
135         inMemoryDataTree.setSchemaContext(schemaContext);
136         final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(
137                 QName.create(TestModel.TEST_QNAME, "choice1"));
138         final YangInstanceIdentifier.NodeIdentifier case2ContId = new YangInstanceIdentifier.NodeIdentifier(
139                 QName.create(TestModel.TEST_QNAME, "case2-cont"));
140         final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
141         final ContainerNode case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
142                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
143
144         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
145         modificationTree.write(ii, case2Cont);
146
147         inMemoryDataTree.validate(modificationTree);
148         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
149         inMemoryDataTree.commit(prepare);
150     }
151
152     @Test(expected=SchemaValidationFailedException.class)
153     public void testOnDataCaseLeafFail() throws DataValidationFailedException {
154         final InMemoryDataTree inMemoryDataTree =
155                 (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
156         inMemoryDataTree.setSchemaContext(schemaContext);
157         final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(
158                 QName.create(TestModel.TEST_QNAME, "choice1"));
159         final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id);
160         final ChoiceNode choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
161                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
162
163         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
164         modificationTree.write(ii, choice1);
165         modificationTree.ready();
166         inMemoryDataTree.validate(modificationTree);
167         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
168         inMemoryDataTree.commit(prepare);
169     }
170 }