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