2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 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.List;
19 import org.junit.Test;
22 * Unit tests for RpcResultBuilder.
24 * @author Thomas Pantelis
26 public class RpcResultBuilderTest {
27 private static final ErrorTag TAG = new ErrorTag("tag");
30 public void testSuccess() {
31 RpcResult<String> result = RpcResultBuilder.<String>success().withResult("foo").build();
32 verifyRpcResult(result, true, "foo");
33 assertNotNull("getErrors returned null", result.getErrors());
34 assertEquals("getErrors size", 0, result.getErrors().size());
36 result = RpcResultBuilder.success("bar").build();
37 verifyRpcResult(result, true, "bar");
41 public void testFailed() {
42 Throwable cause = new Throwable("mock cause");
43 Throwable cause2 = new Throwable("mock cause2");
44 RpcResult<String> result = RpcResultBuilder.<String>failed()
45 .withError(ErrorType.PROTOCOL, "error message 1")
46 .withError(ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "error message 2")
47 .withError(ErrorType.RPC, ErrorTag.IN_USE, "error message 3", "my-app-tag", "my-info", cause)
48 .withError(ErrorType.TRANSPORT, "error message 4", cause2)
50 verifyRpcResult(result, false, null);
51 verifyRpcError(result, 0, ErrorSeverity.ERROR, ErrorType.PROTOCOL, ErrorTag.OPERATION_FAILED, "error message 1",
53 verifyRpcError(result, 1, ErrorSeverity.ERROR, ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "error message 2",
55 verifyRpcError(result, 2, ErrorSeverity.ERROR, ErrorType.RPC, ErrorTag.IN_USE, "error message 3", "my-app-tag",
57 verifyRpcError(result, 3, ErrorSeverity.ERROR, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED,
58 "error message 4", null, null, cause2);
59 assertEquals("getErrors size", 4, result.getErrors().size());
63 public void testWithWarnings() {
64 Throwable cause = new Throwable("mock cause");
65 RpcResult<String> result = RpcResultBuilder.<String>success()
66 .withWarning(ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "message 1")
67 .withWarning(ErrorType.RPC, ErrorTag.IN_USE, "message 2", "my-app-tag", "my-info", cause)
69 verifyRpcResult(result, true, null);
70 verifyRpcError(result, 0, ErrorSeverity.WARNING, ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "message 1", null,
72 verifyRpcError(result, 1, ErrorSeverity.WARNING, ErrorType.RPC, ErrorTag.IN_USE, "message 2", "my-app-tag",
74 assertEquals("getErrors size", 2, result.getErrors().size());
78 public void testFrom() {
79 Throwable cause = new Throwable("mock cause");
80 RpcResult<String> result = RpcResultBuilder.<String>success()
82 .withWarning(ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag", "my-info", cause)
85 RpcResult<String> copy = RpcResultBuilder.from(result)
86 .withError(ErrorType.PROTOCOL, "error message")
88 verifyRpcResult(copy, true, "foo");
89 verifyRpcError(copy, 0, ErrorSeverity.WARNING, ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag",
91 verifyRpcError(copy, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, ErrorTag.OPERATION_FAILED, "error message",
96 public void testWithRpcErrors() {
97 Throwable cause = new Throwable("mock cause");
98 RpcResult<String> result = RpcResultBuilder.<String>failed()
99 .withWarning(ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag", "my-info", cause)
100 .withError(ErrorType.PROTOCOL, "error message")
103 RpcResult<String> result2 = RpcResultBuilder.<String>failed()
104 .withRpcErrors(result.getErrors())
106 verifyRpcError(result2, 0, ErrorSeverity.WARNING, ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag",
108 verifyRpcError(result2, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, ErrorTag.OPERATION_FAILED, "error message",
113 public void testErrors() {
114 final RpcResultBuilder<Object> rpcResultBuilder = RpcResultBuilder.status(true);
115 final RpcError rpcErrorShort = RpcResultBuilder.newError(ErrorType.RPC, TAG, "msg");
116 final RpcError rpcErrorLong = RpcResultBuilder.newError(ErrorType.RPC, TAG, "msg", "applicationTag", "info",
118 final RpcError rpcErrorShortWarn = RpcResultBuilder.newWarning(ErrorType.RPC, TAG, "msg");
119 final RpcError rpcErrorLongWarn = RpcResultBuilder.newWarning(ErrorType.RPC, TAG, "msg", "applicationTag",
121 rpcResultBuilder.withRpcError(rpcErrorShort);
122 final RpcResult<Object> rpcResult = rpcResultBuilder.build();
124 assertEquals(rpcErrorShort.getErrorType(), rpcErrorShortWarn.getErrorType());
125 assertEquals(rpcErrorLong.getErrorType(), rpcErrorLongWarn.getErrorType());
126 assertNotNull(rpcResultBuilder.buildFuture());
127 assertEquals("RpcResult [successful=true, result=null, errors=[RpcError [message=msg, severity=ERROR, "
128 + "errorType=RPC, tag=tag, applicationTag=null, info=null, cause=null]]]", rpcResult.toString());
131 @SuppressWarnings("unchecked")
133 public void testSerialization() throws Exception {
134 RpcResult<String> result = RpcResultBuilder.<String>success().withResult("foo").build();
136 ByteArrayOutputStream bos = new ByteArrayOutputStream();
137 ObjectOutputStream out = new ObjectOutputStream(bos);
138 out.writeObject(result);
140 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
141 RpcResult<String> clone = (RpcResult<String>) in.readObject();
143 verifyRpcResult(clone, true, "foo");
145 Throwable cause = new Throwable("mock cause");
146 result = RpcResultBuilder.<String>failed()
147 .withError(ErrorType.RPC, ErrorTag.IN_USE, "error message", "my-app-tag", "my-info", cause)
150 bos = new ByteArrayOutputStream();
151 out = new ObjectOutputStream(bos);
152 out.writeObject(result);
154 in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
155 clone = (RpcResult<String>) in.readObject();
157 verifyRpcResult(clone, false, null);
158 verifyRpcError(result, 0, ErrorSeverity.ERROR, ErrorType.RPC, ErrorTag.IN_USE, "error message", "my-app-tag",
162 void verifyRpcError(final RpcResult<?> result, final int errorIndex, final ErrorSeverity expSeverity,
163 final ErrorType expErrorType, final ErrorTag expTag, final String expMessage, final String expAppTag,
164 final String expInfo, final Throwable expCause) {
166 List<RpcError> errors = result.getErrors();
167 assertTrue("Expected error at index " + errorIndex + " not found", errorIndex < errors.size());
168 RpcError error = errors.get(errorIndex);
169 assertEquals("getSeverity", expSeverity, error.getSeverity());
170 assertEquals("getErrorType", expErrorType, error.getErrorType());
171 assertEquals("getTag", expTag, error.getTag());
172 assertEquals("getMessage", expMessage, error.getMessage());
173 assertEquals("getApplicationTag", expAppTag, error.getApplicationTag());
174 assertEquals("getInfo", expInfo, error.getInfo());
175 assertEquals("getCause", expCause, error.getCause());
178 void verifyRpcResult(final RpcResult<?> result, final boolean expSuccess, final Object expValue) {
179 assertEquals("isSuccessful", expSuccess, result.isSuccessful());
180 assertEquals("getResult", expValue, result.getResult());