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.Mockito.mock;
11 import static org.mockito.Mockito.when;
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 import java.util.Optional;
15 import org.glassfish.jersey.internal.util.collection.ImmutableMultivaluedMap;
16 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
17 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
18 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
19 import org.opendaylight.restconf.openapi.DocGenTestHelper;
20 import org.opendaylight.restconf.openapi.api.OpenApiService;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 import org.skyscreamer.jsonassert.JSONCompareMode;
26 public abstract class AbstractDocumentTest {
27 protected static final ObjectMapper MAPPER = new ObjectMapper();
29 * We want flexibility in comparing the resulting JSONs by not enforcing strict ordering of array contents.
30 * This comparison mode allows us to do that and also to restrict extensibility (extensibility = additional fields)
32 protected static final JSONCompareMode IGNORE_ORDER = JSONCompareMode.NON_EXTENSIBLE;
33 private static final String URI = "http://localhost:8181/openapi/api/v3/";
34 private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
35 .node(QName.create("", "nodes"))
36 .node(QName.create("", "node"))
37 .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
39 private static OpenApiService openApiService;
41 protected static void initializeClass(final String yangPath) {
42 final var schemaService = mock(DOMSchemaService.class);
43 final var context = YangParserTestUtils.parseYangResourceDirectory(yangPath);
44 when(schemaService.getGlobalContext()).thenReturn(context);
46 final var mountPoint = mock(DOMMountPoint.class);
47 when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
49 final var service = mock(DOMMountPointService.class);
50 when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
52 final var mountPointRFC8040 = new MountPointOpenApiGeneratorRFC8040(schemaService, service);
53 final var openApiGeneratorRFC8040 = new OpenApiGeneratorRFC8040(schemaService);
54 mountPointRFC8040.getMountPointOpenApi().onMountPointCreated(INSTANCE_ID);
55 openApiService = new OpenApiServiceImpl(mountPointRFC8040, openApiGeneratorRFC8040);
58 protected static String getExpectedDoc(final String jsonPath) throws Exception {
59 return MAPPER.writeValueAsString(MAPPER.readTree(
60 AbstractDocumentTest.class.getClassLoader().getResourceAsStream(jsonPath)));
63 protected static String getAllModulesDoc() throws Exception {
64 final var getAllController = DocGenTestHelper.createMockUriInfo(URI + "single");
65 final var controllerDocAll = openApiService.getAllModulesDoc(getAllController).getEntity();
67 return MAPPER.writeValueAsString(controllerDocAll);
70 protected static String getDocByModule(final String moduleName, final String revision) throws Exception {
71 var uri = URI + moduleName;
72 if (revision != null) {
73 uri = uri + "(" + revision + ")";
75 final var getModuleController = DocGenTestHelper.createMockUriInfo(uri);
76 final var controllerDocModule = openApiService.getDocByModule(moduleName, revision, getModuleController);
78 return MAPPER.writeValueAsString(controllerDocModule.getEntity());
82 protected static String getMountDoc() throws Exception {
83 final var getAllDevice = DocGenTestHelper.createMockUriInfo(URI + "mounts/1");
84 when(getAllDevice.getQueryParameters()).thenReturn(ImmutableMultivaluedMap.empty());
85 final var deviceDocAll = openApiService.getMountDoc("1", getAllDevice);
87 return MAPPER.writeValueAsString(deviceDocAll.getEntity());
91 protected static String getMountDocByModule(final String moduleName, final String revision) throws Exception {
92 final var getToasterDevice = DocGenTestHelper.createMockUriInfo(URI + "mounts/1/" + moduleName);
93 final var deviceDocToaster = openApiService.getMountDocByModule("1", moduleName, revision, getToasterDevice);
95 return MAPPER.writeValueAsString(deviceDocToaster.getEntity());