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