Migrate mdsal-common-api to JUnit5 02/107602/3
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 30 Aug 2023 19:19:39 +0000 (21:19 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 31 Aug 2023 06:07:27 +0000 (08:07 +0200)
This is mostly a trivial conversion, but we also expand the test suite
to increase coverage and assertions.

Change-Id: I6ad81c99cc153e2fee3cd937dba42371a98ea047
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
12 files changed:
common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/OptimisticLockFailedException.java
common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/TransactionCommitDeadlockException.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/CommitInfoTest.java [new file with mode: 0644]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/DataStoreUnavailableExceptionTest.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/DataValidationFailedExceptionTest.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/EmptyCommitInfoTest.java [new file with mode: 0644]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/LogicalDatastoreTypeTest.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/OptimisticLockFailedExceptionTest.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/PostCommitStepTest.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/ReadFailedExceptionTest.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/TransactionCommitDeadlockExceptionTest.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/TransactionCommitFailedExceptionTest.java

index 7353100280b4b6bca90e81bd532ed674a754f790..ee89d9fe8a95a32b9e96906dff6be5d1f3f7a6b9 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import java.io.Serial;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
@@ -19,7 +18,7 @@ import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 * updated transaction.
 */
 public class OptimisticLockFailedException extends TransactionCommitFailedException {
-    @Serial
+    @java.io.Serial
     private static final long serialVersionUID = 1L;
 
     public OptimisticLockFailedException(final String message, final Throwable cause) {
index 50e338f125decdc7d6ebb12d533c16154ac04d23..8c43156624a2c6b67e1cc97d7d4e5eeeb839b4c3 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import java.io.Serial;
 import java.util.function.Supplier;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
@@ -24,11 +23,12 @@ import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
  * @author Thomas Pantelis
  */
 public class TransactionCommitDeadlockException extends TransactionCommitFailedException {
-    @Serial
+    @java.io.Serial
     private static final long serialVersionUID = 1L;
-    private static final String DEADLOCK_MESSAGE = "An attempt to block on a ListenableFuture via a get method from a "
-        + "write transaction submit was detected that would result in deadlock. The commit result must be obtained "
-        + "asynchronously, e.g. via Futures#addCallback, to avoid deadlock.";
+    private static final String DEADLOCK_MESSAGE = """
+        An attempt to block on a ListenableFuture via a get method from a write transaction submit was detected that \
+        would result in deadlock. The commit result must be obtained asynchronously, e.g. via Futures#addCallback, to \
+        avoid deadlock.""";
     private static final RpcError DEADLOCK_RPCERROR =
             RpcResultBuilder.newError(ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, DEADLOCK_MESSAGE);
 
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/CommitInfoTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/CommitInfoTest.java
new file mode 100644 (file)
index 0000000..33f1c9a
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2023 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.mdsal.common.api;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import org.junit.jupiter.api.Test;
+
+class CommitInfoTest {
+    @Test
+    void testEmpty() {
+        assertSame(EmptyCommitInfo.INSTANCE, CommitInfo.empty());
+    }
+
+    @Test
+    void testEmptyFluentFuture() {
+        assertSame(EmptyCommitInfo.FLUENT_INSTANCE, CommitInfo.emptyFluentFuture());
+    }
+}
index 1911a90716d0b880fc26c996c7cdd338b694dd40..88b0cfd28045b06aa0905877bad57d92dcab634c 100644 (file)
@@ -7,12 +7,16 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
 
-public class DataStoreUnavailableExceptionTest {
+import org.junit.jupiter.api.Test;
 
-    @Test(expected = DataStoreUnavailableException.class)
-    public void dataStoreUnavailableExceptionTest() throws Exception {
-        throw new DataStoreUnavailableException("test", null);
+class DataStoreUnavailableExceptionTest {
+    @Test
+    void dataStoreUnavailableExceptionTest() {
+        final var ex = new DataStoreUnavailableException("test", null);
+        assertEquals("test", ex.getMessage());
+        assertNull(ex.getCause());
     }
 }
\ No newline at end of file
index 58683fd3e756c6bd7ac5340796eedd3e391e92ba..e524adaaf12f19f72aaac4a10c4b1c75eb8f8ec1 100644 (file)
@@ -7,26 +7,23 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
 
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
 
-public class DataValidationFailedExceptionTest {
-
-    @Test(expected = DataValidationFailedException.class)
-    public void dataValidationFailedExceptionTest() throws Exception {
-        final TestClass testClass = new TestClass();
-        final DataValidationFailedException dataValidationFailedException =
-                new DataValidationFailedException(TestClass.class, testClass, "test");
-
-        assertEquals(testClass, dataValidationFailedException.getPath());
-        assertEquals(TestClass.class, dataValidationFailedException.getPathType());
-
-        throw dataValidationFailedException;
+class DataValidationFailedExceptionTest {
+    @Test
+    void dataValidationFailedExceptionTest() throws Exception {
+        final var testObj = new TestClass();
+        final var dataValidationFailedException = new DataValidationFailedException(TestClass.class, testObj, "test");
+        assertEquals(testObj, dataValidationFailedException.getPath());
+        assertSame(TestClass.class, dataValidationFailedException.getPathType());
     }
 
     private static final class TestClass implements HierarchicalIdentifier<TestClass> {
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
         @Override
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/EmptyCommitInfoTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/EmptyCommitInfoTest.java
new file mode 100644 (file)
index 0000000..43546e8
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2023 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.mdsal.common.api;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import com.google.common.util.concurrent.Futures;
+import org.junit.jupiter.api.Test;
+
+class EmptyCommitInfoTest {
+    @Test
+    void testFuture() throws Exception {
+        final var future = EmptyCommitInfo.FLUENT_INSTANCE;
+        assertTrue(future.isDone());
+        assertSame(EmptyCommitInfo.INSTANCE, Futures.getDone(future));
+    }
+}
index 74696ba334479613099186b10f4723654e4e96b5..27123ece3b79c42a2ca91787021c4c4907986ae1 100644 (file)
@@ -7,14 +7,48 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import static org.junit.Assert.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
-import org.junit.Test;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.stream.Stream;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
 
-public class LogicalDatastoreTypeTest {
+class LogicalDatastoreTypeTest {
+    @ParameterizedTest
+    @MethodSource
+    void serialization(final LogicalDatastoreType type, final byte[] expected) throws Exception {
+        final var bout = new ByteArrayOutputStream();
+        try (var out = new DataOutputStream(bout)) {
+            type.writeTo(out);
+        }
+        assertArrayEquals(expected, bout.toByteArray());
+
+        try (var in = new DataInputStream(new ByteArrayInputStream(expected))) {
+            assertSame(type, LogicalDatastoreType.readFrom(in));
+        }
+    }
+
+    static Stream<Object[]> serialization() {
+        return Stream.of(
+            new Object[] { LogicalDatastoreType.OPERATIONAL, new byte[] { 1 }},
+            new Object[] { LogicalDatastoreType.CONFIGURATION, new byte[] { 2 }});
+    }
 
     @Test
-    public void basicTest() {
-        assertFalse(LogicalDatastoreType.CONFIGURATION.equals(LogicalDatastoreType.OPERATIONAL));
+    void invalidSerialization() throws Exception {
+        try (var in = new DataInputStream(new ByteArrayInputStream(new byte[] { 0 }))) {
+            final var ex = assertThrows(IOException.class, () -> LogicalDatastoreType.readFrom(in));
+            assertEquals("Unknown type 0", ex.getMessage());
+        }
     }
+
 }
\ No newline at end of file
index 94ab79e94ce564ddf5adc0cebe3e3d21590bf730..91807c7dc14f7f46b480e473e4dc19d78856e973 100644 (file)
@@ -7,12 +7,25 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
 
-public class OptimisticLockFailedExceptionTest {
+import org.junit.jupiter.api.Test;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 
-    @Test(expected = OptimisticLockFailedException.class)
-    public void optimisticLockFailedExceptionTest() throws Exception {
-        throw new OptimisticLockFailedException("test");
+class OptimisticLockFailedExceptionTest {
+    @Test
+    void optimisticLockFailedExceptionTest() {
+        final var ex = new OptimisticLockFailedException("test");
+        assertEquals("test", ex.getMessage());
+        final var errors = ex.getErrorList();
+        assertEquals(1, errors.size());
+        final var error = errors.get(0);
+        assertEquals(ErrorType.APPLICATION, error.getErrorType());
+        assertEquals(ErrorTag.RESOURCE_DENIED, error.getTag());
+        assertNull(error.getApplicationTag());
+        assertNull(error.getInfo());
+        assertNull(error.getCause());
     }
 }
\ No newline at end of file
index 97434165cb6c652c811cb65b43e91ff2b5c2efda..44d44ffa7a5ee8c99fb2d1cbd57a48b3ca577b5c 100644 (file)
@@ -7,26 +7,23 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.opendaylight.mdsal.common.api.PostPreCommitStep.NOOP_COMMIT_FUTURE;
 
-import org.junit.Test;
-
-public class PostCommitStepTest {
+import org.junit.jupiter.api.Test;
 
+class PostCommitStepTest {
     @Test
-    public void preCommitTest() throws Exception {
-        final PostCanCommitStep postCanCommitStep = PostCanCommitStep.NOOP;
-
-        assertEquals(ThreePhaseCommitStep.NOOP_ABORT_FUTURE ,postCanCommitStep.abort());
-        assertEquals(PostPreCommitStep.NOOP_FUTURE, postCanCommitStep.preCommit());
+    void preCommitTest() throws Exception {
+        final var noop = PostCanCommitStep.NOOP;
+        assertEquals(ThreePhaseCommitStep.NOOP_ABORT_FUTURE ,noop.abort());
+        assertEquals(PostPreCommitStep.NOOP_FUTURE, noop.preCommit());
     }
 
     @Test
-    public void canCommitTest() throws Exception {
-        final PostPreCommitStep postPreCommitStep = PostPreCommitStep.NOOP;
-
-        assertEquals(ThreePhaseCommitStep.NOOP_ABORT_FUTURE ,postPreCommitStep.abort());
-        assertEquals(NOOP_COMMIT_FUTURE, postPreCommitStep.commit());
+    void canCommitTest() throws Exception {
+        final var noop = PostPreCommitStep.NOOP;
+        assertEquals(ThreePhaseCommitStep.NOOP_ABORT_FUTURE ,noop.abort());
+        assertEquals(NOOP_COMMIT_FUTURE, noop.commit());
     }
 }
\ No newline at end of file
index 95511e673950134a7a8cd60544ec88ffe7641312..4991ef3eeb7f7ae36da16df4ba9c938427f505fa 100644 (file)
@@ -7,21 +7,59 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import static org.mockito.Mockito.mock;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
 
-import org.junit.Test;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.opendaylight.yangtools.yang.common.ErrorSeverity;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcError;
 
-public class ReadFailedExceptionTest {
+@ExtendWith(MockitoExtension.class)
+class ReadFailedExceptionTest {
+    @Mock
+    private RpcError rpcError;
 
-    @Test(expected = ReadFailedException.class)
-    public void readFailedExceptionTest() throws Exception {
-        throw new ReadFailedException("test", mock(RpcError.class));
+    @Test
+    void readFailedExceptionTest() throws Exception {
+        final var ex = new ReadFailedException("test", rpcError);
+        assertEquals("test", ex.getMessage());
+        assertEquals(List.of(rpcError), ex.getErrorList());
     }
 
-    @Test(expected = ReadFailedException.class)
-    public void readFailedExceptionWithThrowableTest() throws Exception {
-        throw new ReadFailedException("test", ReadFailedException.MAPPER.apply(
-                new NullPointerException()).getCause(), mock(RpcError.class));
+    @Test
+    void readFailedExceptionWithThrowableTest() throws Exception {
+        final var npe = new NullPointerException();
+        final var ex = new ReadFailedException("test", ReadFailedException.MAPPER.apply(npe));
+        assertEquals("test", ex.getMessage());
+        final var errors = ex.getErrorList();
+        assertEquals(1, errors.size());
+        final var error = errors.get(0);
+        assertEquals(ErrorSeverity.ERROR, error.getSeverity());
+        assertEquals(ErrorType.APPLICATION, error.getErrorType());
+        assertEquals(ErrorTag.OPERATION_FAILED, error.getTag());
+        assertNull(error.getApplicationTag());
+        assertNull(error.getInfo());
+
+        final var cause = assertInstanceOf(ReadFailedException.class, ex.getCause());
+        assertEquals("read encountered an unexpected failure", cause.getMessage());
+        final var causeErrors = cause.getErrorList();
+        assertEquals(1, causeErrors.size());
+        final var causeError = causeErrors.get(0);
+        assertEquals("read encountered an unexpected failure", causeError.getMessage());
+        assertEquals(ErrorSeverity.ERROR, causeError.getSeverity());
+        assertEquals(ErrorType.APPLICATION, causeError.getErrorType());
+        assertEquals(ErrorTag.OPERATION_FAILED, causeError.getTag());
+        assertNull(causeError.getApplicationTag());
+        assertNull(causeError.getInfo());
+
+        assertSame(npe, causeError.getCause());
     }
 }
\ No newline at end of file
index 6d82c762e8febb2676c9cd2e7272cde83a8cebf8..e0ffbfecbd8e152a9a64b0a094eda514c7cce0b6 100644 (file)
@@ -7,16 +7,28 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import static org.mockito.Mockito.mock;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
 
-import org.junit.Test;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.yangtools.yang.common.RpcError;
 
-public class TransactionCommitDeadlockExceptionTest {
+@ExtendWith(MockitoExtension.class)
+class TransactionCommitDeadlockExceptionTest {
+    @Mock
+    private RpcError rpcError;
 
-    @Test(expected = TransactionCommitDeadlockException.class)
-    public void transactionCommitDeadlockExceptionTest() throws Exception {
-        throw new TransactionCommitDeadlockException(TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER
-                .get().getMessage(), mock(RpcError.class));
+    @Test
+    void transactionCommitDeadlockExceptionTest() throws Exception {
+        final var message = TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER.get().getMessage();
+        final var ex = new TransactionCommitDeadlockException(message, rpcError);
+        assertSame(message, ex.getMessage());
+        assertNull(ex.getCause());
+        assertEquals(List.of(rpcError), ex.getErrorList());
     }
 }
\ No newline at end of file
index e016ed0d46e2d9e52526c6715c25a00d4dd95b84..5432099dfe2ab66966b331be1d3c6f20033e552d 100644 (file)
@@ -7,15 +7,26 @@
  */
 package org.opendaylight.mdsal.common.api;
 
-import static org.mockito.Mockito.mock;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
 
-import org.junit.Test;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.yangtools.yang.common.RpcError;
 
-public class TransactionCommitFailedExceptionTest {
+@ExtendWith(MockitoExtension.class)
+class TransactionCommitFailedExceptionTest {
+    @Mock
+    private RpcError error;
 
-    @Test(expected = TransactionCommitFailedException.class)
-    public void transactionCommitFailedExceptionTest() throws Exception {
-        throw new TransactionCommitFailedException("test", mock(RpcError.class));
+    @Test
+    void transactionCommitFailedExceptionTest() {
+        final var ex = new TransactionCommitFailedException("test", error);
+        assertEquals("test", ex.getMessage());
+        assertNull(ex.getCause());
+        assertEquals(List.of(error), ex.getErrorList());
     }
 }
\ No newline at end of file