Skip to content

Commit e31e590

Browse files
committed
aadarsh-st/SK-2521-Fixed test cases and coverage
1 parent cdde053 commit e31e590

File tree

1 file changed

+299
-0
lines changed

1 file changed

+299
-0
lines changed

src/test/java/com/skyflow/vault/controller/ConnectionControllerTests.java

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
package com.skyflow.vault.controller;
22

3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
35
import com.skyflow.Skyflow;
46
import com.skyflow.config.ConnectionConfig;
57
import com.skyflow.config.Credentials;
68
import com.skyflow.enums.LogLevel;
9+
import com.skyflow.enums.RequestMethod;
710
import com.skyflow.errors.ErrorCode;
811
import com.skyflow.errors.ErrorMessage;
912
import com.skyflow.errors.SkyflowException;
13+
import com.skyflow.utils.HttpUtility;
1014
import com.skyflow.vault.connection.InvokeConnectionRequest;
15+
import com.skyflow.vault.connection.InvokeConnectionResponse;
1116
import org.junit.Assert;
17+
import org.junit.Before;
1218
import org.junit.BeforeClass;
1319
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.powermock.api.mockito.PowerMockito;
22+
import org.powermock.core.classloader.annotations.PrepareForTest;
23+
import org.powermock.modules.junit4.PowerMockRunner;
1424

25+
import java.net.URL;
1526
import java.util.HashMap;
27+
import java.util.Map;
1628

