Further cleanups of code
[yangtools.git] / yang / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / RpcResultBuilderTest.java
index 6eee59a9183021da2440c7dbde55ddf5696611e0..45991b2e285a871aefc0e06c46eb314f6543dadb 100644 (file)
@@ -8,11 +8,15 @@
 
 package org.opendaylight.yangtools.yang.common;
 
-import static org.junit.Assert.*;
-
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+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;
@@ -31,7 +35,7 @@ public class RpcResultBuilderTest {
         assertNotNull( "getErrors returned null", result.getErrors() );
         assertEquals( "getErrors size", 0, result.getErrors().size() );
 
-        result = RpcResultBuilder.<String>success( "bar" ).build();
+        result = RpcResultBuilder.success( "bar" ).build();
         verifyRpcResult( result, true, "bar" );
     }
 
@@ -80,7 +84,7 @@ public class RpcResultBuilderTest {
                 .withWarning( ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause )
                 .build();
 
-        RpcResult<String> copy = RpcResultBuilder.<String>from( result )
+        RpcResult<String> copy = RpcResultBuilder.from( result )
                 .withError( ErrorType.PROTOCOL, "error message" )
                 .build();
         verifyRpcResult( copy, true, "foo" );
@@ -107,9 +111,65 @@ public class RpcResultBuilderTest {
                         "error message", null, null, null );
     }
 
-    void verifyRpcError( RpcResult<?> result, int errorIndex, ErrorSeverity expSeverity,
-            ErrorType expErrorType, String expTag, String expMessage, String expAppTag,
-            String expInfo, Throwable expCause ) {
+    @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);
+        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());
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testSerialization() throws Exception {
+        RpcResult<String> result = RpcResultBuilder.<String>success().withResult( "foo" ).build();
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream(bos);
+        out.writeObject(result);
+
+        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
+        RpcResult<String> clone = (RpcResult<String>) in.readObject();
+
+        verifyRpcResult(clone, true, "foo");
+
+        Throwable cause = new Throwable( "mock cause" );
+        result = RpcResultBuilder.<String>failed()
+                .withError( ErrorType.RPC, "in-use", "error message", "my-app-tag", "my-info", cause )
+                .build();
+
+        bos = new ByteArrayOutputStream();
+        out = new ObjectOutputStream(bos);
+        out.writeObject(result);
+
+        in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
+        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 );
+    }
+
+    void verifyRpcError( final RpcResult<?> result, final int errorIndex, final ErrorSeverity expSeverity,
+            final ErrorType expErrorType, final String 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",
@@ -124,8 +184,8 @@ public class RpcResultBuilderTest {
         assertEquals( "getCause", expCause, error.getCause() );
     }
 
-    void verifyRpcResult( RpcResult<?> result, boolean expSuccess, Object expValue ) {
+    void verifyRpcResult( final RpcResult<?> result, final boolean expSuccess, final Object expValue ) {
         assertEquals( "isSuccessful", expSuccess, result.isSuccessful() );
         assertEquals( "getResult", expValue, result.getResult() );
     }
-}
+}
\ No newline at end of file