Merge "Improve codec performance"
[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.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.ObjectInputStream;
16 import java.io.ObjectOutputStream;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
22 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
23
24 /**
25  * Unit tests for RpcResultBuilder.
26  *
27  * @author Thomas Pantelis
28  */
29 public class RpcResultBuilderTest {
30
31     @Test
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() );
37
38         result = RpcResultBuilder.<String>success( "bar" ).build();
39         verifyRpcResult( result, true, "bar" );
40     }
41
42     @Test
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 )
51                   .build();
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() );
62     }
63
64     @Test
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 )
70                   .build();
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() );
77     }
78
79     @Test
80     public void testFrom() {
81         Throwable cause = new Throwable( "mock cause" );
82         RpcResult<String> result = RpcResultBuilder.<String>success()
83                 .withResult( "foo" )
84                 .withWarning( ErrorType.RPC, "in-use", "message", "my-app-tag", "my-info", cause )
85                 .build();
86
87         RpcResult<String> copy = RpcResultBuilder.<String>from( result )
88                 .withError( ErrorType.PROTOCOL, "error message" )
89                 .build();
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 );
95     }
96
97     @Test
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" )
103                 .build();
104
105         RpcResult<String> result2 = RpcResultBuilder.<String>failed()
106                 .withRpcErrors( result.getErrors() )
107                 .build();
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 );
112     }
113
114     @SuppressWarnings("unchecked")
115     @Test
116     public void testSerialization() throws Exception {
117         RpcResult<String> result = RpcResultBuilder.<String>success().withResult( "foo" ).build();
118
119         ByteArrayOutputStream bos = new ByteArrayOutputStream();
120         ObjectOutputStream out = new ObjectOutputStream(bos);
121         out.writeObject(result);
122
123         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
124         RpcResult<String> clone = (RpcResult<String>) in.readObject();
125
126         verifyRpcResult(clone, true, "foo");
127
128         Throwable cause = new Throwable( "mock cause" );
129         result = RpcResultBuilder.<String>failed()
130                 .withError( ErrorType.RPC, "in-use", "error message", "my-app-tag", "my-info", cause )
131                 .build();
132
133         bos = new ByteArrayOutputStream();
134         out = new ObjectOutputStream(bos);
135         out.writeObject(result);
136
137         in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
138         clone = (RpcResult<String>) in.readObject();
139
140         verifyRpcResult(clone, false, null);
141         verifyRpcError( result, 0, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
142                 "error message", "my-app-tag", "my-info", cause );
143     }
144
145     void verifyRpcError( RpcResult<?> result, int errorIndex, ErrorSeverity expSeverity,
146             ErrorType expErrorType, String expTag, String expMessage, String expAppTag,
147             String expInfo, Throwable expCause ) {
148
149         List<RpcError> errors = new ArrayList<>( result.getErrors() );
150         assertTrue( "Expected error at index " + errorIndex + " not found",
151                     errorIndex < errors.size() );
152         RpcError error = errors.get( errorIndex );
153         assertEquals( "getSeverity", expSeverity, error.getSeverity() );
154         assertEquals( "getErrorType", expErrorType, error.getErrorType() );
155         assertEquals( "getTag", expTag, error.getTag() );
156         assertEquals( "getMessage", expMessage, error.getMessage() );
157         assertEquals( "getApplicationTag", expAppTag, error.getApplicationTag() );
158         assertEquals( "getInfo", expInfo, error.getInfo() );
159         assertEquals( "getCause", expCause, error.getCause() );
160     }
161
162     void verifyRpcResult( RpcResult<?> result, boolean expSuccess, Object expValue ) {
163         assertEquals( "isSuccessful", expSuccess, result.isSuccessful() );
164         assertEquals( "getResult", expValue, result.getResult() );
165     }
166 }