Switch to use PayloadVersion.CHLORINE_SR2
[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().getModificationType());
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         Optional<NormalizedNode> actual = dataTree.takeSnapshot().readNode(path);
172         assertTrue("After pruning present", actual.isPresent());
173         assertEquals("After pruning", prunedNode, actual.get());
174     }
175
176     @Test
177     public void testMergeWithValidNamespaceAndInvalidNodeName() throws DataValidationFailedException {
178         NormalizedNode normalizedNode = ImmutableNodes.containerNode(INVALID_TEST_QNAME);
179         YangInstanceIdentifier path = INVALID_TEST_PATH;
180
181         pruningDataTreeModification.merge(path, normalizedNode);
182
183         verify(mockModification, times(1)).merge(path, normalizedNode);
184
185         DataTreeCandidate candidate = getCandidate();
186         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
187     }
188
189     @Test
190     public void testWrite() {
191         NormalizedNode normalizedNode = CarsModel.create();
192         YangInstanceIdentifier path = CarsModel.BASE_PATH;
193         pruningDataTreeModification.write(path, normalizedNode);
194
195         verify(mockModification, times(1)).write(path, normalizedNode);
196     }
197
198     @Test
199     public void testWriteRootNode() throws Exception {
200         final DataTree localDataTree = new InMemoryDataTreeFactory().create(
201             DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
202
203         DataTreeModification mod = localDataTree.takeSnapshot().newModification();
204         mod.write(CarsModel.BASE_PATH, CarsModel.create());
205         mod.ready();
206         localDataTree.validate(mod);
207         localDataTree.commit(localDataTree.prepare(mod));
208
209         NormalizedNode normalizedNode = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.empty()).get();
210         pruningDataTreeModification.write(YangInstanceIdentifier.empty(), normalizedNode);
211         dataTree.commit(getCandidate());
212
213         Optional<NormalizedNode> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.empty());
214         assertTrue("Root present", actual.isPresent());
215         assertEquals("Root node", normalizedNode, actual.get());
216     }
217
218     @Test
219     public void testWriteRootNodeWithInvalidChild() throws Exception {
220         final Shard mockShard = Mockito.mock(Shard.class);
221
222         ShardDataTree shardDataTree = new ShardDataTree(mockShard, SCHEMA_CONTEXT, TreeType.CONFIGURATION);
223         NormalizedNode root = shardDataTree.readNode(YangInstanceIdentifier.empty()).get();
224
225         NormalizedNode normalizedNode = Builders.containerBuilder()
226             .withNodeIdentifier(new NodeIdentifier(root.getIdentifier().getNodeType()))
227             .withChild(ImmutableNodes.containerNode(AUG_CONTAINER))
228             .build();
229         pruningDataTreeModification.write(YangInstanceIdentifier.empty(), normalizedNode);
230         dataTree.commit(getCandidate());
231
232         Optional<NormalizedNode> actual = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.empty());
233         assertEquals("Root present", true, actual.isPresent());
234         assertEquals("Root node", root, actual.get());
235
236     }
237
238     @Test
239     public void testWriteWithInvalidNamespace() throws DataValidationFailedException {
240         NormalizedNode normalizedNode = PeopleModel.emptyContainer();
241         YangInstanceIdentifier path = PeopleModel.BASE_PATH;
242
243         pruningDataTreeModification.write(path, normalizedNode);
244
245         verify(mockModification, times(1)).write(path, normalizedNode);
246
247         DataTreeCandidate candidate = getCandidate();
248         assertEquals("getModificationType", ModificationType.UNMODIFIED, candidate.getRootNode().getModificationType());
249     }
250
251     @Test
252     public void testWriteWithInvalidChildNodeNames() throws DataValidationFailedException {
253         DataContainerChild outerNode = outerNode(outerNodeEntry(1, innerNode("one", "two")));
254         ContainerNode normalizedNode = Builders.containerBuilder()
255             .withNodeIdentifier(new NodeIdentifier(TEST_QNAME))
256             .withChild(outerNode)
257             .withChild(Builders.containerBuilder()
258                 .withNodeIdentifier(new NodeIdentifier(AUG_CONTAINER))
259                 .withChild(ImmutableNodes.containerNode(AUG_INNER_CONTAINER))
260                 .build())
261             .withChild(ImmutableNodes.leafNode(AUG_QNAME, "aug"))
262             .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name"))
263             .build();
264
265         YangInstanceIdentifier path = TestModel.TEST_PATH;
266
267         pruningDataTreeModification.write(path, normalizedNode);
268
269         dataTree.commit(getCandidate());
270
271         ContainerNode prunedNode = Builders.containerBuilder()
272             .withNodeIdentifier(new NodeIdentifier(TEST_QNAME))
273             .withChild(outerNode)
274             .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name"))
275             .build();
276
277         Optional<NormalizedNode> actual = dataTree.takeSnapshot().readNode(path);
278         assertTrue("After pruning present", actual.isPresent());
279         assertEquals("After pruning", prunedNode, actual.get());
280     }
281
282     @Test
283     public void testReady() {
284         pruningDataTreeModification.ready();
285
286         verify(mockModification).ready();
287     }
288
289     @Test
290     public void testApplyToCursor() {
291         DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
292         pruningDataTreeModification.applyToCursor(dataTreeModificationCursor);
293
294         verify(mockModification).applyToCursor(dataTreeModificationCursor);
295     }
296
297     @Test
298     public void testReadNode() {
299         pruningDataTreeModification.readNode(CarsModel.BASE_PATH);
300
301         verify(mockModification).readNode(CarsModel.BASE_PATH);
302     }
303
304     @Test
305     public void testNewModification() {
306         realModification.ready();
307         DataTreeModification dataTreeModification = pruningDataTreeModification.newModification();
308
309         assertTrue("new modification not of type PruningDataTreeModification",
310                 dataTreeModification instanceof PruningDataTreeModification);
311     }
312
313     private DataTreeCandidate getCandidate() throws DataValidationFailedException {
314         pruningDataTreeModification.ready();
315         DataTreeModification mod = pruningDataTreeModification.delegate();
316         mod = mod == proxyModification ? realModification : mod;
317         dataTree.validate(mod);
318         return dataTree.prepare(mod);
319     }
320 }