Migrate AbstractResourceBodyTest to JUnit5 10/109110/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Nov 2023 21:18:32 +0000 (22:18 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Nov 2023 21:18:32 +0000 (22:18 +0100)
Clean up test interactions and migrate to JUnit5.

Change-Id: I4a62a1a980552205712b10552bb64229ead95b83
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/AbstractResourceBodyTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/JsonResourceBodyTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/XmlResourceBodyTest.java

index 4345bc491948eb2693ea91147760c0977fa3a52b..0cef2808ee260b0fcbea36b60658a66d998742a6 100644 (file)
@@ -7,25 +7,22 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.databind;
 
-import static java.util.Objects.requireNonNull;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.text.ParseException;
-import java.util.Optional;
 import java.util.function.Function;
 import org.eclipse.jdt.annotation.NonNull;
-import org.junit.BeforeClass;
-import org.junit.function.ThrowingRunnable;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.function.Executable;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
-import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
 import org.opendaylight.restconf.api.ApiPath;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
@@ -34,11 +31,11 @@ import org.opendaylight.restconf.server.api.DatabindContext;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
+@ExtendWith(MockitoExtension.class)
 abstract class AbstractResourceBodyTest extends AbstractBodyTest {
     static final NodeIdentifier CONT_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
     static final NodeIdentifier CONT1_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont1"));
@@ -55,23 +52,20 @@ abstract class AbstractResourceBodyTest extends AbstractBodyTest {
 
     private static DatabindContext DATABIND;
 
+    @Mock
+    DOMMountPointService mountPointService;
+    @Mock
+    DOMMountPoint mountPoint;
+
     private final Function<InputStream, ResourceBody> bodyConstructor;
-    private final DOMMountPointService mountPointService;
-    private final DOMMountPoint mountPoint;
 
     AbstractResourceBodyTest(final Function<InputStream, ResourceBody> bodyConstructor) {
-        this.bodyConstructor = requireNonNull(bodyConstructor);
-
-        mountPointService = mock(DOMMountPointService.class);
-        mountPoint = mock(DOMMountPoint.class);
-        doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
-        doReturn(Optional.of(FixedDOMSchemaService.of(IID_SCHEMA))).when(mountPoint)
-            .getService(DOMSchemaService.class);
-
+        assertNotNull(bodyConstructor);
+        this.bodyConstructor = bodyConstructor;
     }
 
-    @BeforeClass
-    public static final void initModelContext() throws Exception {
+    @BeforeAll
+    static final void initModelContext() throws Exception {
         final var testFiles = loadFiles("/instanceidentifier/yang");
         testFiles.addAll(loadFiles("/modules"));
         testFiles.addAll(loadFiles("/foo-xml-test/yang"));
@@ -104,8 +98,8 @@ abstract class AbstractResourceBodyTest extends AbstractBodyTest {
         }
     }
 
-    static final void assertRangeViolation(final ThrowingRunnable runnable) {
-        final var ex = assertThrows(RestconfDocumentedException.class, runnable);
+    static final void assertRangeViolation(final Executable executable) {
+        final var ex = assertThrows(RestconfDocumentedException.class, executable);
         final var errors = ex.getErrors();
         assertEquals(1, errors.size());
 
index 7eda6c2607108690b54ba792988bf9cb57282a21..aa1436a18b6dc541cf9f500ce424b932c67cc116 100644 (file)
@@ -7,10 +7,10 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.databind;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import java.util.Map;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -18,13 +18,13 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithV
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 
-public class JsonResourceBodyTest extends AbstractResourceBodyTest {
-    public JsonResourceBodyTest() {
+class JsonResourceBodyTest extends AbstractResourceBodyTest {
+    JsonResourceBodyTest() {
         super(JsonResourceBody::new);
     }
 
     @Test
-    public void moduleDataTest() throws Exception {
+    void moduleDataTest() throws Exception {
         final var entryId = NodeIdentifierWithPredicates.of(LST11,
             Map.of(KEYVALUE111, "value1", KEYVALUE112, "value2"));
 
@@ -64,7 +64,7 @@ public class JsonResourceBodyTest extends AbstractResourceBodyTest {
     }
 
     @Test
-    public void moduleSubContainerDataPutTest() throws Exception {
+    void moduleSubContainerDataPutTest() throws Exception {
         assertEquals(Builders.containerBuilder()
             .withNodeIdentifier(CONT1_NID)
             .withChild(ImmutableNodes.leafNode(LF11, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
@@ -74,7 +74,7 @@ public class JsonResourceBodyTest extends AbstractResourceBodyTest {
     }
 
     @Test
-    public void testRangeViolation() throws Exception {
+    void testRangeViolation() throws Exception {
         assertRangeViolation(() -> parse("netconf786:foo", """
             {
               "netconf786:foo": {
index 16afd8ce695a663c85a96c2b0f847d8277abeab9..3389113073b73685aa0a9a913b940e4b36cf1e91 100644 (file)
@@ -7,11 +7,16 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.databind;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
 
 import java.util.Map;
-import org.junit.Test;
+import java.util.Optional;
+import org.junit.jupiter.api.Test;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
@@ -23,20 +28,28 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithV
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 
-public class XmlResourceBodyTest extends AbstractResourceBodyTest {
+class XmlResourceBodyTest extends AbstractResourceBodyTest {
     private static final QName TOP_LEVEL_LIST = QName.create("foo", "2017-08-09", "top-level-list");
 
-    public XmlResourceBodyTest() {
+    XmlResourceBodyTest() {
         super(XmlResourceBody::new);
     }
 
+    private void mockMount() {
+        doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
+        doReturn(Optional.of(FixedDOMSchemaService.of(IID_SCHEMA))).when(mountPoint)
+            .getService(DOMSchemaService.class);
+    }
+
     /**
      * Test PUT operation when message root element is not the same as the last element in request URI.
      * PUT operation message should always start with schema node from URI otherwise exception should be
      * thrown.
      */
     @Test
-    public void wrongRootElementTest() {
+    void wrongRootElementTest() {
+        mockMount();
+
         assertThrowsException("",
             "Incorrect message root element (instance:identifier:module)cont1, should be "
                 + "(urn:ietf:params:xml:ns:yang:ietf-restconf)data");
@@ -62,13 +75,13 @@ public class XmlResourceBodyTest extends AbstractResourceBodyTest {
     }
 
     @Test
-    public void testRangeViolation() throws Exception {
+    void testRangeViolation() throws Exception {
         assertRangeViolation(() -> parse("netconf786:foo", """
             <foo xmlns="netconf786"><bar>100</bar></foo>"""));
     }
 
     @Test
-    public void putXmlTest() throws Exception {
+    void putXmlTest() throws Exception {
         final var keyName = QName.create(TOP_LEVEL_LIST, "key-leaf");
         assertEquals(Builders.mapEntryBuilder()
             .withNodeIdentifier(NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST, keyName, "key-value"))
@@ -78,12 +91,14 @@ public class XmlResourceBodyTest extends AbstractResourceBodyTest {
     }
 
     @Test
-    public void moduleDataTest() throws Exception {
+    void moduleDataTest() throws Exception {
         testModuleData("instance-identifier-module:cont");
     }
 
     @Test
-    public void moduleDataMountPointTest() throws Exception {
+    void moduleDataMountPointTest() throws Exception {
+        mockMount();
+
         testModuleData("instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont");
     }
 
@@ -123,12 +138,14 @@ public class XmlResourceBodyTest extends AbstractResourceBodyTest {
     }
 
     @Test
-    public void moduleSubContainerDataPutTest() throws Exception {
+    void moduleSubContainerDataPutTest() throws Exception {
         testModuleSubContainerDataPut("instance-identifier-module:cont/cont1");
     }
 
     @Test
-    public void moduleSubContainerDataPutMountPointTest() throws Exception {
+    void moduleSubContainerDataPutMountPointTest() throws Exception {
+        mockMount();
+
         testModuleSubContainerDataPut(
             "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1");
     }