Bug 5485: Improve DataTreeModification pruning on recovery
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / PruningDataTreeModificationTest.java
1 /*
2  * Copyright (c) 2014, 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
9 package org.opendaylight.controller.cluster.datastore.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.opendaylight.controller.md.cluster.datastore.model.CompositeModel.AUG_CONTAINER;
18 import static org.opendaylight.controller.md.cluster.datastore.model.CompositeModel.AUG_INNER_CONTAINER;
19 import static org.opendaylight.controller.md.cluster.datastore.model.CompositeModel.AUG_QNAME;
20 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.NAME_QNAME;
21 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_QNAME;
22 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerNode;
23 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNode;
24 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNodeEntry;
25 import com.google.common.base.Optional;
26 import com.google.common.reflect.Reflection;
27 import java.lang.reflect.InvocationHandler;
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
35 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
36 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
37 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
38 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
51 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
52 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
53 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
54 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56
57 public class PruningDataTreeModificationTest {
58     static final SchemaContext SCHEMA_CONTEXT = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
59             SchemaContextHelper.ODL_DATASTORE_TEST_YANG);
60
61     static final QName INVALID_TEST_QNAME = QName.create(TestModel.TEST_QNAME, "invalid");
62     static final YangInstanceIdentifier INVALID_TEST_PATH = YangInstanceIdentifier.of(INVALID_TEST_QNAME);
63
64     @Mock
65     private DataTreeModification mockModification;
66
67     private TipProducingDataTree dataTree;
68     private DataTreeModification realModification;
69     private DataTreeModification proxyModification;
70     private PruningDataTreeModification pruningDataTreeModification;
71
72     @Before
73     public void setUp(){
74         MockitoAnnotations.initMocks(this);
75
76         dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
77         dataTree.setSchemaContext(SCHEMA_CONTEXT);
78
79         realModification = dataTree.takeSnapshot().newModification();
80         proxyModification = Reflection.newProxy(DataTreeModification.class, new InvocationHandler() {
81             @Override
82             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
83                 try {
84                     method.invoke(mockModification, args);
85                     return method.invoke(realModification, args);
86                 } catch (InvocationTargetException e) {
87                     throw e.getCause();
88                 }
89             }
90         });
91
92         pruningDataTreeModification = new PruningDataTreeModification(proxyModification, dataTree, SCHEMA_CONTEXT);
93     }
94
95     @Test
96     public void testDelete(){
97         pruningDataTreeModification.delete(CarsModel.BASE_PATH);
98
99         verify(mockModification, times(1)).delete(CarsModel.BASE_PATH);
100     }
101
102     @Test
103     public void testDeleteOnException(){
104         YangInstanceIdentifier path = CarsModel.BASE_PATH;
105         doThrow(SchemaValidationFailedException.class).when(mockModification).delete(path);
106
107         pruningDataTreeModification.delete(path);
108
109         verify(mockModification, times(1)).delete(path);
110     }
111
112
113     @Test
114     public void testMerge(){
115         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
116         YangInstanceIdentifier path = CarsModel.BASE_PATH;
117         pruningDataTreeModification.merge(path, normalizedNode);
118
119         verify(mockModification, times(1)).merge(path, normalizedNode);
120     }
121
122     @Test
123     public void testMergeWithInvalidNamespace() throws DataValidationFailedException{
124         NormalizedNode<?, ?> normalizedNode = PeopleModel.emptyContainer();
125         YangInstanceIdentifier path = PeopleModel.BASE_PATH;
126
127         pruningDataTreeModification.merge(path, normalizedNode);
128
129         verify(mockModification, times(1)).merge(path, normalizedNode);
130
131         DataTreeCandidateTip candidate = getCandidate();
132         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
133     }
134
135     @Test
136     public void testMergeWithInvalidChildNodeNames() throws DataValidationFailedException{
137         ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
138                 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
139                         ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
140
141         DataContainerChild<?, ?> outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
142         ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
143                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).withChild(augContainer).
144                             withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug")).build();
145
146         YangInstanceIdentifier path = TestModel.TEST_PATH;
147
148         pruningDataTreeModification.merge(path, normalizedNode);
149
150         dataTree.commit(getCandidate());
151
152         ContainerNode prunedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
153                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).build();
154
155         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(path);
156         assertEquals("After pruning present", true, actual.isPresent());
157         assertEquals("After pruning", prunedNode, actual.get());
158     }
159
160     @Test
161     public void testMergeWithValidNamespaceAndInvalidNodeName() throws DataValidationFailedException{
162         NormalizedNode<?, ?> normalizedNode = ImmutableNodes.containerNode(INVALID_TEST_QNAME);
163         YangInstanceIdentifier path = INVALID_TEST_PATH;
164
165         pruningDataTreeModification.merge(path, normalizedNode);
166
167         verify(mockModification, times(1)).merge(path, normalizedNode);
168
169         DataTreeCandidateTip candidate = getCandidate();
170         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
171     }
172
173     @Test
174     public void testWrite(){
175         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
176         YangInstanceIdentifier path = CarsModel.BASE_PATH;
177         pruningDataTreeModification.write(path, normalizedNode);
178
179         verify(mockModification, times(1)).write(path, normalizedNode);
180     }
181
182     @Test
183     public void testWriteRootNode() throws Exception{
184         ShardDataTree shardDataTree = new ShardDataTree(SCHEMA_CONTEXT, TreeType.CONFIGURATION);
185         DataTreeModification mod = shardDataTree.newModification();
186
187         mod.write(CarsModel.BASE_PATH, CarsModel.create());
188         shardDataTree.commit(mod);
189
190         NormalizedNode<?, ?> normalizedNode = shardDataTree.readNode(YangInstanceIdentifier.EMPTY).get();
191         pruningDataTreeModification.write(YangInstanceIdentifier.EMPTY, normalizedNode);
192         dataTree.commit(getCandidate());
193
194         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
195         assertEquals("Root present", true, actual.isPresent());
196         assertEquals("Root node", normalizedNode, actual.get());
197
198     }
199
200     @Test
201     public void testWriteRootNodeWithInvalidChild() throws Exception{
202         ShardDataTree shardDataTree = new ShardDataTree(SCHEMA_CONTEXT, TreeType.CONFIGURATION);
203         NormalizedNode<?, ?> root = shardDataTree.readNode(YangInstanceIdentifier.EMPTY).get();
204
205         NormalizedNode<?, ?> normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
206                 new YangInstanceIdentifier.NodeIdentifier(root.getNodeType())).withChild(
207                         ImmutableNodes.containerNode(AUG_CONTAINER)).build();
208         pruningDataTreeModification.write(YangInstanceIdentifier.EMPTY, normalizedNode);
209         dataTree.commit(getCandidate());
210
211         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY);
212         assertEquals("Root present", true, actual.isPresent());
213         assertEquals("Root node", root, actual.get());
214
215     }
216
217     @Test
218     public void testWriteWithInvalidNamespace() throws DataValidationFailedException{
219         NormalizedNode<?, ?> normalizedNode = PeopleModel.emptyContainer();
220         YangInstanceIdentifier path = PeopleModel.BASE_PATH;
221
222         pruningDataTreeModification.write(path, normalizedNode);
223
224         verify(mockModification, times(1)).write(path, normalizedNode);
225
226         DataTreeCandidateTip candidate = getCandidate();
227         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
228     }
229
230     @Test
231     public void testWriteWithInvalidChildNodeNames() throws DataValidationFailedException{
232         ContainerNode augContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
233                 new YangInstanceIdentifier.NodeIdentifier(AUG_CONTAINER)).withChild(
234                         ImmutableNodes.containerNode(AUG_INNER_CONTAINER)).build();
235
236         DataContainerChild<?, ?> outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
237         ContainerNode normalizedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
238                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).withChild(augContainer).
239                             withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug")).
240                                 withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
241
242         YangInstanceIdentifier path = TestModel.TEST_PATH;
243
244         pruningDataTreeModification.write(path, normalizedNode);
245
246         dataTree.commit(getCandidate());
247
248         ContainerNode prunedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
249                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME)).withChild(outerNode).
250                         withChild(ImmutableNodes.leafNode(NAME_QNAME, "name")).build();
251
252         Optional<NormalizedNode<?, ?>> actual = dataTree.takeSnapshot().readNode(path);
253         assertEquals("After pruning present", true, actual.isPresent());
254         assertEquals("After pruning", prunedNode, actual.get());
255     }
256
257     @Test
258     public void testReady(){
259         pruningDataTreeModification.ready();
260
261         verify(mockModification).ready();
262     }
263
264     @Test
265     public void testApplyToCursor(){
266         DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
267         pruningDataTreeModification.applyToCursor(dataTreeModificationCursor);
268
269         verify(mockModification).applyToCursor(dataTreeModificationCursor);
270     }
271
272     @Test
273     public void testReadNode(){
274         pruningDataTreeModification.readNode(CarsModel.BASE_PATH);
275
276         verify(mockModification).readNode(CarsModel.BASE_PATH);
277     }
278
279     @Test
280     public void testNewModification(){
281         realModification.ready();
282         DataTreeModification dataTreeModification = pruningDataTreeModification.newModification();
283
284         assertTrue("new modification not of type PruningDataTreeModification", dataTreeModification instanceof PruningDataTreeModification);
285     }
286
287     private DataTreeCandidateTip getCandidate() throws DataValidationFailedException {
288         pruningDataTreeModification.ready();
289         DataTreeModification mod = pruningDataTreeModification.getResultingModification();
290         mod = mod == proxyModification ? realModification : mod;
291         dataTree.validate(mod);
292         DataTreeCandidateTip candidate = dataTree.prepare(mod);
293         return candidate;
294     }
295 }