Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/main/java/com/skyflow/Skyflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,23 @@ public LogLevel getLogLevel() {
return this.builder.logLevel;
}

public VaultController vault() {
String vaultId = (String) this.builder.vaultClientsMap.keySet().toArray()[0];
public VaultController vault() throws SkyflowException {
Object[] array = this.builder.vaultClientsMap.keySet().toArray();
if (array.length < 1) {
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
}
String vaultId = (String) array[0];
return this.vault(vaultId);
}

public VaultController vault(String vaultId) {
return this.builder.vaultClientsMap.get(vaultId);
public VaultController vault(String vaultId) throws SkyflowException {
VaultController controller = this.builder.vaultClientsMap.get(vaultId);
if (controller == null) {
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
}
return controller;
}

public ConnectionController connection() {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/skyflow/utils/HttpUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@

public final class HttpUtility {

private static final String LINE_FEED = "\r\n";
private static String requestID;

public static String getRequestID() {
return requestID;
}

private static final String LINE_FEED = "\r\n";

public static String sendRequest(String method, URL url, JsonObject params, Map<String, String> headers) throws IOException, SkyflowException {

HttpURLConnection connection = null;
Expand Down Expand Up @@ -55,7 +54,7 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
input = formatJsonToFormEncodedString(params).getBytes(StandardCharsets.UTF_8);
} else if (requestContentType.contains("multipart/form-data")) {
input = formatJsonToMultiPartFormDataString(params, boundary).getBytes(StandardCharsets.UTF_8);
}else {
} else {
input = params.toString().getBytes(StandardCharsets.UTF_8);
}

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

int status = connection.getResponseCode();
String requestID = connection.getHeaderField("x-request-id");
HttpUtility.requestID = requestID;
HttpUtility.requestID = requestID.split(",")[0];

Reader streamReader;
if (status > 299) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/skyflow/utils/validations/Validations.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.skyflow.utils.validations;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.skyflow.config.ConnectionConfig;
import com.skyflow.config.Credentials;
import com.skyflow.config.VaultConfig;
Expand Down Expand Up @@ -132,8 +134,9 @@ public static void validateInvokeConnectionRequest(InvokeConnectionRequest invok
}

if (requestBody != null) {
Map<String, String> requestBodyMap = (Map<String, String>) requestBody;
if (requestBodyMap.isEmpty()) {
Gson gson = new Gson();
JsonObject bodyObject = gson.toJsonTree(requestBody).getAsJsonObject();
if (bodyObject.isEmpty()) {
LogUtil.printErrorLog(Utils.parameterizedString(
ErrorLogs.EMPTY_REQUEST_BODY.getLog(), InterfaceName.INVOKE_CONNECTION.getName()));
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyRequestBody.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package com.skyflow.vault.connection;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class InvokeConnectionResponse {
private JsonObject response;
private final JsonObject data;
private final JsonObject metadata;

public InvokeConnectionResponse(JsonObject response) {
this.response = response;
public InvokeConnectionResponse(JsonObject data, JsonObject metadata) {
this.data = data;
this.metadata = metadata;
}

public JsonObject getResponse() {
return response;
public JsonObject getData() {
return data;
}

public JsonObject getMetadata() {
return metadata;
}

@Override
public String toString() {
Gson gson = new Gson();
JsonObject responseObject = JsonParser.parseString(gson.toJson(this)).getAsJsonObject();
responseObject = responseObject.remove("response").getAsJsonObject();
return responseObject.toString();
Gson gson = new GsonBuilder().serializeNulls().create();
return gson.toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
}

String response = HttpUtility.sendRequest(requestMethod.name(), new URL(filledURL), requestBody, headers);
connectionResponse = new InvokeConnectionResponse((JsonObject) JsonParser.parseString(response));
JsonObject data = JsonParser.parseString(response).getAsJsonObject();
JsonObject metadata = new JsonObject();
metadata.addProperty("requestId", HttpUtility.getRequestID());
connectionResponse = new InvokeConnectionResponse(data, metadata);

Check warning on line 70 in src/main/java/com/skyflow/vault/controller/ConnectionController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/skyflow/vault/controller/ConnectionController.java#L67-L70

Added lines #L67 - L70 were not covered by tests
LogUtil.printInfoLog(InfoLogs.INVOKE_CONNECTION_REQUEST_RESOLVED.getLog());
} catch (IOException e) {
LogUtil.printErrorLog(ErrorLogs.INVOKE_CONNECTION_REQUEST_REJECTED.getLog());
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/skyflow/SkyflowTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,40 @@ public void testGettingNonExistentVaultConfigInSkyflowClient() {
}
}

@Test
public void testGettingAlreadyRemovedVaultFromEmptyConfigs() {
try {
Skyflow skyflowClient = Skyflow.builder().build();
skyflowClient.vault();
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
Assert.assertEquals(ErrorMessage.VaultIdNotInConfigList.getMessage(), e.getMessage());
}
}

@Test
public void testGettingAlreadyRemovedVaultFromNonEmptyConfigs() {
try {
VaultConfig primaryConfig = new VaultConfig();
primaryConfig.setVaultId(vaultID);
primaryConfig.setClusterId(clusterID);
primaryConfig.setEnv(Env.SANDBOX);

VaultConfig secondaryConfig = new VaultConfig();
secondaryConfig.setVaultId(vaultID + "123");
secondaryConfig.setClusterId(clusterID);
secondaryConfig.setEnv(Env.SANDBOX);
Skyflow skyflowClient = Skyflow.builder().addVaultConfig(primaryConfig).addVaultConfig(secondaryConfig).build();
skyflowClient.removeVaultConfig(vaultID);
skyflowClient.vault(vaultID);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
Assert.assertEquals(ErrorMessage.VaultIdNotInConfigList.getMessage(), e.getMessage());
}
}


@Test
public void testAddingInvalidConnectionConfigInSkyflowBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,17 @@ public void testEmptyRequestBodyInInvokeConnectionRequestValidations() {
@Test
public void testInvokeConnectionResponse() {
try {
JsonObject responseObject = new JsonObject();
responseObject.addProperty("test_key_1", "test_value_1");
responseObject.addProperty("test_key_2", "test_value_2");
InvokeConnectionResponse connectionResponse = new InvokeConnectionResponse(responseObject);
Assert.assertEquals(2, connectionResponse.getResponse().size());
Assert.assertEquals(responseObject.toString(), connectionResponse.toString());
JsonObject data = new JsonObject();
data.addProperty("test_key_1", "test_value_1");
data.addProperty("test_key_2", "test_value_2");
JsonObject metadata = new JsonObject();
metadata.addProperty("requestId", "12345");
InvokeConnectionResponse connectionResponse = new InvokeConnectionResponse(data, metadata);
String responseString = "{\"data\":{\"test_key_1\":\"test_value_1\",\"test_key_2\":\"test_value_2\"}," +
"\"metadata\":{\"requestId\":\"12345\"}}";
Assert.assertEquals(2, connectionResponse.getData().size());
Assert.assertEquals(responseString, connectionResponse.toString());
Assert.assertEquals(1, connectionResponse.getMetadata().size());
} catch (Exception e) {
Assert.fail(INVALID_EXCEPTION_THROWN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void setup() {
@Test
public void testInvalidRequestInInvokeConnectionMethod() {
try {
HashMap<String,String> requestBody = new HashMap<>();
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);
Expand Down
Loading