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