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