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