Clean up RestconfDataServiceImplTest
[netconf.git] / restconf / restconf-nb / 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.assertThrows;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.doNothing;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.mock;
20 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
21 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
22 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
23
24 import java.net.URI;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Optional;
28 import java.util.Set;
29 import javax.ws.rs.container.AsyncResponse;
30 import javax.ws.rs.core.MultivaluedHashMap;
31 import javax.ws.rs.core.MultivaluedMap;
32 import javax.ws.rs.core.Response;
33 import javax.ws.rs.core.UriBuilder;
34 import javax.ws.rs.core.UriInfo;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Mock;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.opendaylight.mdsal.common.api.CommitInfo;
42 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
43 import org.opendaylight.mdsal.dom.api.DOMActionService;
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.spi.FixedDOMSchemaService;
53 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
54 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
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.AbstractJukeboxTest;
60 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
61 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
62 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
63 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
64 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
65 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
66 import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.yang.patch.Edit.Operation;
68 import org.opendaylight.yangtools.yang.common.ErrorTag;
69 import org.opendaylight.yangtools.yang.common.ErrorType;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
71 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
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.NormalizedNode;
75 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
76 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
77
78 @RunWith(MockitoJUnitRunner.StrictStubs.class)
79 public class RestconfDataServiceImplTest extends AbstractJukeboxTest {
80     private static final NodeIdentifier PLAYLIST_NID = new NodeIdentifier(PLAYLIST_QNAME);
81     private static final NodeIdentifier LIBRARY_NID = new NodeIdentifier(LIBRARY_QNAME);
82
83     // config contains one child the same as in operational and one additional
84     private static final ContainerNode CONFIG_JUKEBOX = Builders.containerBuilder()
85             .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME))
86             .withChild(CONT_PLAYER)
87             .withChild(Builders.containerBuilder().withNodeIdentifier(LIBRARY_NID).build())
88             .build();
89     // operational contains one child the same as in config and one additional
90     private static final ContainerNode OPER_JUKEBOX = Builders.containerBuilder()
91             .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME))
92             .withChild(CONT_PLAYER)
93             .withChild(Builders.mapBuilder().withNodeIdentifier(PLAYLIST_NID).build())
94             .build();
95
96     private RestconfDataServiceImpl dataService;
97
98     @Mock
99     private DOMTransactionChain domTransactionChain;
100     @Mock
101     private UriInfo uriInfo;
102     @Mock
103     private DOMDataTreeReadWriteTransaction readWrite;
104     @Mock
105     private DOMDataTreeReadTransaction read;
106     @Mock
107     private DOMDataTreeWriteTransaction write;
108     @Mock
109     private DOMMountPointService mountPointService;
110     @Mock
111     private DOMMountPoint mountPoint;
112     @Mock
113     private DOMDataBroker mountDataBroker;
114     @Mock
115     private NetconfDataTreeService netconfService;
116     @Mock
117     private DOMActionService actionService;
118     @Mock
119     private RestconfStreamsSubscriptionService delegRestconfSubscrService;
120     @Mock
121     private MultivaluedMap<String, String> queryParamenters;
122     @Mock
123     private AsyncResponse asyncResponse;
124
125     @Before
126     public void setUp() throws Exception {
127         doReturn(Set.of()).when(queryParamenters).entrySet();
128         doReturn(queryParamenters).when(uriInfo).getQueryParameters();
129
130         doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
131
132         final var dataBroker = mock(DOMDataBroker.class);
133         doReturn(read).when(dataBroker).newReadOnlyTransaction();
134         doReturn(readWrite).when(dataBroker).newReadWriteTransaction();
135
136         dataService = new RestconfDataServiceImpl(() -> DatabindContext.ofModel(JUKEBOX_SCHEMA), dataBroker,
137                 mountPointService, delegRestconfSubscrService, actionService, new StreamsConfiguration(0, 1, 0, false));
138         doReturn(Optional.of(mountPoint)).when(mountPointService)
139                 .getMountPoint(any(YangInstanceIdentifier.class));
140         doReturn(Optional.of(FixedDOMSchemaService.of(JUKEBOX_SCHEMA))).when(mountPoint)
141                 .getService(DOMSchemaService.class);
142         doReturn(Optional.of(mountDataBroker)).when(mountPoint).getService(DOMDataBroker.class);
143         doReturn(Optional.empty()).when(mountPoint).getService(NetconfDataTreeService.class);
144         doReturn(read).when(mountDataBroker).newReadOnlyTransaction();
145         doReturn(readWrite).when(mountDataBroker).newReadWriteTransaction();
146     }
147
148     @Test
149     public void testReadData() {
150         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
151         doReturn(immediateFluentFuture(Optional.of(EMPTY_JUKEBOX))).when(read)
152                 .read(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
153         doReturn(immediateFluentFuture(Optional.empty()))
154                 .when(read).read(LogicalDatastoreType.OPERATIONAL, JUKEBOX_IID);
155         final Response response = dataService.readData("example-jukebox:jukebox", uriInfo);
156         assertNotNull(response);
157         assertEquals(200, response.getStatus());
158         assertEquals(EMPTY_JUKEBOX, ((NormalizedNodePayload) response.getEntity()).getData());
159     }
160
161     @Test
162     public void testReadRootData() {
163         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
164         doReturn(immediateFluentFuture(Optional.of(wrapNodeByDataRootContainer(CONFIG_JUKEBOX))))
165                 .when(read)
166                 .read(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.of());
167         doReturn(immediateFluentFuture(Optional.of(wrapNodeByDataRootContainer(OPER_JUKEBOX))))
168                 .when(read)
169                 .read(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.of());
170         final Response response = dataService.readData(uriInfo);
171         assertNotNull(response);
172         assertEquals(200, response.getStatus());
173
174         final NormalizedNode data = ((NormalizedNodePayload) response.getEntity()).getData();
175         assertTrue(data instanceof ContainerNode);
176         final Collection<DataContainerChild> rootNodes = ((ContainerNode) data).body();
177         assertEquals(1, rootNodes.size());
178         final Collection<DataContainerChild> allDataChildren = ((ContainerNode) rootNodes.iterator().next()).body();
179         assertEquals(3, allDataChildren.size());
180     }
181
182     private static ContainerNode wrapNodeByDataRootContainer(final DataContainerChild data) {
183         return Builders.containerBuilder()
184             .withNodeIdentifier(NodeIdentifier.create(SchemaContext.NAME))
185             .withChild(data)
186             .build();
187     }
188
189     /**
190      * Test read data from mount point when both {@link LogicalDatastoreType#CONFIGURATION} and
191      * {@link LogicalDatastoreType#OPERATIONAL} contains the same data and some additional data to be merged.
192      */
193     @Test
194     public void testReadDataMountPoint() {
195         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
196         doReturn(immediateFluentFuture(Optional.of(CONFIG_JUKEBOX))).when(read)
197                 .read(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
198         doReturn(immediateFluentFuture(Optional.of(OPER_JUKEBOX))).when(read)
199                 .read(LogicalDatastoreType.OPERATIONAL, JUKEBOX_IID);
200
201         final Response response = dataService.readData(
202                 "example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", uriInfo);
203
204         assertNotNull(response);
205         assertEquals(200, response.getStatus());
206
207         // response must contain all child nodes from config and operational containers merged in one container
208         final NormalizedNode data = ((NormalizedNodePayload) response.getEntity()).getData();
209         assertTrue(data instanceof ContainerNode);
210         assertEquals(3, ((ContainerNode) data).size());
211         assertNotNull(((ContainerNode) data).childByArg(CONT_PLAYER.name()));
212         assertNotNull(((ContainerNode) data).childByArg(LIBRARY_NID));
213         assertNotNull(((ContainerNode) data).childByArg(PLAYLIST_NID));
214     }
215
216     @Test
217     public void testReadDataNoData() {
218         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
219         doReturn(immediateFluentFuture(Optional.empty()))
220                 .when(read).read(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
221         doReturn(immediateFluentFuture(Optional.empty()))
222                 .when(read).read(LogicalDatastoreType.OPERATIONAL, JUKEBOX_IID);
223
224         final var errors = assertThrows(RestconfDocumentedException.class,
225             () -> dataService.readData("example-jukebox:jukebox", uriInfo)).getErrors();
226         assertEquals(1, errors.size());
227         final var error = errors.get(0);
228         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
229         assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
230         assertEquals("Request could not be completed because the relevant data model content does not exist",
231             error.getErrorMessage());
232     }
233
234     /**
235      * Read data from config datastore according to content parameter.
236      */
237     @Test
238     public void testReadDataConfigTest() {
239         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
240         parameters.put("content", List.of("config"));
241
242         doReturn(parameters).when(uriInfo).getQueryParameters();
243         doReturn(immediateFluentFuture(Optional.of(CONFIG_JUKEBOX))).when(read)
244                 .read(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
245
246         final Response response = dataService.readData("example-jukebox:jukebox", uriInfo);
247
248         assertNotNull(response);
249         assertEquals(200, response.getStatus());
250
251         // response must contain only config data
252         final NormalizedNode data = ((NormalizedNodePayload) response.getEntity()).getData();
253
254         // config data present
255         assertNotNull(((ContainerNode) data).childByArg(CONT_PLAYER.name()));
256         assertNotNull(((ContainerNode) data).childByArg(LIBRARY_NID));
257
258         // state data absent
259         assertNull(((ContainerNode) data).childByArg(PLAYLIST_NID));
260     }
261
262     /**
263      * Read data from operational datastore according to content parameter.
264      */
265     @Test
266     public void testReadDataOperationalTest() {
267         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
268         parameters.put("content", List.of("nonconfig"));
269
270         doReturn(parameters).when(uriInfo).getQueryParameters();
271         doReturn(immediateFluentFuture(Optional.of(OPER_JUKEBOX))).when(read)
272                 .read(LogicalDatastoreType.OPERATIONAL, JUKEBOX_IID);
273
274         final Response response = dataService.readData("example-jukebox:jukebox", uriInfo);
275
276         assertNotNull(response);
277         assertEquals(200, response.getStatus());
278
279         // response must contain only operational data
280         final NormalizedNode data = ((NormalizedNodePayload) response.getEntity()).getData();
281
282         // state data present
283         assertNotNull(((ContainerNode) data).childByArg(CONT_PLAYER.name()));
284         assertNotNull(((ContainerNode) data).childByArg(PLAYLIST_NID));
285
286         // config data absent
287         assertNull(((ContainerNode) data).childByArg(LIBRARY_NID));
288     }
289
290     @Test
291     public void testPutData() {
292         final InstanceIdentifierContext iidContext = InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, JUKEBOX_IID);
293         final NormalizedNodePayload payload = NormalizedNodePayload.of(iidContext, EMPTY_JUKEBOX);
294
295         doReturn(immediateTrueFluentFuture()).when(read)
296                 .exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
297         doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX);
298         final Response response = dataService.putData(null, payload, uriInfo);
299         assertNotNull(response);
300         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
301     }
302
303     @Test
304     public void testPutDataWithMountPoint() {
305         final InstanceIdentifierContext iidContext =
306             InstanceIdentifierContext.ofMountPointPath(mountPoint, JUKEBOX_SCHEMA, JUKEBOX_IID);
307         final NormalizedNodePayload payload = NormalizedNodePayload.of(iidContext, EMPTY_JUKEBOX);
308
309         doReturn(immediateTrueFluentFuture()).when(read)
310                 .exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
311         doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX);
312         final Response response = dataService.putData(null, payload, uriInfo);
313         assertNotNull(response);
314         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
315     }
316
317     @Test
318     public void testPostData() {
319         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
320         final var node = JUKEBOX_IID.node(BAND_ENTRY.name());
321         doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
322         doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, node, BAND_ENTRY);
323         doReturn(UriBuilder.fromUri("http://localhost:8181/rests/")).when(uriInfo).getBaseUriBuilder();
324
325         final var response = dataService.postData(NormalizedNodePayload.of(
326             InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, JUKEBOX_IID),
327             Builders.mapBuilder().withNodeIdentifier(PLAYLIST_NID).withChild(BAND_ENTRY).build()),
328             uriInfo);
329         assertEquals(201, response.getStatus());
330         assertEquals(URI.create("http://localhost:8181/rests/data/example-jukebox:jukebox"), response.getLocation());
331     }
332
333     @Test
334     public void testPostMapEntryData() {
335         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
336         final var node = PLAYLIST_IID.node(BAND_ENTRY.name());
337         doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
338         doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, node, BAND_ENTRY);
339         doReturn(UriBuilder.fromUri("http://localhost:8181/rests/")).when(uriInfo).getBaseUriBuilder();
340
341         final var response = dataService.postData(NormalizedNodePayload.of(
342             InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, PLAYLIST_IID),
343             Builders.mapBuilder().withNodeIdentifier(PLAYLIST_NID).withChild(BAND_ENTRY).build()),
344             uriInfo);
345         assertEquals(201, response.getStatus());
346         assertEquals(URI.create("http://localhost:8181/rests/data/example-jukebox:jukebox/playlist=name%20of%20band"),
347             response.getLocation());
348     }
349
350     @Test
351     public void testDeleteData() {
352         doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
353         doReturn(immediateTrueFluentFuture())
354                 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
355         final var captor = ArgumentCaptor.forClass(Response.class);
356         doReturn(true).when(asyncResponse).resume(captor.capture());
357         dataService.deleteData("example-jukebox:jukebox", asyncResponse);
358
359         assertEquals(204, captor.getValue().getStatus());
360     }
361
362     @Test
363     public void testDeleteDataNotExisting() {
364         doReturn(immediateFalseFluentFuture())
365                 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
366         final var captor = ArgumentCaptor.forClass(RestconfDocumentedException.class);
367         doReturn(true).when(asyncResponse).resume(captor.capture());
368         dataService.deleteData("example-jukebox:jukebox", asyncResponse);
369
370         final var errors = captor.getValue().getErrors();
371         assertEquals(1, errors.size());
372         final var error = errors.get(0);
373         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
374         assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
375     }
376
377     /**
378      * Test of deleting data on mount point.
379      */
380     @Test
381     public void testDeleteDataMountPoint() {
382         doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
383         doReturn(immediateTrueFluentFuture())
384                 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
385         final var captor = ArgumentCaptor.forClass(Response.class);
386         doReturn(true).when(asyncResponse).resume(captor.capture());
387         dataService.deleteData("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", asyncResponse);
388
389         assertEquals(204, captor.getValue().getStatus());
390     }
391
392     @Test
393     public void testPatchData() {
394         final InstanceIdentifierContext iidContext = InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, JUKEBOX_IID);
395         final PatchContext patch = new PatchContext(iidContext, List.of(
396             new PatchEntity("create data", Operation.Create, JUKEBOX_IID, EMPTY_JUKEBOX),
397             new PatchEntity("replace data", Operation.Replace, JUKEBOX_IID, EMPTY_JUKEBOX),
398             new PatchEntity("delete data", Operation.Delete, GAP_IID)), "test patch id");
399
400         doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, GAP_IID);
401         doReturn(immediateFalseFluentFuture())
402                 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
403         doReturn(immediateTrueFluentFuture())
404                 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, GAP_IID);
405         final PatchStatusContext status = dataService.yangPatchData(iidContext, patch);
406         assertTrue(status.ok());
407         assertEquals(3, status.editCollection().size());
408         assertEquals("replace data", status.editCollection().get(1).getEditId());
409     }
410
411     @Test
412     public void testPatchDataMountPoint() throws Exception {
413         final InstanceIdentifierContext iidContext = InstanceIdentifierContext.ofMountPointPath(mountPoint,
414             JUKEBOX_SCHEMA, JUKEBOX_IID);
415         final PatchContext patch = new PatchContext(iidContext, List.of(
416             new PatchEntity("create data", Operation.Create, JUKEBOX_IID, EMPTY_JUKEBOX),
417             new PatchEntity("replace data", Operation.Replace, JUKEBOX_IID, EMPTY_JUKEBOX),
418             new PatchEntity("delete data", Operation.Delete, GAP_IID)), "test patch id");
419
420         doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, GAP_IID);
421         doReturn(immediateFalseFluentFuture())
422                 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
423         doReturn(immediateTrueFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, GAP_IID);
424
425         final PatchStatusContext status = dataService.yangPatchData(iidContext, patch);
426         assertTrue(status.ok());
427         assertEquals(3, status.editCollection().size());
428         assertNull(status.globalErrors());
429     }
430
431     @Test
432     public void testPatchDataDeleteNotExist() {
433         final InstanceIdentifierContext iidContext = InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, JUKEBOX_IID);
434         final PatchContext patch = new PatchContext(iidContext, List.of(
435             new PatchEntity("create data", Operation.Create, JUKEBOX_IID, EMPTY_JUKEBOX),
436             new PatchEntity("remove data", Operation.Remove, GAP_IID),
437             new PatchEntity("delete data", Operation.Delete, GAP_IID)), "test patch id");
438
439         doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, GAP_IID);
440         doReturn(immediateFalseFluentFuture())
441                 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
442         doReturn(immediateFalseFluentFuture())
443                 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, GAP_IID);
444         doReturn(true).when(readWrite).cancel();
445         final PatchStatusContext status = dataService.yangPatchData(iidContext, patch);
446
447         assertFalse(status.ok());
448         assertEquals(3, status.editCollection().size());
449         assertTrue(status.editCollection().get(0).isOk());
450         assertTrue(status.editCollection().get(1).isOk());
451         assertFalse(status.editCollection().get(2).isOk());
452         assertFalse(status.editCollection().get(2).getEditErrors().isEmpty());
453         final String errorMessage = status.editCollection().get(2).getEditErrors().get(0).getErrorMessage();
454         assertEquals("Data does not exist", errorMessage);
455     }
456
457     @Test
458     public void testGetRestconfStrategy() {
459         RestconfStrategy restconfStrategy = dataService.getRestconfStrategy(mountPoint);
460         assertTrue(restconfStrategy instanceof MdsalRestconfStrategy);
461
462         doReturn(Optional.of(netconfService)).when(mountPoint).getService(NetconfDataTreeService.class);
463         restconfStrategy = dataService.getRestconfStrategy(mountPoint);
464         assertTrue(restconfStrategy instanceof NetconfRestconfStrategy);
465     }
466
467     @Test
468     public void testValidInputData() {
469         RestconfDataServiceImpl.validInputData(true, NormalizedNodePayload.of(
470             InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, GAP_IID), GAP_LEAF));
471     }
472
473     @Test
474     public void testValidTopLevelNodeName() {
475         RestconfDataServiceImpl.validTopLevelNodeName(GAP_IID, NormalizedNodePayload.of(
476             InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, GAP_IID), GAP_LEAF));
477         RestconfDataServiceImpl.validTopLevelNodeName(JUKEBOX_IID, NormalizedNodePayload.of(
478             InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, JUKEBOX_IID), EMPTY_JUKEBOX));
479     }
480
481     @Test
482     public void testValidTopLevelNodeNamePathEmpty() {
483         final var iidContext = InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, GAP_IID);
484         final var payload = NormalizedNodePayload.of(iidContext, GAP_LEAF);
485
486         // FIXME: more asserts
487         assertThrows(RestconfDocumentedException.class,
488             () -> RestconfDataServiceImpl.validTopLevelNodeName(YangInstanceIdentifier.of(), payload));
489     }
490
491     @Test
492     public void testValidTopLevelNodeNameWrongTopIdentifier() {
493         final var iidContext = InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, GAP_IID);
494         final var payload = NormalizedNodePayload.of(iidContext, GAP_LEAF);
495
496         // FIXME: more asserts
497         assertThrows(RestconfDocumentedException.class,
498             () -> RestconfDataServiceImpl.validTopLevelNodeName(GAP_IID.getAncestor(1), payload));
499     }
500
501     @Test
502     public void testValidateListKeysEqualityInPayloadAndUri() {
503         final var iidContext = InstanceIdentifierContext.ofLocalPath(JUKEBOX_SCHEMA, YangInstanceIdentifier.builder()
504             .node(JUKEBOX_QNAME)
505             .node(PLAYLIST_QNAME)
506             .nodeWithKey(PLAYLIST_QNAME, NAME_QNAME, "name of band")
507             .build());
508         final NormalizedNodePayload payload = NormalizedNodePayload.of(iidContext, BAND_ENTRY);
509         RestconfDataServiceImpl.validateListKeysEqualityInPayloadAndUri(payload);
510     }
511 }