Merge "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         Throwable cause2 = new Throwable( "mock cause2" );
42         RpcResult<String> result = RpcResultBuilder.<String>failed()
43                   .withError( ErrorType.PROTOCOL, "error message 1" )
44                   .withError( ErrorType.APPLICATION, "lock_denied", "error message 2" )
45                   .withError( ErrorType.RPC, "in-use", "error message 3", "my-app-tag", "my-info", cause )
46                   .withError( ErrorType.TRANSPORT, "error message 4", cause2 )
47                   .build();
48         verifyRpcResult( result, false, null );
49         verifyRpcError( result, 0, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
50                         "error message 1", null, null, null );
51         verifyRpcError( result, 1, ErrorSeverity.ERROR, ErrorType.APPLICATION, "lock_denied",
52                         "error message 2", null, null, null );
53         verifyRpcError( result, 2, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
54                         "error message 3", "my-app-tag", "my-info", cause );
55         verifyRpcError( result, 3, ErrorSeverity.ERROR, ErrorType.TRANSPORT, "operation-failed",
56                         "error message 4", null, null, cause2 );
57         assertEquals( "getErrors size", 4, result.getErrors().size() );
58     }
59
60     @Test
61     public void testWithWarnings() {
62         Throwable cause = new Throwable( "mock cause" );
63         RpcResult<String> result = RpcResultBuilder.<String>success()
64                   .withWarning( ErrorType.APPLICATION, "lock_denied", "message 1" )
65                   .withWarning( ErrorType.RPC, "in-use", "message 2", "my-app-tag", "my-info", cause )
66                   .build();
67         verifyRpcResult( result, true, null );
68         verifyRpcError( result, 0, ErrorSeverity.WARNING, ErrorType.APPLICATION, "lock_denied",
69                         "message 1", null, null, null );
70         verifyRpcError( result, 1, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
71                         "message 2", "my-app-tag", "my-info", cause );
72         assertEquals( "getErrors size", 2, result.getErrors().size() );
73     }
74
75     @Test
76     public void testFrom() {
77         Throwable cause = new Throwable( "mock cause" );
78         RpcResult<String> result = RpcResultBuilder.<String>success()
79                 .withResult( "foo" )
80                 .withWarning( ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause )
81                 .build();
82
83         RpcResult<String> copy = RpcResultBuilder.<String>from( result )
84                 .withError( ErrorType.PROTOCOL, "error message" )
85                 .build();
86         verifyRpcResult( copy, true, "foo" );
87         verifyRpcError( copy, 0, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
88                         "message", "my-app-tag", "my-info", cause );
89         verifyRpcError( copy, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
90                         "error message", null, null, null );
91     }
92
93     @Test
94     public void testWithRpcErrors() {
95         Throwable cause = new Throwable( "mock cause" );
96         RpcResult<String> result = RpcResultBuilder.<String>failed()
97                 .withWarning( ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause )
98                 .withError( ErrorType.PROTOCOL, "error message" )
99                 .build();
100
101         RpcResult<String> result2 = RpcResultBuilder.<String>failed()
102                 .withRpcErrors( result.getErrors() )
103                 .build();
104         verifyRpcError( result2, 0, ErrorSeverity.WARNING, ErrorType.RPC, "in-use",
105                         "message", "my-app-tag", "my-info", cause );
106         verifyRpcError( result2, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, "operation-failed",
107                         "error message", null, null, null );
108     }
109
110     void verifyRpcError( RpcResult<?> result, int errorIndex, ErrorSeverity expSeverity,
111             ErrorType expErrorType, String expTag, String expMessage, String expAppTag,
112             String expInfo, Throwable expCause ) {
113
114         List<RpcError> errors = new ArrayList<>( result.getErrors() );
115         assertTrue( "Expected error at index " + errorIndex + " not found",
116                     errorIndex < errors.size() );
117         RpcError error = errors.get( errorIndex );
118         assertEquals( "getSeverity", expSeverity, error.getSeverity() );
119         assertEquals( "getErrorType", expErrorType, error.getErrorType() );
120         assertEquals( "getTag", expTag, error.getTag() );
121         assertEquals( "getMessage", expMessage, error.getMessage() );
122         assertEquals( "getApplicationTag", expAppTag, error.getApplicationTag() );
123         assertEquals( "getInfo", expInfo, error.getInfo() );
124         assertEquals( "getCause", expCause, error.getCause() );
125     }
126
127     void verifyRpcResult( RpcResult<?> result, boolean expSuccess, Object expValue ) {
128         assertEquals( "isSuccessful", expSuccess, result.isSuccessful() );
129         assertEquals( "getResult", expValue, result.getResult() );
130     }
131 }