96a4c8b4a3fcab0231a388ac290f8e9dca3ff46a
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / RestconfDataPostTest.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.jaxrs;
9
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;
18
19 import java.net.URI;
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;
36
37 @ExtendWith(MockitoExtension.class)
38 class RestconfDataPostTest extends AbstractRestconfTest {
39     @Mock
40     private DOMDataTreeReadWriteTransaction tx;
41
42     @BeforeEach
43     void beforeEach() {
44         lenient().doReturn(CommitInfo.emptyFluentFuture()).when(tx).commit();
45         doReturn(tx).when(dataBroker).newReadWriteTransaction();
46     }
47
48     @Test
49     void testPostData() {
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();
55
56         assertEquals(URI.create("http://localhost:8181/rests/data/example-jukebox:jukebox"),
57             assertResponse(201, ar -> restconf.postDataJSON(stringInputStream("""
58                 {
59                   "example-jukebox:jukebox" : {
60                   }
61                 }"""), uriInfo, ar)).getLocation());
62     }
63
64     @Test
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();
71
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("""
74                 {
75                   "example-jukebox:playlist" : {
76                     "name" : "name of band",
77                     "description" : "band description"
78                   }
79                 }"""), uriInfo, ar)).getLocation());
80     }
81
82     @Test
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);
88
89         final var ex = assertThrows(RestconfDocumentedException.class, () -> restconf
90             .postDataJSON(stringInputStream("""
91                 {
92                   "example-jukebox:jukebox" : {
93                   }
94                 }"""),
95             uriInfo, mockAsyncResponse));
96
97         final var error = ex.getErrors().get(0);
98         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
99         assertEquals(ErrorTag.DATA_EXISTS, error.getErrorTag());
100     }
101 }