Migrate yang-common to JUnit5
[yangtools.git] / common / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / RpcResultBuilderTest.java
index 74166dfede8792046f199e77a5a8b8711409c5e2..5795bc7b1cbaa3bf4bcea266e6c7d6fdfd4090e6 100644 (file)
@@ -5,22 +5,19 @@
  * 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.yangtools.yang.common;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.util.ArrayList;
 import java.util.List;
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
-import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
+import org.junit.jupiter.api.Test;
 
 /**
  * Unit tests for RpcResultBuilder.
@@ -28,13 +25,13 @@ import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
  * @author Thomas Pantelis
  */
 public class RpcResultBuilderTest {
+    private static final ErrorTag TAG = new ErrorTag("tag");
 
     @Test
     public void testSuccess() {
         RpcResult<String> result = RpcResultBuilder.<String>success().withResult("foo").build();
         verifyRpcResult(result, true, "foo");
-        assertNotNull("getErrors returned null", result.getErrors());
-        assertEquals("getErrors size", 0, result.getErrors().size());
+        assertEquals(List.of(), result.getErrors());
 
         result = RpcResultBuilder.success("bar").build();
         verifyRpcResult(result, true, "bar");
@@ -46,35 +43,35 @@ public class RpcResultBuilderTest {
         Throwable cause2 = new Throwable("mock cause2");
         RpcResult<String> result = RpcResultBuilder.<String>failed()
                   .withError(ErrorType.PROTOCOL, "error message 1")
-                  .withError(ErrorType.APPLICATION, "lock_denied", "error message 2")
-                  .withError(ErrorType.RPC, "in-use", "error message 3", "my-app-tag", "my-info", cause)
+                  .withError(ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "error message 2")
+                  .withError(ErrorType.RPC, ErrorTag.IN_USE, "error message 3", "my-app-tag", "my-info", cause)
                   .withError(ErrorType.TRANSPORT, "error message 4", cause2)
                   .build();
         verifyRpcResult(result, false, null);
-        verifyRpcError(result, 0, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
-                        "error message 1", null, null, null);
-        verifyRpcError(result, 1, ErrorSeverity.ERROR, ErrorType.APPLICATION, "lock_denied",
-                        "error message 2", null, null, null);
-        verifyRpcError(result, 2, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
-                        "error message 3", "my-app-tag", "my-info", cause);
-        verifyRpcError(result, 3, ErrorSeverity.ERROR, ErrorType.TRANSPORT, "operation-failed",
+        verifyRpcError(result, 0, ErrorSeverity.ERROR, ErrorType.PROTOCOL, ErrorTag.OPERATION_FAILED, "error message 1",
+                        null, null, null);
+        verifyRpcError(result, 1, ErrorSeverity.ERROR, ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "error message 2",
+                        null, null, null);
+        verifyRpcError(result, 2, ErrorSeverity.ERROR, ErrorType.RPC, ErrorTag.IN_USE, "error message 3", "my-app-tag",
+                        "my-info", cause);
+        verifyRpcError(result, 3, ErrorSeverity.ERROR, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED,
                         "error message 4", null, null, cause2);
-        assertEquals("getErrors size", 4, result.getErrors().size());
+        assertEquals(4, result.getErrors().size());
     }
 
     @Test
     public void testWithWarnings() {
         Throwable cause = new Throwable("mock cause");
         RpcResult<String> result = RpcResultBuilder.<String>success()
-                  .withWarning(ErrorType.APPLICATION, "lock_denied", "message 1")
-                  .withWarning(ErrorType.RPC, "in-use", "message 2", "my-app-tag", "my-info", cause)
+                  .withWarning(ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "message 1")
+                  .withWarning(ErrorType.RPC, ErrorTag.IN_USE, "message 2", "my-app-tag", "my-info", cause)
                   .build();
         verifyRpcResult(result, true, null);
-        verifyRpcError(result, 0, ErrorSeverity.WARNING, ErrorType.APPLICATION, "lock_denied",
-                        "message 1", null, null, null);
-        verifyRpcError(result, 1, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
-                        "message 2", "my-app-tag", "my-info", cause);
-        assertEquals("getErrors size", 2, result.getErrors().size());
+        verifyRpcError(result, 0, ErrorSeverity.WARNING, ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "message 1", null,
+                        null, null);
+        verifyRpcError(result, 1, ErrorSeverity.WARNING, ErrorType.RPC, ErrorTag.IN_USE, "message 2", "my-app-tag",
+                        "my-info", cause);
+        assertEquals(2, result.getErrors().size());
     }
 
     @Test
@@ -82,56 +79,50 @@ public class RpcResultBuilderTest {
         Throwable cause = new Throwable("mock cause");
         RpcResult<String> result = RpcResultBuilder.<String>success()
                 .withResult("foo")
-                .withWarning(ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause)
+                .withWarning(ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag", "my-info", cause)
                 .build();
 
         RpcResult<String> copy = RpcResultBuilder.from(result)
                 .withError(ErrorType.PROTOCOL, "error message")
                 .build();
         verifyRpcResult(copy, true, "foo");
-        verifyRpcError(copy, 0, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
-                        "message", "my-app-tag", "my-info", cause);
-        verifyRpcError(copy, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
-                        "error message", null, null, null);
+        verifyRpcError(copy, 0, ErrorSeverity.WARNING, ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag",
+                        "my-info", cause);
+        verifyRpcError(copy, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, ErrorTag.OPERATION_FAILED, "error message",
+                        null, null, null);
     }
 
     @Test
     public void testWithRpcErrors() {
         Throwable cause = new Throwable("mock cause");
         RpcResult<String> result = RpcResultBuilder.<String>failed()
-                .withWarning(ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause)
+                .withWarning(ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag", "my-info", cause)
                 .withError(ErrorType.PROTOCOL, "error message")
                 .build();
 
         RpcResult<String> result2 = RpcResultBuilder.<String>failed()
                 .withRpcErrors(result.getErrors())
                 .build();
-        verifyRpcError(result2, 0, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
-                        "message", "my-app-tag", "my-info", cause);
-        verifyRpcError(result2, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
-                        "error message", null, null, null);
+        verifyRpcError(result2, 0, ErrorSeverity.WARNING, ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag",
+                "my-info", cause);
+        verifyRpcError(result2, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, ErrorTag.OPERATION_FAILED, "error message",
+                null, null, null);
     }
 
     @Test
     public void testErrors() {
         final RpcResultBuilder<Object> rpcResultBuilder = RpcResultBuilder.status(true);
-        final RpcError rpcErrorShort = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "tag", "msg");
-        final RpcError rpcErrorLong = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "tag", "msg", "applicationTag",
-                "info", null);
-        final RpcError rpcErrorShortWarn = RpcResultBuilder.newWarning(RpcError.ErrorType.RPC, "tag", "msg");
-        final RpcError rpcErrorLongWarn = RpcResultBuilder.newWarning(RpcError.ErrorType.RPC, "tag", "msg",
-                "applicationTag",
-                "info", null);
+        final RpcError rpcErrorShort = RpcResultBuilder.newError(ErrorType.RPC, TAG, "msg");
+        final RpcError rpcErrorLong = RpcResultBuilder.newError(ErrorType.RPC, TAG, "msg", "applicationTag", "info",
+            null);
+        final RpcError rpcErrorShortWarn = RpcResultBuilder.newWarning(ErrorType.RPC, TAG, "msg");
+        final RpcError rpcErrorLongWarn = RpcResultBuilder.newWarning(ErrorType.RPC, TAG, "msg", "applicationTag",
+            "info", null);
         rpcResultBuilder.withRpcError(rpcErrorShort);
         final RpcResult<Object> rpcResult = rpcResultBuilder.build();
-        final RpcResultBuilder<RpcResult<Object>> rpcResultRpcResultBuilder1 = RpcResultBuilder.success(
-                rpcResultBuilder);
-        final RpcResultBuilder<RpcResult<Object>> rpcResultRpcResultBuilder2 = rpcResultRpcResultBuilder1.withResult(
-                rpcResultBuilder);
 
         assertEquals(rpcErrorShort.getErrorType(), rpcErrorShortWarn.getErrorType());
         assertEquals(rpcErrorLong.getErrorType(), rpcErrorLongWarn.getErrorType());
-        assertEquals(rpcResultRpcResultBuilder1, rpcResultRpcResultBuilder2);
         assertNotNull(rpcResultBuilder.buildFuture());
         assertEquals("RpcResult [successful=true, result=null, errors=[RpcError [message=msg, severity=ERROR, "
                 + "errorType=RPC, tag=tag, applicationTag=null, info=null, cause=null]]]", rpcResult.toString());
@@ -153,7 +144,7 @@ public class RpcResultBuilderTest {
 
         Throwable cause = new Throwable("mock cause");
         result = RpcResultBuilder.<String>failed()
-                .withError(ErrorType.RPC, "in-use", "error message", "my-app-tag", "my-info", cause)
+                .withError(ErrorType.RPC, ErrorTag.IN_USE, "error message", "my-app-tag", "my-info", cause)
                 .build();
 
         bos = new ByteArrayOutputStream();
@@ -164,29 +155,29 @@ public class RpcResultBuilderTest {
         clone = (RpcResult<String>) in.readObject();
 
         verifyRpcResult(clone, false, null);
-        verifyRpcError(result, 0, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
-                "error message", "my-app-tag", "my-info", cause);
+        verifyRpcError(result, 0, ErrorSeverity.ERROR, ErrorType.RPC, ErrorTag.IN_USE, "error message", "my-app-tag",
+            "my-info", cause);
     }
 
     void verifyRpcError(final RpcResult<?> result, final int errorIndex, final ErrorSeverity expSeverity,
-            final ErrorType expErrorType, final String expTag, final String expMessage, final String expAppTag,
+            final ErrorType expErrorType, final ErrorTag expTag, final String expMessage, final String expAppTag,
             final String expInfo, final Throwable expCause) {
 
-        List<RpcError> errors = new ArrayList<>(result.getErrors());
-        assertTrue("Expected error at index " + errorIndex + " not found",
-                    errorIndex < errors.size());
+        List<RpcError> errors = result.getErrors();
+        assertThat(errors.size(), greaterThanOrEqualTo(errorIndex));
+
         RpcError error = errors.get(errorIndex);
-        assertEquals("getSeverity", expSeverity, error.getSeverity());
-        assertEquals("getErrorType", expErrorType, error.getErrorType());
-        assertEquals("getTag", expTag, error.getTag());
-        assertEquals("getMessage", expMessage, error.getMessage());
-        assertEquals("getApplicationTag", expAppTag, error.getApplicationTag());
-        assertEquals("getInfo", expInfo, error.getInfo());
-        assertEquals("getCause", expCause, error.getCause());
+        assertEquals(expSeverity, error.getSeverity());
+        assertEquals(expErrorType, error.getErrorType());
+        assertEquals(expTag, error.getTag());
+        assertEquals(expMessage, error.getMessage());
+        assertEquals(expAppTag, error.getApplicationTag());
+        assertEquals(expInfo, error.getInfo());
+        assertEquals(expCause, error.getCause());
     }
 
     void verifyRpcResult(final RpcResult<?> result, final boolean expSuccess, final Object expValue) {
-        assertEquals("isSuccessful", expSuccess, result.isSuccessful());
-        assertEquals("getResult", expValue, result.getResult());
+        assertEquals(expSuccess, result.isSuccessful());
+        assertEquals(expValue, result.getResult());
     }
 }