Cleanup DataTree interfaces and InMemmoryDataTreeFactory
[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.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 com.google.common.base.VerifyException;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33
34 // TODO: expand these tests to catch some more obscure cases
35 public class ConfigStatementValidationTest {
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
40             .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
41             .build();
42
43     private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier
44             .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
45             .build();
46
47     private static final MapEntryNode INNER_FOO_ENTRY_NODE = ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME,
48             TestModel.NAME_QNAME, "foo");
49
50     private static final MapEntryNode INNER_BAR_ENTRY_NODE = ImmutableNodes
51             .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 static ContainerNode createFooTestContainerNode() {
67         return ImmutableContainerNodeBuilder.create()
68                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
69                 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(FOO_NODE).build()).build();
70     }
71
72     private static ContainerNode createBarTestContainerNode() {
73         return ImmutableContainerNodeBuilder.create()
74                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
75                 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(BAR_NODE).build()).build();
76     }
77
78     @Before
79     public void prepare() {
80         schemaContext = TestModel.createTestContext();
81         assertNotNull("Schema context must not be null.", schemaContext);
82     }
83
84     @Test(expected = SchemaValidationFailedException.class)
85     public void testOnPathFail() throws DataValidationFailedException {
86         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
87             DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
88         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
89         final YangInstanceIdentifier ii = OUTER_LIST_1_PATH.node(
90                 new YangInstanceIdentifier.NodeIdentifier(TestModel.INNER_LIST_QNAME)).node(
91                 INNER_FOO_ENTRY_NODE.getIdentifier());
92         modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
93
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 testOnDataFail() throws DataValidationFailedException {
101         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
102             DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
103         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
104         modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
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 testOnDataLeafFail() throws DataValidationFailedException {
113         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
114             DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
115         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
116         modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
117         modificationTree.ready();
118         inMemoryDataTree.validate(modificationTree);
119         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
120         inMemoryDataTree.commit(prepare);
121     }
122
123     @Test(expected = SchemaValidationFailedException.class)
124     public void testOnPathCaseLeafFail() throws DataValidationFailedException {
125         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
126             DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
127         final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(QName.create(
128                 TestModel.TEST_QNAME, "choice1"));
129         final YangInstanceIdentifier.NodeIdentifier case2ContId = new YangInstanceIdentifier.NodeIdentifier(
130                 QName.create(TestModel.TEST_QNAME, "case2-cont"));
131         final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
132         final ContainerNode case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
133                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
134
135         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
136         modificationTree.write(ii, case2Cont);
137         modificationTree.ready();
138         inMemoryDataTree.validate(modificationTree);
139         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
140         inMemoryDataTree.commit(prepare);
141     }
142
143     @Test(expected = VerifyException.class)
144     public void testOnDataCaseLeafFail() throws DataValidationFailedException {
145         final DataTree inMemoryDataTree = new InMemoryDataTreeFactory().create(
146             DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
147         final YangInstanceIdentifier.NodeIdentifier choice1Id = new YangInstanceIdentifier.NodeIdentifier(QName.create(
148                 TestModel.TEST_QNAME, "choice1"));
149         final YangInstanceIdentifier ii = TestModel.TEST_PATH.node(choice1Id);
150         final ChoiceNode choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
151                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
152
153         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
154         modificationTree.write(ii, choice1);
155         modificationTree.ready();
156         inMemoryDataTree.validate(modificationTree);
157         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
158         inMemoryDataTree.commit(prepare);
159     }
160 }