Skip to content

Commit

Permalink
Merge pull request #84 from skyflowapi/release/23.6.2
Browse files Browse the repository at this point in the history
SK-816 Release/23.6.2
  • Loading branch information
skyflow-bharti authored Oct 25, 2023
2 parents 24d213b + 3983fd1 commit 3e9fb70
Show file tree
Hide file tree
Showing 12 changed files with 613 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.skyflow</groupId>
<artifactId>skyflow-java</artifactId>
<version>1.11.0</version>
<version>1.12.0</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/skyflow/common/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class Constants {

public static final String SDK_METRICS_HEADER_KEY = "sky-metadata";
public static final String SDK_VERSION = "1.11.0";
public static final String SDK_VERSION = "1.12.0";

}
9 changes: 6 additions & 3 deletions src/main/java/com/skyflow/common/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static StringBuilder constructGetByIdRequestURLParams(GetByIdRecordInput
return paramsList;
}

public static StringBuilder constructGetRequestURLParams(GetRecordInput record) {
public static StringBuilder constructGetRequestURLParams(GetRecordInput record, GetOptions getOptions) {
StringBuilder paramsList = new StringBuilder();

if (record.getIds() != null) {
Expand All @@ -162,8 +162,11 @@ public static StringBuilder constructGetRequestURLParams(GetRecordInput record)
paramsList.append("column_values=").append(value).append("&");
}
}

paramsList.append("redaction=").append(record.getRedaction());
if(record.getRedaction() == null && getOptions.getOptionToken() == true){
paramsList.append("tokenization=").append(getOptions.getOptionToken());
} else {
paramsList.append("redaction=").append(record.getRedaction());
}
return paramsList;
}

Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/skyflow/common/utils/Validators.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,31 +108,49 @@ public static void validateGetByIdRequestRecord(GetByIdRecordInput record) throw
}
}

