Use RestconfFuture/AsyncResponse for postData()
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / Netconf799Test.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech s.r.o. 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.assertNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.doReturn;
15
16 import com.google.common.util.concurrent.Futures;
17 import javax.ws.rs.container.AsyncResponse;
18 import javax.ws.rs.core.Response;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.Captor;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.dom.api.DOMActionService;
26 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
27 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
28 import org.opendaylight.mdsal.dom.api.DOMRpcService;
29 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
30 import org.opendaylight.restconf.nb.rfc8040.AbstractInstanceIdentifierTest;
31 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
32 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
33 import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
34 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
38 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
39
40 @RunWith(MockitoJUnitRunner.StrictStubs.class)
41 public class Netconf799Test extends AbstractInstanceIdentifierTest {
42     private static final QName OUTPUT_QNAME = QName.create(CONT_QNAME, "output");
43
44     @Mock
45     private DOMDataBroker dataBroker;
46     @Mock
47     private DOMActionService actionService;
48     @Mock
49     private DOMRpcService rpcService;
50     @Mock
51     private DOMMountPointService mountPointService;
52     @Mock
53     private RestconfStreamsSubscriptionService restconfStreamSubService;
54     @Mock
55     private AsyncResponse asyncResponse;
56     @Captor
57     private ArgumentCaptor<Response> captor;
58
59     @Test
60     public void testInvokeAction() {
61         doReturn(Futures.immediateFuture(new SimpleDOMActionResult(
62             Builders.containerBuilder().withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME)).build())))
63             .when(actionService).invokeAction(eq(Absolute.of(CONT_QNAME, CONT1_QNAME, RESET_QNAME)), any(), any());
64
65         final var dataService = new RestconfDataServiceImpl(() -> DatabindContext.ofModel(IID_SCHEMA),
66             new MdsalRestconfServer(dataBroker, rpcService, mountPointService), dataBroker, restconfStreamSubService,
67             actionService, new ListenersBroker(), new StreamsConfiguration(0, 1, 0, false));
68
69         doReturn(true).when(asyncResponse).resume(captor.capture());
70         dataService.postDataJSON("instance-identifier-module:cont/cont1/reset",
71             stringInputStream("""
72             {
73               "instance-identifier-module:input": {
74                 "delay": 600
75               }
76             }"""), null, asyncResponse);
77         final var response = captor.getValue();
78         assertEquals(204, response.getStatus());
79         assertNull(response.getEntity());
80     }
81 }