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