Do no use Builder concept in RpcResultBuilder
[yangtools.git] / common / 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 package org.opendaylight.yangtools.yang.common;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
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;
20
21 /**
22  * Unit tests for RpcResultBuilder.
23  *
24  * @author Thomas Pantelis
25  */
26 public class RpcResultBuilderTest {
27     private static final ErrorTag TAG = new ErrorTag("tag");
28
29     @Test
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());
35
36         result = RpcResultBuilder.success("bar").build();
37         verifyRpcResult(result, true, "bar");
38     }
39
40     @Test
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)
49                   .build();
50         verifyRpcResult(result, false, null);
51         verifyRpcError(result, 0, ErrorSeverity.ERROR, ErrorType.PROTOCOL, ErrorTag.OPERATION_FAILED, "error message 1",
52                         null, null, null);
53         verifyRpcError(result, 1, ErrorSeverity.ERROR, ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "error message 2",
54                         null, null, null);
55         verifyRpcError(result, 2, ErrorSeverity.ERROR, ErrorType.RPC, ErrorTag.IN_USE, "error message 3", "my-app-tag",
56                         "my-info", cause);
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());
60     }
61
62     @Test
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)
68                   .build();
69         verifyRpcResult(result, true, null);
70         verifyRpcError(result, 0, ErrorSeverity.WARNING, ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "message 1", null,
71                         null, null);
72         verifyRpcError(result, 1, ErrorSeverity.WARNING, ErrorType.RPC, ErrorTag.IN_USE, "message 2", "my-app-tag",
73                         "my-info", cause);
74         assertEquals("getErrors size", 2, result.getErrors().size());
75     }
76
77     @Test
78     public void testFrom() {
79         Throwable cause = new Throwable("mock cause");
80         RpcResult<String> result = RpcResultBuilder.<String>success()
81                 .withResult("foo")
82                 .withWarning(ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag", "my-info", cause)
83                 .build();
84
85         RpcResult<String> copy = RpcResultBuilder.from(result)
86                 .withError(ErrorType.PROTOCOL, "error message")
87                 .build();
88         verifyRpcResult(copy, true, "foo");
89         verifyRpcError(copy, 0, ErrorSeverity.WARNING, ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag",
90                         "my-info", cause);
91         verifyRpcError(copy, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, ErrorTag.OPERATION_FAILED, "error message",
92                         null, null, null);
93     }
94
95     @Test
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")
101                 .build();
102
103         RpcResult<String> result2 = RpcResultBuilder.<String>failed()
104                 .withRpcErrors(result.getErrors())
105                 .build();
106         verifyRpcError(result2, 0, ErrorSeverity.WARNING, ErrorType.RPC, ErrorTag.IN_USE, "message", "my-app-tag",
107                 "my-info", cause);
108         verifyRpcError(result2, 1, ErrorSeverity.ERROR, ErrorType.PROTOCOL, ErrorTag.OPERATION_FAILED, "error message",
109                 null, null, null);
110     }
111
112     @Test
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",
117             null);
118         final RpcError rpcErrorShortWarn = RpcResultBuilder.newWarning(ErrorType.RPC, TAG, "msg");
119         final RpcError rpcErrorLongWarn = RpcResultBuilder.newWarning(ErrorType.RPC, TAG, "msg", "applicationTag",
120             "info", null);
121         rpcResultBuilder.withRpcError(rpcErrorShort);
122         final RpcResult<Object> rpcResult = rpcResultBuilder.build();
123
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());
129     }
130
131     @SuppressWarnings("unchecked")
132     @Test
133     public void testSerialization() throws Exception {
134         RpcResult<String> result = RpcResultBuilder.<String>success().withResult("foo").build();
135
136         ByteArrayOutputStream bos = new ByteArrayOutputStream();
137         ObjectOutputStream out = new ObjectOutputStream(bos);
138         out.writeObject(result);
139
140         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
141         RpcResult<String> clone = (RpcResult<String>) in.readObject();
142
143         verifyRpcResult(clone, true, "foo");
144
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)
148                 .build();
149
150         bos = new ByteArrayOutputStream();
151         out = new ObjectOutputStream(bos);
152         out.writeObject(result);
153
154         in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
155         clone = (RpcResult<String>) in.readObject();
156
157         verifyRpcResult(clone, false, null);
158         verifyRpcError(result, 0, ErrorSeverity.ERROR, ErrorType.RPC, ErrorTag.IN_USE, "error message", "my-app-tag",
159             "my-info", cause);
160     }
161
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) {
165
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());
176     }
177
178     void verifyRpcResult(final RpcResult<?> result, final boolean expSuccess, final Object expValue) {
179         assertEquals("isSuccessful", expSuccess, result.isSuccessful());
180         assertEquals("getResult", expValue, result.getResult());
181     }
182 }