From a832537f7cde27ca46b112f36f0ecfaa38d12c8d Mon Sep 17 00:00:00 2001 From: skyflow-bharti Date: Sun, 11 Jun 2023 13:24:21 +0530 Subject: [PATCH 1/2] SK-791-development-add-options-tokens-support-for-get-methods-in-java-sdk --- .../com/skyflow/common/utils/Helpers.java | 9 +- .../com/skyflow/common/utils/Validators.java | 18 +- .../java/com/skyflow/entities/GetOptions.java | 16 + .../java/com/skyflow/errors/ErrorCode.java | 5 +- src/main/java/com/skyflow/logs/ErrorLogs.java | 5 +- src/main/java/com/skyflow/vault/Get.java | 10 +- src/main/java/com/skyflow/vault/Skyflow.java | 6 +- .../com/skyflow/common/utils/HelpersTest.java | 78 ++++- .../skyflow/common/utils/ValidatorsTest.java | 128 +++++++ .../java/com/skyflow/vault/GetMethodTest.java | 312 ++++++++++++++++++ 10 files changed, 574 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/skyflow/entities/GetOptions.java create mode 100644 src/test/java/com/skyflow/common/utils/ValidatorsTest.java diff --git a/src/main/java/com/skyflow/common/utils/Helpers.java b/src/main/java/com/skyflow/common/utils/Helpers.java index 185f131..2abf64a 100644 --- a/src/main/java/com/skyflow/common/utils/Helpers.java +++ b/src/main/java/com/skyflow/common/utils/Helpers.java @@ -115,7 +115,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) { @@ -130,8 +130,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; } diff --git a/src/main/java/com/skyflow/common/utils/Validators.java b/src/main/java/com/skyflow/common/utils/Validators.java index d7c52e1..2a658f4 100644 --- a/src/main/java/com/skyflow/common/utils/Validators.java +++ b/src/main/java/com/skyflow/common/utils/Validators.java @@ -108,16 +108,21 @@ 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()); @@ -128,10 +133,19 @@ public static void validateGetRequestRecord(GetRecordInput record) throws Skyflo 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); + } + } } } diff --git a/src/main/java/com/skyflow/entities/GetOptions.java b/src/main/java/com/skyflow/entities/GetOptions.java new file mode 100644 index 0000000..7620b76 --- /dev/null +++ b/src/main/java/com/skyflow/entities/GetOptions.java @@ -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; + } +} diff --git a/src/main/java/com/skyflow/errors/ErrorCode.java b/src/main/java/com/skyflow/errors/ErrorCode.java index bc2063e..d8b3925 100644 --- a/src/main/java/com/skyflow/errors/ErrorCode.java +++ b/src/main/java/com/skyflow/errors/ErrorCode.java @@ -50,7 +50,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; diff --git a/src/main/java/com/skyflow/logs/ErrorLogs.java b/src/main/java/com/skyflow/logs/ErrorLogs.java index ea375c1..5fd174a 100644 --- a/src/main/java/com/skyflow/logs/ErrorLogs.java +++ b/src/main/java/com/skyflow/logs/ErrorLogs.java @@ -45,7 +45,10 @@ public enum ErrorLogs { InvalidColumnInUpsertOption("Invalid column in upsert object, non empty string is required."), InvalidUpsertObjectType("upsert option cannot be null, should be an UpsertOption object."), InvalidSkyflowId("Skyflow Id is missing"), - InvalidField("Fields 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."); private final String log; diff --git a/src/main/java/com/skyflow/vault/Get.java b/src/main/java/com/skyflow/vault/Get.java index a95369b..5951c27 100644 --- a/src/main/java/com/skyflow/vault/Get.java +++ b/src/main/java/com/skyflow/vault/Get.java @@ -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; @@ -25,19 +26,22 @@ public final class Get implements Callable { private final String vaultURL; private final Map headers; - public Get(GetRecordInput record, String vaultID, String vaultURL, Map headers) { + private final GetOptions getOptions; + + public Get(GetRecordInput record, String vaultID, String vaultURL, Map 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; diff --git a/src/main/java/com/skyflow/vault/Skyflow.java b/src/main/java/com/skyflow/vault/Skyflow.java index f6b9926..ec782df 100644 --- a/src/main/java/com/skyflow/vault/Skyflow.java +++ b/src/main/java/com/skyflow/vault/Skyflow.java @@ -208,8 +208,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")); @@ -230,7 +232,7 @@ public JSONObject get(JSONObject getInput) throws SkyflowException { FutureTask[] futureTasks = new FutureTask[recordInputs.length]; for (int i = 0; i < recordInputs.length; i++) { - Callable callable = new Get(recordInputs[i], configuration.getVaultID(), configuration.getVaultURL(), headers); + Callable callable = new Get(recordInputs[i], configuration.getVaultID(), configuration.getVaultURL(), headers, getOptions); futureTasks[i] = new FutureTask(callable); Thread t = new Thread(futureTasks[i]); diff --git a/src/test/java/com/skyflow/common/utils/HelpersTest.java b/src/test/java/com/skyflow/common/utils/HelpersTest.java index 1501268..7d7832b 100644 --- a/src/test/java/com/skyflow/common/utils/HelpersTest.java +++ b/src/test/java/com/skyflow/common/utils/HelpersTest.java @@ -4,17 +4,47 @@ 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 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; - +@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"); @@ -72,4 +102,50 @@ public void testInvalidKeySpec(){ assertEquals(exception.getMessage(), ErrorCode.InvalidKeySpec.getDescription()); } } + @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")); + + } + } diff --git a/src/test/java/com/skyflow/common/utils/ValidatorsTest.java b/src/test/java/com/skyflow/common/utils/ValidatorsTest.java new file mode 100644 index 0000000..0974890 --- /dev/null +++ b/src/test/java/com/skyflow/common/utils/ValidatorsTest.java @@ -0,0 +1,128 @@ +package com.skyflow.common.utils; + + +import com.skyflow.entities.GetOptions; +import com.skyflow.entities.GetRecordInput; +import com.skyflow.entities.RedactionType; +import com.skyflow.errors.ErrorCode; +import com.skyflow.errors.SkyflowException; +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; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(fullyQualifiedNames = "com.skyflow.common.utils.TokenUtils") +public class ValidatorsTest { + 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 + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void validateGetRequestRecordValidTest() { + //column values with redaction + GetRecordInput recordInput = new GetRecordInput(); + + recordInput.setTable(tableName); + recordInput.setColumnValues(columnValue); + recordInput.setColumnName(columnName); + recordInput.setRedaction(RedactionType.PLAIN_TEXT); + try { + Validators.validateGetRequestRecord(recordInput, new GetOptions(false)); + } catch (SkyflowException exception) { + exception.printStackTrace(); + Assert.fail(INVALID_EXCEPTION_THROWN); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void validateGetRequestRecordValidIdTest() { + //Id values with redaction + + GetRecordInput recordInput = new GetRecordInput(); + recordInput.setTable(tableName); + recordInput.setIds(ids); + recordInput.setRedaction(RedactionType.PLAIN_TEXT); + try { + Validators.validateGetRequestRecord(recordInput, new GetOptions(false)); + } catch (SkyflowException exception) { + exception.printStackTrace(); + Assert.fail(INVALID_EXCEPTION_THROWN); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void validateGetRequestRecordValidIdTokenTrueTest() throws SkyflowException { + //Id values with redaction + + GetRecordInput recordInput = new GetRecordInput(); + recordInput.setTable(tableName); + recordInput.setIds(ids); + try { + Validators.validateGetRequestRecord(recordInput, new GetOptions(true)); + } catch (SkyflowException exception) { + exception.printStackTrace(); + Assert.fail(INVALID_EXCEPTION_THROWN); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void validateGetRequestRecordIdRedactionTokenTest() throws SkyflowException { + //Id values with redaction + + GetRecordInput recordInput = new GetRecordInput(); + recordInput.setTable(tableName); + recordInput.setIds(ids); + recordInput.setRedaction(RedactionType.PLAIN_TEXT); + + try { + Validators.validateGetRequestRecord(recordInput, new GetOptions(true)); + } catch (SkyflowException e) { + Assert.assertTrue(ErrorCode.RedactionWithTokenNotSupported.getDescription().contains(e.getMessage())); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void validateGetRequestRecordColumnRedactionTokenTest() { + //Id values with redaction + GetRecordInput recordInput = new GetRecordInput(); + recordInput.setTable(tableName); + recordInput.setColumnName(columnName); + recordInput.setColumnValues(columnValue); + recordInput.setRedaction(RedactionType.PLAIN_TEXT); + + try { + Validators.validateGetRequestRecord(recordInput, new GetOptions(true)); + } catch (SkyflowException e) { + Assert.assertTrue(ErrorCode.TokensGetColumnNotSupported.getDescription().contains(e.getMessage())); + } + } + @Test + public void getOptionsTest(){ + GetOptions options = new GetOptions(); + Assert.assertFalse(options.getOptionToken()); + } + @Test + public void getOptionsTestWhenTokenTrue(){ + GetOptions options = new GetOptions(true); + Assert.assertTrue(options.getOptionToken()); + } + +} diff --git a/src/test/java/com/skyflow/vault/GetMethodTest.java b/src/test/java/com/skyflow/vault/GetMethodTest.java index a1418f3..2477cbb 100644 --- a/src/test/java/com/skyflow/vault/GetMethodTest.java +++ b/src/test/java/com/skyflow/vault/GetMethodTest.java @@ -2,6 +2,8 @@ import com.skyflow.common.utils.HttpUtility; import com.skyflow.common.utils.TokenUtils; +import com.skyflow.entities.GetOptions; +import com.skyflow.entities.GetRecordInput; import com.skyflow.entities.RedactionType; import com.skyflow.entities.SkyflowConfiguration; import com.skyflow.errors.ErrorCode; @@ -555,4 +557,314 @@ public void testGetMethodParseException() { Assert.fail(INVALID_EXCEPTION_THROWN); } } + + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void testGetMethodWithNoRedactionNoToken() { + try { + SkyflowConfiguration config = new SkyflowConfiguration(vaultID, vaultURL, new DemoTokenProvider()); + + Skyflow skyflowClient = Skyflow.init(config); + + JSONObject records = new JSONObject(); + JSONArray recordsArray = new JSONArray(); + JSONObject record = new JSONObject(); + + JSONArray ids = new JSONArray(); + ids.add(skyflowId); + + record.put("ids", ids); + record.put("table", tableName); + + recordsArray.add(record); + records.put("records", recordsArray); + + skyflowClient.get(records); + + } catch (SkyflowException e) { + JSONObject error = (JSONObject) ((JSONObject) (((JSONArray) e.getData().get("errors")).get(0))).get("error"); + + JSONArray errors = (JSONArray) e.getData().get("errors"); + Assert.assertEquals(1, errors.size()); + Assert.assertEquals(error.get("description"), ErrorCode.MissingRedaction.getDescription()); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void testGetMethodWithRedactionToken() { + try { + SkyflowConfiguration config = new SkyflowConfiguration(vaultID, vaultURL, new DemoTokenProvider()); + + Skyflow skyflowClient = Skyflow.init(config); + + JSONObject records = new JSONObject(); + JSONArray recordsArray = new JSONArray(); + JSONObject record = new JSONObject(); + + JSONArray ids = new JSONArray(); + ids.add(skyflowId); + + record.put("ids", ids); + record.put("table", tableName); + record.put("redaction", RedactionType.PLAIN_TEXT.toString()); + recordsArray.add(record); + records.put("records", recordsArray); + + skyflowClient.get(records, new GetOptions(true)); + + } catch (SkyflowException e) { + JSONObject error = (JSONObject) ((JSONObject) (((JSONArray) e.getData().get("errors")).get(0))).get("error"); + + JSONArray errors = (JSONArray) e.getData().get("errors"); + Assert.assertEquals(1, errors.size()); + Assert.assertEquals(error.get("description"), ErrorCode.RedactionWithTokenNotSupported.getDescription()); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void testGetMethodWithNoRedactionTokenForColumnValues() { + try { + SkyflowConfiguration config = new SkyflowConfiguration(vaultID, vaultURL, new DemoTokenProvider()); + + Skyflow skyflowClient = Skyflow.init(config); + + + JSONObject records = new JSONObject(); + JSONArray recordsArray = new JSONArray(); + JSONObject record = new JSONObject(); + + JSONArray values = new JSONArray(); + values.add(columnValue); + + record.put("table", tableName); + record.put("columnName", columnName); + record.put("columnValues", values); + + recordsArray.add(record); + records.put("records", recordsArray); + + skyflowClient.get(records, new GetOptions(true)); + + } catch (SkyflowException e) { + JSONObject error = (JSONObject) ((JSONObject) (((JSONArray) e.getData().get("errors")).get(0))).get("error"); + + JSONArray errors = (JSONArray) e.getData().get("errors"); + Assert.assertEquals(1, errors.size()); + Assert.assertEquals(error.get("description"), ErrorCode.TokensGetColumnNotSupported.getDescription()); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void testGetMethodWithRedactionWithTokenForColumnValues() { + try { + SkyflowConfiguration config = new SkyflowConfiguration(vaultID, vaultURL, new DemoTokenProvider()); + + Skyflow skyflowClient = Skyflow.init(config); + + + JSONObject records = new JSONObject(); + JSONArray recordsArray = new JSONArray(); + JSONObject record = new JSONObject(); + + JSONArray values = new JSONArray(); + values.add(columnValue); + + record.put("table", tableName); + record.put("columnName", columnName); + record.put("columnValues", values); + record.put("redaction", RedactionType.PLAIN_TEXT.toString()); + + recordsArray.add(record); + records.put("records", recordsArray); + + + skyflowClient.get(records, new GetOptions(true)); + + } catch (SkyflowException e) { + JSONObject error = (JSONObject) ((JSONObject) (((JSONArray) e.getData().get("errors")).get(0))).get("error"); + + JSONArray errors = (JSONArray) e.getData().get("errors"); + Assert.assertEquals(1, errors.size()); + Assert.assertEquals(error.get("description"), ErrorCode.TokensGetColumnNotSupported.getDescription()); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void testGetMethodWithNoRedactionNoTokenForColumnValues() { + try { + SkyflowConfiguration config = new SkyflowConfiguration(vaultID, vaultURL, new DemoTokenProvider()); + + Skyflow skyflowClient = Skyflow.init(config); + + + JSONObject records = new JSONObject(); + JSONArray recordsArray = new JSONArray(); + JSONObject record = new JSONObject(); + + JSONArray values = new JSONArray(); + values.add(columnValue); + + record.put("table", tableName); + record.put("columnName", columnName); + record.put("columnValues", values); + + recordsArray.add(record); + records.put("records", recordsArray); + + skyflowClient.get(records); + + } catch (SkyflowException e) { + JSONObject error = (JSONObject) ((JSONObject) (((JSONArray) e.getData().get("errors")).get(0))).get("error"); + + JSONArray errors = (JSONArray) e.getData().get("errors"); + Assert.assertEquals(1, errors.size()); + Assert.assertEquals(error.get("description"), ErrorCode.MissingRedaction.getDescription()); + } + + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void testGetMethodWithNoRedactionFalseTokenForColumnValues() { + try { + SkyflowConfiguration config = new SkyflowConfiguration(vaultID, vaultURL, new DemoTokenProvider()); + + Skyflow skyflowClient = Skyflow.init(config); + + + JSONObject records = new JSONObject(); + JSONArray recordsArray = new JSONArray(); + JSONObject record = new JSONObject(); + + JSONArray values = new JSONArray(); + values.add(columnValue); + + record.put("table", tableName); + record.put("columnName", columnName); + record.put("columnValues", values); + + recordsArray.add(record); + records.put("records", recordsArray); + + skyflowClient.get(records, new GetOptions(false)); + + } catch (SkyflowException e) { + JSONObject error = (JSONObject) ((JSONObject) (((JSONArray) e.getData().get("errors")).get(0))).get("error"); + + JSONArray errors = (JSONArray) e.getData().get("errors"); + Assert.assertEquals(1, errors.size()); + Assert.assertEquals(error.get("description"), ErrorCode.MissingRedaction.getDescription()); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void testGetMethodWithNoRedactionFalseToken() { + try { + SkyflowConfiguration config = new SkyflowConfiguration(vaultID, vaultURL, new DemoTokenProvider()); + + Skyflow skyflowClient = Skyflow.init(config); + + JSONObject records = new JSONObject(); + JSONArray recordsArray = new JSONArray(); + JSONObject record = new JSONObject(); + + JSONArray ids = new JSONArray(); + ids.add(skyflowId); + + record.put("ids", ids); + record.put("table", tableName); + recordsArray.add(record); + records.put("records", recordsArray); + + skyflowClient.get(records, new GetOptions(false)); + + } catch (SkyflowException e) { + JSONObject error = (JSONObject) ((JSONObject) (((JSONArray) e.getData().get("errors")).get(0))).get("error"); + + JSONArray errors = (JSONArray) e.getData().get("errors"); + Assert.assertEquals(1, errors.size()); + Assert.assertEquals(error.get("description"), ErrorCode.MissingRedaction.getDescription()); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void testGetMethodWithRedactionFalseTokenSuccess() { + try { + SkyflowConfiguration config = new SkyflowConfiguration(vaultID, vaultURL, new DemoTokenProvider()); + + Skyflow skyflowClient = Skyflow.init(config); + + JSONObject records = new JSONObject(); + JSONArray recordsArray = new JSONArray(); + JSONObject record = new JSONObject(); + + JSONArray ids = new JSONArray(); + ids.add(skyflowId); + + record.put("ids", ids); + record.put("table", tableName); + record.put("redaction", RedactionType.PLAIN_TEXT.toString()); + recordsArray.add(record); + records.put("records", recordsArray); + + PowerMockito.mockStatic(HttpUtility.class); + String mockResponse = "{\"records\":[{\"fields\":{\"bank_account_number\":\"123451234554321\",\"pin_code\":\"121342\",\"name\":\"demo\",\"skyflow_id\":\"skyflowId123\"}}]}"; + PowerMockito.when(HttpUtility.sendRequest(ArgumentMatchers.anyString(), + ArgumentMatchers.any(), + ArgumentMatchers.any(), + ArgumentMatchers.anyMap())) + .thenReturn(mockResponse); + JSONObject response = skyflowClient.get(records, new GetOptions(false)); + JSONArray responseRecords = (JSONArray) response.get("records"); + + Assert.assertEquals(1, responseRecords.size()); + Assert.assertEquals(tableName, ((JSONObject) responseRecords.get(0)).get("table")); + Assert.assertNotNull(((JSONObject) responseRecords.get(0)).get("fields")); + + } catch (SkyflowException | IOException exception) { + exception.printStackTrace(); + Assert.fail(INVALID_EXCEPTION_THROWN); + } + } + @Test + @PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility", "com.skyflow.common.utils.TokenUtils"}) + public void testGetMethodWithRedactionFalseTokenSuccess2() { + try { + SkyflowConfiguration config = new SkyflowConfiguration(vaultID, vaultURL, new DemoTokenProvider()); + + Skyflow skyflowClient = Skyflow.init(config); + JSONObject records = new JSONObject(); + JSONArray recordsArray = new JSONArray(); + JSONObject record = new JSONObject(); + + JSONArray values = new JSONArray(); + values.add(columnValue); + + record.put("table", tableName); + record.put("columnName", columnName); + record.put("columnValues", values); + record.put("redaction", RedactionType.PLAIN_TEXT.toString()); + + recordsArray.add(record); + records.put("records", recordsArray); + + PowerMockito.mockStatic(HttpUtility.class); + String mockResponse = "{\"records\":[{\"fields\":{\"bank_account_number\":\"123451234554321\",\"pin_code\":\"121342\",\"name\":\"demo\",\"skyflow_id\":\"skyflowId123\"}}]}"; + PowerMockito.when(HttpUtility.sendRequest(ArgumentMatchers.anyString(), + ArgumentMatchers.any(), + ArgumentMatchers.any(), + ArgumentMatchers.anyMap())) + .thenReturn(mockResponse); + JSONObject response = skyflowClient.get(records, new GetOptions(false)); + JSONArray responseRecords = (JSONArray) response.get("records"); + + Assert.assertEquals(1, responseRecords.size()); + Assert.assertEquals(tableName, ((JSONObject) responseRecords.get(0)).get("table")); + Assert.assertNotNull(((JSONObject) responseRecords.get(0)).get("fields")); + + } catch (SkyflowException | IOException exception) { + exception.printStackTrace(); + Assert.fail(INVALID_EXCEPTION_THROWN); + } + } + } From 3983fd11178bb3b3dc2b3f0160080d696a32f2a7 Mon Sep 17 00:00:00 2001 From: skyflow-bharti Date: Wed, 25 Oct 2023 14:41:02 +0530 Subject: [PATCH 2/2] SK-816 Updated versions --- pom.xml | 2 +- src/main/java/com/skyflow/common/utils/Constants.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index f5ab18b..d99fbd5 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.skyflow skyflow-java - 1.11.0 + 1.12.0 jar ${project.groupId}:${project.artifactId} diff --git a/src/main/java/com/skyflow/common/utils/Constants.java b/src/main/java/com/skyflow/common/utils/Constants.java index 78489c0..46eefd1 100644 --- a/src/main/java/com/skyflow/common/utils/Constants.java +++ b/src/main/java/com/skyflow/common/utils/Constants.java @@ -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"; }