Rework unit test infrastructure 58/106258/3
authorOleksandrZharov <Oleksandr.Zharov@pantheon.tech>
Tue, 30 May 2023 09:15:11 +0000 (11:15 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Wed, 31 May 2023 08:55:38 +0000 (10:55 +0200)
Changed AbstractOpenApiTest class into util class named
OpenApiTestUtils.

Copied contents of BeforeClass to each affected test.
This allow us configure resources for each test more freely.

JIRA: NETCONF-1040
Change-Id: I80392d46495b443a56ac568755716511c7e97298
Signed-off-by: OleksandrZharov <Oleksandr.Zharov@pantheon.tech>
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/AbstractOpenApiTest.java [deleted file]
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/OpenApiTestUtils.java [new file with mode: 0644]
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/DefinitionGeneratorTest.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/OpenApiGeneratorRFC8040Test.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/OpenApiServiceImplTest.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/mountpoints/MountPointOpenApiTest.java

diff --git a/restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/AbstractOpenApiTest.java b/restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/AbstractOpenApiTest.java
deleted file mode 100644 (file)
index 6d5ab6e..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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;
-    }
-}
diff --git a/restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/OpenApiTestUtils.java b/restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/OpenApiTestUtils.java
new file mode 100644 (file)
index 0000000..fda19fd
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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;
+    }
+}
index 12738bd2f2eb957f48e4cb40cf8172c398d93684..b443d2855f1aeaccc22628a3c22ec2175d6ab5f7 100644 (file)
@@ -9,42 +9,57 @@ package org.opendaylight.restconf.openapi.impl;
 
 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();
index 17b90c6afc6d1269a386c9affdbcfa9e6fbc5188..2288cff8f15e1ff4889dc5c5e8abec87e31cb931 100644 (file)
@@ -11,20 +11,26 @@ import static org.junit.Assert.assertEquals;
 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";
@@ -33,15 +39,25 @@ public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
     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",
@@ -69,8 +85,8 @@ public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
                 "/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);
@@ -87,8 +103,8 @@ public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
      */
     @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);
@@ -142,8 +158,8 @@ public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
      */
     @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();
@@ -159,8 +175,8 @@ public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
 
     @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();
@@ -184,8 +200,8 @@ public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
             "/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();
@@ -227,8 +243,8 @@ public final class OpenApiGeneratorRFC8040Test extends AbstractOpenApiTest {
      */
     @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));
index ec823b027f63e8b3be6f256100bf82766bcca858..4e1eff7cbd7b6cf571039a7e52e899702325af83 100644 (file)
@@ -15,17 +15,19 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 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"))
@@ -33,18 +35,28 @@ public final class OpenApiServiceImplTest extends AbstractOpenApiTest {
             .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);
     }
index d4284f2d5bbe48eef8d3fef46583e54a1a80f09a..a236f7627d07a965e5837130794fc9ba7786e353 100644 (file)
@@ -13,6 +13,7 @@ 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.ArrayList;
@@ -23,19 +24,21 @@ import java.util.Set;
 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"))
@@ -43,20 +46,30 @@ public final class MountPointOpenApiTest extends AbstractOpenApiTest {
             .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()