c2b5155f8a801197a9dc550733130cd2e7505bb4
[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.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;
16
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;
42
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);
47
48     @Mock
49     private UriInfo uriInfo;
50     @Mock
51     private DOMDataBroker dataBroker;
52     @Mock
53     private DOMActionService actionService;
54     @Mock
55     private DOMRpcService rpcService;
56     @Mock
57     private DOMMountPointService mountPointService;
58     @Mock
59     private AsyncResponse asyncResponse;
60     @Captor
61     private ArgumentCaptor<Response> captor;
62
63     @Test
64     void testInvokeAction() throws Exception {
65         doReturn(Futures.immediateFuture(new SimpleDOMActionResult(ImmutableNodes.newContainerBuilder()
66             .withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME))
67             .build())))
68             .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
69
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"),
75             stringInputStream("""
76             {
77               "instance-identifier-module:input": {
78                 "delay": 600
79               }
80             }"""), uriInfo, asyncResponse);
81         final var response = captor.getValue();
82         assertEquals(204, response.getStatus());
83         assertNull(response.getEntity());
84     }
85
86     @Test
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"))
91             .build())))
92             .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
93
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"),
99             stringInputStream("""
100             {
101               "instance-identifier-module:input": {
102                 "delay": 600
103               }
104             }"""), uriInfo, asyncResponse);
105         final var response = captor.getValue();
106         assertEquals(200, response.getStatus());
107
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);
113     }
114 }