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.jaxrs;
10 import static org.junit.Assert.assertThrows;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.lenient;
15 import static org.mockito.Mockito.mock;
16 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
17 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
20 import javax.ws.rs.container.AsyncResponse;
21 import javax.ws.rs.core.MultivaluedHashMap;
22 import javax.ws.rs.core.UriBuilder;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.opendaylight.mdsal.common.api.CommitInfo;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
31 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
32 import org.opendaylight.yangtools.yang.common.ErrorTag;
33 import org.opendaylight.yangtools.yang.common.ErrorType;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
37 @ExtendWith(MockitoExtension.class)
38 class RestconfDataPostTest extends AbstractRestconfTest {
40 private DOMDataTreeReadWriteTransaction tx;
44 lenient().doReturn(CommitInfo.emptyFluentFuture()).when(tx).commit();
45 doReturn(tx).when(dataBroker).newReadWriteTransaction();
50 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
51 doReturn(immediateFalseFluentFuture()).when(tx).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
52 doNothing().when(tx).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID,
53 Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME)).build());
54 doReturn(UriBuilder.fromUri("http://localhost:8181/rests/")).when(uriInfo).getBaseUriBuilder();
56 assertEquals(URI.create("http://localhost:8181/rests/data/example-jukebox:jukebox"),
57 assertResponse(201, ar -> restconf.postDataJSON(stringInputStream("""
59 "example-jukebox:jukebox" : {
61 }"""), uriInfo, ar)).getLocation());
65 public void testPostMapEntryData() {
66 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
67 final var node = PLAYLIST_IID.node(BAND_ENTRY.name());
68 doReturn(immediateFalseFluentFuture()).when(tx).exists(LogicalDatastoreType.CONFIGURATION, node);
69 doNothing().when(tx).put(LogicalDatastoreType.CONFIGURATION, node, BAND_ENTRY);
70 doReturn(UriBuilder.fromUri("http://localhost:8181/rests/")).when(uriInfo).getBaseUriBuilder();
72 assertEquals(URI.create("http://localhost:8181/rests/data/example-jukebox:jukebox/playlist=name%20of%20band"),
73 assertResponse(201, ar -> restconf.postDataJSON(JUKEBOX_API_PATH, stringInputStream("""
75 "example-jukebox:playlist" : {
76 "name" : "name of band",
77 "description" : "band description"
79 }"""), uriInfo, ar)).getLocation());
83 public void testPostExistingData() {
84 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
85 doReturn(immediateTrueFluentFuture())
86 .when(tx).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
87 final var mockAsyncResponse = mock(AsyncResponse.class);
89 final var ex = assertThrows(RestconfDocumentedException.class, () -> restconf
90 .postDataJSON(stringInputStream("""
92 "example-jukebox:jukebox" : {
95 uriInfo, mockAsyncResponse));
97 final var error = ex.getErrors().get(0);
98 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
99 assertEquals(ErrorTag.DATA_EXISTS, error.getErrorTag());