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