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