BUG-1276: fixed generated union constructor
[yangtools.git] / yang / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / RpcResultBuilderTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.common;
10
11 import static org.junit.Assert.*;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
18 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
19
20 /**
21  * Unit tests for RpcResultBuilder.
22  *
23  * @author Thomas Pantelis
24  */
25 public class RpcResultBuilderTest {
26
27     @Test
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() );
33
34         result = RpcResultBuilder.<String>success( "bar" ).build();
35         verifyRpcResult( result, true, "bar" );
36     }
37
38     @Test
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 )
45                   .build();
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() );
54     }
55
56     @Test
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 )
62                   .build();
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() );
69     }
70
71     void verifyRpcError( RpcResult<?> result, int errorIndex, ErrorSeverity expSeverity,
72             ErrorType expErrorType, String expTag, String expMessage, String expAppTag,
73             String expInfo, Throwable expCause ) {
74
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() );
86     }
87
88     void verifyRpcResult( RpcResult<?> result, boolean expSuccess, Object expValue ) {
89         assertEquals( "isSuccessful", expSuccess, result.isSuccessful() );
90         assertEquals( "getResult", expValue, result.getResult() );
91     }
92 }