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.*;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
18 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
21 * Unit tests for RpcResultBuilder.
23 * @author Thomas Pantelis
25 public class RpcResultBuilderTest {
28 public void testSuccess() {
29 RpcResult<String> result = RpcResultBuilder.<String>success().withResult( "foo" ).build();
30 verifyRpcResult( result, true, "foo" );
31 assertNotNull( "getErrors returned null", result.getErrors() );
32 assertEquals( "getErrors size", 0, result.getErrors().size() );
34 result = RpcResultBuilder.<String>success( "bar" ).build();
35 verifyRpcResult( result, true, "bar" );
39 public void testFailed() {
40 Throwable cause = new Throwable( "mock cause" );
41 RpcResult<String> result = RpcResultBuilder.<String>failed()
42 .withError( ErrorType.PROTOCOL, "error message 1" )
43 .withError( ErrorType.APPLICATION, "lock_denied", "error message 2" )
44 .withError( ErrorType.RPC, "in-use", "error message 3", "my-app-tag", "my-info", cause )
46 verifyRpcResult( result, false, null );
47 verifyRpcError( result, 0, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
48 "error message 1", null, null, null );
49 verifyRpcError( result, 1, ErrorSeverity.ERROR, ErrorType.APPLICATION, "lock_denied",
50 "error message 2", null, null, null );
51 verifyRpcError( result, 2, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
52 "error message 3", "my-app-tag", "my-info", cause );
53 assertEquals( "getErrors size", 3, result.getErrors().size() );
57 public void testWithWarnings() {
58 Throwable cause = new Throwable( "mock cause" );
59 RpcResult<String> result = RpcResultBuilder.<String>success()
60 .withWarning( ErrorType.APPLICATION, "lock_denied", "message 1" )
61 .withWarning( ErrorType.RPC, "in-use", "message 2", "my-app-tag", "my-info", cause )
63 verifyRpcResult( result, true, null );
64 verifyRpcError( result, 0, ErrorSeverity.WARNING, ErrorType.APPLICATION, "lock_denied",
65 "message 1", null, null, null );
66 verifyRpcError( result, 1, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
67 "message 2", "my-app-tag", "my-info", cause );
68 assertEquals( "getErrors size", 2, result.getErrors().size() );
71 void verifyRpcError( RpcResult<?> result, int errorIndex, ErrorSeverity expSeverity,
72 ErrorType expErrorType, String expTag, String expMessage, String expAppTag,
73 String expInfo, Throwable expCause ) {
75 List<RpcError> errors = new ArrayList<>( result.getErrors() );
76 assertTrue( "Expected error at index " + errorIndex + " not found",
77 errorIndex < errors.size() );
78 RpcError error = errors.get( errorIndex );
79 assertEquals( "getSeverity", expSeverity, error.getSeverity() );
80 assertEquals( "getErrorType", expErrorType, error.getErrorType() );
81 assertEquals( "getTag", expTag, error.getTag() );
82 assertEquals( "getMessage", expMessage, error.getMessage() );
83 assertEquals( "getApplicationTag", expAppTag, error.getApplicationTag() );
84 assertEquals( "getInfo", expInfo, error.getInfo() );
85 assertEquals( "getCause", expCause, error.getCause() );
88 void verifyRpcResult( RpcResult<?> result, boolean expSuccess, Object expValue ) {
89 assertEquals( "isSuccessful", expSuccess, result.isSuccessful() );
90 assertEquals( "getResult", expValue, result.getResult() );