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