Introduce restconf.server.{api,spi,mdsal}
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfOperationsServiceImplTest.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.rfc8040.rests.services.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doReturn;
13
14 import java.util.Optional;
15 import org.junit.Before;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
22 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMRpcService;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
27 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
28 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
29 import org.opendaylight.yang.gen.v1.module._1.rev140101.Module1Data;
30 import org.opendaylight.yang.gen.v1.module._2.rev140102.Module2Data;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33
34 @RunWith(MockitoJUnitRunner.StrictStubs.class)
35 public class RestconfOperationsServiceImplTest {
36     private static final String DEVICE_ID = "network-topology:network-topology/topology=topology-netconf/"
37         + "node=device/yang-ext:mount";
38     private static final String DEVICE_RPC1_MODULE1_ID = DEVICE_ID + "module1:dummy-rpc1-module1";
39     private static final String EXPECTED_JSON = """
40         {
41           "ietf-restconf:operations" : {
42             "module1:dummy-rpc1-module1": [null],
43             "module1:dummy-rpc2-module1": [null],
44             "module2:dummy-rpc1-module2": [null],
45             "module2:dummy-rpc2-module2": [null]
46           }
47         }""";
48     private static final String EXPECTED_XML = """
49         <?xml version="1.0" encoding="UTF-8"?>
50         <operations xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf"
51                     xmlns:ns0="module:1"
52                     xmlns:ns1="module:2" >
53           <ns0:dummy-rpc1-module1/>
54           <ns0:dummy-rpc2-module1/>
55           <ns1:dummy-rpc1-module2/>
56           <ns1:dummy-rpc2-module2/>
57         </operations>""";
58
59     private static EffectiveModelContext SCHEMA;
60
61     @Mock
62     private DOMMountPointService mountPointService;
63     @Mock
64     private DOMMountPoint mountPoint;
65     @Mock
66     private DOMSchemaService schemaService;
67     @Mock
68     private DOMDataBroker dataBroker;
69     @Mock
70     private DOMRpcService rpcService;
71
72     private RestconfOperationsServiceImpl opService;
73
74     @BeforeClass
75     public static void beforeClass() {
76         SCHEMA = BindingRuntimeHelpers.createRuntimeContext(Module1Data.class, Module2Data.class, NetworkTopology.class)
77             .getEffectiveModelContext();
78     }
79
80     @Before
81     public void before() {
82         doReturn(SCHEMA).when(schemaService).getGlobalContext();
83         doReturn(Optional.of(schemaService)).when(mountPoint).getService(DOMSchemaService.class);
84         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any());
85
86         final DatabindProvider databindProvider = () -> DatabindContext.ofModel(SCHEMA);
87         opService = new RestconfOperationsServiceImpl(
88             new MdsalRestconfServer(databindProvider, dataBroker, rpcService, mountPointService));
89     }
90
91     @Test
92     public void testOperationsJson() {
93         final var operationsJSON = opService.getOperationsJSON();
94         assertEquals(EXPECTED_JSON, operationsJSON);
95     }
96
97     @Test
98     public void testOperationsXml() {
99         final var operationsXML = opService.getOperationsXML();
100         assertEquals(EXPECTED_XML, operationsXML);
101     }
102
103     @Test
104     public void testMountPointOperationsJson() {
105         final var operationJSON = opService.getOperationJSON(DEVICE_ID);
106         assertEquals(EXPECTED_JSON, operationJSON);
107     }
108
109     @Test
110     public void testMountPointOperationsXml() {
111         final var operationXML = opService.getOperationXML(DEVICE_ID);
112         assertEquals(EXPECTED_XML, operationXML);
113     }
114
115     @Test
116     public void testMountPointSpecificOperationsJson() {
117         final var operationJSON = opService.getOperationJSON(DEVICE_RPC1_MODULE1_ID);
118         assertEquals("""
119             {
120               "ietf-restconf:operations" : {
121                 "module1:dummy-rpc1-module1": [null]
122               }
123             }""", operationJSON);
124     }
125
126     @Test
127     public void testMountPointSpecificOperationsXml() {
128         final var operationXML = opService.getOperationXML(DEVICE_RPC1_MODULE1_ID);
129         assertEquals("""
130             <?xml version="1.0" encoding="UTF-8"?>
131             <operations xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf"
132                         xmlns:ns0="module:1" >
133               <ns0:dummy-rpc1-module1/>
134             </operations>""", operationXML);
135     }
136 }