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.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
19 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
20 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
21 import org.opendaylight.restconf.openapi.DocGenTestHelper;
22 import org.opendaylight.restconf.openapi.api.OpenApiService;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26 import org.skyscreamer.jsonassert.JSONAssert;
27 import org.skyscreamer.jsonassert.JSONCompareMode;
29 public class ToasterDocumentTest {
30 private static final ObjectMapper MAPPER = new ObjectMapper();
32 * We want flexibility in comparing the resulting JSONs by not enforcing strict ordering of array contents.
33 * This comparison mode allows us to do that and also to restrict extensibility (extensibility = additional fields)
35 private static final JSONCompareMode IGNORE_ORDER = JSONCompareMode.NON_EXTENSIBLE;
36 private static final String TOASTER = "toaster";
37 private static final String TOASTER_REV = "2009-11-20";
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;
46 public static void beforeClass() {
47 final var schemaService = mock(DOMSchemaService.class);
48 final var context = YangParserTestUtils.parseYangResource("/toaster-document/toaster.yang");
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);
58 final var openApiGeneratorRFC8040 = new OpenApiGeneratorRFC8040(schemaService);
59 mountPointRFC8040.getMountPointOpenApi().onMountPointCreated(INSTANCE_ID);
60 openApiService = new OpenApiServiceImpl(mountPointRFC8040, openApiGeneratorRFC8040);
64 * Tests the swagger document that is result of the call to the '/single' endpoint.
67 public void getAllModulesDocTest() throws Exception {
68 final var getAllController = DocGenTestHelper.createMockUriInfo("http://localhost:8181/openapi/api/v3/single");
69 final var controllerDocAll = openApiService.getAllModulesDoc(getAllController).getEntity();
71 final var jsonControllerDoc = MAPPER.writeValueAsString(controllerDocAll);
72 final var expectedJson = MAPPER.writeValueAsString(MAPPER.readTree(
73 getClass().getClassLoader().getResourceAsStream("toaster-document/controller-all.json")));
74 JSONAssert.assertEquals(expectedJson, jsonControllerDoc, IGNORE_ORDER);
78 * Tests the swagger document that is result of the call to the '/toaster(2009-11-20)' endpoint.
81 * Model toaster is used for test correct generating of complex openapi object.
84 public void getDocByModuleTest() throws Exception {
85 final var getToasterController = DocGenTestHelper.createMockUriInfo("http://localhost:8181/openapi/api/v3/toaster(2009-11-20)");
86 final var controllerDocToaster = openApiService.getDocByModule(TOASTER, TOASTER_REV, getToasterController);
88 final var jsonControllerDoc = MAPPER.writeValueAsString(controllerDocToaster.getEntity());
89 final var expectedJson = MAPPER.writeValueAsString(MAPPER.readTree(
90 getClass().getClassLoader().getResourceAsStream("toaster-document/controller-toaster.json")));
91 JSONAssert.assertEquals(expectedJson, jsonControllerDoc, IGNORE_ORDER);
95 * Tests the swagger document that is result of the call to the '/mounts/1' endpoint.
98 public void getMountDocTest() throws Exception {
99 final var getAllDevice = DocGenTestHelper.createMockUriInfo("http://localhost:8181/openapi/api/v3/mounts/1");
100 when(getAllDevice.getQueryParameters()).thenReturn(ImmutableMultivaluedMap.empty());
101 final var deviceDocAll = openApiService.getMountDoc("1", getAllDevice);
103 final var jsonDeviceDoc = MAPPER.writeValueAsString(deviceDocAll.getEntity());
104 final var expectedJson = MAPPER.writeValueAsString(MAPPER.readTree(
105 getClass().getClassLoader().getResourceAsStream("toaster-document/device-all.json")));
106 JSONAssert.assertEquals(expectedJson, jsonDeviceDoc, IGNORE_ORDER);
110 * Tests the swagger document that is result of the call to the '/mounts/1/toaster(2009-11-20)' endpoint.
113 * Model toaster is used for test correct generating of complex openapi object.
116 public void getMountDocByModuleTest() throws Exception {
117 final var getToasterDevice = DocGenTestHelper.createMockUriInfo("http://localhost:8181/openapi/api/v3/mounts/1/toaster(2009-11-20)");
118 final var deviceDocToaster = openApiService.getMountDocByModule("1", TOASTER, TOASTER_REV, getToasterDevice);
120 final var jsonDeviceDoc = MAPPER.writeValueAsString(deviceDocToaster.getEntity());
121 final var expectedJson = MAPPER.writeValueAsString(MAPPER.readTree(
122 getClass().getClassLoader().getResourceAsStream("toaster-document/device-toaster.json")));
123 JSONAssert.assertEquals(expectedJson, jsonDeviceDoc, IGNORE_ORDER);