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.impl;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
14 import com.fasterxml.jackson.databind.ObjectMapper;
16 import java.nio.charset.StandardCharsets;
17 import java.util.Optional;
18 import javax.ws.rs.core.UriBuilder;
19 import javax.ws.rs.core.UriInfo;
20 import org.glassfish.jersey.internal.util.collection.ImmutableMultivaluedMap;
21 import org.mockito.ArgumentCaptor;
22 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
23 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.opendaylight.restconf.openapi.api.OpenApiService;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 import org.skyscreamer.jsonassert.JSONCompareMode;
31 abstract class AbstractDocumentTest {
32 protected static final ObjectMapper MAPPER = new ObjectMapper();
34 * We want flexibility in comparing the resulting JSONs by not enforcing strict ordering of array contents.
35 * This comparison mode allows us to do that and also to restrict extensibility (extensibility = additional fields)
37 protected static final JSONCompareMode IGNORE_ORDER = JSONCompareMode.NON_EXTENSIBLE;
38 private static final String URI = "http://localhost:8181/openapi/api/v3/";
39 private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
40 .node(QName.create("", "nodes"))
41 .node(QName.create("", "node"))
42 .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
44 private static OpenApiService openApiService;
46 protected static void initializeClass(final String yangPath) {
47 final var schemaService = mock(DOMSchemaService.class);
48 final var context = YangParserTestUtils.parseYangResourceDirectory(yangPath);
49 when(schemaService.getGlobalContext()).thenReturn(context);
51 final var mountPoint = mock(DOMMountPoint.class);
52 when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
54 final var service = mock(DOMMountPointService.class);
55 when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
57 final var mountPointRFC8040 = new MountPointOpenApiGeneratorRFC8040(schemaService, service, "rests");
58 final var openApiGeneratorRFC8040 = new OpenApiGeneratorRFC8040(schemaService, "rests");
59 mountPointRFC8040.getMountPointOpenApi().onMountPointCreated(INSTANCE_ID);
60 openApiService = new OpenApiServiceImpl(mountPointRFC8040, openApiGeneratorRFC8040);
63 protected static String getExpectedDoc(final String jsonPath) throws Exception {
64 return MAPPER.writeValueAsString(MAPPER.readTree(
65 AbstractDocumentTest.class.getClassLoader().getResourceAsStream(jsonPath)));
68 protected static String getAllModulesDoc(final Integer width) throws Exception {
69 final var getAllController = createMockUriInfo(URI + "single");
70 final var controllerDocAll = openApiService.getAllModulesDoc(getAllController, width).getEntity();
72 return new String(((OpenApiInputStream) controllerDocAll).readAllBytes(),
73 StandardCharsets.UTF_8);
76 protected static String getDocByModule(final String moduleName, final String revision, final Integer width)
78 var uri = URI + moduleName;
79 if (revision != null) {
80 uri = uri + "(" + revision + ")";
82 final var getModuleController = createMockUriInfo(uri);
83 final var controllerDocModule = openApiService.getDocByModule(moduleName, revision, getModuleController, width);
85 return new String(((OpenApiInputStream) controllerDocModule.getEntity()).readAllBytes(),
86 StandardCharsets.UTF_8);
89 protected static String getMountDoc(final Integer width) throws Exception {
90 final var getAllDevice = createMockUriInfo(URI + "mounts/1");
91 when(getAllDevice.getQueryParameters()).thenReturn(ImmutableMultivaluedMap.empty());
92 final var deviceDocAll = openApiService.getMountDoc("1", getAllDevice, width);
94 return new String(((OpenApiInputStream) deviceDocAll.getEntity()).readAllBytes(),
95 StandardCharsets.UTF_8);
98 protected static String getMountDocByModule(final String moduleName, final String revision, final Integer width)
100 final var getDevice = createMockUriInfo(URI + "mounts/1/" + moduleName);
101 final var deviceDoc = openApiService.getMountDocByModule("1", moduleName, revision, getDevice, width);
103 return new String(((OpenApiInputStream) deviceDoc.getEntity()).readAllBytes(),
104 StandardCharsets.UTF_8);
107 protected static UriInfo createMockUriInfo(final String urlPrefix) throws Exception {
108 final var uri = new URI(urlPrefix);
109 final var mockBuilder = mock(UriBuilder.class);
111 final var subStringCapture = ArgumentCaptor.forClass(String.class);
112 when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
113 when(mockBuilder.build()).then(invocation -> java.net.URI.create(uri + "/" + subStringCapture.getValue()));
115 final var info = mock(UriInfo.class);
116 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
117 when(mockBuilder.replaceQuery(any())).thenReturn(mockBuilder);
118 when(info.getBaseUri()).thenReturn(uri);