+++ /dev/null
-/*
- * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.restconf.openapi;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import org.junit.BeforeClass;
-import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.restconf.openapi.model.Path;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-
-public abstract class AbstractOpenApiTest {
- protected static EffectiveModelContext CONTEXT;
- protected static DOMSchemaService SCHEMA_SERVICE;
-
- @BeforeClass
- public static void beforeClass() {
- CONTEXT = YangParserTestUtils.parseYangResourceDirectory("/yang");
- SCHEMA_SERVICE = mock(DOMSchemaService.class);
- when(SCHEMA_SERVICE.getGlobalContext()).thenReturn(CONTEXT);
- }
-
- protected static List<String> getPathParameters(final Map<String, Path> paths, final String path) {
- final var params = new ArrayList<String>();
- paths.get(path).getPost().get("parameters").elements()
- .forEachRemaining(p -> params.add(p.get("name").asText()));
- return params;
- }
-}
--- /dev/null
+/*
+ * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.restconf.openapi;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.opendaylight.restconf.openapi.model.Path;
+
+public final class OpenApiTestUtils {
+
+ private OpenApiTestUtils() {
+ // Hidden on purpose
+ }
+
+ /**
+ * Get path parameters name for {@code path}.
+ *
+ * @return {@link List} of parameters
+ */
+ public static List<String> getPathParameters(final Map<String, Path> paths, final String path) {
+ final var params = new ArrayList<String>();
+ paths.get(path).getPost().get("parameters").elements()
+ .forEachRemaining(p -> params.add(p.get("name").asText()));
+ return params;
+ }
+}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import java.io.IOException;
+import org.junit.BeforeClass;
import org.junit.Test;
-import org.opendaylight.restconf.openapi.AbstractOpenApiTest;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public final class DefinitionGeneratorTest {
+ private static EffectiveModelContext context;
+ private static DOMSchemaService schemaService;
+
+ @BeforeClass
+ public static void beforeClass() {
+ schemaService = mock(DOMSchemaService.class);
+ context = YangParserTestUtils.parseYangResourceDirectory("/yang");
+ when(schemaService.getGlobalContext()).thenReturn(context);
+ }
-public final class DefinitionGeneratorTest extends AbstractOpenApiTest {
@Test
public void testConvertToSchemas() throws IOException {
- final var module = CONTEXT.findModule("opflex", Revision.of("2014-05-28")).orElseThrow();
+ final var module = context.findModule("opflex", Revision.of("2014-05-28")).orElseThrow();
final DefinitionGenerator generator = new DefinitionGenerator();
- final var schemas = generator.convertToSchemas(module, CONTEXT, new DefinitionNames(), true);
+ final var schemas = generator.convertToSchemas(module, context, new DefinitionNames(), true);
assertNotNull(schemas);
}
@Test
public void testActionTypes() throws IOException {
- final var module = CONTEXT.findModule("action-types").orElseThrow();
+ final var module = context.findModule("action-types").orElseThrow();
final DefinitionGenerator generator = new DefinitionGenerator();
- final var schemas = generator.convertToSchemas(module, CONTEXT, new DefinitionNames(), true);
+ final var schemas = generator.convertToSchemas(module, context, new DefinitionNames(), true);
assertNotNull(schemas);
}
@Test
public void testStringTypes() throws IOException {
- final var module = CONTEXT.findModule("string-types").orElseThrow();
+ final var module = context.findModule("string-types").orElseThrow();
final DefinitionGenerator generator = new DefinitionGenerator();
- final var schemas = generator.convertToSchemas(module, CONTEXT, new DefinitionNames(), true);
+ final var schemas = generator.convertToSchemas(module, context, new DefinitionNames(), true);
assertNotNull(schemas);
}
@Test
public void testStringFromRegex() throws IOException {
- final var module = CONTEXT.findModule("strings-from-regex").orElseThrow();
+ final var module = context.findModule("strings-from-regex").orElseThrow();
final var generator = new DefinitionGenerator();
- final var jsonObject = generator.convertToSchemas(module, CONTEXT, new DefinitionNames(), true);
+ final var jsonObject = generator.convertToSchemas(module, context, new DefinitionNames(), true);
assertNotNull(jsonObject);
var properties = jsonObject.get("strings-from-regex_test").getProperties();
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathParameters;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import org.junit.BeforeClass;
import org.junit.Test;
-import org.opendaylight.restconf.openapi.AbstractOpenApiTest;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.restconf.openapi.DocGenTestHelper;
import org.opendaylight.restconf.openapi.model.OpenApiObject;
import org.opendaylight.restconf.openapi.model.Path;
import org.opendaylight.restconf.openapi.model.Schema;
import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
+public final class OpenApiGeneratorRFC8040Test {
private static final String NAME = "toaster2";
private static final String REVISION_DATE = "2009-11-20";
private static final String NAME_2 = "toaster";
private static final String PATH_PARAMS_TEST_MODULE = "path-params-test";
private static final String RECURSIVE_TEST_MODULE = "recursive";
- private final OpenApiGeneratorRFC8040 generator = new OpenApiGeneratorRFC8040(SCHEMA_SERVICE);
+ private static EffectiveModelContext context;
+ private static DOMSchemaService schemaService;
+
+ private final OpenApiGeneratorRFC8040 generator = new OpenApiGeneratorRFC8040(schemaService);
+
+ @BeforeClass
+ public static void beforeClass() {
+ schemaService = mock(DOMSchemaService.class);
+ context = YangParserTestUtils.parseYangResourceDirectory("/yang");
+ when(schemaService.getGlobalContext()).thenReturn(context);
+ }
/**
* Test that paths are generated according to the model.
*/
@Test
public void testPaths() {
- final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
- final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
+ final var module = context.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
+ final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", context);
assertEquals(Set.of("/rests/data",
"/rests/data/toaster2:toaster",
"/rests/data/toaster2:lst/cont1/lst11",
"/rests/data/toaster2:lst/lst1={key1},{key2}");
- final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
- final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
+ final var module = context.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
+ final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", context);
for (final String path : configPaths) {
final Path node = doc.getPaths().get(path);
*/
@Test
public void testSchemas() {
- final var module = CONTEXT.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
- final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
+ final var module = context.findModule(NAME, Revision.of(REVISION_DATE)).orElseThrow();
+ final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", context);
final Map<String, Schema> schemas = doc.getComponents().getSchemas();
assertNotNull(schemas);
*/
@Test
public void testRPC() {
- final var module = CONTEXT.findModule(NAME_2, Revision.of(REVISION_DATE_2)).orElseThrow();
- final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
+ final var module = context.findModule(NAME_2, Revision.of(REVISION_DATE_2)).orElseThrow();
+ final OpenApiObject doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", context);
assertNotNull(doc);
final Map<String, Schema> schemas = doc.getComponents().getSchemas();
@Test
public void testChoice() {
- final var module = CONTEXT.findModule(CHOICE_TEST_MODULE).orElseThrow();
- final var doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
+ final var module = context.findModule(CHOICE_TEST_MODULE).orElseThrow();
+ final var doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", context);
assertNotNull(doc);
final var schemas = doc.getComponents().getSchemas();
"/rests/data/recursive:container-root/root-list={name}/nested-list={name1}", 2,
"/rests/data/recursive:container-root/root-list={name}/nested-list={name1}/super-nested-list={name2}", 3);
- final var module = CONTEXT.findModule(RECURSIVE_TEST_MODULE, Revision.of("2023-05-22")).orElseThrow();
- final var doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
+ final var module = context.findModule(RECURSIVE_TEST_MODULE, Revision.of("2023-05-22")).orElseThrow();
+ final var doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", context);
assertNotNull(doc);
final var paths = doc.getPaths();
*/
@Test
public void testParametersNumbering() {
- final var module = CONTEXT.findModule(PATH_PARAMS_TEST_MODULE).orElseThrow();
- final var doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", CONTEXT);
+ final var module = context.findModule(PATH_PARAMS_TEST_MODULE).orElseThrow();
+ final var doc = generator.getOpenApiSpec(module, "http", "localhost:8181", "/", "", context);
var pathToList1 = "/rests/data/path-params-test:cont/list1={name}";
assertTrue(doc.getPaths().containsKey(pathToList1));
import java.util.Optional;
import javax.ws.rs.core.UriInfo;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.opendaylight.mdsal.dom.api.DOMMountPoint;
import org.opendaylight.mdsal.dom.api.DOMMountPointService;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.restconf.openapi.AbstractOpenApiTest;
import org.opendaylight.restconf.openapi.DocGenTestHelper;
import org.opendaylight.restconf.openapi.api.OpenApiService;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-public final class OpenApiServiceImplTest extends AbstractOpenApiTest {
+public final class OpenApiServiceImplTest {
private static final String HTTP_URL = "http://localhost/path";
private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
.node(QName.create("", "nodes"))
.nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
private static final ObjectMapper MAPPER = new ObjectMapper();
+ private static EffectiveModelContext context;
+ private static DOMSchemaService schemaService;
+
private OpenApiService openApiService;
+ @BeforeClass
+ public static void beforeClass() {
+ schemaService = mock(DOMSchemaService.class);
+ context = YangParserTestUtils.parseYangResourceDirectory("/yang");
+ when(schemaService.getGlobalContext()).thenReturn(context);
+ }
+
@Before
public void before() {
final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
- when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(SCHEMA_SERVICE));
+ when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
final DOMMountPointService service = mock(DOMMountPointService.class);
when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
final MountPointOpenApiGeneratorRFC8040 mountPointRFC8040 =
- new MountPointOpenApiGeneratorRFC8040(SCHEMA_SERVICE, service);
- final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040 = new OpenApiGeneratorRFC8040(SCHEMA_SERVICE);
+ new MountPointOpenApiGeneratorRFC8040(schemaService, service);
+ final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040 = new OpenApiGeneratorRFC8040(schemaService);
mountPointRFC8040.getMountPointOpenApi().onMountPointCreated(INSTANCE_ID);
openApiService = new OpenApiServiceImpl(mountPointRFC8040, openApiGeneratorRFC8040);
}
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathParameters;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.ArrayList;
import java.util.TreeSet;
import javax.ws.rs.core.UriInfo;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.opendaylight.mdsal.dom.api.DOMMountPoint;
import org.opendaylight.mdsal.dom.api.DOMMountPointService;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.restconf.openapi.AbstractOpenApiTest;
import org.opendaylight.restconf.openapi.DocGenTestHelper;
import org.opendaylight.restconf.openapi.impl.MountPointOpenApiGeneratorRFC8040;
import org.opendaylight.restconf.openapi.model.OpenApiObject;
import org.opendaylight.restconf.openapi.model.Path;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-public final class MountPointOpenApiTest extends AbstractOpenApiTest {
+public final class MountPointOpenApiTest {
private static final String HTTP_URL = "http://localhost/path";
private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
.node(QName.create("", "nodes"))
.nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
private static final String INSTANCE_URL = "/nodes/node=123/";
+ private static EffectiveModelContext context;
+ private static DOMSchemaService schemaService;
+
private MountPointOpenApi openApi;
+ @BeforeClass
+ public static void beforeClass() {
+ schemaService = mock(DOMSchemaService.class);
+ context = YangParserTestUtils.parseYangResourceDirectory("/yang");
+ when(schemaService.getGlobalContext()).thenReturn(context);
+ }
+
@Before
public void before() {
// We are sharing the global schema service and the mount schema service
// in our test.
// OK for testing - real thing would have separate instances.
final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
- when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(SCHEMA_SERVICE));
+ when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
final DOMMountPointService service = mock(DOMMountPointService.class);
when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
- openApi = new MountPointOpenApiGeneratorRFC8040(SCHEMA_SERVICE, service).getMountPointOpenApi();
+ openApi = new MountPointOpenApiGeneratorRFC8040(schemaService, service).getMountPointOpenApi();
}
@Test()