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