Split out yang-data-tree-impl
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / 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.tree.impl;
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 java.util.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.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNodeFactory;
30 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
31 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
32 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
33 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
34 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36
37 /*
38  * Schema structure of document is
39  *
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  */
60 public class ModificationMetadataTreeTest extends AbstractTestModelTest {
61
62     private static final Short ONE_ID = 1;
63     private static final Short TWO_ID = 2;
64     private static final String TWO_ONE_NAME = "one";
65     private static final String TWO_TWO_NAME = "two";
66
67     private static final YangInstanceIdentifier OUTER_LIST_1_PATH =
68             YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
69             .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
70             .build();
71
72     private static final YangInstanceIdentifier OUTER_LIST_2_PATH =
73             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 RootApplyStrategy rootOper;
94
95     @Before
96     public void prepare() throws ExcludedDataSchemaNodeException {
97         rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
98             DataTreeConfiguration.DEFAULT_OPERATIONAL));
99     }
100
101     /**
102      * Returns a test document.
103      * <pre>
104      * test
105      *     outer-list
106      *          id 1
107      *     outer-list
108      *          id 2
109      *          inner-list
110      *                  name "one"
111      *          inner-list
112      *                  name "two"
113      *
114      * </pre>
115      *
116      * @return a test document
117      */
118     public NormalizedNode createDocumentOne() {
119         return ImmutableContainerNodeBuilder
120                 .create()
121                 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
122                 .withChild(createTestContainer()).build();
123
124     }
125
126     private static ContainerNode createTestContainer() {
127         return ImmutableContainerNodeBuilder
128                 .create()
129                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
130                 .withChild(
131                         mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
132                         .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
133                         .withChild(BAR_NODE).build()).build();
134     }
135
136     @Test
137     public void basicReadWrites() {
138         final DataTreeModification modificationTree = new InMemoryDataTreeModification(
139             new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
140                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper), rootOper);
141         final Optional<NormalizedNode> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
142         assertTrue(originalBarNode.isPresent());
143         assertSame(BAR_NODE, originalBarNode.get());
144
145         // writes node to /outer-list/1/inner_list/two/value
146         modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
147
148         // reads node to /outer-list/1/inner_list/two/value
149         // and checks if node is already present
150         final Optional<NormalizedNode> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
151         assertTrue(barTwoCModified.isPresent());
152         assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
153
154         // delete node to /outer-list/1/inner_list/two/value
155         modificationTree.delete(TWO_TWO_VALUE_PATH);
156         final Optional<NormalizedNode> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
157         assertFalse(barTwoCAfterDelete.isPresent());
158     }
159
160
161     public DataTreeModification createEmptyModificationTree() {
162         /**
163          * Creates empty Snapshot with associated schema context.
164          */
165         final DataTree t = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
166             SCHEMA_CONTEXT);
167
168         /**
169          *
170          * Creates Mutable Data Tree based on provided snapshot and schema
171          * context.
172          *
173          */
174         return t.takeSnapshot().newModification();
175     }
176
177     @Test
178     public void createFromEmptyState() {
179
180         final DataTreeModification modificationTree = createEmptyModificationTree();
181         // Writes empty container node to /test
182         modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
183
184         // Writes empty list node to /test/outer-list
185         modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
186             .build());
187
188         // Reads list node from /test/outer-list.
189         final Optional<NormalizedNode> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
190         assertFalse(potentialOuterList.isPresent());
191
192         // Reads container node from /test and verifies that it contains test node.
193         final Optional<NormalizedNode> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
194         assertPresentAndType(potentialTest, ContainerNode.class);
195     }
196
197     @Test
198     public void writeSubtreeReadChildren() {
199         final DataTreeModification modificationTree = createEmptyModificationTree();
200         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
201         final Optional<NormalizedNode> potential = modificationTree.readNode(TWO_TWO_PATH);
202         assertPresentAndType(potential, MapEntryNode.class);
203     }
204
205     @Test
206     public void writeSubtreeDeleteChildren() {
207         final DataTreeModification modificationTree = createEmptyModificationTree();
208         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
209
210         // We verify data are present
211         final Optional<NormalizedNode> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
212         assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
213
214         modificationTree.delete(TWO_TWO_PATH);
215         final Optional<NormalizedNode> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
216         assertFalse(potentialAfterDelete.isPresent());
217
218     }
219
220     private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
221         assertNotNull(potential);
222         assertTrue(potential.isPresent());
223         assertTrue(type.isInstance(potential.get()));
224         return type.cast(potential.get());
225     }
226 }