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