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