Move action invocation to MdsalRestconfServer
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / MdsalRestconfServerTest.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.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.junit.jupiter.api.Assertions.assertThrows;
14 import static org.mockito.Mockito.doReturn;
15
16 import java.util.Optional;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.mdsal.dom.api.DOMActionService;
24 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
27 import org.opendaylight.mdsal.dom.api.DOMRpcService;
28 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest;
31 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
32 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
33 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
34 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
35 import org.opendaylight.yangtools.yang.common.ErrorTag;
36 import org.opendaylight.yangtools.yang.common.ErrorType;
37
38 @RunWith(MockitoJUnitRunner.StrictStubs.class)
39 public class MdsalRestconfServerTest extends AbstractJukeboxTest {
40     private static DatabindProvider DATABIND_PROVIDER;
41
42     @Mock
43     private DOMMountPointService mountPointService;
44     @Mock
45     private DOMMountPoint mountPoint;
46     @Mock
47     private DOMDataBroker dataBroker;
48     @Mock
49     private NetconfDataTreeService netconfService;
50     @Mock
51     private DOMRpcService rpcService;
52     @Mock
53     private DOMActionService actionService;
54
55     private MdsalRestconfServer server;
56
57     @BeforeClass
58     public static void setupDatabind() {
59         DATABIND_PROVIDER = () -> DatabindContext.ofModel(JUKEBOX_SCHEMA);
60     }
61
62     @Before
63     public void before() {
64         server = new MdsalRestconfServer(DATABIND_PROVIDER, dataBroker, rpcService, actionService, mountPointService);
65         doReturn(Optional.of(rpcService)).when(mountPoint).getService(DOMRpcService.class);
66     }
67
68     @Test
69     public void testGetRestconfStrategyLocal() {
70         assertInstanceOf(MdsalRestconfStrategy.class, server.getRestconfStrategy(JUKEBOX_SCHEMA, null));
71     }
72
73     @Test
74     public void testGetRestconfStrategyMountDataBroker() {
75         doReturn(Optional.empty()).when(mountPoint).getService(NetconfDataTreeService.class);
76         doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
77         assertInstanceOf(MdsalRestconfStrategy.class, server.getRestconfStrategy(JUKEBOX_SCHEMA, mountPoint));
78     }
79
80     @Test
81     public void testGetRestconfStrategyMountNetconfService() {
82         doReturn(Optional.of(netconfService)).when(mountPoint).getService(NetconfDataTreeService.class);
83         assertInstanceOf(NetconfRestconfStrategy.class, server.getRestconfStrategy(JUKEBOX_SCHEMA, mountPoint));
84     }
85
86     @Test
87     public void testGetRestconfStrategyMountNone() {
88         doReturn(JUKEBOX_IID).when(mountPoint).getIdentifier();
89         doReturn(Optional.empty()).when(mountPoint).getService(NetconfDataTreeService.class);
90         doReturn(Optional.empty()).when(mountPoint).getService(DOMDataBroker.class);
91         final var ex = assertThrows(RestconfDocumentedException.class,
92             () -> server.getRestconfStrategy(JUKEBOX_SCHEMA, mountPoint));
93         final var errors = ex.getErrors();
94         assertEquals(1, errors.size());
95         final var error = errors.get(0);
96         assertEquals(ErrorType.APPLICATION, error.getErrorType());
97         assertEquals(ErrorTag.OPERATION_FAILED, error.getErrorTag());
98         assertEquals("Could not find a supported access interface in mount point "
99             + "/(http://example.com/ns/example-jukebox?revision=2015-04-04)jukebox", error.getErrorMessage());
100         // FIXME: should be JUKEBOX_IID
101         assertNull(error.getErrorPath());
102     }
103 }