29+
import static org.mockito.ArgumentMatchers.*;
30+
import static org.powermock.api.mockito.PowerMockito.when;
31+
32+
@RunWith(PowerMockRunner.class)
33+
@PrepareForTest({HttpUtility.class})
1734
public class ConnectionControllerTests {
1835
private static final String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";
1936
private static final String EXCEPTION_NOT_THROWN = "Should have thrown an exception";
@@ -36,6 +53,12 @@ public static void setup() {
3653
connectionConfig.setCredentials(credentials);
3754
}
3855

56+
@Before
57+
public void setupMocks() throws Exception {
58+
PowerMockito.mockStatic(HttpUtility.class);
59+
when(HttpUtility.getRequestID()).thenReturn("test-request-id");
60+
}
61+
3962
@Test
4063
public void testInvalidRequestInInvokeConnectionMethod() {
4164
try {
@@ -138,4 +161,280 @@ public void testInvokeConnectionWithHTMLContentType() {
138161
Assert.fail(INVALID_EXCEPTION_THROWN);
139162
}
140163
}
164+
165+
// Tests for new content-type handling logic with actual controller invocation
166+
167+
@Test
168+
public void testInvokeConnectionWithStringBodyAndJsonContentType() throws Exception {
169+
String jsonResponse = "{\"success\":true}";
170+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
171+
.thenReturn(jsonResponse);
172+
173+
Skyflow client = Skyflow.builder()
174+
.setLogLevel(LogLevel.DEBUG)
175+
.addConnectionConfig(connectionConfig)
176+
.build();
177+
178+
String stringBody = "{\"key\":\"value\"}";
179+
Map<String, String> headers = new HashMap<>();
180+
headers.put("content-type", "application/json");
181+
182+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
183+
.method(RequestMethod.POST)
184+
.requestBody(stringBody)
185+
.requestHeaders(headers)
186+
.build();
187+
188+
InvokeConnectionResponse response = client.connection().invoke(request);
189+
Assert.assertNotNull(response);
190+
Assert.assertTrue(response.getData() instanceof JsonObject);
191+
}
192+
193+
@Test
194+
public void testInvokeConnectionWithStringBodyAndXmlContentType() throws Exception {
195+
String xmlResponse = "<response><status>success</status></response>";
196+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
197+
.thenReturn(xmlResponse);
198+
199+
Skyflow client = Skyflow.builder()
200+
.setLogLevel(LogLevel.DEBUG)
201+
.addConnectionConfig(connectionConfig)
202+
.build();
203+
204+
String xmlBody = "<xml><data>test</data></xml>";
205+
Map<String, String> headers = new HashMap<>();
206+
headers.put("content-type", "application/xml");
207+
208+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
209+
.method(RequestMethod.POST)
210+
.requestBody(xmlBody)
211+
.requestHeaders(headers)
212+
.build();
213+
214+
InvokeConnectionResponse response = client.connection().invoke(request);
215+
Assert.assertNotNull(response);
216+
// When response is not valid JSON, it should be returned as String
217+
Assert.assertTrue(response.getData() instanceof String);
218+
Assert.assertEquals(xmlResponse, response.getData());
219+
}
220+
221+
@Test
222+
public void testInvokeConnectionWithJsonObjectBody() throws Exception {
223+
String jsonResponse = "{\"result\":\"ok\"}";
224+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
225+
.thenReturn(jsonResponse);
226+
227+
Skyflow client = Skyflow.builder()
228+
.setLogLevel(LogLevel.DEBUG)
229+
.addConnectionConfig(connectionConfig)
230+
.build();
231+
232+
JsonObject jsonBody = new JsonObject();
233+
jsonBody.addProperty("test", "value");
234+
235+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
236+
.method(RequestMethod.POST)
237+
.requestBody(jsonBody)
238+
.build();
239+
240+
InvokeConnectionResponse response = client.connection().invoke(request);
241+
Assert.assertNotNull(response);
242+
Assert.assertTrue(response.getData() instanceof JsonObject);
243+
}
244+
245+
@Test
246+
public void testInvokeConnectionWithNullRequestBody() throws Exception {
247+
String jsonResponse = "{\"status\":\"success\"}";
248+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
249+
.thenReturn(jsonResponse);
250+
251+
Skyflow client = Skyflow.builder()
252+
.setLogLevel(LogLevel.DEBUG)
253+
.addConnectionConfig(connectionConfig)
254+
.build();
255+
256+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
257+
.method(RequestMethod.GET)
258+
.requestBody(null)
259+
.build();
260+
261+
InvokeConnectionResponse response = client.connection().invoke(request);
262+
Assert.assertNotNull(response);
263+
Assert.assertTrue(response.getData() instanceof JsonObject);
264+
}
265+
266+
@Test
267+
public void testInvokeConnectionWithNonJsonResponse() throws Exception {
268+
String plainTextResponse = "This is a plain text response";
269+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
270+
.thenReturn(plainTextResponse);
271+
272+
Skyflow client = Skyflow.builder()
273+
.setLogLevel(LogLevel.DEBUG)
274+
.addConnectionConfig(connectionConfig)
275+
.build();
276+
277+
JsonObject jsonBody = new JsonObject();
278+
jsonBody.addProperty("key", "value");
279+
280+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
281+
.method(RequestMethod.POST)
282+
.requestBody(jsonBody)
283+
.build();
284+
285+
InvokeConnectionResponse response = client.connection().invoke(request);
286+
Assert.assertNotNull(response);
287+
// Non-JSON response should be returned as String
288+
Assert.assertTrue(response.getData() instanceof String);
289+
Assert.assertEquals(plainTextResponse, response.getData());
290+
}
291+
292+
@Test
293+
public void testInvokeConnectionWithContentTypeCaseInsensitive() throws Exception {
294+
String jsonResponse = "{\"data\":\"test\"}";
295+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
296+
.thenReturn(jsonResponse);
297+
298+
Skyflow client = Skyflow.builder()
299+
.setLogLevel(LogLevel.DEBUG)
300+
.addConnectionConfig(connectionConfig)
301+
.build();
302+
303+
String stringBody = "test data";
304+
Map<String, String> headers = new HashMap<>();
305+
// Test with uppercase Content-Type
306+
headers.put("Content-Type", "APPLICATION/JSON");
307+
308+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
309+
.method(RequestMethod.POST)
310+
.requestBody(stringBody)
311+
.requestHeaders(headers)
312+
.build();
313+
314+
InvokeConnectionResponse response = client.connection().invoke(request);
315+
Assert.assertNotNull(response);
316+
Assert.assertTrue(response.getData() instanceof JsonObject);
317+
}
318+
319+
@Test
320+
public void testInvokeConnectionWithMixedCaseContentType() throws Exception {
321+
String xmlResponse = "<xml>data</xml>";
322+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
323+
.thenReturn(xmlResponse);
324+
325+
Skyflow client = Skyflow.builder()
326+
.setLogLevel(LogLevel.DEBUG)
327+
.addConnectionConfig(connectionConfig)
328+
.build();
329+
330+
String stringBody = "<data>test</data>";
331+
Map<String, String> headers = new HashMap<>();
332+
headers.put("content-type", "Application/Xml");
333+
334+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
335+
.method(RequestMethod.POST)
336+
.requestBody(stringBody)
337+
.requestHeaders(headers)
338+
.build();
339+
340+
InvokeConnectionResponse response = client.connection().invoke(request);
341+
Assert.assertNotNull(response);
342+
Assert.assertTrue(response.getData() instanceof String);
343+
}
344+
345+
@Test
346+
public void testInvokeConnectionWithMapRequestBody() throws Exception {
347+
String jsonResponse = "{\"created\":true}";
348+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
349+
.thenReturn(jsonResponse);
350+
351+
Skyflow client = Skyflow.builder()
352+
.setLogLevel(LogLevel.DEBUG)
353+
.addConnectionConfig(connectionConfig)
354+
.build();
355+
356+
Map<String, String> mapBody = new HashMap<>();
357+
mapBody.put("name", "John");
358+
mapBody.put("age", "30");
359+
360+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
361+
.method(RequestMethod.POST)
362+
.requestBody(mapBody)
363+
.build();
364+
365+
InvokeConnectionResponse response = client.connection().invoke(request);
366+
Assert.assertNotNull(response);
367+
Assert.assertTrue(response.getData() instanceof JsonObject);
368+
}
369+
370+
@Test
371+
public void testInvokeConnectionWithDefaultContentType() throws Exception {
372+
String jsonResponse = "{\"message\":\"success\"}";
373+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
374+
.thenReturn(jsonResponse);
375+
376+
Skyflow client = Skyflow.builder()
377+
.setLogLevel(LogLevel.DEBUG)
378+
.addConnectionConfig(connectionConfig)
379+
.build();
380+
381+
String stringBody = "string data";
382+
// No content-type header - should default to application/json
383+
384+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
385+
.method(RequestMethod.POST)
386+
.requestBody(stringBody)
387+
.build();
388+
389+
InvokeConnectionResponse response = client.connection().invoke(request);
390+
Assert.assertNotNull(response);
391+
Assert.assertTrue(response.getData() instanceof JsonObject);
392+
}
393+
394+
@Test
395+
public void testInvokeConnectionWithFormUrlEncodedContentType() throws Exception {
396+
String jsonResponse = "{\"submitted\":true}";
397+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
398+
.thenReturn(jsonResponse);
399+
400+
Skyflow client = Skyflow.builder()
401+
.setLogLevel(LogLevel.DEBUG)
402+
.addConnectionConfig(connectionConfig)
403+
.build();
404+
405+
String formBody = "param1=value1&param2=value2";
406+
Map<String, String> headers = new HashMap<>();
407+
headers.put("content-type", "application/x-www-form-urlencoded");
408+
409+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
410+
.method(RequestMethod.POST)
411+
.requestBody(formBody)
412+
.requestHeaders(headers)
413+
.build();
414+
415+
InvokeConnectionResponse response = client.connection().invoke(request);
416+
Assert.assertNotNull(response);
417+
Assert.assertTrue(response.getData() instanceof JsonObject);
418+
}
419+
420+
@Test
421+
public void testInvokeConnectionMetadataContainsRequestId() throws Exception {
422+
String jsonResponse = "{\"data\":\"value\"}";
423+
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(JsonObject.class), anyMap()))
424+
.thenReturn(jsonResponse);
425+
426+
Skyflow client = Skyflow.builder()
427+
.setLogLevel(LogLevel.DEBUG)
428+
.addConnectionConfig(connectionConfig)
429+
.build();
430+
431+
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
432+
.method(RequestMethod.GET)
433+
.build();
434+
435+
InvokeConnectionResponse response = client.connection().invoke(request);
436+
Assert.assertNotNull(response);
437+
Assert.assertNotNull(response.getMetadata());
438+
Assert.assertEquals("test-request-id", response.getMetadata().get("requestId"));
439+
}
141440
}

0 commit comments

Comments
 (0)