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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.