31430a83e0c1c899b70609cf9223c4601a79d7bd
[netconf.git] / restconf / restconf-openapi / src / test / java / org / opendaylight / restconf / openapi / mountpoints / CustomOpenApiBasePathTest.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.openapi.mountpoints;
9
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;
14
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.Set;
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;
33
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();
40
41     private MountPointOpenApi openApi;
42
43     @Parameter
44     public static String basePath;
45
46     @Parameters(name = "{0}")
47     public static Collection<String> data() {
48         return List.of("rests", "restconf");
49     }
50
51     @Before
52     public void before() {
53         final var schemaService = mock(DOMSchemaService.class);
54         final var context = YangParserTestUtils.parseYang("""
55             module custom-base-path-test {
56               yang-version 1.1;
57               namespace urn:opendaylight:action-path-test;
58               prefix "path-test";
59
60               revision "2023-05-30" {
61                 description
62                 "Initial revision.";
63               }
64
65               rpc rpc-call {
66                 input {
67                   leaf rpc-call-input {
68                     type string;
69                   }
70                 }
71                 output {
72                   leaf rpc-call-output {
73                     type string;
74                   }
75                 }
76               }
77
78               container foo {
79                 list foo-list {
80                   key "fooListKey";
81                   leaf fooListKey {
82                     type string;
83                   }
84                 }
85                 action foo-action {
86                   input {
87                     leaf foo-action-input {
88                       type string;
89                     }
90                   }
91                   output {
92                     leaf foo-action-output {
93                       type string;
94                     }
95                   }
96                 }
97               }
98             }
99             """);
100
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);
109     }
110
111     @Test
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);
116
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",
124             containerDataRoot);
125
126         assertEquals("Unexpected paths", expectedPaths, mountPointApi.paths().keySet());
127     }
128 }