public static void validateGetRequestRecord(GetRecordInput record) throws SkyflowException {
public static void validateGetRequestRecord(GetRecordInput record, GetOptions getOptions) throws SkyflowException {
String[] ids = record.getIds();
String table = record.getTable();
String columnName = record.getColumnName();
String[] columnValues = record.getColumnValues();
RedactionType redaction = record.getRedaction();

if (table == null || table.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.InvalidTable.getLog());
throw new SkyflowException(ErrorCode.InvalidTable);
}
if (getOptions.getOptionToken() == false && redaction == null) {
LogUtil.printErrorLog((ErrorLogs.MissingRedaction.getLog()));
throw new SkyflowException(ErrorCode.MissingRedaction);
}

if (ids == null && columnName == null && columnValues == null) {
LogUtil.printErrorLog(ErrorLogs.MissingIdAndColumnName.getLog());
throw new SkyflowException(ErrorCode.MissingIdAndColumnName);
}
if( ids != null && columnName != null) {
LogUtil.printErrorLog(ErrorLogs.SkyflowIdAndColumnNameBothSpecified.getLog());
throw new SkyflowException(ErrorCode.SkyflowIdAndColumnNameBothSpecified);
}

if (columnName != null && columnValues == null) {
LogUtil.printErrorLog(ErrorLogs.MissingRecordColumnValue.getLog());
throw new SkyflowException(ErrorCode.MissingRecordColumnValue);
}

if (columnName == null && columnValues != null) {
LogUtil.printErrorLog(ErrorLogs.MissingRecordColumnName.getLog());
throw new SkyflowException(ErrorCode.MissingRecordColumnName);
}
if (getOptions.getOptionToken() == true) {
if (columnName != null || columnValues != null) {
LogUtil.printErrorLog(ErrorLogs.TokensGetColumnNotSupported.getLog());
throw new SkyflowException(ErrorCode.TokensGetColumnNotSupported);
}
if (redaction != null) {
LogUtil.printErrorLog(ErrorLogs.RedactionWithTokenNotSupported.getLog());
throw new SkyflowException(ErrorCode.RedactionWithTokenNotSupported);
}
}
}
public static void validateDeleteBySkyflowId(DeleteRecordInput deleteRecordInput) throws SkyflowException{
String table = deleteRecordInput.getTable();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/skyflow/entities/GetOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.skyflow.entities;

public class GetOptions {
private boolean tokens;

public GetOptions(){
this.tokens = false;
}
public GetOptions(boolean tokens){
this.tokens = tokens;
}

public boolean getOptionToken(){
return tokens;
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/skyflow/errors/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum ErrorCode {
InvalidGetByIdInput(400, "Invalid getById input"),
InvalidGetInput(400, "Invalid get input"),
MissingIdAndColumnName(400, "Provide either Ids or column name to get records."),
SkyflowIdAndColumnNameBothSpecified(400, "ids and columnName can not be specified together."),
MissingRecordColumnValue(400, "Column Values can not be empty when Column Name is specified."),
MissingRecordColumnName(400, "Column Name can not be empty when Column Values are specified."),
ResponseParsingError(500, "Unable to parse response"),
Expand All @@ -54,7 +55,10 @@ public enum ErrorCode {
InvalidUpsertOptionType(400, "upsert options should be an non empty UpsertOption array."),
InvalidUpsertObjectType(400, "upsert option cannot be null, should be an UpsertOption object."),
InvalidTableInUpsertOption(400, "Invalid table in upsert object, non empty string is required."),
InvalidColumnInUpsertOption(400, "Invalid column in upsert object, non empty string is required.");
InvalidColumnInUpsertOption(400, "Invalid column in upsert object, non empty string is required."),
MissingRedaction(400, "Missing Redaction Property"),
TokensGetColumnNotSupported(400,"Interface: get method - column_name or column_values cannot be used with tokens in options."),
RedactionWithTokenNotSupported(400, "Interface: get method - redaction cannot be used when tokens are true in options.");

private final int code;
private final String description;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/skyflow/logs/ErrorLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum ErrorLogs {
InvalidGetByIdInput("Invalid getById input"),
InvalidGetInput("Invalid get input"),
MissingIdAndColumnName("Provide either Ids or column name to get records."),
SkyflowIdAndColumnNameBothSpecified("ids and columnName can not be specified together."),
MissingRecordColumnValue("Column Values can not be empty when Column Name is specified."),
MissingRecordColumnName("Column Name can not be empty when Column Values are specified."),
InvalidInvokeConnectionInput("Invalid invokeConnection Input"),
Expand Down Expand Up @@ -51,6 +52,9 @@ public enum ErrorLogs {
InvalidUpsertObjectType("upsert option cannot be null, should be an UpsertOption object."),
InvalidSkyflowId("Skyflow Id is missing"),
InvalidField("Fields missing"),
MissingRedaction("Missing Redaction property."),
TokensGetColumnNotSupported("Interface: get method - column_name or column_values cannot be used with tokens in options."),
RedactionWithTokenNotSupported("Interface: get method - redaction cannot be used when tokens are true in options."),
InvalidToken("Invalid Token value");

private final String log;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/skyflow/vault/Get.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.skyflow.common.utils.HttpUtility;
import com.skyflow.common.utils.LogUtil;
import com.skyflow.common.utils.Validators;
import com.skyflow.entities.GetOptions;
import com.skyflow.entities.GetRecordInput;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.SkyflowException;
Expand All @@ -25,19 +26,22 @@ public final class Get implements Callable<String> {
private final String vaultURL;
private final Map<String, String> headers;

public Get(GetRecordInput record, String vaultID, String vaultURL, Map<String, String> headers) {
private final GetOptions getOptions;

public Get(GetRecordInput record, String vaultID, String vaultURL, Map<String, String> headers, GetOptions getOptions) {
this.record = record;
this.vaultID = vaultID;
this.vaultURL = vaultURL;
this.headers = headers;
this.getOptions = getOptions;
}

@Override
public String call() throws Exception {
String response = null;
try {
Validators.validateGetRequestRecord(record);
StringBuilder paramsList = Helpers.constructGetRequestURLParams(record);
Validators.validateGetRequestRecord(record, getOptions);
StringBuilder paramsList = Helpers.constructGetRequestURLParams(record, getOptions);

String url = vaultURL + "/v1/vaults/" + vaultID + "/" + record.getTable() + "?" + paramsList;

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/skyflow/vault/Skyflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ public JSONObject getById(JSONObject getByIdInput) throws SkyflowException {

return finalResponse;
}

public JSONObject get(JSONObject getInput) throws SkyflowException {
return get(getInput, new GetOptions(false));
}
public JSONObject get(JSONObject getInput, GetOptions getOptions ) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.GetMethodCalled.getLog());
Validators.validateConfiguration(configuration);
LogUtil.printInfoLog(Helpers.parameterizedString(InfoLogs.ValidatedSkyflowConfiguration.getLog(), "get"));
Expand All @@ -226,7 +228,7 @@ public JSONObject get(JSONObject getInput) throws SkyflowException {
JSONArray successRecordsArray = new JSONArray();
JSONArray errorRecordsArray = new JSONArray();
try {
GetInput input = new ObjectMapper().readValue(getInput.toString(), GetInput.class);
GetInput input = new ObjectMapper().readValue(getInput.toJSONString(), GetInput.class);
GetRecordInput[] recordInputs = input.getRecords();

if (recordInputs == null || recordInputs.length == 0) {
Expand All @@ -239,7 +241,7 @@ public JSONObject get(JSONObject getInput) throws SkyflowException {

FutureTask[] futureTasks = new FutureTask[recordInputs.length];
for (int i = 0; i < recordInputs.length; i++) {
Callable<String> callable = new Get(recordInputs[i], configuration.getVaultID(), configuration.getVaultURL(), headers);
Callable<String> callable = new Get(recordInputs[i], configuration.getVaultID(), configuration.getVaultURL(), headers, getOptions);
futureTasks[i] = new FutureTask(callable);

Thread t = new Thread(futureTasks[i]);
Expand Down
76 changes: 76 additions & 0 deletions src/test/java/com/skyflow/common/utils/HelpersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,54 @@
package com.skyflow.common.utils;

import com.skyflow.Configuration;
import com.skyflow.entities.GetOptions;
import com.skyflow.entities.GetRecordInput;
import com.skyflow.entities.LogLevel;
import com.skyflow.entities.RedactionType;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.SkyflowException;

import static com.skyflow.common.utils.Helpers.constructGetRequestURLParams;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

import java.security.PrivateKey;
import org.json.simple.JSONObject;
import org.junit.Assert;
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 org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;


@RunWith(PowerMockRunner.class)
@PrepareForTest(fullyQualifiedNames = "com.skyflow.common.utils.TokenUtils")
public class HelpersTest {

private static String tableName = null;
private static String columnName = null;
private static String[] columnValue = new String[1];
private static String[] ids = new String[1];
private static String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";

@BeforeClass
public static void setup() throws SkyflowException {
PowerMockito.mockStatic(TokenUtils.class);
PowerMockito.when(TokenUtils.isTokenValid("valid_token")).thenReturn(true);
PowerMockito.when(TokenUtils.isTokenValid("not_a_valid_token")).thenReturn(false);

tableName = "account_details";
columnName = "card_number";
columnValue[0] = "123451234554321";
ids[0] = "123451234554321";
}

@Test
public void testMessageWithRequestID(){
String message = Helpers.appendRequestId("message", "abc");
Expand Down Expand Up @@ -79,6 +110,51 @@ public void testInvalidKeySpec(){
}
}
@Test
public void constructGetRequestURLParamsColumnValueTest(){
GetRecordInput recordInput = new GetRecordInput();

recordInput.setTable(tableName);
recordInput.setColumnValues(columnValue);
recordInput.setColumnName(columnName);
recordInput.setRedaction(RedactionType.PLAIN_TEXT);
StringBuilder paramsList = constructGetRequestURLParams(recordInput, new GetOptions(false));

Assert.assertTrue(paramsList.toString().contains("&"));

Assert.assertTrue(paramsList.toString().contains("column_name="+ columnName));
Assert.assertTrue(paramsList.toString().contains("column_values="+ columnValue[0]));
Assert.assertTrue(paramsList.toString().contains("redaction="+ RedactionType.PLAIN_TEXT.toString()));
}
@Test
public void constructGetRequestURLParamsIdTest(){
GetRecordInput recordInput = new GetRecordInput();

recordInput.setTable(tableName);
recordInput.setIds(ids);
recordInput.setRedaction(RedactionType.PLAIN_TEXT);
StringBuilder paramsList = constructGetRequestURLParams(recordInput, new GetOptions(false));
Assert.assertTrue(paramsList.toString().contains("&"));

Assert.assertTrue(paramsList.toString().contains("skyflow_ids="+ ids[0]));
Assert.assertTrue(paramsList.toString().contains("redaction="+"PLAIN_TEXT"));

}
@Test
public void constructGetRequestURLParamsIdTokenTrueTest(){
GetRecordInput recordInput = new GetRecordInput();

recordInput.setTable(tableName);
recordInput.setIds(ids);
StringBuilder paramsList = constructGetRequestURLParams(recordInput, new GetOptions(true));

Assert.assertTrue(paramsList.toString().contains("&"));
Assert.assertFalse(paramsList.toString().contains("redaction=PLAIN_TEXT"));

Assert.assertTrue(paramsList.toString().contains("skyflow_ids="+ ids[0]));
Assert.assertTrue(paramsList.toString().contains("tokenization="+"true"));

}
@Test
public void testGetMetrics() {
JSONObject metrics = Helpers.getMetrics();
assertEquals(true, metrics.containsKey("sdk_name_version"));
Expand Down
Loading

0 comments on commit 3e9fb70

Please sign in to comment.