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