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