2 * Copyright (c) 2014 Brocade Communications Systems, Inc. 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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import com.fasterxml.jackson.databind.JsonNode;
18 import java.util.ArrayList;
19 import java.util.List;
21 import java.util.Optional;
23 import java.util.TreeSet;
24 import javax.ws.rs.core.UriInfo;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
28 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
29 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
30 import org.opendaylight.restconf.openapi.AbstractOpenApiTest;
31 import org.opendaylight.restconf.openapi.DocGenTestHelper;
32 import org.opendaylight.restconf.openapi.impl.MountPointOpenApiGeneratorRFC8040;
33 import org.opendaylight.restconf.openapi.model.OpenApiObject;
34 import org.opendaylight.restconf.openapi.model.Path;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 public final class MountPointOpenApiTest extends AbstractOpenApiTest {
39 private static final String HTTP_URL = "http://localhost/path";
40 private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
41 .node(QName.create("", "nodes"))
42 .node(QName.create("", "node"))
43 .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
44 private static final String INSTANCE_URL = "/nodes/node=123/";
46 private MountPointOpenApi openApi;
49 public void before() {
50 // We are sharing the global schema service and the mount schema service
52 // OK for testing - real thing would have separate instances.
53 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
54 when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(SCHEMA_SERVICE));
56 final DOMMountPointService service = mock(DOMMountPointService.class);
57 when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
59 openApi = new MountPointOpenApiGeneratorRFC8040(SCHEMA_SERVICE, service).getMountPointOpenApi();
63 public void getInstanceIdentifiers() {
64 assertEquals(0, openApi.getInstanceIdentifiers().size());
65 openApi.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
66 assertEquals(1, openApi.getInstanceIdentifiers().size());
67 assertEquals((Long) 1L, openApi.getInstanceIdentifiers().entrySet().iterator().next()
69 assertEquals(INSTANCE_URL, openApi.getInstanceIdentifiers().entrySet().iterator().next()
71 openApi.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount points
72 assertEquals(0, openApi.getInstanceIdentifiers().size());
76 public void testGetDataStoreApi() throws Exception {
77 final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
78 openApi.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
80 final OpenApiObject mountPointApi = openApi.getMountPointApi(mockInfo, 1L, "Datastores", "-");
81 assertNotNull("Failed to find Datastore API", mountPointApi);
83 final Map<String, Path> paths = mountPointApi.getPaths();
86 assertEquals("Unexpected api list size", 2, paths.size());
88 final Set<String> actualUrls = new TreeSet<>();
90 for (final Map.Entry<String, Path> path : paths.entrySet()) {
91 actualUrls.add(path.getKey());
92 final JsonNode getOperation = path.getValue().getGet();
93 assertNotNull("Unexpected operation method on " + path, getOperation);
94 assertNotNull("Expected non-null desc on " + path, getOperation.get("description"));
97 assertEquals(Set.of("/rests/data" + INSTANCE_URL + "yang-ext:mount",
98 "/rests/operations" + INSTANCE_URL + "yang-ext:mount"), actualUrls);
102 * Test that creates mount point api with all models from yang folder and checks operation paths for these models.
105 public void testGetMountPointApi() throws Exception {
106 final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
107 openApi.onMountPointCreated(INSTANCE_ID);
109 final OpenApiObject mountPointApi = openApi.getMountPointApi(mockInfo, 1L, Optional.empty());
110 assertNotNull("Failed to find Datastore API", mountPointApi);
112 final Map<String, Path> paths = mountPointApi.getPaths();
113 assertNotNull(paths);
115 assertEquals("Unexpected api list size", 31, paths.size());
117 final List<JsonNode> getOperations = new ArrayList<>();
118 final List<JsonNode> postOperations = new ArrayList<>();
119 final List<JsonNode> putOperations = new ArrayList<>();
120 final List<JsonNode> patchOperations = new ArrayList<>();
121 final List<JsonNode> deleteOperations = new ArrayList<>();
123 for (final Map.Entry<String, Path> path : paths.entrySet()) {
124 Optional.ofNullable(path.getValue().getGet()).ifPresent(getOperations::add);
125 Optional.ofNullable(path.getValue().getPost()).ifPresent(postOperations::add);
126 Optional.ofNullable(path.getValue().getPut()).ifPresent(putOperations::add);
127 Optional.ofNullable(path.getValue().getPatch()).ifPresent(patchOperations::add);
128 Optional.ofNullable(path.getValue().getDelete()).ifPresent(deleteOperations::add);
131 assertEquals("Unexpected GET paths size", 23, getOperations.size());
132 assertEquals("Unexpected POST paths size", 29, postOperations.size());
133 assertEquals("Unexpected PUT paths size", 21, putOperations.size());
134 assertEquals("Unexpected PATCH paths size", 21, patchOperations.size());
135 assertEquals("Unexpected DELETE paths size", 21, deleteOperations.size());
139 * Test that checks for correct amount of parameters in requests.
142 @SuppressWarnings("checkstyle:lineLength")
143 public void testMountPointRecursiveParameters() throws Exception {
144 final var configPaths = Map.of("/rests/data/nodes/node=123/yang-ext:mount/recursive:container-root", 0,
145 "/rests/data/nodes/node=123/yang-ext:mount/recursive:container-root/root-list={name}", 1,
146 "/rests/data/nodes/node=123/yang-ext:mount/recursive:container-root/root-list={name}/nested-list={name1}", 2,
147 "/rests/data/nodes/node=123/yang-ext:mount/recursive:container-root/root-list={name}/nested-list={name1}/super-nested-list={name2}", 3);
149 final var mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
150 openApi.onMountPointCreated(INSTANCE_ID);
152 final var mountPointApi = openApi.getMountPointApi(mockInfo, 1L, "recursive", "2023-05-22");
153 assertNotNull("Failed to find Datastore API", mountPointApi);
155 final var paths = mountPointApi.getPaths();
156 assertEquals(5, paths.size());
158 for (final var expectedPath : configPaths.entrySet()) {
159 assertTrue(paths.containsKey(expectedPath.getKey()));
160 final int expectedSize = expectedPath.getValue();
162 final var path = paths.get(expectedPath.getKey());
164 final var get = path.getGet();
165 assertFalse(get.isMissingNode());
166 assertEquals(expectedSize + 1, get.get("parameters").size());
168 final var put = path.getPut();
169 assertFalse(put.isMissingNode());
170 assertEquals(expectedSize, put.get("parameters").size());
172 final var delete = path.getDelete();
173 assertFalse(delete.isMissingNode());
174 assertEquals(expectedSize, delete.get("parameters").size());
176 final var post = path.getPost();
177 assertFalse(post.isMissingNode());
178 assertEquals(expectedSize, post.get("parameters").size());
180 final var patch = path.getPatch();
181 assertFalse(patch.isMissingNode());
182 assertEquals(expectedSize, patch.get("parameters").size());