-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConnectionControllerTests.java
More file actions
358 lines (299 loc) · 14 KB
/
Copy pathConnectionControllerTests.java
File metadata and controls
358 lines (299 loc) · 14 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package com.skyflow.vault.controller;
import com.skyflow.Skyflow;
import com.skyflow.config.ConnectionConfig;
import com.skyflow.config.Credentials;
import com.skyflow.enums.LogLevel;
import com.skyflow.enums.RequestMethod;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.ErrorMessage;
import com.skyflow.errors.SkyflowException;
import com.skyflow.utils.HttpUtility;
import com.skyflow.vault.connection.InvokeConnectionRequest;
import com.skyflow.vault.connection.InvokeConnectionResponse;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({HttpUtility.class})
public class ConnectionControllerTests {
private static final String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";
private static final String EXCEPTION_NOT_THROWN = "Should have thrown an exception";
private static final String API_KEY = "sky-ab123-abcd1234cdef1234abcd4321cdef4321"; // gitleaks:allow
private static final String REQUEST_ID = "req-test-123";
private static ConnectionConfig connectionConfig;
private static Credentials credentials;
private ConnectionController controller;
@BeforeClass
public static void setupClass() {
credentials = new Credentials();
credentials.setApiKey(API_KEY);
connectionConfig = new ConnectionConfig();
connectionConfig.setConnectionId("conn123");
connectionConfig.setConnectionUrl("https://test.connection.url");
connectionConfig.setCredentials(credentials);
}
@Before
public void setup() {
controller = new ConnectionController(connectionConfig, credentials);
PowerMockito.mockStatic(HttpUtility.class);
}
// --- existing validation test (kept) ---
@Test
public void testInvalidRequestInInvokeConnectionMethod() {
try {
HashMap<String, String> requestBody = new HashMap<>();
InvokeConnectionRequest connectionRequest = InvokeConnectionRequest.builder().requestBody(requestBody).build();
Skyflow skyflowClient = Skyflow.builder().setLogLevel(LogLevel.DEBUG).addConnectionConfig(connectionConfig).build();
skyflowClient.connection().invoke(connectionRequest);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
Assert.assertEquals(ErrorMessage.EmptyRequestBody.getMessage(), e.getMessage());
}
}
// --- happy-path tests ---
@Test
public void testInvoke_successWithDefaultRequest() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"data\":\"test-value\"}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
InvokeConnectionRequest request = InvokeConnectionRequest.builder().build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
Assert.assertNotNull(response.getData());
}
@Test
public void testInvoke_successWithGetMethod() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"result\":\"ok\"}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.method(RequestMethod.GET)
.build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
}
@Test
public void testInvoke_successWithDeleteMethod() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"deleted\":true}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.method(RequestMethod.DELETE)
.build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
}
@Test
public void testInvoke_successWithPutMethod() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"updated\":true}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
Map<String, Object> body = new HashMap<>();
body.put("field", "value");
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.method(RequestMethod.PUT)
.requestBody(body)
.build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
}
@Test
public void testInvoke_successWithObjectBody() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"result\":\"ok\"}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
Map<String, Object> body = new HashMap<>();
body.put("card_number", "4111111111111111");
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.method(RequestMethod.POST)
.requestBody(body)
.build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
}
@Test
public void testInvoke_withPathParams() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"data\":\"ok\"}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
Map<String, String> pathParams = new HashMap<>();
pathParams.put("id", "record-123");
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.method(RequestMethod.GET)
.pathParams(pathParams)
.build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
}
@Test
public void testInvoke_withQueryParams() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"data\":\"ok\"}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
Map<String, String> queryParams = new HashMap<>();
queryParams.put("limit", "10");
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.method(RequestMethod.GET)
.queryParams(queryParams)
.build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
}
@Test
public void testInvoke_withRequestHeaders() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"ok\":true}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
Map<String, String> headers = new HashMap<>();
headers.put("x-custom-header", "custom-value");
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.method(RequestMethod.GET)
.requestHeaders(headers)
.build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
}
@Test
public void testInvoke_nonJsonResponseWrappedUnderResponseKey() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("plain-text-response");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
InvokeConnectionRequest request = InvokeConnectionRequest.builder().build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
Assert.assertNotNull(response.getData());
JsonObject data = JsonParser.parseString(response.getData().toString()).getAsJsonObject();
Assert.assertTrue(data.has("response"));
Assert.assertEquals("plain-text-response", data.get("response").getAsString());
}
@Test
public void testInvoke_responseContainsRequestId() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"data\":\"ok\"}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
InvokeConnectionRequest request = InvokeConnectionRequest.builder().build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
Assert.assertNotNull(response.getMetadata());
Assert.assertEquals(REQUEST_ID, response.getMetadata().get("requestId"));
}
@Test
public void testInvoke_errorsNullOnSuccess() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"data\":\"ok\"}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
InvokeConnectionRequest request = InvokeConnectionRequest.builder().build();
InvokeConnectionResponse response = controller.invoke(request);
Assert.assertNotNull(response);
Assert.assertNull(response.getErrors());
}
// --- error / validation-failure tests ---
@Test
public void testInvoke_ioExceptionThrowsSkyflowException() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenThrow(new IOException("connection refused"));
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
try {
InvokeConnectionRequest request = InvokeConnectionRequest.builder().build();
controller.invoke(request);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertNotNull(e.getMessage());
}
}
@Test
public void testInvoke_skyflowExceptionFromSendRequestPropagates() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenThrow(new SkyflowException("upstream error", new RuntimeException()));
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);
try {
InvokeConnectionRequest request = InvokeConnectionRequest.builder().build();
controller.invoke(request);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertNotNull(e.getMessage());
}
}
@Test
public void testInvoke_emptyRequestHeadersThrowsSkyflowException() {
try {
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.requestHeaders(new HashMap<>())
.build();
controller.invoke(request);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
Assert.assertEquals(ErrorMessage.EmptyRequestHeaders.getMessage(), e.getMessage());
}
}
@Test
public void testInvoke_emptyPathParamsThrowsSkyflowException() {
try {
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.pathParams(new HashMap<>())
.build();
controller.invoke(request);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
Assert.assertEquals(ErrorMessage.EmptyPathParams.getMessage(), e.getMessage());
}
}
@Test
public void testInvoke_emptyQueryParamsThrowsSkyflowException() {
try {
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.queryParams(new HashMap<>())
.build();
controller.invoke(request);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
Assert.assertEquals(ErrorMessage.EmptyQueryParams.getMessage(), e.getMessage());
}
}
@Test
public void testInvoke_emptyHashMapBodyThrowsSkyflowException() {
try {
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.requestBody(new HashMap<>())
.build();
controller.invoke(request);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
Assert.assertEquals(ErrorMessage.EmptyRequestBody.getMessage(), e.getMessage());
}
}
@Test
public void testInvoke_nullHeaderValueThrowsSkyflowException() {
try {
Map<String, String> headers = new HashMap<>();
headers.put("x-header", null);
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.requestHeaders(headers)
.build();
controller.invoke(request);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
Assert.assertEquals(ErrorMessage.InvalidRequestHeaders.getMessage(), e.getMessage());
}
}
}