Use ErrorTag for RpcError.getTag() 54/97454/4
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 12 Sep 2021 15:14:48 +0000 (17:14 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 12 Sep 2021 16:05:58 +0000 (18:05 +0200)
We have a dedicated class (and constants) to cover error-tag, use it
instead of plain String.

Change-Id: I3b6776365ec7fcd3769ff485a4a01f5e038f4c1b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/RpcError.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/RpcResultBuilder.java
common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/OperationFailedExceptionTest.java
common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/RpcResultBuilderTest.java

index 9a22d22732d3d7cc24cf596110b6b401092998c8..c0f317a4d1850abcf4e2879f1cfed2feb830e272 100644 (file)
@@ -48,8 +48,7 @@ public interface RpcError {
      * </pre>
      * @return a string if available or null otherwise.
      */
-    // FIXME: 8.0.0: return ErrorTag here
-    String getTag();
+    ErrorTag getTag();
 
     /**
      * Returns a short string that identifies the specific type of error condition as
index 3289d3d7ddc9ca8ff8b9e7c0a5d335466eaf32b2..6b0cc3fb2d607f456ea55f225c78c8441b883c9f 100644 (file)
@@ -65,16 +65,15 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
         private static final long serialVersionUID = 1L;
 
         private final String applicationTag;
-        private final String tag;
+        private final ErrorTag tag;
         private final String info;
         private final ErrorSeverity severity;
         private final String message;
         private final ErrorType errorType;
         private final Throwable cause;
 
-        RpcErrorImpl(final ErrorSeverity severity, final ErrorType errorType,
-                final String tag, final String message, final String applicationTag, final String info,
-                final Throwable cause) {
+        RpcErrorImpl(final ErrorSeverity severity, final ErrorType errorType, final ErrorTag tag, final String message,
+                final String applicationTag, final String info, final Throwable cause) {
             this.severity = severity;
             this.errorType = errorType;
             this.tag = tag;
@@ -90,7 +89,7 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
         }
 
         @Override
-        public String getTag() {
+        public ErrorTag getTag() {
             return tag;
         }
 
@@ -198,9 +197,9 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
      *
      * @return an RpcError
      */
-    public static @NonNull RpcError newError(final ErrorType errorType, final String tag, final String message) {
-        return new RpcErrorImpl(ErrorSeverity.ERROR, errorType,
-                tag != null ? tag : "operation-failed", message, null, null, null);
+    public static @NonNull RpcError newError(final ErrorType errorType, final ErrorTag tag, final String message) {
+        return new RpcErrorImpl(ErrorSeverity.ERROR, errorType, tag != null ? tag : ErrorTag.OPERATION_FAILED, message,
+            null, null, null);
     }
 
     /**
@@ -217,10 +216,10 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
      *
      * @return an RpcError
      */
-    public static @NonNull RpcError newError(final ErrorType errorType, final String tag, final String message,
+    public static @NonNull RpcError newError(final ErrorType errorType, final ErrorTag tag, final String message,
             final String applicationTag, final String info, final Throwable cause) {
-        return new RpcErrorImpl(ErrorSeverity.ERROR, errorType,
-                tag != null ? tag : "operation-failed", message, applicationTag, info, cause);
+        return new RpcErrorImpl(ErrorSeverity.ERROR, errorType, tag != null ? tag : ErrorTag.OPERATION_FAILED, message,
+            applicationTag, info, cause);
     }
 
     /**
@@ -233,7 +232,7 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
      *
      * @return an RpcError
      */
-    public static @NonNull RpcError newWarning(final ErrorType errorType, final String tag, final String message) {
+    public static @NonNull RpcError newWarning(final ErrorType errorType, final ErrorTag tag, final String message) {
         return new RpcErrorImpl(ErrorSeverity.WARNING, errorType, tag, message, null, null, null);
     }
 
@@ -251,10 +250,9 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
      *
      * @return an RpcError
      */
-    public static @NonNull RpcError newWarning(final ErrorType errorType, final String tag, final String message,
+    public static @NonNull RpcError newWarning(final ErrorType errorType, final ErrorTag tag, final String message,
             final String applicationTag, final String info, final Throwable cause) {
-        return new RpcErrorImpl(ErrorSeverity.WARNING, errorType, tag, message,
-                                 applicationTag, info, cause);
+        return new RpcErrorImpl(ErrorSeverity.WARNING, errorType, tag, message, applicationTag, info, cause);
     }
 
     /**
@@ -277,13 +275,10 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
         return withResult(builder.build());
     }
 
-    private void addError(final ErrorSeverity severity, final ErrorType errorType,
-            final String tag, final String message, final String applicationTag, final String info,
-            final Throwable cause) {
-
-        addError(new RpcErrorImpl(severity, errorType,
-                                    tag != null ? tag : "operation-failed", message,
-                                    applicationTag, info, cause));
+    private void addError(final ErrorSeverity severity, final ErrorType errorType, final ErrorTag tag,
+            final String message, final String applicationTag, final String info, final Throwable cause) {
+        addError(new RpcErrorImpl(severity, errorType, tag != null ? tag : ErrorTag.OPERATION_FAILED, message,
+            applicationTag, info, cause));
     }
 
     private void addError(final RpcError error) {
@@ -303,7 +298,8 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
      *        {@link RpcError#getTag} for a list of suggested values.
      * @param message a string suitable for human display that describes the warning condition.
      */
-    public @NonNull RpcResultBuilder<T> withWarning(final ErrorType errorType, final String tag, final String message) {
+    public @NonNull RpcResultBuilder<T> withWarning(final ErrorType errorType, final ErrorTag tag,
+            final String message) {
         addError(ErrorSeverity.WARNING, errorType, tag, message, null, null, null);
         return this;
     }
@@ -320,7 +316,7 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
      *        and/or implementation-specific debugging information.
      * @param cause the exception that triggered the warning.
      */
-    public @NonNull RpcResultBuilder<T> withWarning(final ErrorType errorType, final String tag, final String message,
+    public @NonNull RpcResultBuilder<T> withWarning(final ErrorType errorType, final ErrorTag tag, final String message,
             final String applicationTag, final String info, final Throwable cause) {
         addError(ErrorSeverity.WARNING, errorType, tag, message, applicationTag, info, cause);
         return this;
@@ -345,7 +341,7 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
      *        {@link RpcError#getTag} for a list of suggested values.
      * @param message a string suitable for human display that describes the error condition.
      */
-    public @NonNull RpcResultBuilder<T> withError(final ErrorType errorType, final String tag, final String message) {
+    public @NonNull RpcResultBuilder<T> withError(final ErrorType errorType, final ErrorTag tag, final String message) {
         addError(ErrorSeverity.ERROR, errorType, tag, message, null, null, null);
         return this;
     }
@@ -375,7 +371,7 @@ public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
      *        and/or implementation-specific debugging information.
      * @param cause the exception that triggered the error.
      */
-    public @NonNull RpcResultBuilder<T> withError(final ErrorType errorType, final String tag, final String message,
+    public @NonNull RpcResultBuilder<T> withError(final ErrorType errorType, final ErrorTag tag, final String message,
             final String applicationTag, final String info, final Throwable cause) {
         addError(ErrorSeverity.ERROR, errorType, tag, message, applicationTag, info, cause);
         return this;
index 8bc171c122d5948dd88fbab3d1de1fde52c142cb..e47e53ec855e92bbc356f99b5f14fb327fa73324 100644 (file)
@@ -17,7 +17,7 @@ public class OperationFailedExceptionTest {
     @Test
     public void testOperationFailedException() {
         final Throwable cause = new Throwable("mock cause");
-        final RpcError rpcErrorShort = RpcResultBuilder.newError(ErrorType.RPC, "tag", "msg");
+        final RpcError rpcErrorShort = RpcResultBuilder.newError(ErrorType.RPC, new ErrorTag("tag"), "msg");
         final OperationFailedException operationFailedException1 = new OperationFailedException("error msg", cause,
                 rpcErrorShort);
         final OperationFailedException operationFailedException2 = new OperationFailedException("error msg",
index eae1e797c2b30046c825711b0592dcefbca99f7b..ad600d87eaec58901eb670ddea3939720ef1f884 100644 (file)
@@ -15,7 +15,6 @@ 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;
 
@@ -25,6 +24,7 @@ import org.junit.Test;
  * @author Thomas Pantelis
  */
 public class RpcResultBuilderTest {
+    private static final ErrorTag TAG = new ErrorTag("tag");
 
     @Test
     public void testSuccess() {
@@ -43,18 +43,18 @@ 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());
     }
@@ -63,14 +63,14 @@ public class RpcResultBuilderTest {
     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);
+        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("getErrors size", 2, result.getErrors().size());
     }
 
@@ -79,45 +79,45 @@ 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(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);
+        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(
@@ -149,7 +149,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();
@@ -160,17 +160,16 @@ 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();
+        assertTrue("Expected error at index " + errorIndex + " not found", errorIndex < errors.size());
         RpcError error = errors.get(errorIndex);
         assertEquals("getSeverity", expSeverity, error.getSeverity());
         assertEquals("getErrorType", expErrorType, error.getErrorType());