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