Reduce use of TransactionChainHandler
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfDataServiceImplTest.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 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.opendaylight.restconf.common.patch.PatchEditOperation.CREATE;
20 import static org.opendaylight.restconf.common.patch.PatchEditOperation.DELETE;
21 import static org.opendaylight.restconf.common.patch.PatchEditOperation.REMOVE;
22 import static org.opendaylight.restconf.common.patch.PatchEditOperation.REPLACE;
23 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
24 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
25 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
26
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.Set;
32 import javax.ws.rs.core.MultivaluedHashMap;
33 import javax.ws.rs.core.MultivaluedMap;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.UriBuilder;
36 import javax.ws.rs.core.UriInfo;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.opendaylight.mdsal.common.api.CommitInfo;
43 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
44 import org.opendaylight.mdsal.dom.api.DOMActionService;
45 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
46 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
47 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
48 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
49 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
50 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
51 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
52 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
53 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
54 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
55 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
56 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
57 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
58 import org.opendaylight.restconf.common.patch.PatchContext;
59 import org.opendaylight.restconf.common.patch.PatchEntity;
60 import org.opendaylight.restconf.common.patch.PatchStatusContext;
61 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
62 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
63 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
64 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
65 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
66 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
67 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
68 import org.opendaylight.yangtools.yang.common.QName;
69 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
71 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
72 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
73 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
74 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
75 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
76 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
78 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
79 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
80 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
81 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
82 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
83 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
84 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
85 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
86
87 @RunWith(MockitoJUnitRunner.StrictStubs.class)
88 public class RestconfDataServiceImplTest {
89
90     private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
91
92     private ContainerNode buildBaseCont;
93     private ContainerNode buildBaseContConfig;
94     private ContainerNode buildBaseContOperational;
95     private EffectiveModelContext contextRef;
96     private YangInstanceIdentifier iidBase;
97     private DataSchemaNode schemaNode;
98     private RestconfDataServiceImpl dataService;
99     private QName baseQName;
100     private QName containerPlayerQname;
101     private QName leafQname;
102     private ContainerNode buildPlayerCont;
103     private ContainerNode buildLibraryCont;
104     private MapNode buildPlaylistList;
105
106     @Mock
107     private DOMTransactionChain domTransactionChain;
108     @Mock
109     private UriInfo uriInfo;
110     @Mock
111     private DOMDataTreeReadWriteTransaction readWrite;
112     @Mock
113     private DOMDataTreeReadTransaction read;
114     @Mock
115     private DOMDataTreeWriteTransaction write;
116     @Mock
117     private DOMMountPointService mountPointService;
118     @Mock
119     private DOMMountPoint mountPoint;
120     @Mock
121     private DOMDataBroker mountDataBroker;
122     @Mock
123     private NetconfDataTreeService netconfService;
124     @Mock
125     private DOMActionService actionService;
126     @Mock
127     private RestconfStreamsSubscriptionService delegRestconfSubscrService;
128     @Mock
129     private Configuration configuration;
130     @Mock
131     private MultivaluedMap<String, String> queryParamenters;
132
133     @Before
134     public void setUp() throws Exception {
135         doReturn(Set.of()).when(queryParamenters).entrySet();
136         doReturn(queryParamenters).when(this.uriInfo).getQueryParameters();
137
138         this.baseQName = QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
139         this.containerPlayerQname = QName.create(this.baseQName, "player");
140         this.leafQname = QName.create(this.baseQName, "gap");
141
142         final QName containerLibraryQName = QName.create(this.baseQName, "library");
143         final QName listPlaylistQName = QName.create(this.baseQName, "playlist");
144
145         final LeafNode<?> buildLeaf = Builders.leafBuilder()
146                 .withNodeIdentifier(new NodeIdentifier(this.leafQname))
147                 .withValue(0.2)
148                 .build();
149
150         this.buildPlayerCont = Builders.containerBuilder()
151                 .withNodeIdentifier(new NodeIdentifier(this.containerPlayerQname))
152                 .withChild(buildLeaf)
153                 .build();
154
155         this.buildLibraryCont = Builders.containerBuilder()
156                 .withNodeIdentifier(new NodeIdentifier(containerLibraryQName))
157                 .build();
158
159         this.buildPlaylistList = Builders.mapBuilder()
160                 .withNodeIdentifier(new NodeIdentifier(listPlaylistQName))
161                 .build();
162
163         this.buildBaseCont = Builders.containerBuilder()
164                 .withNodeIdentifier(new NodeIdentifier(this.baseQName))
165                 .withChild(this.buildPlayerCont)
166                 .build();
167
168         // config contains one child the same as in operational and one additional
169         this.buildBaseContConfig = Builders.containerBuilder()
170                 .withNodeIdentifier(new NodeIdentifier(this.baseQName))
171                 .withChild(this.buildPlayerCont)
172                 .withChild(this.buildLibraryCont)
173                 .build();
174
175         // operational contains one child the same as in config and one additional
176         this.buildBaseContOperational = Builders.containerBuilder()
177                 .withNodeIdentifier(new NodeIdentifier(this.baseQName))
178                 .withChild(this.buildPlayerCont)
179                 .withChild(this.buildPlaylistList)
180                 .build();
181
182         this.iidBase = YangInstanceIdentifier.builder()
183                 .node(this.baseQName)
184                 .build();
185
186         this.contextRef =
187                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
188         this.schemaNode = DataSchemaContextTree.from(this.contextRef).findChild(this.iidBase).orElseThrow(
189             ).getDataSchemaNode();
190
191         doReturn(CommitInfo.emptyFluentFuture()).when(this.write).commit();
192         doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
193
194         DOMDataBroker mockDataBroker = mock(DOMDataBroker.class);
195         doReturn(this.read).when(mockDataBroker).newReadOnlyTransaction();
196         doReturn(this.readWrite).when(mockDataBroker).newReadWriteTransaction();
197         doReturn(this.write).when(mockDataBroker).newWriteOnlyTransaction();
198
199         final SchemaContextHandler schemaContextHandler = new SchemaContextHandler(
200                 mockDataBroker, mock(DOMSchemaService.class));
201
202         schemaContextHandler.onModelContextUpdated(this.contextRef);
203         this.dataService = new RestconfDataServiceImpl(schemaContextHandler, mockDataBroker, mountPointService,
204                 this.delegRestconfSubscrService, this.actionService, configuration);
205         doReturn(Optional.of(this.mountPoint)).when(this.mountPointService)
206                 .getMountPoint(any(YangInstanceIdentifier.class));
207         doReturn(Optional.of(FixedDOMSchemaService.of(this.contextRef))).when(this.mountPoint)
208                 .getService(DOMSchemaService.class);
209         doReturn(Optional.of(this.mountDataBroker)).when(this.mountPoint).getService(DOMDataBroker.class);
210         doReturn(Optional.empty()).when(this.mountPoint).getService(NetconfDataTreeService.class);
211         doReturn(this.read).when(this.mountDataBroker).newReadOnlyTransaction();
212         doReturn(this.readWrite).when(this.mountDataBroker).newReadWriteTransaction();
213     }
214
215     @Test
216     public void testReadData() {
217         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
218         doReturn(immediateFluentFuture(Optional.of(this.buildBaseCont))).when(this.read)
219                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
220         doReturn(immediateFluentFuture(Optional.empty()))
221                 .when(this.read).read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
222         final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
223         assertNotNull(response);
224         assertEquals(200, response.getStatus());
225         assertEquals(this.buildBaseCont, ((NormalizedNodeContext) response.getEntity()).getData());
226     }
227
228     @Test
229     public void testReadRootData() {
230         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
231         doReturn(immediateFluentFuture(Optional.of(wrapNodeByDataRootContainer(this.buildBaseContConfig))))
232                 .when(this.read)
233                 .read(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
234         doReturn(immediateFluentFuture(Optional.of(wrapNodeByDataRootContainer(this.buildBaseContOperational))))
235                 .when(this.read)
236                 .read(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.empty());
237         final Response response = this.dataService.readData(this.uriInfo);
238         assertNotNull(response);
239         assertEquals(200, response.getStatus());
240
241         final NormalizedNode data = ((NormalizedNodeContext) response.getEntity()).getData();
242         assertTrue(data instanceof ContainerNode);
243         final Collection<DataContainerChild> rootNodes = ((ContainerNode) data).body();
244         assertEquals(1, rootNodes.size());
245         final Collection<DataContainerChild> allDataChildren = ((ContainerNode) rootNodes.iterator().next()).body();
246         assertEquals(3, allDataChildren.size());
247     }
248
249     private static ContainerNode wrapNodeByDataRootContainer(final DataContainerChild data) {
250         return ImmutableContainerNodeBuilder.create()
251                 .withNodeIdentifier(NodeIdentifier.create(SchemaContext.NAME))
252                 .withChild(data)
253                 .build();
254     }
255
256     /**
257      * Test read data from mount point when both {@link LogicalDatastoreType#CONFIGURATION} and
258      * {@link LogicalDatastoreType#OPERATIONAL} contains the same data and some additional data to be merged.
259      */
260     @Test
261     public void testReadDataMountPoint() {
262         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
263         doReturn(immediateFluentFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
264                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
265         doReturn(immediateFluentFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
266                 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
267
268         final Response response = this.dataService.readData(
269                 "example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", this.uriInfo);
270
271         assertNotNull(response);
272         assertEquals(200, response.getStatus());
273
274         // response must contain all child nodes from config and operational containers merged in one container
275         final NormalizedNode data = ((NormalizedNodeContext) response.getEntity()).getData();
276         assertTrue(data instanceof ContainerNode);
277         assertEquals(3, ((ContainerNode) data).size());
278         assertTrue(((ContainerNode) data).findChildByArg(this.buildPlayerCont.getIdentifier()).isPresent());
279         assertTrue(((ContainerNode) data).findChildByArg(this.buildLibraryCont.getIdentifier()).isPresent());
280         assertTrue(((ContainerNode) data).findChildByArg(this.buildPlaylistList.getIdentifier()).isPresent());
281     }
282
283     @Test(expected = RestconfDocumentedException.class)
284     public void testReadDataNoData() {
285         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
286         doReturn(immediateFluentFuture(Optional.empty()))
287                 .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
288         doReturn(immediateFluentFuture(Optional.empty()))
289                 .when(this.read).read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
290         this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
291     }
292
293     /**
294      * Read data from config datastore according to content parameter.
295      */
296     @Test
297     public void testReadDataConfigTest() {
298         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
299         parameters.put("content", List.of("config"));
300
301         doReturn(parameters).when(this.uriInfo).getQueryParameters();
302         doReturn(immediateFluentFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
303                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
304
305         final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
306
307         assertNotNull(response);
308         assertEquals(200, response.getStatus());
309
310         // response must contain only config data
311         final NormalizedNode data = ((NormalizedNodeContext) response.getEntity()).getData();
312
313         // config data present
314         assertTrue(((ContainerNode) data).findChildByArg(this.buildPlayerCont.getIdentifier()).isPresent());
315         assertTrue(((ContainerNode) data).findChildByArg(this.buildLibraryCont.getIdentifier()).isPresent());
316
317         // state data absent
318         assertFalse(((ContainerNode) data).findChildByArg(this.buildPlaylistList.getIdentifier()).isPresent());
319     }
320
321     /**
322      * Read data from operational datastore according to content parameter.
323      */
324     @Test
325     public void testReadDataOperationalTest() {
326         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
327         parameters.put("content", List.of("nonconfig"));
328
329         doReturn(parameters).when(this.uriInfo).getQueryParameters();
330         doReturn(immediateFluentFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
331                 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
332
333         final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
334
335         assertNotNull(response);
336         assertEquals(200, response.getStatus());
337
338         // response must contain only operational data
339         final NormalizedNode data = ((NormalizedNodeContext) response.getEntity()).getData();
340
341         // state data present
342         assertTrue(((ContainerNode) data).findChildByArg(this.buildPlayerCont.getIdentifier()).isPresent());
343         assertTrue(((ContainerNode) data).findChildByArg(this.buildPlaylistList.getIdentifier()).isPresent());
344
345         // config data absent
346         assertFalse(((ContainerNode) data).findChildByArg(this.buildLibraryCont.getIdentifier()).isPresent());
347     }
348
349     @Test
350     public void testPutData() {
351         final InstanceIdentifierContext<DataSchemaNode> iidContext =
352                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef);
353         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
354
355         doReturn(immediateTrueFluentFuture()).when(this.read)
356                 .exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
357         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
358         final Response response = this.dataService.putData(null, payload, this.uriInfo);
359         assertNotNull(response);
360         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
361     }
362
363     @Test
364     public void testPutDataWithMountPoint() {
365         final InstanceIdentifierContext<DataSchemaNode> iidContext =
366                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, mountPoint, this.contextRef);
367         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
368
369         doReturn(immediateTrueFluentFuture()).when(this.read)
370                 .exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
371         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
372         final Response response = this.dataService.putData(null, payload, this.uriInfo);
373         assertNotNull(response);
374         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
375     }
376
377     @Test
378     public void testPostData() {
379         final QName listQname = QName.create(this.baseQName, "playlist");
380         final QName listKeyQname = QName.create(this.baseQName, "name");
381         final NodeIdentifierWithPredicates nodeWithKey =
382                 NodeIdentifierWithPredicates.of(listQname, listKeyQname, "name of band");
383         final LeafNode<Object> content = Builders.leafBuilder()
384                 .withNodeIdentifier(new NodeIdentifier(QName.create(this.baseQName, "name")))
385                 .withValue("name of band")
386                 .build();
387         final LeafNode<Object> content2 = Builders.leafBuilder()
388             .withNodeIdentifier(new NodeIdentifier(QName.create(this.baseQName, "description")))
389             .withValue("band description")
390             .build();
391         final MapEntryNode mapEntryNode = Builders.mapEntryBuilder()
392                 .withNodeIdentifier(nodeWithKey)
393                 .withChild(content)
394                 .withChild(content2)
395                 .build();
396         final MapNode buildList = Builders.mapBuilder()
397                 .withNodeIdentifier(new NodeIdentifier(listQname))
398                 .withChild(mapEntryNode)
399                 .build();
400
401         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
402         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
403                 new InstanceIdentifierContext<>(this.iidBase, null, null, this.contextRef);
404         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, buildList);
405         final MapNode data = (MapNode) payload.getData();
406         final MapEntryNode entryNode = data.body().iterator().next();
407         final NodeIdentifierWithPredicates identifier = entryNode.getIdentifier();
408         final YangInstanceIdentifier node =
409                 payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
410         doReturn(immediateFalseFluentFuture())
411                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
412         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
413         doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(this.uriInfo).getBaseUriBuilder();
414
415         final Response response = this.dataService.postData(null, payload, this.uriInfo);
416         assertEquals(201, response.getStatus());
417     }
418
419     @Test
420     public void testDeleteData() {
421         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
422         doReturn(immediateTrueFluentFuture())
423                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
424         final Response response = this.dataService.deleteData("example-jukebox:jukebox");
425         assertNotNull(response);
426         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
427     }
428
429     /**
430      * Test of deleting data on mount point.
431      */
432     @Test
433     public void testDeleteDataMountPoint() {
434         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
435         doReturn(immediateTrueFluentFuture())
436                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
437         final Response response =
438                 this.dataService.deleteData("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox");
439         assertNotNull(response);
440         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
441     }
442
443     @Test
444     public void testPatchData() {
445         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
446                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef);
447         final List<PatchEntity> entity = new ArrayList<>();
448         final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
449                 .node(this.containerPlayerQname)
450                 .node(this.leafQname)
451                 .build();
452         entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
453         entity.add(new PatchEntity("replace data", REPLACE, this.iidBase, this.buildBaseCont));
454         entity.add(new PatchEntity("delete data", DELETE, iidleaf));
455         final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
456
457         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
458         doReturn(immediateFalseFluentFuture())
459                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
460         doReturn(immediateTrueFluentFuture())
461                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
462         final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
463         assertTrue(status.isOk());
464         assertEquals(3, status.getEditCollection().size());
465         assertEquals("replace data", status.getEditCollection().get(1).getEditId());
466     }
467
468     @Test
469     public void testPatchDataMountPoint() throws Exception {
470         final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(
471                 this.iidBase, this.schemaNode, this.mountPoint, this.contextRef);
472         final List<PatchEntity> entity = new ArrayList<>();
473         final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
474                 .node(this.containerPlayerQname)
475                 .node(this.leafQname)
476                 .build();
477         entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
478         entity.add(new PatchEntity("replace data", REPLACE, this.iidBase, this.buildBaseCont));
479         entity.add(new PatchEntity("delete data", DELETE, iidleaf));
480         final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
481
482         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
483         doReturn(immediateFalseFluentFuture())
484                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
485         doReturn(immediateTrueFluentFuture()).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
486
487         final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
488         assertTrue(status.isOk());
489         assertEquals(3, status.getEditCollection().size());
490         assertNull(status.getGlobalErrors());
491     }
492
493     @Test
494     public void testPatchDataDeleteNotExist() {
495         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
496                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef);
497         final List<PatchEntity> entity = new ArrayList<>();
498         final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
499                 .node(this.containerPlayerQname)
500                 .node(this.leafQname)
501                 .build();
502         entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
503         entity.add(new PatchEntity("remove data", REMOVE, iidleaf));
504         entity.add(new PatchEntity("delete data", DELETE, iidleaf));
505         final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
506
507         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
508         doReturn(immediateFalseFluentFuture())
509                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
510         doReturn(immediateFalseFluentFuture())
511                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
512         doReturn(true).when(this.readWrite).cancel();
513         final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
514
515         assertFalse(status.isOk());
516         assertEquals(3, status.getEditCollection().size());
517         assertTrue(status.getEditCollection().get(0).isOk());
518         assertTrue(status.getEditCollection().get(1).isOk());
519         assertFalse(status.getEditCollection().get(2).isOk());
520         assertFalse(status.getEditCollection().get(2).getEditErrors().isEmpty());
521         final String errorMessage = status.getEditCollection().get(2).getEditErrors().get(0).getErrorMessage();
522         assertEquals("Data does not exist", errorMessage);
523     }
524
525     @Test
526     public void testGetRestconfStrategy() {
527         RestconfStrategy restconfStrategy = this.dataService.getRestconfStrategy(this.mountPoint);
528         assertTrue(restconfStrategy instanceof MdsalRestconfStrategy);
529
530         doReturn(Optional.of(this.netconfService)).when(this.mountPoint).getService(NetconfDataTreeService.class);
531         restconfStrategy = this.dataService.getRestconfStrategy(this.mountPoint);
532         assertTrue(restconfStrategy instanceof NetconfRestconfStrategy);
533     }
534 }