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.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import java.util.List;
20 import org.junit.jupiter.api.Test;
23 * Unit tests for RpcResultBuilder.
25 * @author Thomas Pantelis
27 public class RpcResultBuilderTest {
28 private static final ErrorTag TAG = new ErrorTag("tag");
31 public void testSuccess() {
32 RpcResult<String> result = RpcResultBuilder.<String>success().withResult("foo").build();
33 verifyRpcResult(result, true, "foo");
34 assertEquals(List.of(), result.getErrors());
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(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(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 assertThat(errors.size(), greaterThanOrEqualTo(errorIndex));
169 RpcError error = errors.get(errorIndex);
170 assertEquals(expSeverity, error.getSeverity());
171 assertEquals(expErrorType, error.getErrorType());
172 assertEquals(expTag, error.getTag());
173 assertEquals(expMessage, error.getMessage());
174 assertEquals(expAppTag, error.getApplicationTag());
175 assertEquals(expInfo, error.getInfo());
176 assertEquals(expCause, error.getCause());
179 void verifyRpcResult(final RpcResult<?> result, final boolean expSuccess, final Object expValue) {
180 assertEquals(expSuccess, result.isSuccessful());
181 assertEquals(expValue, result.getResult());