Fix various warnings
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / restful / utils / PatchDataTransactionUtilTest.java
1 /*
2  * Copyright (c) 2016 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.restconf.restful.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.MockitoAnnotations.initMocks;
17
18 import com.google.common.util.concurrent.Futures;
19 import java.lang.reflect.Field;
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
30 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
31 import org.opendaylight.netconf.sal.restconf.impl.PatchContext;
32 import org.opendaylight.netconf.sal.restconf.impl.PatchEntity;
33 import org.opendaylight.netconf.sal.restconf.impl.PatchStatusContext;
34 import org.opendaylight.netconf.sal.restconf.impl.PatchStatusEntity;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
36 import org.opendaylight.restconf.RestConnectorProvider;
37 import org.opendaylight.restconf.common.references.SchemaContextRef;
38 import org.opendaylight.restconf.handlers.TransactionChainHandler;
39 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
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.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
46 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
47 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
48 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
49
50 public class PatchDataTransactionUtilTest {
51
52     private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
53
54     @Mock
55     private DOMTransactionChain transactionChain;
56
57     @Mock
58     private DOMDataReadWriteTransaction rwTransaction;
59
60     private SchemaContextRef refSchemaCtx;
61     private YangInstanceIdentifier instanceIdCreateAndDelete;
62     private YangInstanceIdentifier instanceIdMerge;
63     private ContainerNode buildBaseContainerForTests;
64     private YangInstanceIdentifier targetNodeForCreateAndDelete;
65     private YangInstanceIdentifier targetNodeMerge;
66     private MapNode buildArtistList;
67
68     // Fields used when delete operation fails to reset transaction chain
69     private static Field handler;
70     private static Field broker;
71
72     @Before
73     public void setUp() throws Exception {
74         initMocks(this);
75
76         PatchDataTransactionUtilTest.handler = RestConnectorProvider.class.getDeclaredField("transactionChainHandler");
77         PatchDataTransactionUtilTest.broker = RestConnectorProvider.class.getDeclaredField("dataBroker");
78
79         PatchDataTransactionUtilTest.handler.setAccessible(true);
80         PatchDataTransactionUtilTest.handler.set(RestConnectorProvider.class, mock(TransactionChainHandler.class));
81
82         PatchDataTransactionUtilTest.broker.setAccessible(true);
83         PatchDataTransactionUtilTest.broker.set(RestConnectorProvider.class, mock(DOMDataBroker.class));
84
85         this.refSchemaCtx = new SchemaContextRef(
86                 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)));
87         final QName baseQName = QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
88         final QName containerPlayerQName = QName.create(baseQName, "player");
89         final QName leafGapQName = QName.create(baseQName, "gap");
90         final QName containerLibraryQName = QName.create(baseQName, "library");
91         final QName listArtistQName = QName.create(baseQName, "artist");
92         final QName leafNameQName = QName.create(baseQName, "name");
93         final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeWithKey =
94             new YangInstanceIdentifier.NodeIdentifierWithPredicates(listArtistQName, leafNameQName, "name of artist");
95
96         /* instance identifier for accessing leaf node "gap" */
97         this.instanceIdCreateAndDelete = YangInstanceIdentifier.builder()
98                 .node(baseQName)
99                 .node(containerPlayerQName)
100                 .node(leafGapQName)
101                 .build();
102
103         /* values that are used for creating leaf for testPatchDataCreateAndDelete test */
104         final LeafNode<?> buildGapLeaf = Builders.leafBuilder()
105                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(leafGapQName))
106                 .withValue(0.2)
107                 .build();
108
109         final ContainerNode buildPlayerContainer = Builders.containerBuilder()
110                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(containerPlayerQName))
111                 .withChild(buildGapLeaf)
112                 .build();
113
114         this.buildBaseContainerForTests = Builders.containerBuilder()
115                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(baseQName))
116                 .withChild(buildPlayerContainer)
117                 .build();
118
119         this.targetNodeForCreateAndDelete = YangInstanceIdentifier.builder(this.instanceIdCreateAndDelete)
120                 .node(containerPlayerQName)
121                 .node(leafGapQName)
122                 .build();
123
124         /* instance identifier for accessing leaf node "name" in list "artist" */
125         this.instanceIdMerge = YangInstanceIdentifier.builder()
126                 .node(baseQName)
127                 .node(containerLibraryQName)
128                 .node(listArtistQName)
129                 .nodeWithKey(listArtistQName, QName.create(listArtistQName, "name"), "name of artist")
130                 .node(leafNameQName)
131                 .build();
132
133         /* values that are used for creating leaf for testPatchDataReplaceMergeAndRemove test */
134         final LeafNode<Object> contentName = Builders.leafBuilder()
135                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(baseQName, "name")))
136                 .withValue("name of artist")
137                 .build();
138
139         final LeafNode<Object> contentDescription = Builders.leafBuilder()
140                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(baseQName, "description")))
141                 .withValue("description of artist")
142                 .build();
143
144         final MapEntryNode mapEntryNode = Builders.mapEntryBuilder()
145                 .withNodeIdentifier(nodeWithKey)
146                 .withChild(contentName)
147                 .withChild(contentDescription)
148                 .build();
149
150         this.buildArtistList = Builders.mapBuilder()
151                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listArtistQName))
152                 .withChild(mapEntryNode)
153                 .build();
154
155         this.targetNodeMerge = YangInstanceIdentifier.builder()
156                 .node(baseQName)
157                 .node(containerLibraryQName)
158                 .node(listArtistQName)
159                 .nodeWithKey(listArtistQName, leafNameQName, "name of artist")
160                 .build();
161
162         /* Mocks */
163         doReturn(this.rwTransaction).when(this.transactionChain).newReadWriteTransaction();
164         doReturn(Futures.immediateCheckedFuture(null)).when(this.rwTransaction).submit();
165     }
166
167     @Test
168     public void testPatchDataReplaceMergeAndRemove() {
169         doReturn(Futures.immediateCheckedFuture(false)).doReturn(Futures.immediateCheckedFuture(true))
170                 .when(this.rwTransaction).exists(LogicalDatastoreType.CONFIGURATION, this.targetNodeMerge);
171
172         final PatchEntity entityReplace =
173                 new PatchEntity("edit1", "REPLACE", this.targetNodeMerge, this.buildArtistList);
174         final PatchEntity entityMerge = new PatchEntity("edit2", "MERGE", this.targetNodeMerge, this.buildArtistList);
175         final PatchEntity entityRemove = new PatchEntity("edit3", "REMOVE", this.targetNodeMerge);
176         final List<PatchEntity> entities = new ArrayList<>();
177
178         entities.add(entityReplace);
179         entities.add(entityMerge);
180         entities.add(entityRemove);
181
182         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
183                 new InstanceIdentifierContext<>(this.instanceIdMerge, null, null, this.refSchemaCtx.get());
184         final PatchContext patchContext = new PatchContext(iidContext, entities, "patchRMRm");
185         final TransactionVarsWrapper wrapper = new TransactionVarsWrapper(iidContext, null, this.transactionChain);
186         final PatchStatusContext patchStatusContext =
187                 PatchDataTransactionUtil.patchData(patchContext, wrapper, this.refSchemaCtx);
188
189         for (final PatchStatusEntity entity : patchStatusContext.getEditCollection()) {
190             assertTrue(entity.isOk());
191         }
192         assertTrue(patchStatusContext.isOk());
193     }
194
195     @Test
196     public void testPatchDataCreateAndDelete() throws Exception {
197         doReturn(Futures.immediateCheckedFuture(false)).doReturn(Futures.immediateCheckedFuture(true))
198                 .when(this.rwTransaction).exists(LogicalDatastoreType.CONFIGURATION, this.targetNodeForCreateAndDelete);
199
200         final PatchEntity entityCreate =
201                 new PatchEntity("edit1", "CREATE", this.targetNodeForCreateAndDelete, this.buildBaseContainerForTests);
202         final PatchEntity entityDelete =
203                 new PatchEntity("edit2", "DELETE", this.targetNodeForCreateAndDelete);
204         final List<PatchEntity> entities = new ArrayList<>();
205
206         entities.add(entityCreate);
207         entities.add(entityDelete);
208
209         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
210                 new InstanceIdentifierContext<>(this.instanceIdCreateAndDelete, null, null, this.refSchemaCtx.get());
211         final PatchContext patchContext = new PatchContext(iidContext, entities, "patchCD");
212         final TransactionVarsWrapper wrapper = new TransactionVarsWrapper(iidContext, null, this.transactionChain);
213         final PatchStatusContext patchStatusContext =
214                 PatchDataTransactionUtil.patchData(patchContext, wrapper, this.refSchemaCtx);
215
216         for (final PatchStatusEntity entity : patchStatusContext.getEditCollection()) {
217             assertTrue(entity.isOk());
218         }
219         assertTrue(patchStatusContext.isOk());
220     }
221
222     @Test
223     public void deleteNonexistentDataTest() {
224         doReturn(Futures.immediateCheckedFuture(false))
225                 .when(this.rwTransaction).exists(LogicalDatastoreType.CONFIGURATION, this.targetNodeForCreateAndDelete);
226
227         final PatchEntity entityDelete =
228                 new PatchEntity("edit", "DELETE", this.targetNodeForCreateAndDelete);
229         final List<PatchEntity> entities = new ArrayList<>();
230
231         entities.add(entityDelete);
232
233         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
234                 new InstanceIdentifierContext<>(this.instanceIdCreateAndDelete, null, null, this.refSchemaCtx.get());
235         final PatchContext patchContext = new PatchContext(iidContext, entities, "patchD");
236         final TransactionVarsWrapper wrapper = new TransactionVarsWrapper(iidContext, null, this.transactionChain);
237         final PatchStatusContext patchStatusContext =
238                 PatchDataTransactionUtil.patchData(patchContext, wrapper, this.refSchemaCtx);
239
240         assertFalse(patchStatusContext.isOk());
241         assertEquals(RestconfError.ErrorType.PROTOCOL,
242                 patchStatusContext.getEditCollection().get(0).getEditErrors().get(0).getErrorType());
243         assertEquals(RestconfError.ErrorTag.DATA_MISSING,
244                 patchStatusContext.getEditCollection().get(0).getEditErrors().get(0).getErrorTag());
245     }
246
247     @Test
248     public void testPatchMergePutContainer() throws Exception {
249         doReturn(Futures.immediateCheckedFuture(false)).doReturn(Futures.immediateCheckedFuture(true))
250                 .when(this.rwTransaction).exists(LogicalDatastoreType.CONFIGURATION, this.targetNodeForCreateAndDelete);
251
252         final PatchEntity entityMerge =
253                 new PatchEntity("edit1", "MERGE", this.targetNodeForCreateAndDelete, this.buildBaseContainerForTests);
254         final List<PatchEntity> entities = new ArrayList<>();
255
256         entities.add(entityMerge);
257
258         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
259                 new InstanceIdentifierContext<>(this.instanceIdCreateAndDelete, null, null, this.refSchemaCtx.get());
260         final PatchContext patchContext = new PatchContext(iidContext, entities, "patchM");
261         final TransactionVarsWrapper wrapper = new TransactionVarsWrapper(iidContext, null, this.transactionChain);
262         final PatchStatusContext patchStatusContext =
263                 PatchDataTransactionUtil.patchData(patchContext, wrapper, this.refSchemaCtx);
264
265         for (final PatchStatusEntity entity : patchStatusContext.getEditCollection()) {
266             assertTrue(entity.isOk());
267         }
268         assertTrue(patchStatusContext.isOk());
269     }
270 }