2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.openapi.mountpoints;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.junit.runners.Parameterized;
23 import org.junit.runners.Parameterized.Parameter;
24 import org.junit.runners.Parameterized.Parameters;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
27 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
28 import org.opendaylight.restconf.openapi.DocGenTestHelper;
29 import org.opendaylight.restconf.openapi.impl.MountPointOpenApiGeneratorRFC8040;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34 @RunWith(Parameterized.class)
35 public final class CustomOpenApiBasePathTest {
36 private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
37 .node(QName.create("", "nodes"))
38 .node(QName.create("", "node"))
39 .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
41 private MountPointOpenApi openApi;
44 public static String basePath;
46 @Parameters(name = "{0}")
47 public static Collection<String> data() {
48 return List.of("rests", "restconf");
52 public void before() {
53 final var schemaService = mock(DOMSchemaService.class);
54 final var context = YangParserTestUtils.parseYang("""
55 module custom-base-path-test {
57 namespace urn:opendaylight:action-path-test;
60 revision "2023-05-30" {
72 leaf rpc-call-output {
87 leaf foo-action-input {
92 leaf foo-action-output {
101 when(schemaService.getGlobalContext()).thenReturn(context);
102 final var mountPoint = mock(DOMMountPoint.class);
103 when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
104 final var mountPointService = mock(DOMMountPointService.class);
105 when(mountPointService.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
106 final var generator = new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService, basePath);
107 openApi = generator.getMountPointOpenApi();
108 openApi.onMountPointCreated(INSTANCE_ID);
112 public void testCustomOpenApiBasePath() throws Exception {
113 final var mockInfo = DocGenTestHelper.createMockUriInfo("http://localhost/path");
114 final var mountPointApi = openApi.getMountPointApi(mockInfo, 1L, "custom-base-path-test", "2023-05-30");
115 assertNotNull("Failed to find Datastore API", mountPointApi);
117 final var containerOperationRoot = "/" + basePath + "/operations/nodes/node=123/yang-ext:mount";
118 final var containerDataRoot = "/" + basePath + "/data/nodes/node=123/yang-ext:mount";
119 final var expectedPaths = Set.of(
120 containerOperationRoot + "/custom-base-path-test:rpc-call",
121 containerOperationRoot + "/custom-base-path-test:foo/foo-action",
122 containerDataRoot + "/custom-base-path-test:foo/foo-list={fooListKey}",
123 containerDataRoot + "/custom-base-path-test:foo",
126 assertEquals("Unexpected paths", expectedPaths, mountPointApi.paths().keySet());