Switch SchemaAwareApplyOperation error reporting
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ModificationMetadataTreeTest.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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
16 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
17 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
18
19 import java.util.Optional;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
34
35 /*
36  * Schema structure of document is
37  *
38  * container root { 
39  *      list list-a {
40  *              key leaf-a;
41  *              leaf leaf-a;
42  *              choice choice-a {
43  *                      case one {
44  *                              leaf one;
45  *                      }
46  *                      case two-three {
47  *                              leaf two;
48  *                              leaf three;
49  *                      }
50  *              }
51  *              list list-b {
52  *                      key leaf-b;
53  *                      leaf leaf-b;
54  *              }
55  *      }
56  * }
57  */
58 public class ModificationMetadataTreeTest extends AbstractTestModelTest {
59
60     private static final Short ONE_ID = 1;
61     private static final Short TWO_ID = 2;
62     private static final String TWO_ONE_NAME = "one";
63     private static final String TWO_TWO_NAME = "two";
64
65     private static final YangInstanceIdentifier OUTER_LIST_1_PATH =
66             YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
67             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
68             .build();
69
70     private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
71             YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
72             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
73             .build();
74
75     private static final YangInstanceIdentifier TWO_TWO_PATH = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
76             .node(TestModel.INNER_LIST_QNAME)
77             .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
78             .build();
79
80     private static final YangInstanceIdentifier TWO_TWO_VALUE_PATH = YangInstanceIdentifier.builder(TWO_TWO_PATH)
81             .node(TestModel.VALUE_QNAME)
82             .build();
83
84     private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
85             .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
86                     .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
87                     .withChild(mapEntry(TestModel.INNER_LIST_QNAME,TestModel.NAME_QNAME, TWO_TWO_NAME))
88                     .build())
89                     .build();
90
91     private RootApplyStrategy rootOper;
92
93     @Before
94     public void prepare() throws ExcludedDataSchemaNodeException {
95         rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
96             DataTreeConfiguration.DEFAULT_OPERATIONAL));
97     }
98
99     /**
100      * Returns a test document.
101      * <pre>
102      * test
103      *     outer-list
104      *          id 1
105      *     outer-list
106      *          id 2
107      *          inner-list
108      *                  name "one"
109      *          inner-list
110      *                  name "two"
111      *
112      * </pre>
113      *
114      * @return a test document
115      */
116     public NormalizedNode<?, ?> createDocumentOne() {
117         return ImmutableContainerNodeBuilder
118                 .create()
119                 .withNodeIdentifier(new NodeIdentifier(SCHEMA_CONTEXT.getQName()))
120                 .withChild(createTestContainer()).build();
121
122     }
123
124     private static ContainerNode createTestContainer() {
125         return ImmutableContainerNodeBuilder
126                 .create()
127                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
128                 .withChild(
129                         mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
130                         .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
131                         .withChild(BAR_NODE).build()).build();
132     }
133
134     @Test
135     public void basicReadWrites() {
136         final DataTreeModification modificationTree = new InMemoryDataTreeModification(
137             new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
138                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper), rootOper);
139         final Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
140         assertTrue(originalBarNode.isPresent());
141         assertSame(BAR_NODE, originalBarNode.get());
142
143         // writes node to /outer-list/1/inner_list/two/value
144         modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
145
146         // reads node to /outer-list/1/inner_list/two/value
147         // and checks if node is already present
148         final Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
149         assertTrue(barTwoCModified.isPresent());
150         assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
151
152         // delete node to /outer-list/1/inner_list/two/value
153         modificationTree.delete(TWO_TWO_VALUE_PATH);
154         final Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
155         assertFalse(barTwoCAfterDelete.isPresent());
156     }
157
158
159     public DataTreeModification createEmptyModificationTree() {
160         /**
161          * Creates empty Snapshot with associated schema context.
162          */
163         final DataTree t = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
164             SCHEMA_CONTEXT);
165
166         /**
167          *
168          * Creates Mutable Data Tree based on provided snapshot and schema
169          * context.
170          *
171          */
172         return t.takeSnapshot().newModification();
173     }
174
175     @Test
176     public void createFromEmptyState() {
177
178         final DataTreeModification modificationTree = createEmptyModificationTree();
179         // Writes empty container node to /test
180         modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
181
182         // Writes empty list node to /test/outer-list
183         modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
184             .build());
185
186         // Reads list node from /test/outer-list.
187         final Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
188         assertFalse(potentialOuterList.isPresent());
189
190         // Reads container node from /test and verifies that it contains test node.
191         final Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
192         assertPresentAndType(potentialTest, ContainerNode.class);
193     }
194
195     @Test
196     public void writeSubtreeReadChildren() {
197         final DataTreeModification modificationTree = createEmptyModificationTree();
198         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
199         final Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
200         assertPresentAndType(potential, MapEntryNode.class);
201     }
202
203     @Test
204     public void writeSubtreeDeleteChildren() {
205         final DataTreeModification modificationTree = createEmptyModificationTree();
206         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
207
208         // We verify data are present
209         final Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
210         assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
211
212         modificationTree.delete(TWO_TWO_PATH);
213         final Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
214         assertFalse(potentialAfterDelete.isPresent());
215
216     }
217
218     private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
219         assertNotNull(potential);
220         assertTrue(potential.isPresent());
221         assertTrue(type.isInstance(potential.get()));
222         return type.cast(potential.get());
223     }
224 }