Factor out MdsalDatabindProvider
[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.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(new MdsalRestconfServer(
72             new MdsalDatabindProvider(new FixedDOMSchemaService(IID_SCHEMA)), dataBroker, rpcService, actionService,
73             mountPointService));
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"),
77             stringInputStream("""
78             {
79               "instance-identifier-module:input": {
80                 "delay": 600
81               }
82             }"""), uriInfo, asyncResponse);
83         final var response = captor.getValue();
84         assertEquals(204, response.getStatus());
85         assertNull(response.getEntity());
86     }
87
88     @Test
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"))
93             .build())))
94             .when(actionService).invokeAction(eq(RESET_PATH), any(), any());
95
96         final var restconf = new JaxRsRestconf(new MdsalRestconfServer(
97             new MdsalDatabindProvider(new FixedDOMSchemaService(IID_SCHEMA)), dataBroker, rpcService, actionService,
98             mountPointService));
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("""
103             {
104               "instance-identifier-module:input": {
105                 "delay": 600
106               }
107             }"""), uriInfo, asyncResponse);
108         final var response = captor.getValue();
109         assertEquals(200, response.getStatus());
110
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);
116     }
117 }