ff85274bb2ffd06e02b77a9bfaad2b36a7f65604
[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.DataTreeConfiguration;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37
38 /*
39  * Schema structure of document is
40  *
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  */
61 public class ModificationMetadataTreeTest {
62
63     private static final Short ONE_ID = 1;
64     private static final Short TWO_ID = 2;
65     private static final String TWO_ONE_NAME = "one";
66     private static final String TWO_TWO_NAME = "two";
67
68     private static final YangInstanceIdentifier OUTER_LIST_1_PATH =
69             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 =
74             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() throws ReactorException {
99         schemaContext = TestModel.createTestContext();
100         assertNotNull("Schema context must not be null.", schemaContext);
101         rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext,
102             DataTreeConfiguration.DEFAULT_OPERATIONAL));
103     }
104
105     /**
106      * Returns a test document.
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 a test document
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(
143             new InMemoryDataTreeSnapshot(schemaContext,
144                 TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper), 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(DataTreeConfiguration.DEFAULT_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         // Writes empty container node to /test
186         modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
187
188         // Writes empty list node to /test/outer-list
189         modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
190             .build());
191
192         // Reads list node from /test/outer-list.
193         final Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
194         assertTrue(potentialOuterList.isPresent());
195
196         // Reads container node from /test and verifies that it contains test node.
197         final Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
198         final ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
199
200         /**
201          *
202          * Gets list from returned snapshot of /test and verifies it contains
203          * outer-list
204          *
205          */
206         assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
207     }
208
209     @Test
210     public void writeSubtreeReadChildren() {
211         final DataTreeModification modificationTree = createEmptyModificationTree();
212         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
213         final Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
214         assertPresentAndType(potential, MapEntryNode.class);
215     }
216
217     @Test
218     public void writeSubtreeDeleteChildren() {
219         final DataTreeModification modificationTree = createEmptyModificationTree();
220         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
221
222         // We verify data are present
223         final Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
224         assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
225
226         modificationTree.delete(TWO_TWO_PATH);
227         final Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
228         assertFalse(potentialAfterDelete.isPresent());
229
230     }
231
232     private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
233         assertNotNull(potential);
234         assertTrue(potential.isPresent());
235         assertTrue(type.isInstance(potential.get()));
236         return type.cast(potential.get());
237     }
238 }