Remove RestconfDocumentedException throws
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / RestconfOperationsGetTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  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.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doReturn;
12
13 import java.util.Optional;
14 import org.junit.jupiter.api.Test;
15 import org.junit.jupiter.api.extension.ExtendWith;
16 import org.mockito.junit.jupiter.MockitoExtension;
17 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
18 import org.opendaylight.mdsal.dom.api.DOMActionService;
19 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
20 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
21 import org.opendaylight.mdsal.dom.api.DOMRpcService;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
23 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
24 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
25 import org.opendaylight.restconf.api.ApiPath;
26 import org.opendaylight.yang.gen.v1.module._1.rev140101.Module1Data;
27 import org.opendaylight.yang.gen.v1.module._2.rev140102.Module2Data;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30
31 @ExtendWith(MockitoExtension.class)
32 class RestconfOperationsGetTest extends AbstractRestconfTest {
33     private static final ApiPath DEVICE_ID =
34         apiPath("network-topology:network-topology/topology=topology-netconf/node=device/yang-ext:mount");
35     private static final ApiPath DEVICE_RPC1_MODULE1_ID = apiPath("network-topology:network-topology/"
36         + "topology=topology-netconf/node=device/yang-ext:mount/module1:dummy-rpc1-module1");
37     private static final String EXPECTED_JSON = """
38         {
39           "ietf-restconf:operations" : {
40             "module1:dummy-rpc1-module1" : [null],
41             "module1:dummy-rpc2-module1" : [null],
42             "module2:dummy-rpc1-module2" : [null],
43             "module2:dummy-rpc2-module2" : [null]
44           }
45         }""";
46     private static final String EXPECTED_XML = """
47         <operations xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf">
48           <dummy-rpc1-module1 xmlns="module:1"/>
49           <dummy-rpc2-module1 xmlns="module:1"/>
50           <dummy-rpc1-module2 xmlns="module:2"/>
51           <dummy-rpc2-module2 xmlns="module:2"/>
52         </operations>""";
53
54     private static final EffectiveModelContext MODEL_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(
55         Module1Data.class, Module2Data.class, NetworkTopology.class).modelContext();
56
57     @Override
58     EffectiveModelContext modelContext() {
59         return MODEL_CONTEXT;
60     }
61
62     @Test
63     void testOperations() {
64         final var body = assertFormattableBody(200, ar -> restconf.operationsGET(ar));
65
66         assertFormat(EXPECTED_JSON, body::formatToJSON, true);
67         assertFormat(EXPECTED_XML, body::formatToXML, true);
68     }
69
70     private void mockMountPoint() {
71         doReturn(Optional.of(new FixedDOMSchemaService(MODEL_CONTEXT))).when(mountPoint)
72             .getService(DOMSchemaService.class);
73         doReturn(Optional.empty()).when(mountPoint).getService(DOMRpcService.class);
74         doReturn(Optional.empty()).when(mountPoint).getService(DOMActionService.class);
75         doReturn(Optional.empty()).when(mountPoint).getService(DOMMountPointService.class);
76         doReturn(Optional.empty()).when(mountPoint).getService(NetconfDataTreeService.class);
77         doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
78         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any());
79     }
80
81     @Test
82     void testMountPointOperations() {
83         mockMountPoint();
84
85         final var body = assertFormattableBody(200, ar -> restconf.operationsGET(DEVICE_ID, ar));
86         assertFormat(EXPECTED_JSON, body::formatToJSON, true);
87         assertFormat(EXPECTED_XML, body::formatToXML, true);
88     }
89
90     @Test
91     void testMountPointSpecificOperationsJson() {
92         mockMountPoint();
93
94         final var body = assertFormattableBody(200, ar -> restconf.operationsGET(DEVICE_RPC1_MODULE1_ID, ar));
95         assertFormat("""
96             { "module1:dummy-rpc1-module1" : [null] }""", body::formatToJSON, false);
97         assertFormat("""
98             <dummy-rpc1-module1 xmlns="module:1"/>""", body::formatToXML, false);
99     }
100 }