Skip to content

Commit 8339ce7

Browse files
Merge pull request #145 from skyflowapi/SK-1737-fix-inconcistencies-in-java-sdk
SK-1737 Fix inconsistencies in Java SDK
2 parents 25e3118 + 038bd74 commit 8339ce7

File tree

8 files changed

+86
-28
lines changed

8 files changed

+86
-28
lines changed

src/main/java/com/skyflow/Skyflow.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,23 @@ public LogLevel getLogLevel() {
8282
return this.builder.logLevel;
8383
}
8484

85-
public VaultController vault() {
86-
String vaultId = (String) this.builder.vaultClientsMap.keySet().toArray()[0];
85+
public VaultController vault() throws SkyflowException {
86+
Object[] array = this.builder.vaultClientsMap.keySet().toArray();
87+
if (array.length < 1) {
88+
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
89+
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
90+
}
91+
String vaultId = (String) array[0];
8792
return this.vault(vaultId);
8893
}
8994

90-
public VaultController vault(String vaultId) {
91-
return this.builder.vaultClientsMap.get(vaultId);
95+
public VaultController vault(String vaultId) throws SkyflowException {
96+
VaultController controller = this.builder.vaultClientsMap.get(vaultId);
97+
if (controller == null) {
98+
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
99+
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
100+
}
101+
return controller;
92102
}
93103

94104
public ConnectionController connection() {

src/main/java/com/skyflow/utils/HttpUtility.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313

1414
public final class HttpUtility {
1515

16+
private static final String LINE_FEED = "\r\n";
1617
private static String requestID;
1718

1819
public static String getRequestID() {
1920
return requestID;
2021
}
2122

22-
private static final String LINE_FEED = "\r\n";
23-
2423
public static String sendRequest(String method, URL url, JsonObject params, Map<String, String> headers) throws IOException, SkyflowException {
2524

2625
HttpURLConnection connection = null;
@@ -55,7 +54,7 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
5554
input = formatJsonToFormEncodedString(params).getBytes(StandardCharsets.UTF_8);
5655
} else if (requestContentType.contains("multipart/form-data")) {
5756
input = formatJsonToMultiPartFormDataString(params, boundary).getBytes(StandardCharsets.UTF_8);
58-
}else {
57+
} else {
5958
input = params.toString().getBytes(StandardCharsets.UTF_8);
6059
}
6160

@@ -66,7 +65,7 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
6665

6766
int status = connection.getResponseCode();
6867
String requestID = connection.getHeaderField("x-request-id");
69-
HttpUtility.requestID = requestID;
68+
HttpUtility.requestID = requestID.split(",")[0];
7069

7170
Reader streamReader;
7271
if (status > 299) {

src/main/java/com/skyflow/utils/validations/Validations.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.skyflow.utils.validations;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
35
import com.skyflow.config.ConnectionConfig;
46
import com.skyflow.config.Credentials;
57
import com.skyflow.config.VaultConfig;
@@ -132,8 +134,9 @@ public static void validateInvokeConnectionRequest(InvokeConnectionRequest invok
132134
}
133135

134136
if (requestBody != null) {
135-
Map<String, String> requestBodyMap = (Map<String, String>) requestBody;
136-
if (requestBodyMap.isEmpty()) {
137+
Gson gson = new Gson();
138+
JsonObject bodyObject = gson.toJsonTree(requestBody).getAsJsonObject();
139+
if (bodyObject.isEmpty()) {
137140
LogUtil.printErrorLog(Utils.parameterizedString(
138141
ErrorLogs.EMPTY_REQUEST_BODY.getLog(), InterfaceName.INVOKE_CONNECTION.getName()));
139142
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyRequestBody.getMessage());
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
package com.skyflow.vault.connection;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
45
import com.google.gson.JsonObject;
5-
import com.google.gson.JsonParser;
66

77
public class InvokeConnectionResponse {
8-
private JsonObject response;
8+
private final JsonObject data;
9+
private final JsonObject metadata;
910

10-
public InvokeConnectionResponse(JsonObject response) {
11-
this.response = response;
11+
public InvokeConnectionResponse(JsonObject data, JsonObject metadata) {
12+
this.data = data;
13+
this.metadata = metadata;
1214
}
1315

14-
public JsonObject getResponse() {
15-
return response;
16+
public JsonObject getData() {
17+
return data;
18+
}
19+
20+
public JsonObject getMetadata() {
21+
return metadata;
1622
}
1723

1824
@Override
1925
public String toString() {
20-
Gson gson = new Gson();
21-
JsonObject responseObject = JsonParser.parseString(gson.toJson(this)).getAsJsonObject();
22-
responseObject = responseObject.remove("response").getAsJsonObject();
23-
return responseObject.toString();
26+
Gson gson = new GsonBuilder().serializeNulls().create();
27+
return gson.toJson(this);
2428
}
2529
}

src/main/java/com/skyflow/vault/controller/ConnectionController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ public InvokeConnectionResponse invoke(InvokeConnectionRequest invokeConnectionR
6464
}
6565

6666
String response = HttpUtility.sendRequest(requestMethod.name(), new URL(filledURL), requestBody, headers);
67-
connectionResponse = new InvokeConnectionResponse((JsonObject) JsonParser.parseString(response));
67+
JsonObject data = JsonParser.parseString(response).getAsJsonObject();
68+
JsonObject metadata = new JsonObject();
69+
metadata.addProperty("requestId", HttpUtility.getRequestID());
70+
connectionResponse = new InvokeConnectionResponse(data, metadata);
6871
LogUtil.printInfoLog(InfoLogs.INVOKE_CONNECTION_REQUEST_RESOLVED.getLog());
6972
} catch (IOException e) {
7073
LogUtil.printErrorLog(ErrorLogs.INVOKE_CONNECTION_REQUEST_REJECTED.getLog());

src/test/java/com/skyflow/SkyflowTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,40 @@ public void testGettingNonExistentVaultConfigInSkyflowClient() {
198198
}
199199
}
200200

201+
@Test
202+
public void testGettingAlreadyRemovedVaultFromEmptyConfigs() {
203+
try {
204+
Skyflow skyflowClient = Skyflow.builder().build();
205+
skyflowClient.vault();
206+
Assert.fail(EXCEPTION_NOT_THROWN);
207+
} catch (SkyflowException e) {
208+
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
209+
Assert.assertEquals(ErrorMessage.VaultIdNotInConfigList.getMessage(), e.getMessage());
210+
}
211+
}
212+
213+
@Test
214+
public void testGettingAlreadyRemovedVaultFromNonEmptyConfigs() {
215+
try {
216+
VaultConfig primaryConfig = new VaultConfig();
217+
primaryConfig.setVaultId(vaultID);
218+
primaryConfig.setClusterId(clusterID);
219+
primaryConfig.setEnv(Env.SANDBOX);
220+
221+
VaultConfig secondaryConfig = new VaultConfig();
222+
secondaryConfig.setVaultId(vaultID + "123");
223+
secondaryConfig.setClusterId(clusterID);
224+
secondaryConfig.setEnv(Env.SANDBOX);
225+
Skyflow skyflowClient = Skyflow.builder().addVaultConfig(primaryConfig).addVaultConfig(secondaryConfig).build();
226+
skyflowClient.removeVaultConfig(vaultID);
227+
skyflowClient.vault(vaultID);
228+
Assert.fail(EXCEPTION_NOT_THROWN);
229+
} catch (SkyflowException e) {
230+
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
231+
Assert.assertEquals(ErrorMessage.VaultIdNotInConfigList.getMessage(), e.getMessage());
232+
}
233+
}
234+
201235

202236
@Test
203237
public void testAddingInvalidConnectionConfigInSkyflowBuilder() {

src/test/java/com/skyflow/vault/connection/InvokeConnectionTests.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,17 @@ public void testEmptyRequestBodyInInvokeConnectionRequestValidations() {
420420
@Test
421421
public void testInvokeConnectionResponse() {
422422
try {
423-
JsonObject responseObject = new JsonObject();
424-
responseObject.addProperty("test_key_1", "test_value_1");
425-
responseObject.addProperty("test_key_2", "test_value_2");
426-
InvokeConnectionResponse connectionResponse = new InvokeConnectionResponse(responseObject);
427-
Assert.assertEquals(2, connectionResponse.getResponse().size());
428-
Assert.assertEquals(responseObject.toString(), connectionResponse.toString());
423+
JsonObject data = new JsonObject();
424+
data.addProperty("test_key_1", "test_value_1");
425+
data.addProperty("test_key_2", "test_value_2");
426+
JsonObject metadata = new JsonObject();
427+
metadata.addProperty("requestId", "12345");
428+
InvokeConnectionResponse connectionResponse = new InvokeConnectionResponse(data, metadata);
429+
String responseString = "{\"data\":{\"test_key_1\":\"test_value_1\",\"test_key_2\":\"test_value_2\"}," +
430+
"\"metadata\":{\"requestId\":\"12345\"}}";
431+
Assert.assertEquals(2, connectionResponse.getData().size());
432+
Assert.assertEquals(responseString, connectionResponse.toString());
433+
Assert.assertEquals(1, connectionResponse.getMetadata().size());
429434
} catch (Exception e) {
430435
Assert.fail(INVALID_EXCEPTION_THROWN);
431436
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void setup() {
3939
@Test
4040
public void testInvalidRequestInInvokeConnectionMethod() {
4141
try {
42-
HashMap<String,String> requestBody = new HashMap<>();
42+
HashMap<String, String> requestBody = new HashMap<>();
4343
InvokeConnectionRequest connectionRequest = InvokeConnectionRequest.builder().requestBody(requestBody).build();
4444
Skyflow skyflowClient = Skyflow.builder().setLogLevel(LogLevel.DEBUG).addConnectionConfig(connectionConfig).build();
4545
skyflowClient.connection().invoke(connectionRequest);

0 commit comments

Comments
 (0)