2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertNull;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16 import static org.junit.jupiter.api.Assertions.assertTrue;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.doNothing;
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
22 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
23 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
25 import java.io.ByteArrayInputStream;
26 import java.io.InputStream;
28 import java.nio.charset.StandardCharsets;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.Optional;
33 import javax.ws.rs.container.AsyncResponse;
34 import javax.ws.rs.core.MultivaluedHashMap;
35 import javax.ws.rs.core.MultivaluedMap;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Captor;
44 import org.mockito.Mock;
45 import org.mockito.junit.MockitoJUnitRunner;
46 import org.opendaylight.mdsal.common.api.CommitInfo;
47 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
48 import org.opendaylight.mdsal.dom.api.DOMActionService;
49 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
50 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
51 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
52 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
53 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
54 import org.opendaylight.mdsal.dom.api.DOMRpcService;
55 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
56 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
57 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
58 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
59 import org.opendaylight.restconf.common.patch.PatchContext;
60 import org.opendaylight.restconf.common.patch.PatchEntity;
61 import org.opendaylight.restconf.common.patch.PatchStatusContext;
62 import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest;
63 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
64 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
65 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.yang.patch.Edit.Operation;
67 import org.opendaylight.yangtools.yang.common.ErrorTag;
68 import org.opendaylight.yangtools.yang.common.ErrorType;
69 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
71 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
72 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
73 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
74 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
75 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
77 @RunWith(MockitoJUnitRunner.StrictStubs.class)
78 public class RestconfDataServiceImplTest extends AbstractJukeboxTest {
79 private static final NodeIdentifier PLAYLIST_NID = new NodeIdentifier(PLAYLIST_QNAME);
80 private static final NodeIdentifier LIBRARY_NID = new NodeIdentifier(LIBRARY_QNAME);
82 // config contains one child the same as in operational and one additional
83 private static final ContainerNode CONFIG_JUKEBOX = Builders.containerBuilder()
84 .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME))
85 .withChild(CONT_PLAYER)
86 .withChild(Builders.containerBuilder().withNodeIdentifier(LIBRARY_NID).build())
88 // operational contains one child the same as in config and one additional
89 private static final ContainerNode OPER_JUKEBOX = Builders.containerBuilder()
90 .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME))
91 .withChild(CONT_PLAYER)
92 .withChild(Builders.mapBuilder().withNodeIdentifier(PLAYLIST_NID).build())
96 private UriInfo uriInfo;
98 private DOMDataTreeReadWriteTransaction readWrite;
100 private DOMDataTreeReadTransaction read;
102 private DOMMountPointService mountPointService;
104 private DOMMountPoint mountPoint;
106 private DOMDataBroker mountDataBroker;
108 private NetconfDataTreeService netconfService;
110 private DOMActionService actionService;
112 private DOMRpcService rpcService;
114 private MultivaluedMap<String, String> queryParamenters;
116 private AsyncResponse asyncResponse;
118 private ArgumentCaptor<Response> responseCaptor;
120 private RestconfDataServiceImpl dataService;
123 public void setUp() throws Exception {
124 doReturn(Set.of()).when(queryParamenters).entrySet();
125 doReturn(queryParamenters).when(uriInfo).getQueryParameters();
127 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
129 final var dataBroker = mock(DOMDataBroker.class);
130 doReturn(read).when(dataBroker).newReadOnlyTransaction();
131 doReturn(readWrite).when(dataBroker).newReadWriteTransaction();
133 final DatabindProvider databindProvider = () -> DatabindContext.ofModel(JUKEBOX_SCHEMA);
134 dataService = new RestconfDataServiceImpl(databindProvider,
135 new MdsalRestconfServer(databindProvider, dataBroker, rpcService, actionService, mountPointService));
136 doReturn(Optional.of(mountPoint)).when(mountPointService)
137 .getMountPoint(any(YangInstanceIdentifier.class));
138 doReturn(Optional.of(FixedDOMSchemaService.of(JUKEBOX_SCHEMA))).when(mountPoint)
139 .getService(DOMSchemaService.class);
140 doReturn(Optional.of(mountDataBroker)).when(mountPoint).getService(DOMDataBroker.class);
141 doReturn(Optional.of(rpcService)).when(mountPoint).getService(DOMRpcService.class);
142 doReturn(Optional.empty()).when(mountPoint).getService(NetconfDataTreeService.class);
143 doReturn(read).when(mountDataBroker).newReadOnlyTransaction();
144 doReturn(readWrite).when(mountDataBroker).newReadWriteTransaction();
148 public void testReadData() {
149 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
150 doReturn(immediateFluentFuture(Optional.of(EMPTY_JUKEBOX))).when(read)
151 .read(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
152 doReturn(immediateFluentFuture(Optional.empty()))
153 .when(read).read(LogicalDatastoreType.OPERATIONAL, JUKEBOX_IID);
154 final Response response = dataService.readData("example-jukebox:jukebox", uriInfo);
155 assertNotNull(response);
156 assertEquals(200, response.getStatus());
157 assertEquals(EMPTY_JUKEBOX, ((NormalizedNodePayload) response.getEntity()).data());
161 public void testReadRootData() {
162 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
163 doReturn(immediateFluentFuture(Optional.of(wrapNodeByDataRootContainer(CONFIG_JUKEBOX))))
165 .read(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.of());
166 doReturn(immediateFluentFuture(Optional.of(wrapNodeByDataRootContainer(OPER_JUKEBOX))))
168 .read(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.of());
169 final Response response = dataService.readData(uriInfo);
170 assertNotNull(response);
171 assertEquals(200, response.getStatus());
173 final NormalizedNode data = ((NormalizedNodePayload) response.getEntity()).data();
174 assertTrue(data instanceof ContainerNode);
175 final Collection<DataContainerChild> rootNodes = ((ContainerNode) data).body();
176 assertEquals(1, rootNodes.size());
177 final Collection<DataContainerChild> allDataChildren = ((ContainerNode) rootNodes.iterator().next()).body();
178 assertEquals(3, allDataChildren.size());
181 private static ContainerNode wrapNodeByDataRootContainer(final DataContainerChild data) {
182 return Builders.containerBuilder()
183 .withNodeIdentifier(NodeIdentifier.create(SchemaContext.NAME))
189 * Test read data from mount point when both {@link LogicalDatastoreType#CONFIGURATION} and
190 * {@link LogicalDatastoreType#OPERATIONAL} contains the same data and some additional data to be merged.
193 public void testReadDataMountPoint() {
194 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
195 doReturn(immediateFluentFuture(Optional.of(CONFIG_JUKEBOX))).when(read)
196 .read(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
197 doReturn(immediateFluentFuture(Optional.of(OPER_JUKEBOX))).when(read)
198 .read(LogicalDatastoreType.OPERATIONAL, JUKEBOX_IID);
200 final Response response = dataService.readData(
201 "example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", uriInfo);
203 assertNotNull(response);
204 assertEquals(200, response.getStatus());
206 // response must contain all child nodes from config and operational containers merged in one container
207 final NormalizedNode data = ((NormalizedNodePayload) response.getEntity()).data();
208 assertTrue(data instanceof ContainerNode);
209 assertEquals(3, ((ContainerNode) data).size());
210 assertNotNull(((ContainerNode) data).childByArg(CONT_PLAYER.name()));
211 assertNotNull(((ContainerNode) data).childByArg(LIBRARY_NID));
212 assertNotNull(((ContainerNode) data).childByArg(PLAYLIST_NID));
216 public void testReadDataNoData() {
217 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
218 doReturn(immediateFluentFuture(Optional.empty()))
219 .when(read).read(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
220 doReturn(immediateFluentFuture(Optional.empty()))
221 .when(read).read(LogicalDatastoreType.OPERATIONAL, JUKEBOX_IID);
223 final var errors = assertThrows(RestconfDocumentedException.class,
224 () -> dataService.readData("example-jukebox:jukebox", uriInfo)).getErrors();
225 assertEquals(1, errors.size());
226 final var error = errors.get(0);
227 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
228 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
229 assertEquals("Request could not be completed because the relevant data model content does not exist",
230 error.getErrorMessage());
234 * Read data from config datastore according to content parameter.
237 public void testReadDataConfigTest() {
238 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
239 parameters.put("content", List.of("config"));
241 doReturn(parameters).when(uriInfo).getQueryParameters();
242 doReturn(immediateFluentFuture(Optional.of(CONFIG_JUKEBOX))).when(read)
243 .read(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
245 final Response response = dataService.readData("example-jukebox:jukebox", uriInfo);
247 assertNotNull(response);
248 assertEquals(200, response.getStatus());
250 // response must contain only config data
251 final NormalizedNode data = ((NormalizedNodePayload) response.getEntity()).data();
253 // config data present
254 assertNotNull(((ContainerNode) data).childByArg(CONT_PLAYER.name()));
255 assertNotNull(((ContainerNode) data).childByArg(LIBRARY_NID));
258 assertNull(((ContainerNode) data).childByArg(PLAYLIST_NID));
262 * Read data from operational datastore according to content parameter.
265 public void testReadDataOperationalTest() {
266 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
267 parameters.put("content", List.of("nonconfig"));
269 doReturn(parameters).when(uriInfo).getQueryParameters();
270 doReturn(immediateFluentFuture(Optional.of(OPER_JUKEBOX))).when(read)
271 .read(LogicalDatastoreType.OPERATIONAL, JUKEBOX_IID);
273 final Response response = dataService.readData("example-jukebox:jukebox", uriInfo);
275 assertNotNull(response);
276 assertEquals(200, response.getStatus());
278 // response must contain only operational data
279 final NormalizedNode data = ((NormalizedNodePayload) response.getEntity()).data();
281 // state data present
282 assertNotNull(((ContainerNode) data).childByArg(CONT_PLAYER.name()));
283 assertNotNull(((ContainerNode) data).childByArg(PLAYLIST_NID));
285 // config data absent
286 assertNull(((ContainerNode) data).childByArg(LIBRARY_NID));
290 public void testPutData() {
291 doReturn(immediateTrueFluentFuture()).when(read)
292 .exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
293 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX);
295 doReturn(true).when(asyncResponse).resume(responseCaptor.capture());
296 dataService.putDataJSON("example-jukebox:jukebox", uriInfo, stringInputStream("""
298 "example-jukebox:jukebox" : {
303 }"""), asyncResponse);
304 final var response = responseCaptor.getValue();
305 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
309 public void testPutDataWithMountPoint() {
310 doReturn(immediateTrueFluentFuture()).when(read)
311 .exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
312 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX);
314 doReturn(true).when(asyncResponse).resume(responseCaptor.capture());
315 dataService.putDataXML("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox",
316 uriInfo, stringInputStream("""
317 <jukebox xmlns="http://example.com/ns/example-jukebox">
321 </jukebox>"""), asyncResponse);
322 final var response = responseCaptor.getValue();
323 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
326 private static InputStream stringInputStream(final String str) {
327 return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
331 public void testPostData() {
332 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
333 doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
334 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID,
335 Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME)).build());
336 doReturn(UriBuilder.fromUri("http://localhost:8181/rests/")).when(uriInfo).getBaseUriBuilder();
338 final var captor = ArgumentCaptor.forClass(Response.class);
339 doReturn(true).when(asyncResponse).resume(captor.capture());
340 dataService.postDataJSON(stringInputStream("""
342 "example-jukebox:jukebox" : {
344 }"""), uriInfo, asyncResponse);
345 final var response = captor.getValue();
346 assertEquals(201, response.getStatus());
347 assertEquals(URI.create("http://localhost:8181/rests/data/example-jukebox:jukebox"), response.getLocation());
351 public void testPostMapEntryData() {
352 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
353 final var node = PLAYLIST_IID.node(BAND_ENTRY.name());
354 doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
355 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, node, BAND_ENTRY);
356 doReturn(UriBuilder.fromUri("http://localhost:8181/rests/")).when(uriInfo).getBaseUriBuilder();
358 final var captor = ArgumentCaptor.forClass(Response.class);
359 doReturn(true).when(asyncResponse).resume(captor.capture());
360 dataService.postDataJSON("example-jukebox:jukebox", stringInputStream("""
362 "example-jukebox:playlist" : {
363 "name" : "name of band",
364 "description" : "band description"
366 }"""), uriInfo, asyncResponse);
367 final var response = captor.getValue();
368 assertEquals(201, response.getStatus());
369 assertEquals(URI.create("http://localhost:8181/rests/data/example-jukebox:jukebox/playlist=name%20of%20band"),
370 response.getLocation());
374 public void testDeleteData() {
375 doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
376 doReturn(immediateTrueFluentFuture())
377 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
378 final var captor = ArgumentCaptor.forClass(Response.class);
379 doReturn(true).when(asyncResponse).resume(captor.capture());
380 dataService.deleteData("example-jukebox:jukebox", asyncResponse);
382 assertEquals(204, captor.getValue().getStatus());
386 public void testDeleteDataNotExisting() {
387 doReturn(immediateFalseFluentFuture())
388 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
389 final var captor = ArgumentCaptor.forClass(RestconfDocumentedException.class);
390 doReturn(true).when(asyncResponse).resume(captor.capture());
391 dataService.deleteData("example-jukebox:jukebox", asyncResponse);
393 final var errors = captor.getValue().getErrors();
394 assertEquals(1, errors.size());
395 final var error = errors.get(0);
396 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
397 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
401 * Test of deleting data on mount point.
404 public void testDeleteDataMountPoint() {
405 doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
406 doReturn(immediateTrueFluentFuture())
407 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
408 final var captor = ArgumentCaptor.forClass(Response.class);
409 doReturn(true).when(asyncResponse).resume(captor.capture());
410 dataService.deleteData("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", asyncResponse);
412 assertEquals(204, captor.getValue().getStatus());
416 public void testPatchData() {
417 final var patch = new PatchContext("test patch id", List.of(
418 new PatchEntity("create data", Operation.Create, JUKEBOX_IID, EMPTY_JUKEBOX),
419 new PatchEntity("replace data", Operation.Replace, JUKEBOX_IID, EMPTY_JUKEBOX),
420 new PatchEntity("delete data", Operation.Delete, GAP_IID)));
422 doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, GAP_IID);
423 doReturn(immediateFalseFluentFuture())
424 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
425 doReturn(immediateTrueFluentFuture())
426 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, GAP_IID);
427 doReturn(true).when(asyncResponse).resume(responseCaptor.capture());
428 dataService.yangPatchData(JUKEBOX_SCHEMA, patch, null, asyncResponse);
429 final var response = responseCaptor.getValue();
430 assertEquals(200, response.getStatus());
431 final var status = assertInstanceOf(PatchStatusContext.class, response.getEntity());
433 assertTrue(status.ok());
434 assertEquals(3, status.editCollection().size());
435 assertEquals("replace data", status.editCollection().get(1).getEditId());
439 public void testPatchDataMountPoint() throws Exception {
440 final var patch = new PatchContext("test patch id", List.of(
441 new PatchEntity("create data", Operation.Create, JUKEBOX_IID, EMPTY_JUKEBOX),
442 new PatchEntity("replace data", Operation.Replace, JUKEBOX_IID, EMPTY_JUKEBOX),
443 new PatchEntity("delete data", Operation.Delete, GAP_IID)));
445 doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, GAP_IID);
446 doReturn(immediateFalseFluentFuture())
447 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
448 doReturn(immediateTrueFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, GAP_IID);
450 doReturn(true).when(asyncResponse).resume(responseCaptor.capture());
451 dataService.yangPatchData(JUKEBOX_SCHEMA, patch, mountPoint, asyncResponse);
452 final var response = responseCaptor.getValue();
453 assertEquals(200, response.getStatus());
454 final var status = assertInstanceOf(PatchStatusContext.class, response.getEntity());
456 assertTrue(status.ok());
457 assertEquals(3, status.editCollection().size());
458 assertNull(status.globalErrors());
462 public void testPatchDataDeleteNotExist() {
463 final var patch = new PatchContext("test patch id", List.of(
464 new PatchEntity("create data", Operation.Create, JUKEBOX_IID, EMPTY_JUKEBOX),
465 new PatchEntity("remove data", Operation.Remove, GAP_IID),
466 new PatchEntity("delete data", Operation.Delete, GAP_IID)));
468 doNothing().when(readWrite).delete(LogicalDatastoreType.CONFIGURATION, GAP_IID);
469 doReturn(immediateFalseFluentFuture())
470 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
471 doReturn(immediateFalseFluentFuture())
472 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, GAP_IID);
473 doReturn(true).when(readWrite).cancel();
475 doReturn(true).when(asyncResponse).resume(responseCaptor.capture());
476 dataService.yangPatchData(JUKEBOX_SCHEMA, patch, null, asyncResponse);
477 final var response = responseCaptor.getValue();
478 assertEquals(409, response.getStatus());
479 final var status = assertInstanceOf(PatchStatusContext.class, response.getEntity());
481 assertFalse(status.ok());
482 assertEquals(3, status.editCollection().size());
483 assertTrue(status.editCollection().get(0).isOk());
484 assertTrue(status.editCollection().get(1).isOk());
485 assertFalse(status.editCollection().get(2).isOk());
486 assertFalse(status.editCollection().get(2).getEditErrors().isEmpty());
487 final String errorMessage = status.editCollection().get(2).getEditErrors().get(0).getErrorMessage();
488 assertEquals("Data does not exist", errorMessage);