45991b2e285a871aefc0e06c46eb314f6543dadb
[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 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.ObjectInputStream;
17 import java.io.ObjectOutputStream;
18 import java.util.ArrayList;
19 import java.util.List;
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.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.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     @Test
115     public void testErrors() {
116         final RpcResultBuilder<Object> rpcResultBuilder = RpcResultBuilder.status(true);
117         final RpcError rpcErrorShort = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "tag", "msg");
118         final RpcError rpcErrorLong = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "tag", "msg", "applicationTag",
119                 "info", null);
120         final RpcError rpcErrorShortWarn = RpcResultBuilder.newWarning(RpcError.ErrorType.RPC, "tag", "msg");
121         final RpcError rpcErrorLongWarn = RpcResultBuilder.newWarning(RpcError.ErrorType.RPC, "tag", "msg",
122                 "applicationTag",
123                 "info", null);
124         rpcResultBuilder.withRpcError(rpcErrorShort);
125         final RpcResult<Object> rpcResult = rpcResultBuilder.build();
126         final RpcResultBuilder<RpcResult<Object>> rpcResultRpcResultBuilder1 = RpcResultBuilder.success
127                 (rpcResultBuilder);
128         final RpcResultBuilder<RpcResult<Object>> rpcResultRpcResultBuilder2 = rpcResultRpcResultBuilder1.withResult
129                 (rpcResultBuilder);
130
131         assertEquals(rpcErrorShort.getErrorType(), rpcErrorShortWarn.getErrorType());
132         assertEquals(rpcErrorLong.getErrorType(), rpcErrorLongWarn.getErrorType());
133         assertEquals(rpcResultRpcResultBuilder1, rpcResultRpcResultBuilder2);
134         assertNotNull(rpcResultBuilder.buildFuture());
135         assertEquals("RpcResult [successful=true, result=null, errors=[RpcError [message=msg, severity=ERROR, " +
136                 "errorType=RPC, tag=tag, applicationTag=null, info=null, cause=null]]]", rpcResult.toString());
137     }
138
139     @SuppressWarnings("unchecked")
140     @Test
141     public void testSerialization() throws Exception {
142         RpcResult<String> result = RpcResultBuilder.<String>success().withResult( "foo" ).build();
143
144         ByteArrayOutputStream bos = new ByteArrayOutputStream();
145         ObjectOutputStream out = new ObjectOutputStream(bos);
146         out.writeObject(result);
147
148         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
149         RpcResult<String> clone = (RpcResult<String>) in.readObject();
150
151         verifyRpcResult(clone, true, "foo");
152
153         Throwable cause = new Throwable( "mock cause" );
154         result = RpcResultBuilder.<String>failed()
155                 .withError( ErrorType.RPC, "in-use", "error message", "my-app-tag", "my-info", cause )
156                 .build();
157
158         bos = new ByteArrayOutputStream();
159         out = new ObjectOutputStream(bos);
160         out.writeObject(result);
161
162         in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
163         clone = (RpcResult<String>) in.readObject();
164
165         verifyRpcResult(clone, false, null);
166         verifyRpcError( result, 0, ErrorSeverity.ERROR, ErrorType.RPC, "in-use",
167                 "error message", "my-app-tag", "my-info", cause );
168     }
169
170     void verifyRpcError( final RpcResult<?> result, final int errorIndex, final ErrorSeverity expSeverity,
171             final ErrorType expErrorType, final String expTag, final String expMessage, final String expAppTag,
172             final String expInfo, final Throwable expCause ) {
173
174         List<RpcError> errors = new ArrayList<>( result.getErrors() );
175         assertTrue( "Expected error at index " + errorIndex + " not found",
176                     errorIndex < errors.size() );
177         RpcError error = errors.get( errorIndex );
178         assertEquals( "getSeverity", expSeverity, error.getSeverity() );
179         assertEquals( "getErrorType", expErrorType, error.getErrorType() );
180         assertEquals( "getTag", expTag, error.getTag() );
181         assertEquals( "getMessage", expMessage, error.getMessage() );
182         assertEquals( "getApplicationTag", expAppTag, error.getApplicationTag() );
183         assertEquals( "getInfo", expInfo, error.getInfo() );
184         assertEquals( "getCause", expCause, error.getCause() );
185     }
186
187     void verifyRpcResult( final RpcResult<?> result, final boolean expSuccess, final Object expValue ) {
188         assertEquals( "isSuccessful", expSuccess, result.isSuccessful() );
189         assertEquals( "getResult", expValue, result.getResult() );
190     }
191 }