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