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;
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.ObjectInputStream;
17 import java.io.ObjectOutputStream;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
22 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
25 * Unit tests for RpcResultBuilder.
27 * @author Thomas Pantelis
29 public class RpcResultBuilderTest {
32 public void testSuccess() {
33 RpcResult<String> result = RpcResultBuilder.<String>success().withResult( "foo" ).build();
34 verifyRpcResult( result, true, "foo" );
35 assertNotNull( "getErrors returned null", result.getErrors() );
36 assertEquals( "getErrors size", 0, result.getErrors().size() );
38 result = RpcResultBuilder.success( "bar" ).build();
39 verifyRpcResult( result, true, "bar" );
43 public void testFailed() {
44 Throwable cause = new Throwable( "mock cause" );
45 Throwable cause2 = new Throwable( "mock cause2" );
46 RpcResult<String> result = RpcResultBuilder.<String>failed()
47 .withError( ErrorType.PROTOCOL, "error message 1" )
48 .withError( ErrorType.APPLICATION, "lock_denied", "error message 2" )
49 .withError( ErrorType.RPC, "in-use", "error message 3", "my-app-tag", "my-info", cause )
50 .withError( ErrorType.TRANSPORT, "error message 4", cause2 )
52 verifyRpcResult( result, false, null );
53 verifyRpcError( result, 0, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
54 "error message 1", null, null, null );
55 verifyRpcError( result, 1, ErrorSeverity.ERROR, ErrorType.APPLICATION, "lock_denied",
56 "error message 2", null, null, null );
57 verifyRpcError( result, 2, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
58 "error message 3", "my-app-tag", "my-info", cause );
59 verifyRpcError( result, 3, ErrorSeverity.ERROR, ErrorType.TRANSPORT, "operation-failed",
60 "error message 4", null, null, cause2 );
61 assertEquals( "getErrors size", 4, result.getErrors().size() );
65 public void testWithWarnings() {
66 Throwable cause = new Throwable( "mock cause" );
67 RpcResult<String> result = RpcResultBuilder.<String>success()
68 .withWarning( ErrorType.APPLICATION, "lock_denied", "message 1" )
69 .withWarning( ErrorType.RPC, "in-use", "message 2", "my-app-tag", "my-info", cause )
71 verifyRpcResult( result, true, null );
72 verifyRpcError( result, 0, ErrorSeverity.WARNING, ErrorType.APPLICATION, "lock_denied",
73 "message 1", null, null, null );
74 verifyRpcError( result, 1, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
75 "message 2", "my-app-tag", "my-info", cause );
76 assertEquals( "getErrors size", 2, result.getErrors().size() );
80 public void testFrom() {
81 Throwable cause = new Throwable( "mock cause" );
82 RpcResult<String> result = RpcResultBuilder.<String>success()
84 .withWarning( ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause )
87 RpcResult<String> copy = RpcResultBuilder.from( result )
88 .withError( ErrorType.PROTOCOL, "error message" )
90 verifyRpcResult( copy, true, "foo" );
91 verifyRpcError( copy, 0, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
92 "message", "my-app-tag", "my-info", cause );
93 verifyRpcError( copy, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
94 "error message", null, null, null );
98 public void testWithRpcErrors() {
99 Throwable cause = new Throwable( "mock cause" );
100 RpcResult<String> result = RpcResultBuilder.<String>failed()
101 .withWarning( ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause )
102 .withError( ErrorType.PROTOCOL, "error message" )
105 RpcResult<String> result2 = RpcResultBuilder.<String>failed()
106 .withRpcErrors( result.getErrors() )
108 verifyRpcError( result2, 0, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
109 "message", "my-app-tag", "my-info", cause );
110 verifyRpcError( result2, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
111 "error message", null, null, null );
115 public void testErrors() {
116 final RpcResultBuilder<Object> rpcResultBuilder = RpcResultBuilder.status(true);
117 final RpcError rpcErrorShort = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "tag", "msg");
118 final RpcError rpcErrorLong = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "tag", "msg", "applicationTag",
120 final RpcError rpcErrorShortWarn = RpcResultBuilder.newWarning(RpcError.ErrorType.RPC, "tag", "msg");
121 final RpcError rpcErrorLongWarn = RpcResultBuilder.newWarning(RpcError.ErrorType.RPC, "tag", "msg",
124 rpcResultBuilder.withRpcError(rpcErrorShort);
125 final RpcResult<Object> rpcResult = rpcResultBuilder.build();
126 final RpcResultBuilder<RpcResult<Object>> rpcResultRpcResultBuilder1 = RpcResultBuilder.success
128 final RpcResultBuilder<RpcResult<Object>> rpcResultRpcResultBuilder2 = rpcResultRpcResultBuilder1.withResult
131 assertEquals(rpcErrorShort.getErrorType(), rpcErrorShortWarn.getErrorType());
132 assertEquals(rpcErrorLong.getErrorType(), rpcErrorLongWarn.getErrorType());
133 assertEquals(rpcResultRpcResultBuilder1, rpcResultRpcResultBuilder2);
134 assertNotNull(rpcResultBuilder.buildFuture());
135 assertEquals("RpcResult [successful=true, result=null, errors=[RpcError [message=msg, severity=ERROR, " +
136 "errorType=RPC, tag=tag, applicationTag=null, info=null, cause=null]]]", rpcResult.toString());
139 @SuppressWarnings("unchecked")
141 public void testSerialization() throws Exception {
142 RpcResult<String> result = RpcResultBuilder.<String>success().withResult( "foo" ).build();
144 ByteArrayOutputStream bos = new ByteArrayOutputStream();
145 ObjectOutputStream out = new ObjectOutputStream(bos);
146 out.writeObject(result);
148 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
149 RpcResult<String> clone = (RpcResult<String>) in.readObject();
151 verifyRpcResult(clone, true, "foo");
153 Throwable cause = new Throwable( "mock cause" );
154 result = RpcResultBuilder.<String>failed()
155 .withError( ErrorType.RPC, "in-use", "error message", "my-app-tag", "my-info", cause )
158 bos = new ByteArrayOutputStream();
159 out = new ObjectOutputStream(bos);
160 out.writeObject(result);
162 in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
163 clone = (RpcResult<String>) in.readObject();
165 verifyRpcResult(clone, false, null);
166 verifyRpcError( result, 0, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
167 "error message", "my-app-tag", "my-info", cause );
170 void verifyRpcError( final RpcResult<?> result, final int errorIndex, final ErrorSeverity expSeverity,
171 final ErrorType expErrorType, final String expTag, final String expMessage, final String expAppTag,
172 final String expInfo, final Throwable expCause ) {
174 List<RpcError> errors = new ArrayList<>( result.getErrors() );
175 assertTrue( "Expected error at index " + errorIndex + " not found",
176 errorIndex < errors.size() );
177 RpcError error = errors.get( errorIndex );
178 assertEquals( "getSeverity", expSeverity, error.getSeverity() );
179 assertEquals( "getErrorType", expErrorType, error.getErrorType() );
180 assertEquals( "getTag", expTag, error.getTag() );
181 assertEquals( "getMessage", expMessage, error.getMessage() );
182 assertEquals( "getApplicationTag", expAppTag, error.getApplicationTag() );
183 assertEquals( "getInfo", expInfo, error.getInfo() );
184 assertEquals( "getCause", expCause, error.getCause() );
187 void verifyRpcResult( final RpcResult<?> result, final boolean expSuccess, final Object expValue ) {
188 assertEquals( "isSuccessful", expSuccess, result.isSuccessful() );
189 assertEquals( "getResult", expValue, result.getResult() );