2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.common;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
24 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
27 * Unit tests for RpcResultBuilder.
29 * @author Thomas Pantelis
31 public class RpcResultBuilderTest {
34 public void testSuccess() {
35 RpcResult<String> result = RpcResultBuilder.<String>success().withResult( "foo" ).build();
36 verifyRpcResult( result, true, "foo" );
37 assertNotNull( "getErrors returned null", result.getErrors() );
38 assertEquals( "getErrors size", 0, result.getErrors().size() );
40 result = RpcResultBuilder.success( "bar" ).build();
41 verifyRpcResult( result, true, "bar" );
45 public void testFailed() {
46 Throwable cause = new Throwable( "mock cause" );
47 Throwable cause2 = new Throwable( "mock cause2" );
48 RpcResult<String> result = RpcResultBuilder.<String>failed()
49 .withError( ErrorType.PROTOCOL, "error message 1" )
50 .withError( ErrorType.APPLICATION, "lock_denied", "error message 2" )
51 .withError( ErrorType.RPC, "in-use", "error message 3", "my-app-tag", "my-info", cause )
52 .withError( ErrorType.TRANSPORT, "error message 4", cause2 )
54 verifyRpcResult( result, false, null );
55 verifyRpcError( result, 0, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
56 "error message 1", null, null, null );
57 verifyRpcError( result, 1, ErrorSeverity.ERROR, ErrorType.APPLICATION, "lock_denied",
58 "error message 2", null, null, null );
59 verifyRpcError( result, 2, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
60 "error message 3", "my-app-tag", "my-info", cause );
61 verifyRpcError( result, 3, ErrorSeverity.ERROR, ErrorType.TRANSPORT, "operation-failed",
62 "error message 4", null, null, cause2 );
63 assertEquals( "getErrors size", 4, result.getErrors().size() );
67 public void testWithWarnings() {
68 Throwable cause = new Throwable( "mock cause" );
69 RpcResult<String> result = RpcResultBuilder.<String>success()
70 .withWarning( ErrorType.APPLICATION, "lock_denied", "message 1" )
71 .withWarning( ErrorType.RPC, "in-use", "message 2", "my-app-tag", "my-info", cause )
73 verifyRpcResult( result, true, null );
74 verifyRpcError( result, 0, ErrorSeverity.WARNING, ErrorType.APPLICATION, "lock_denied",
75 "message 1", null, null, null );
76 verifyRpcError( result, 1, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
77 "message 2", "my-app-tag", "my-info", cause );
78 assertEquals( "getErrors size", 2, result.getErrors().size() );
82 public void testFrom() {
83 Throwable cause = new Throwable( "mock cause" );
84 RpcResult<String> result = RpcResultBuilder.<String>success()
86 .withWarning( ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause )
89 RpcResult<String> copy = RpcResultBuilder.from( result )
90 .withError( ErrorType.PROTOCOL, "error message" )
92 verifyRpcResult( copy, true, "foo" );
93 verifyRpcError( copy, 0, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
94 "message", "my-app-tag", "my-info", cause );
95 verifyRpcError( copy, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
96 "error message", null, null, null );
100 public void testWithRpcErrors() {
101 Throwable cause = new Throwable( "mock cause" );
102 RpcResult<String> result = RpcResultBuilder.<String>failed()
103 .withWarning( ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause )
104 .withError( ErrorType.PROTOCOL, "error message" )
107 RpcResult<String> result2 = RpcResultBuilder.<String>failed()
108 .withRpcErrors( result.getErrors() )
110 verifyRpcError( result2, 0, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
111 "message", "my-app-tag", "my-info", cause );
112 verifyRpcError( result2, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
113 "error message", null, null, null );
117 public void testErrors() {
118 final RpcResultBuilder<Object> rpcResultBuilder = RpcResultBuilder.status(true);
119 final RpcError rpcErrorShort = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "tag", "msg");
120 final RpcError rpcErrorLong = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "tag", "msg", "applicationTag",
122 final RpcError rpcErrorShortWarn = RpcResultBuilder.newWarning(RpcError.ErrorType.RPC, "tag", "msg");
123 final RpcError rpcErrorLongWarn = RpcResultBuilder.newWarning(RpcError.ErrorType.RPC, "tag", "msg",
126 rpcResultBuilder.withRpcError(rpcErrorShort);
127 final RpcResult<Object> rpcResult = rpcResultBuilder.build();
128 final RpcResultBuilder<RpcResult<Object>> rpcResultRpcResultBuilder1 = RpcResultBuilder.success
130 final RpcResultBuilder<RpcResult<Object>> rpcResultRpcResultBuilder2 = rpcResultRpcResultBuilder1.withResult
133 assertEquals(rpcErrorShort.getErrorType(), rpcErrorShortWarn.getErrorType());
134 assertEquals(rpcErrorLong.getErrorType(), rpcErrorLongWarn.getErrorType());
135 assertEquals(rpcResultRpcResultBuilder1, rpcResultRpcResultBuilder2);
136 assertTrue(rpcResultBuilder.buildFuture() instanceof ListenableFuture);
137 assertEquals("RpcResult [successful=true, result=null, errors=[RpcError [message=msg, severity=ERROR, " +
138 "errorType=RPC, tag=tag, applicationTag=null, info=null, cause=null]]]", rpcResult.toString());
141 @SuppressWarnings("unchecked")
143 public void testSerialization() throws Exception {
144 RpcResult<String> result = RpcResultBuilder.<String>success().withResult( "foo" ).build();
146 ByteArrayOutputStream bos = new ByteArrayOutputStream();
147 ObjectOutputStream out = new ObjectOutputStream(bos);
148 out.writeObject(result);
150 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
151 RpcResult<String> clone = (RpcResult<String>) in.readObject();
153 verifyRpcResult(clone, true, "foo");
155 Throwable cause = new Throwable( "mock cause" );
156 result = RpcResultBuilder.<String>failed()
157 .withError( ErrorType.RPC, "in-use", "error message", "my-app-tag", "my-info", cause )
160 bos = new ByteArrayOutputStream();
161 out = new ObjectOutputStream(bos);
162 out.writeObject(result);
164 in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
165 clone = (RpcResult<String>) in.readObject();
167 verifyRpcResult(clone, false, null);
168 verifyRpcError( result, 0, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
169 "error message", "my-app-tag", "my-info", cause );
172 void verifyRpcError( RpcResult<?> result, int errorIndex, ErrorSeverity expSeverity,
173 ErrorType expErrorType, String expTag, String expMessage, String expAppTag,
174 String expInfo, Throwable expCause ) {
176 List<RpcError> errors = new ArrayList<>( result.getErrors() );
177 assertTrue( "Expected error at index " + errorIndex + " not found",
178 errorIndex < errors.size() );
179 RpcError error = errors.get( errorIndex );
180 assertEquals( "getSeverity", expSeverity, error.getSeverity() );
181 assertEquals( "getErrorType", expErrorType, error.getErrorType() );
182 assertEquals( "getTag", expTag, error.getTag() );
183 assertEquals( "getMessage", expMessage, error.getMessage() );
184 assertEquals( "getApplicationTag", expAppTag, error.getApplicationTag() );
185 assertEquals( "getInfo", expInfo, error.getInfo() );
186 assertEquals( "getCause", expCause, error.getCause() );
189 void verifyRpcResult( RpcResult<?> result, boolean expSuccess, Object expValue ) {
190 assertEquals( "isSuccessful", expSuccess, result.isSuccessful() );
191 assertEquals( "getResult", expValue, result.getResult() );