2 * Copyright (c) 2021 PANTHEON.tech s.r.o. 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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertNull;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.doReturn;
17 import com.google.common.util.concurrent.Futures;
18 import javax.ws.rs.container.AsyncResponse;
19 import javax.ws.rs.core.MultivaluedHashMap;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.UriInfo;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.extension.ExtendWith;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Captor;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.opendaylight.mdsal.dom.api.DOMActionService;
29 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
30 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
31 import org.opendaylight.mdsal.dom.api.DOMRpcService;
32 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
33 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
34 import org.opendaylight.restconf.api.ApiPath;
35 import org.opendaylight.restconf.nb.rfc8040.AbstractInstanceIdentifierTest;
36 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
37 import org.opendaylight.restconf.server.mdsal.MdsalDatabindProvider;
38 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
44 @ExtendWith(MockitoExtension.class)
45 class Netconf799Test extends AbstractInstanceIdentifierTest {
46 private static final QName OUTPUT_QNAME = QName.create(CONT_QNAME, "output");
47 private static final Absolute RESET_PATH = Absolute.of(CONT_QNAME, CONT1_QNAME, RESET_QNAME);
50 private UriInfo uriInfo;
52 private DOMDataBroker dataBroker;
54 private DOMActionService actionService;
56 private DOMRpcService rpcService;
58 private DOMMountPointService mountPointService;
60 private AsyncResponse asyncResponse;
62 private ArgumentCaptor<Response> captor;
65 void testInvokeAction() throws Exception {
66 doReturn(Futures.immediateFuture(new SimpleDOMActionResult(ImmutableNodes.newContainerBuilder()
67 .withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME))
69 .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
71 final var restconf = new JaxRsRestconf(new MdsalRestconfServer(
72 new MdsalDatabindProvider(new FixedDOMSchemaService(IID_SCHEMA)), dataBroker, rpcService, actionService,
74 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
75 doReturn(true).when(asyncResponse).resume(captor.capture());
76 restconf.postDataJSON(ApiPath.parse("instance-identifier-module:cont/cont1/reset"),
79 "instance-identifier-module:input": {
82 }"""), uriInfo, asyncResponse);
83 final var response = captor.getValue();
84 assertEquals(204, response.getStatus());
85 assertNull(response.getEntity());
89 void testInvokeActionOutput() throws Exception {
90 doReturn(Futures.immediateFuture(new SimpleDOMActionResult(ImmutableNodes.newContainerBuilder()
91 .withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME))
92 .withChild(ImmutableNodes.leafNode(QName.create(OUTPUT_QNAME, "timestamp"), "somevalue"))
94 .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
96 final var restconf = new JaxRsRestconf(new MdsalRestconfServer(
97 new MdsalDatabindProvider(new FixedDOMSchemaService(IID_SCHEMA)), dataBroker, rpcService, actionService,
99 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
100 doReturn(true).when(asyncResponse).resume(captor.capture());
101 restconf.postDataJSON(ApiPath.parse("instance-identifier-module:cont/cont1/reset"),
102 stringInputStream("""
104 "instance-identifier-module:input": {
107 }"""), uriInfo, asyncResponse);
108 final var response = captor.getValue();
109 assertEquals(200, response.getStatus());
111 final var payload = assertInstanceOf(NormalizedNodePayload.class, response.getEntity());
112 AbstractRestconfTest.assertJson("""
113 {"instance-identifier-module:output":{"timestamp":"somevalue"}}""", payload);
114 AbstractRestconfTest.assertXml("""
115 <output xmlns="instance:identifier:module"><timestamp>somevalue</timestamp></output>""", payload);