Add utility wrappers for instantiating builders/nodes
[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 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.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
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 {
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 SchemaContext schemaContext;
94     private RootModificationApplyOperation rootOper;
95
96     @Before
97     public void prepare() {
98         schemaContext = TestModel.createTestContext();
99         assertNotNull("Schema context must not be null.", schemaContext);
100         rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext,
101             DataTreeConfiguration.DEFAULT_OPERATIONAL));
102     }
103
104     /**
105      * Returns a test document.
106      * <pre>
107      * test
108      *     outer-list
109      *          id 1
110      *     outer-list
111      *          id 2
112      *          inner-list
113      *                  name "one"
114      *          inner-list
115      *                  name "two"
116      *
117      * </pre>
118      *
119      * @return a test document
120      */
121     public NormalizedNode<?, ?> createDocumentOne() {
122         return ImmutableContainerNodeBuilder
123                 .create()
124                 .withNodeIdentifier(new NodeIdentifier(schemaContext.getQName()))
125                 .withChild(createTestContainer()).build();
126
127     }
128
129     private static ContainerNode createTestContainer() {
130         return ImmutableContainerNodeBuilder
131                 .create()
132                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
133                 .withChild(
134                         mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
135                         .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
136                         .withChild(BAR_NODE).build()).build();
137     }
138
139     @Test
140     public void basicReadWrites() {
141         final DataTreeModification modificationTree = new InMemoryDataTreeModification(
142             new InMemoryDataTreeSnapshot(schemaContext,
143                 TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper), rootOper);
144         final Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
145         assertTrue(originalBarNode.isPresent());
146         assertSame(BAR_NODE, originalBarNode.get());
147
148         // writes node to /outer-list/1/inner_list/two/value
149         modificationTree.write(TWO_TWO_VALUE_PATH, ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"));
150
151         // reads node to /outer-list/1/inner_list/two/value
152         // and checks if node is already present
153         final Optional<NormalizedNode<?, ?>> barTwoCModified = modificationTree.readNode(TWO_TWO_VALUE_PATH);
154         assertTrue(barTwoCModified.isPresent());
155         assertEquals(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "test"), barTwoCModified.get());
156
157         // delete node to /outer-list/1/inner_list/two/value
158         modificationTree.delete(TWO_TWO_VALUE_PATH);
159         final Optional<NormalizedNode<?, ?>> barTwoCAfterDelete = modificationTree.readNode(TWO_TWO_VALUE_PATH);
160         assertFalse(barTwoCAfterDelete.isPresent());
161     }
162
163
164     public DataTreeModification createEmptyModificationTree() {
165         /**
166          * Creates empty Snapshot with associated schema context.
167          */
168         final DataTree t = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
169             schemaContext);
170
171         /**
172          *
173          * Creates Mutable Data Tree based on provided snapshot and schema
174          * context.
175          *
176          */
177         return t.takeSnapshot().newModification();
178     }
179
180     @Test
181     public void createFromEmptyState() {
182
183         final DataTreeModification modificationTree = createEmptyModificationTree();
184         // Writes empty container node to /test
185         modificationTree.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
186
187         // Writes empty list node to /test/outer-list
188         modificationTree.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
189             .build());
190
191         // Reads list node from /test/outer-list.
192         final Optional<NormalizedNode<?, ?>> potentialOuterList = modificationTree.readNode(TestModel.OUTER_LIST_PATH);
193         assertTrue(potentialOuterList.isPresent());
194
195         // Reads container node from /test and verifies that it contains test node.
196         final Optional<NormalizedNode<?, ?>> potentialTest = modificationTree.readNode(TestModel.TEST_PATH);
197         final ContainerNode containerTest = assertPresentAndType(potentialTest, ContainerNode.class);
198
199         /**
200          *
201          * Gets list from returned snapshot of /test and verifies it contains
202          * outer-list
203          *
204          */
205         assertPresentAndType(containerTest.getChild(new NodeIdentifier(TestModel.OUTER_LIST_QNAME)), MapNode.class);
206     }
207
208     @Test
209     public void writeSubtreeReadChildren() {
210         final DataTreeModification modificationTree = createEmptyModificationTree();
211         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
212         final Optional<NormalizedNode<?, ?>> potential = modificationTree.readNode(TWO_TWO_PATH);
213         assertPresentAndType(potential, MapEntryNode.class);
214     }
215
216     @Test
217     public void writeSubtreeDeleteChildren() {
218         final DataTreeModification modificationTree = createEmptyModificationTree();
219         modificationTree.write(TestModel.TEST_PATH, createTestContainer());
220
221         // We verify data are present
222         final Optional<NormalizedNode<?, ?>> potentialBeforeDelete = modificationTree.readNode(TWO_TWO_PATH);
223         assertPresentAndType(potentialBeforeDelete, MapEntryNode.class);
224
225         modificationTree.delete(TWO_TWO_PATH);
226         final Optional<NormalizedNode<?, ?>> potentialAfterDelete = modificationTree.readNode(TWO_TWO_PATH);
227         assertFalse(potentialAfterDelete.isPresent());
228
229     }
230
231     private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
232         assertNotNull(potential);
233         assertTrue(potential.isPresent());
234         assertTrue(type.isInstance(potential.get()));
235         return type.cast(potential.get());
236     }
237 }