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.util.Optional;
17 import javax.ws.rs.core.UriBuilder;
18 import javax.ws.rs.core.UriInfo;
19 import org.glassfish.jersey.internal.util.collection.ImmutableMultivaluedMap;
20 import org.mockito.ArgumentCaptor;
21 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
24 import org.opendaylight.restconf.openapi.api.OpenApiService;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 import org.skyscreamer.jsonassert.JSONCompareMode;
30 public abstract class AbstractDocumentTest {
31 protected static final ObjectMapper MAPPER = new ObjectMapper();
33 * We want flexibility in comparing the resulting JSONs by not enforcing strict ordering of array contents.
34 * This comparison mode allows us to do that and also to restrict extensibility (extensibility = additional fields)
36 protected static final JSONCompareMode IGNORE_ORDER = JSONCompareMode.NON_EXTENSIBLE;
37 private static final String URI = "http://localhost:8181/openapi/api/v3/";
38 private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
39 .node(QName.create("", "nodes"))
40 .node(QName.create("", "node"))
41 .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
43 private static OpenApiService openApiService;
45 protected static void initializeClass(final String yangPath) {
46 final var schemaService = mock(DOMSchemaService.class);
47 final var context = YangParserTestUtils.parseYangResourceDirectory(yangPath);
48 when(schemaService.getGlobalContext()).thenReturn(context);
50 final var mountPoint = mock(DOMMountPoint.class);
51 when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
53 final var service = mock(DOMMountPointService.class);
54 when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
56 final var mountPointRFC8040 = new MountPointOpenApiGeneratorRFC8040(schemaService, service);
57 final var openApiGeneratorRFC8040 = new OpenApiGeneratorRFC8040(schemaService);
58 mountPointRFC8040.getMountPointOpenApi().onMountPointCreated(INSTANCE_ID);
59 openApiService = new OpenApiServiceImpl(mountPointRFC8040, openApiGeneratorRFC8040);
62 protected static String getExpectedDoc(final String jsonPath) throws Exception {
63 return MAPPER.writeValueAsString(MAPPER.readTree(
64 AbstractDocumentTest.class.getClassLoader().getResourceAsStream(jsonPath)));
67 protected static String getAllModulesDoc() throws Exception {
68 final var getAllController = createMockUriInfo(URI + "single");
69 final var controllerDocAll = openApiService.getAllModulesDoc(getAllController).getEntity();
71 return MAPPER.writeValueAsString(controllerDocAll);
74 protected static String getDocByModule(final String moduleName, final String revision) throws Exception {
75 var uri = URI + moduleName;
76 if (revision != null) {
77 uri = uri + "(" + revision + ")";
79 final var getModuleController = createMockUriInfo(uri);
80 final var controllerDocModule = openApiService.getDocByModule(moduleName, revision, getModuleController);
82 return MAPPER.writeValueAsString(controllerDocModule.getEntity());
86 protected static String getMountDoc() throws Exception {
87 final var getAllDevice = createMockUriInfo(URI + "mounts/1");
88 when(getAllDevice.getQueryParameters()).thenReturn(ImmutableMultivaluedMap.empty());
89 final var deviceDocAll = openApiService.getMountDoc("1", getAllDevice);
91 return MAPPER.writeValueAsString(deviceDocAll.getEntity());
95 protected static String getMountDocByModule(final String moduleName, final String revision) throws Exception {
96 final var getDevice = createMockUriInfo(URI + "mounts/1/" + moduleName);
97 final var deviceDoc = openApiService.getMountDocByModule("1", moduleName, revision, getDevice);
99 return MAPPER.writeValueAsString(deviceDoc.getEntity());
102 public static UriInfo createMockUriInfo(final String urlPrefix) throws Exception {
103 final java.net.URI uri = new URI(urlPrefix);
104 final UriBuilder mockBuilder = mock(UriBuilder.class);
106 final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
107 when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
108 when(mockBuilder.build()).then(invocation -> java.net.URI.create(uri + "/" + subStringCapture.getValue()));
110 final UriInfo info = mock(UriInfo.class);
111 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
112 when(mockBuilder.replaceQuery(any())).thenReturn(mockBuilder);
113 when(info.getBaseUri()).thenReturn(uri);