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