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