-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSkyflowExceptionTest.java
More file actions
218 lines (188 loc) · 9.86 KB
/
Copy pathSkyflowExceptionTest.java
File metadata and controls
218 lines (188 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.skyflow.errors;
import org.junit.Assert;
import org.junit.Test;
import java.util.*;
public class SkyflowExceptionTest {
@Test
public void testConstructorWithMessage() {
SkyflowException ex = new SkyflowException("Test message");
Assert.assertEquals("Test message", ex.getMessage());
Assert.assertNull(ex.getHttpStatus());
Assert.assertNull(ex.getGrpcCode());
}
@Test
public void testConstructorWithThrowable() {
Throwable cause = new RuntimeException("Root cause");
SkyflowException ex = new SkyflowException(cause);
Assert.assertEquals("Root cause", ex.getMessage());
}
@Test
public void testConstructorWithMessageAndCause() {
Throwable cause = new RuntimeException("Root cause");
SkyflowException ex = new SkyflowException("Test message", cause);
Assert.assertEquals("Test message", ex.getMessage());
}
@Test
public void testConstructorWithCodeAndMessage() {
SkyflowException ex = new SkyflowException(400, "Bad Request");
Assert.assertEquals(Integer.valueOf(400), Integer.valueOf(ex.getHttpCode()));
Assert.assertEquals("Bad Request", ex.getMessage());
Assert.assertEquals("Bad Request", ex.toString().contains("Bad Request") ? "Bad Request" : null);
Assert.assertNotNull(ex.getDetails());
Assert.assertEquals(0, ex.getDetails().size());
}
@Test
public void testToStringFormat() {
SkyflowException ex = new SkyflowException(404, "Not Found");
String str = ex.toString();
Assert.assertTrue(str.contains("httpCode: 404"));
Assert.assertTrue(str.contains("message: Not Found"));
}
@Test
public void testConstructorWithJsonErrorBodyArrayDetailsNonEmpty() {
String json = "{\"error\":{\"message\":\"json error\",\"grpc_code\":7,\"http_status\":\"NOT_FOUND\",\"details\":[{\"info\":\"detail1\"}]}}";
Map<String, List<String>> headers = new HashMap<>();
headers.put("x-request-id", Collections.singletonList("req-123"));
SkyflowException ex = new SkyflowException(404, new RuntimeException("fail"), headers, json);
Assert.assertEquals("json error", ex.getMessage());
Assert.assertEquals(Integer.valueOf(7), ex.getGrpcCode());
Assert.assertEquals("NOT_FOUND", ex.getHttpStatus());
Assert.assertEquals("req-123", ex.getRequestId());
Assert.assertNotNull(ex.getDetails());
Assert.assertTrue(ex.getDetails().size() > 0);
}
@Test
public void testConstructorWithJsonErrorBodyArrayDetailsEmpty() {
String json = "{\"error\":{\"message\":\"json error\",\"grpc_code\":7,\"http_status\":\"NOT_FOUND\",\"details\":[]}}";
Map<String, List<String>> headers = new HashMap<>();
SkyflowException ex = new SkyflowException(404, new RuntimeException("fail"), headers, json);
Assert.assertEquals("json error", ex.getMessage());
Assert.assertEquals(Integer.valueOf(7), ex.getGrpcCode());
Assert.assertEquals("NOT_FOUND", ex.getHttpStatus());
Assert.assertNotNull(ex.getDetails());
Assert.assertEquals(0, ex.getDetails().size());
}
@Test
public void testConstructorWithNullErrorBody() {
Map<String, List<String>> headers = new HashMap<>();
SkyflowException ex = new SkyflowException(500, new RuntimeException("fail"), headers, null);
Assert.assertEquals("fail", ex.getMessage());
Assert.assertNull(ex.getGrpcCode());
}
@Test
public void testGettersAndSetters() {
SkyflowException ex = new SkyflowException("msg");
Assert.assertNull(ex.getRequestId());
Assert.assertNull(ex.getDetails());
Assert.assertNull(ex.getGrpcCode());
Assert.assertNull(ex.getHttpStatus());
}
@Test
public void testSetDetailsWithErrorFromClientHeader() {
String json = "{\"error\":{\"message\":\"test error\",\"grpc_code\":13,\"details\":[]}}";
Map<String, List<String>> headers = new HashMap<>();
headers.put("error-from-client", Collections.singletonList("client error"));
SkyflowException ex = new SkyflowException(500, new RuntimeException("fail"), headers, json);
Assert.assertNotNull(ex.getDetails());
Assert.assertEquals(1, ex.getDetails().size());
Assert.assertEquals("client error", ex.getDetails().get(0).getAsJsonObject().get("errorFromClient").getAsString());
}
@Test
public void testSetDetailsWithErrorFromClientHeaderAndNullDetails() {
String json = "{\"error\":{\"message\":\"test error\",\"grpc_code\":13}}";
Map<String, List<String>> headers = new HashMap<>();
headers.put("error-from-client", Collections.singletonList("client error"));
SkyflowException ex = new SkyflowException(500, new RuntimeException("fail"), headers, json);
Assert.assertNotNull(ex.getDetails());
Assert.assertEquals(1, ex.getDetails().size());
Assert.assertEquals("client error", ex.getDetails().get(0).getAsJsonObject().get("errorFromClient").getAsString());
}
@Test
public void testSetDetailsWithNoErrorFromClientHeaderAndEmptyDetails() {
String json = "{\"error\":{\"message\":\"test error\",\"grpc_code\":13,\"details\":[]}}";
Map<String, List<String>> headers = new HashMap<>();
SkyflowException ex = new SkyflowException(500, new RuntimeException("fail"), headers, json);
Assert.assertNotNull(ex.getDetails());
Assert.assertEquals(0, ex.getDetails().size());
}
@Test
public void testToStringWithNullFields() {
SkyflowException ex = new SkyflowException("msg");
String str = ex.toString();
Assert.assertTrue(str.contains("requestId: null"));
Assert.assertTrue(str.contains("grpcCode: null"));
Assert.assertTrue(str.contains("httpCode: null"));
Assert.assertTrue(str.contains("httpStatus: null"));
Assert.assertTrue(str.contains("details: null"));
}
// Regression: skyvault used to ship its own copy of this class whose getHttpCode()
// unboxed the null Integer and threw NPE for every constructor that takes no code.
@Test
public void testGetHttpCodeIsZeroWhenNoCodeWasSet() {
Assert.assertEquals(0, new SkyflowException("local failure").getHttpCode());
Assert.assertEquals(0, new SkyflowException(new RuntimeException("boom")).getHttpCode());
Assert.assertEquals(0, new SkyflowException("local failure", new RuntimeException("boom")).getHttpCode());
}
@Test
public void testZeroHttpCodeDefaultsTo400() {
Map<String, List<String>> headers = new HashMap<>();
String json = "{\"error\":{\"message\":\"zero code\",\"grpc_code\":1,\"http_status\":\"BAD_REQUEST\"}}";
SkyflowException ex = new SkyflowException(0, new RuntimeException("fail"), headers, json);
Assert.assertEquals(400, ex.getHttpCode());
}
@Test
public void testJsonBodyWithoutErrorKey() {
Map<String, List<String>> headers = new HashMap<>();
headers.put("x-request-id", Collections.singletonList("req-no-error"));
String json = "{\"message\":\"no error key here\"}";
SkyflowException ex = new SkyflowException(400, new RuntimeException("fail"), headers, json);
Assert.assertEquals("req-no-error", ex.getRequestId());
Assert.assertNull(ex.getGrpcCode());
Assert.assertNull(ex.getMessage());
}
@Test
public void testJsonErrorBodyWithNoMessageField() {
Map<String, List<String>> headers = new HashMap<>();
String json = "{\"error\":{\"grpc_code\":3,\"http_status\":\"INVALID_ARGUMENT\"}}";
SkyflowException ex = new SkyflowException(400, new RuntimeException("fail"), headers, json);
Assert.assertNull(ex.getMessage());
Assert.assertEquals(Integer.valueOf(3), ex.getGrpcCode());
}
@Test
public void testJsonErrorBodyWithNoGrpcCodeField() {
Map<String, List<String>> headers = new HashMap<>();
String json = "{\"error\":{\"message\":\"some error\",\"http_status\":\"BAD_REQUEST\"}}";
SkyflowException ex = new SkyflowException(400, new RuntimeException("fail"), headers, json);
Assert.assertEquals("some error", ex.getMessage());
Assert.assertNull(ex.getGrpcCode());
}
@Test
public void testNonJsonBodyFallsBackToRawBodyAsMessage() {
Map<String, List<String>> headers = new HashMap<>();
String body = "plain text error response";
SkyflowException ex = new SkyflowException(500, new RuntimeException("fail"), headers, body);
Assert.assertEquals("plain text error response", ex.getMessage());
}
@Test
public void testJsonBodyWithGenericApiErrorEnvelopeStillWorks() {
Map<String, List<String>> headers = new HashMap<>();
String json = "{\"error\":{\"grpc_code\":5,\"http_code\":404,"
+ "\"message\":\"Invalid request. Vault not found for vaultID: vault123. "
+ "Specify a valid vaultID.\",\"http_status\":\"Not Found\",\"details\":[]}}";
SkyflowException ex = new SkyflowException(404, new RuntimeException("fail"), headers, json);
Assert.assertEquals("Invalid request. Vault not found for vaultID: vault123. "
+ "Specify a valid vaultID.", ex.getMessage());
Assert.assertEquals(Integer.valueOf(5), ex.getGrpcCode());
Assert.assertEquals("Not Found", ex.getHttpStatus());
Assert.assertEquals(404, ex.getHttpCode());
Assert.assertNotNull(ex.getDetails());
Assert.assertEquals(0, ex.getDetails().size());
}
@Test
public void testNullBodyNullCauseMessageFallsBackToErrorOccurred() {
Map<String, List<String>> headers = new HashMap<>();
SkyflowException ex = new SkyflowException(500, new RuntimeException((String) null), headers, null);
Assert.assertNotNull(ex.getMessage());
Assert.assertTrue(ex.getMessage().contains("API error"));
}
}