Rework body formatting wiring
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / 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.jaxrs;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.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.MultivaluedHashMap;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.UriInfo;
21 import org.junit.jupiter.api.Test;
22 import org.junit.jupiter.api.extension.ExtendWith;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.mockito.Mock;
26 import org.mockito.junit.jupiter.MockitoExtension;
27 import org.opendaylight.mdsal.dom.api.DOMActionService;
28 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
29 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
30 import org.opendaylight.mdsal.dom.api.DOMRpcService;
31 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
32 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
33 import org.opendaylight.restconf.api.ApiPath;
34 import org.opendaylight.restconf.api.query.PrettyPrintParam;
35 import org.opendaylight.restconf.nb.rfc8040.AbstractInstanceIdentifierTest;
36 import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest;
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;
43
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);
48
49     @Mock
50     private UriInfo uriInfo;
51     @Mock
52     private DOMDataBroker dataBroker;
53     @Mock
54     private DOMActionService actionService;
55     @Mock
56     private DOMRpcService rpcService;
57     @Mock
58     private DOMMountPointService mountPointService;
59     @Mock
60     private AsyncResponse asyncResponse;
61     @Captor
62     private ArgumentCaptor<Response> captor;
63
64     @Test
65     void testInvokeAction() throws Exception {
66         doReturn(Futures.immediateFuture(new SimpleDOMActionResult(ImmutableNodes.newContainerBuilder()
67             .withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME))
68             .build())))
69             .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
70
71         final var restconf = new JaxRsRestconf(
72             new MdsalRestconfServer(new MdsalDatabindProvider(new FixedDOMSchemaService(IID_SCHEMA)),
73                 dataBroker, rpcService, actionService, mountPointService),
74             PrettyPrintParam.FALSE);
75         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
76         doReturn(true).when(asyncResponse).resume(captor.capture());
77         restconf.postDataJSON(ApiPath.parse("instance-identifier-module:cont/cont1/reset"),
78             stringInputStream("""
79             {
80               "instance-identifier-module:input": {
81                 "delay": 600
82               }
83             }"""), uriInfo, asyncResponse);
84         final var response = captor.getValue();
85         assertEquals(204, response.getStatus());
86         assertNull(response.getEntity());
87     }
88
89     @Test
90     void testInvokeActionOutput() throws Exception {
91         doReturn(Futures.immediateFuture(new SimpleDOMActionResult(ImmutableNodes.newContainerBuilder()
92             .withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME))
93             .withChild(ImmutableNodes.leafNode(QName.create(OUTPUT_QNAME, "timestamp"), "somevalue"))
94             .build())))
95             .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
96
97         final var restconf = new JaxRsRestconf(
98             new MdsalRestconfServer(new MdsalDatabindProvider(new FixedDOMSchemaService(IID_SCHEMA)),
99                 dataBroker, rpcService, actionService, mountPointService),
100             PrettyPrintParam.FALSE);
101         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
102
103         final var apiPath = ApiPath.parse("instance-identifier-module:cont/cont1/reset");
104         final var body = AbstractRestconfTest.assertFormattableBody(200, ar -> {
105             restconf.postDataJSON(apiPath,
106                 stringInputStream("""
107                     {
108                       "instance-identifier-module:input": {
109                         "delay": 600
110                       }
111                     }"""), uriInfo, ar);
112         });
113
114         AbstractJukeboxTest.assertFormat("""
115             {"instance-identifier-module:output":{"timestamp":"somevalue"}}""", body::formatToJSON, false);
116         AbstractJukeboxTest.assertFormat("""
117             <output xmlns="instance:identifier:module"><timestamp>somevalue</timestamp></output>""", body::formatToXML,
118             false);
119     }
120 }