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.MdsalRestconfServer;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
43 @ExtendWith(MockitoExtension.class)
44 class Netconf799Test extends AbstractInstanceIdentifierTest {
45 private static final QName OUTPUT_QNAME = QName.create(CONT_QNAME, "output");
46 private static final Absolute RESET_PATH = Absolute.of(CONT_QNAME, CONT1_QNAME, RESET_QNAME);
49 private UriInfo uriInfo;
51 private DOMDataBroker dataBroker;
53 private DOMActionService actionService;
55 private DOMRpcService rpcService;
57 private DOMMountPointService mountPointService;
59 private AsyncResponse asyncResponse;
61 private ArgumentCaptor<Response> captor;
64 void testInvokeAction() throws Exception {
65 doReturn(Futures.immediateFuture(new SimpleDOMActionResult(ImmutableNodes.newContainerBuilder()
66 .withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME))
68 .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
70 final var restconf = new JaxRsRestconf(new MdsalRestconfServer(new FixedDOMSchemaService(IID_SCHEMA),
71 dataBroker, rpcService, actionService, mountPointService));
72 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
73 doReturn(true).when(asyncResponse).resume(captor.capture());
74 restconf.postDataJSON(ApiPath.parse("instance-identifier-module:cont/cont1/reset"),
77 "instance-identifier-module:input": {
80 }"""), uriInfo, asyncResponse);
81 final var response = captor.getValue();
82 assertEquals(204, response.getStatus());
83 assertNull(response.getEntity());
87 void testInvokeActionOutput() throws Exception {
88 doReturn(Futures.immediateFuture(new SimpleDOMActionResult(ImmutableNodes.newContainerBuilder()
89 .withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME))
90 .withChild(ImmutableNodes.leafNode(QName.create(OUTPUT_QNAME, "timestamp"), "somevalue"))
92 .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
94 final var restconf = new JaxRsRestconf(new MdsalRestconfServer(new FixedDOMSchemaService(IID_SCHEMA),
95 dataBroker, rpcService, actionService, mountPointService));
96 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
97 doReturn(true).when(asyncResponse).resume(captor.capture());
98 restconf.postDataJSON(ApiPath.parse("instance-identifier-module:cont/cont1/reset"),
101 "instance-identifier-module:input": {
104 }"""), uriInfo, asyncResponse);
105 final var response = captor.getValue();
106 assertEquals(200, response.getStatus());
108 final var payload = assertInstanceOf(NormalizedNodePayload.class, response.getEntity());
109 AbstractRestconfTest.assertJson("""
110 {"instance-identifier-module:output":{"timestamp":"somevalue"}}""", payload);
111 AbstractRestconfTest.assertXml("""
112 <output xmlns="instance:identifier:module"><timestamp>somevalue</timestamp></output>""", payload);