From d2528dd8963a1358542bf99384da3a0978df80ad Mon Sep 17 00:00:00 2001 From: saileshwar-skyflow Date: Tue, 21 May 2024 12:10:52 +0530 Subject: [PATCH 1/3] OE-1156 Updated error messages --- Skyflow/src/main/kotlin/Skyflow/TextField.kt | 10 +++-- .../src/main/kotlin/Skyflow/core/APIClient.kt | 7 ++-- .../src/main/kotlin/Skyflow/core/Messages.kt | 42 +++++++++---------- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/Skyflow/src/main/kotlin/Skyflow/TextField.kt b/Skyflow/src/main/kotlin/Skyflow/TextField.kt index a25be0c..56c546f 100644 --- a/Skyflow/src/main/kotlin/Skyflow/TextField.kt +++ b/Skyflow/src/main/kotlin/Skyflow/TextField.kt @@ -100,7 +100,11 @@ class TextField @JvmOverloads constructor( var builtinValidationError = "" if (isRequired && str.isEmpty()) { builtinValidationError = "value is empty" - setErrorText("value is required") + if(collectInput.label.isEmpty()) + setErrorText("Field is required") + else + setErrorText(collectInput.label +" is required") + return builtinValidationError } builtinValidationError += SkyflowValidator.validate(str, validationRules) @@ -120,9 +124,9 @@ class TextField @JvmOverloads constructor( } } else { if (collectInput.label.isEmpty()) - setErrorText("invalid field") + setErrorText("Invalid value") else - setErrorText("invalid " + collectInput.label) + setErrorText("Invalid " + collectInput.label) return builtinValidationError } } diff --git a/Skyflow/src/main/kotlin/Skyflow/core/APIClient.kt b/Skyflow/src/main/kotlin/Skyflow/core/APIClient.kt index b0ec7aa..624b9fe 100644 --- a/Skyflow/src/main/kotlin/Skyflow/core/APIClient.kt +++ b/Skyflow/src/main/kotlin/Skyflow/core/APIClient.kt @@ -13,6 +13,7 @@ import Skyflow.soap.SoapApiCallback import Skyflow.soap.SoapConnectionConfig import Skyflow.soap.SoapValueCallback import Skyflow.utils.Utils +import com.skyflow_android.BuildConfig import org.json.JSONArray import org.json.JSONObject import java.io.UnsupportedEncodingException @@ -70,7 +71,7 @@ internal class APIClient( Logger.info(tag, Messages.BEARER_TOKEN_RECEIVED.getMessage(), logLevel) if (!isValidToken(responseBody.toString())) { val error = - SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel) + SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel, arrayOf("Android", "${BuildConfig.SDK_VERSION}")) callback.onFailure(error) } else { token = "Bearer $responseBody" @@ -85,7 +86,7 @@ internal class APIClient( logLevel ) val error = - SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel) + SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel, arrayOf("Android", "${BuildConfig.SDK_VERSION}")) callback.onFailure(error) } }) @@ -93,7 +94,7 @@ internal class APIClient( callback.onSuccess(token) } } catch (e: Exception) { - val error = SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel) + val error = SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel, arrayOf("Android", "${BuildConfig.SDK_VERSION}")) callback.onFailure(error) } } diff --git a/Skyflow/src/main/kotlin/Skyflow/core/Messages.kt b/Skyflow/src/main/kotlin/Skyflow/core/Messages.kt index 7ec764e..1065902 100644 --- a/Skyflow/src/main/kotlin/Skyflow/core/Messages.kt +++ b/Skyflow/src/main/kotlin/Skyflow/core/Messages.kt @@ -49,17 +49,17 @@ enum class Messages(val message: String) { INVALID_VAULT_ID("vault id invalid cannot be found"), - INVALID_VAULT_URL("vault url %s is invalid or not secure"), - EMPTY_VAULT_ID("vaultid is empty."), - EMPTY_VAULT_URL("vault url is empty."), - INVALID_BEARER_TOKEN("bearer token is invalid or expired"), - INVALID_TABLE_NAME("Key 'table' doesn't have a value of type String"), + INVALID_VAULT_URL("%s SDK v%s Initialization failed. Invalid client credentials. 'vaultURL' must be begin with 'https://'."), + EMPTY_VAULT_ID("%s SDK v%s Initialization failed. Invalid credentials. Specify a valid 'vaultID'."), + EMPTY_VAULT_URL("%s SDK v%s Initialization failed. Invalid credentials. Specify a valid 'vaultURL'."), + INVALID_BEARER_TOKEN("%s SDK v%s Initialization failed. Invalid client credentials. Specify a valid bearer token."), + INVALID_TABLE_NAME("%s SDK v%s Validation error. Invalid 'table' key in records at index %s. Specify a value of type string instead."), ELEMENT_EMPTY_TABLE_NAME("element with type %s - Table key cannot be empty."), - EMPTY_TABLE_KEY("table key cannot be empty"), + EMPTY_TABLE_KEY("%s SDK v%s Validation error.'table' cannot be empty in records at index %s. Specify a non-empty value for 'table'."), EMPTY_COLUMN_KEY("column key cannot be empty"), - RECORDS_KEY_NOT_FOUND("records object is required"), - ADDITION_RECORDS_KEY_KEY_NOT_FOUND("records object key value not found inside additional Fields"), - EMPTY_RECORDS("records object cannot be empty"), + RECORDS_KEY_NOT_FOUND("%s SDK v%s Validation error. Missing 'records' key. Provide a valid 'records' key."), + ADDITION_RECORDS_KEY_KEY_NOT_FOUND("%s SDK v%s Validation error.'records' key not found in additionalFields. Specify a 'records' key in additionalFields."), + EMPTY_RECORDS("%s SDK v%s Validation error. 'records' key cannot be empty. Provide a non-empty value instead."), TABLE_KEY_ERROR("key \'table\' is missing or payload is incorrectly formatted"), FIELDS_KEY_ERROR("fields key is required"), INVALID_COLUMN_NAME("column with given name is not present in the vault"), @@ -76,20 +76,20 @@ enum class Messages(val message: String) { EMPTY_RECORD_IDS("ids key cannot be empty"), INVALID_RECORD_ID_TYPE("invalid type of records id"), MISSING_TABLE_IN_ELEMENT("element with type %s - Table key is required."), - MISSING_TABLE_KEY("table key is required"), + MISSING_TABLE_KEY("%s SDK v%s Validation error.'table' key not found in records at index %s. Specify a valid value for 'table' key."), INVALID_RECORD_TABLE_VALUE("invalid record table value"), INVALID_CONNECTION_URL("connectionURL %s is invalid "), INVALID_INPUT("%s"), REQUIRED_INPUTS_NOT_PROVIDED("required inputs are not provided"), - INVALID_EVENT_TYPE("provide a valid event type"), - INVALID_EVENT_LISTENER("provide valid event listener"), + INVALID_EVENT_TYPE("%s SDK v%s Invalid event type. Specify a valid event type."), + INVALID_EVENT_LISTENER("%s SDK v%s Invalid event listener. Please specify a valid event listener."), UNKNOWN_ERROR("%s"), TRANSACTION_ERROR("an error occurred during transaction"), CONNECTION_ERROR("error while initializing the connection"), MISSING_REDACTION_VALUE("missing redaction value"), - ELEMENT_NOT_MOUNTED("element for %s is not mounted"), + ELEMENT_NOT_MOUNTED("%s SDK v%s Collect failed. Make sure all elements are mounted before calling 'collect' on the container."), DUPLICATE_COLUMN_FOUND("Duplicate element with %s and %s found in container"), - DUPLICATE_ELEMENT_FOUND("Duplicate Element found in response body"), + DUPLICATE_ELEMENT_FOUND("%s SDK v%s Mount failed. '%s' appeared multiple times in '%s'. Make sure each column in a record is unique."), INVALID_RECORDS_TYPE("Key 'records' is of invalid type"), INVALID_RECORD_IDS("ids are not valid"), MISSING_REDACTION("redaction is missing"), @@ -101,12 +101,12 @@ enum class Messages(val message: String) { INVALID_FIELD_IN_QUERY_PARAMS("invalid data type %s present in query parameters"), INVALID_FIELD_IN_REQUEST_HEADER_PARAMS("invalid data type %s present in request header"), INVALID_FIELD_IN_REQUEST_BODY("invalid data type %s present in request body"), - FAILED_TO_REVEAL("Failed to reveal"), + FAILED_TO_REVEAL("%s SDK v%s Reveal failed. Some errors were encountered."), EMPTY_CONNECTION_URL("Empty connection url is passed"), NOT_FOUND_IN_RESPONSE("%s is not found in response"), BAD_REQUEST("bad request"), MISSING_COLUMN("element with type %s - Column key cannot be empty."), - EMPTY_FIELDS("fields key cannot be empty"), + EMPTY_FIELDS("%s SDK v%s Validation error. Missing 'fields' key in records at index %s. Provide a valid 'fields' key."), SERVER_ERROR("Server error %s"), EMPTY_REQUEST_XML("RequestXML is empty"), INVALID_REQUEST_XML("Invalid RequestXML in SoapConnection - %s"), @@ -118,7 +118,7 @@ enum class Messages(val message: String) { INVALID_ID_IN_RESPONSE_XML("Invalid elementId %s present in ResponseXML"), EMPTY_ID_IN_RESPONSE_XML("empty elementId present in ResponseXML"), DUPLICATE_ID_IN_RESPONSE_XML("duplicate Id present in ResponseXML"), - INVALID_FORMAT_REGEX("Invalid formatRegex - no match found for regex: %s"), + INVALID_FORMAT_REGEX("%s SDK v%s Validation error. Invalid value for 'regex' param found for regex in validations array at index %s. Provide a valid value regular expression for regex param."), NOT_VALID_TOKENS("following tokens are not valid - %s"), NO_TABLE_KEY_IN_UPSERT("\"table\" key is required in upsert options object at index %s"), NO_COLUMN_KEY_IN_UPSERT("\"column\" key is required in upsert option at index %s"), @@ -139,18 +139,18 @@ enum class Messages(val message: String) { INVALID_RECORD_COLUMN_VALUES_TYPE("Invalid Column Values type in record object at index %s. Must be JSONArray"), INVALID_COLUMN_VALUE_TYPE("Invalid Column Value type. Must be String"), EMPTY_RECORD_COLUMN_VALUES("Column Values cannot be empty in record object at index %s"), - EMPTY_COLUMN_VALUE("Column Value is empty"), + EMPTY_COLUMN_VALUE("%s SDK v%s Validation error. 'column' key cannot be empty in records at index %s. Specify a non-empty value instead."), BOTH_IDS_AND_COLUMN_DETAILS_SPECIFIED("Both skyflow ids and column details (name and values) are specified in record object at index %s"), NEITHER_IDS_NOR_COLUMN_DETAILS_SPECIFIED("Neither skyflow ids nor column details (name and values) are specified in record object at index %s"), FAILED_TO_GET("Failed to Get records"), VALIDATE_INPUT_FORMAT_OPTIONS("Validated Input Format Options for %s"), - INPUT_FORMATTING_NOT_SUPPORTED("format or translation are not supported on %s element type."), - INVALID_INPUT_TRANSLATION("invalid or unsupported translation provided for %s element type."), + INPUT_FORMATTING_NOT_SUPPORTED("%s SDK v%s Mount failed. Format must be a non-empty string. Specify a valid format."), + INVALID_INPUT_TRANSLATION("%s SDK v%s Mount failed. Translation must be a non-empty object. Specify a valid translation."), EMPTY_INPUT_TRANSLATION("translation not passed. Switching to default translation %s"), - MISMATCH_ELEMENT_COUNT_LAYOUT_SUM("Created elements count should be equal to sum of layout values.") + MISMATCH_ELEMENT_COUNT_LAYOUT_SUM("%s SDK v%s Mount failed. Invalid layout array values. Make sure all values in the layout array are positive numbers.") } fun Messages.getMessage(vararg values: String?): String { From 5ece6264fa42a227bb668c20bde27c3943bf5d1f Mon Sep 17 00:00:00 2001 From: skyflow-vivek Date: Mon, 27 May 2024 16:14:48 +0530 Subject: [PATCH 2/3] OE-1156 Update Error Messages for Android - Updated error messages used in Android SDK --- .../java/com/Skyflow/GetByIdsTest.kt | 82 ++-- .../androidTest/java/com/Skyflow/GetTest.kt | 5 +- .../java/com/Skyflow/InsertTest.kt | 93 +++-- Skyflow/src/main/kotlin/Skyflow/Client.kt | 102 +++-- .../main/kotlin/Skyflow/RevealContainer.kt | 16 +- .../src/main/kotlin/Skyflow/SkyflowError.kt | 2 +- .../main/kotlin/Skyflow/SkyflowErrorCode.kt | 62 +-- Skyflow/src/main/kotlin/Skyflow/TextField.kt | 6 +- .../collect/client/CollectRequestBody.kt | 116 +++--- .../src/main/kotlin/Skyflow/core/APIClient.kt | 35 +- .../src/main/kotlin/Skyflow/core/Messages.kt | 168 ++++++--- .../src/main/kotlin/Skyflow/utils/Utils.kt | 97 +++-- .../src/test/java/com/Skyflow/CollectTest.kt | 30 +- .../test/java/com/Skyflow/DetokenizeTests.kt | 6 +- Skyflow/src/test/java/com/Skyflow/GetTests.kt | 38 +- .../java/com/Skyflow/InvokeConnectionTest.kt | 12 +- .../src/test/java/com/Skyflow/RevealTest.kt | 33 +- .../src/test/java/com/Skyflow/UnitTests.kt | 357 +++++++----------- 18 files changed, 682 insertions(+), 578 deletions(-) diff --git a/Skyflow/src/androidTest/java/com/Skyflow/GetByIdsTest.kt b/Skyflow/src/androidTest/java/com/Skyflow/GetByIdsTest.kt index b5c137b..4fca697 100644 --- a/Skyflow/src/androidTest/java/com/Skyflow/GetByIdsTest.kt +++ b/Skyflow/src/androidTest/java/com/Skyflow/GetByIdsTest.kt @@ -153,7 +153,9 @@ class GetByIdsTest { override fun onFailure(exception: Any) { Log.d("exp",exception.toString()) - val skyflowError = SkyflowError(SkyflowErrorCode.ELEMENT_EMPTY_TABLE_NAME) + val skyflowError = SkyflowError( + SkyflowErrorCode.EMPTY_TABLE_KEY, params = arrayOf("0") + ) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -162,8 +164,7 @@ class GetByIdsTest { } @Test - fun testEmptyRedaction() - { + fun testEmptyRedaction() { val recordsArray = JSONArray() val record = JSONObject() record.put("table","cards") @@ -176,17 +177,17 @@ class GetByIdsTest { recordsArray.put(record) val records = JSONObject() records.put("records",recordsArray) - skyflowClient.getById(records,object : Callback - { - override fun onSuccess(responseBody: Any) { - } + skyflowClient.getById(records,object : Callback { + + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_REDACTION_VALUE) + val skyflowError = SkyflowError( + SkyflowErrorCode.EMPTY_REDACTION_VALUE, params = arrayOf("0") + ) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } - }) } @@ -211,7 +212,9 @@ class GetByIdsTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_REDACTION_TYPE) + val skyflowError = SkyflowError( + SkyflowErrorCode.INVALID_REDACTION_TYPE, params = arrayOf("0") + ) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -240,7 +243,9 @@ class GetByIdsTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME) + val skyflowError = SkyflowError( + SkyflowErrorCode.INVALID_TABLE_NAME, params = arrayOf("0") + ) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -268,7 +273,9 @@ class GetByIdsTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.TABLE_KEY_ERROR) + val skyflowError = SkyflowError( + SkyflowErrorCode.TABLE_KEY_NOY_FOUND, params = arrayOf("0") + ) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -277,8 +284,7 @@ class GetByIdsTest { } @Test - fun testMissingRedaction() - { + fun testMissingRedaction() { val recordsArray = JSONArray() val record = JSONObject() record.put("table","cards") @@ -291,17 +297,17 @@ class GetByIdsTest { recordsArray.put(record) val records = JSONObject() records.put("records",recordsArray) - skyflowClient.getById(records,object : Callback - { - override fun onSuccess(responseBody: Any) { - } + skyflowClient.getById(records,object : Callback { + + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.REDACTION_KEY_ERROR) + val skyflowError = SkyflowError( + SkyflowErrorCode.REDACTION_KEY_NOT_FOUND, params = arrayOf("0") + ) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } - }) } @@ -356,7 +362,9 @@ class GetByIdsTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_RECORDS) + val skyflowError = SkyflowError( + SkyflowErrorCode.INVALID_RECORDS + ) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -395,8 +403,7 @@ class GetByIdsTest { @Test - fun testMissingIds() - { + fun testMissingIds() { val recordsArray = JSONArray() val record = JSONObject() record.put("table","cards") @@ -409,17 +416,17 @@ class GetByIdsTest { recordsArray.put(record) val records = JSONObject() records.put("records",recordsArray) - skyflowClient.getById(records,object : Callback - { - override fun onSuccess(responseBody: Any) { - } + skyflowClient.getById(records,object : Callback { + + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_IDS) + val skyflowError = SkyflowError( + SkyflowErrorCode.IDS_KEY_NOT_FOUND, params = arrayOf("0") + ) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } - }) } @@ -442,7 +449,9 @@ class GetByIdsTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_RECORD_IDS) + val skyflowError = SkyflowError( + SkyflowErrorCode.EMPTY_RECORD_IDS, params = arrayOf("0") + ) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -451,8 +460,7 @@ class GetByIdsTest { } @Test - fun testInvalidIds() - { + fun testInvalidIds() { val recordsArray = JSONArray() val record = JSONObject() record.put("table","cards") @@ -462,17 +470,15 @@ class GetByIdsTest { recordsArray.put(record) val records = JSONObject() records.put("records",recordsArray) - skyflowClient.getById(records,object : Callback - { - override fun onSuccess(responseBody: Any) { - } + skyflowClient.getById(records,object : Callback { + + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_RECORD_IDS) + val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_IDS, params = arrayOf("0")) assertEquals(skyflowError.getErrorMessage(), getErrorMessage(exception as JSONObject)) } - }) } diff --git a/Skyflow/src/androidTest/java/com/Skyflow/GetTest.kt b/Skyflow/src/androidTest/java/com/Skyflow/GetTest.kt index b2d0bb5..d404687 100644 --- a/Skyflow/src/androidTest/java/com/Skyflow/GetTest.kt +++ b/Skyflow/src/androidTest/java/com/Skyflow/GetTest.kt @@ -2,7 +2,6 @@ package com.Skyflow import Skyflow.* import android.content.Context -import android.util.Log import androidx.test.core.app.ApplicationProvider import junit.framework.Assert.assertEquals import org.json.JSONArray @@ -130,7 +129,9 @@ class GetTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TOKEN) + val skyflowError = SkyflowError( + SkyflowErrorCode.TOKEN_KEY_NOT_FOUND, params = arrayOf("0") + ) assertEquals(skyflowError.getErrorMessage(),getErrorMessage(exception as JSONObject)) } diff --git a/Skyflow/src/androidTest/java/com/Skyflow/InsertTest.kt b/Skyflow/src/androidTest/java/com/Skyflow/InsertTest.kt index 9c79a62..5844bdb 100644 --- a/Skyflow/src/androidTest/java/com/Skyflow/InsertTest.kt +++ b/Skyflow/src/androidTest/java/com/Skyflow/InsertTest.kt @@ -116,85 +116,92 @@ class InsertTest { } @Test - fun testEmptyTableName() - { + fun testEmptyTableName() { val records = JSONObject() val recordsArray = JSONArray() + val record = JSONObject() record.put("table", "") + val fields = JSONObject() fields.put("fullname", "san") fields.put("card_number", "41111111111") fields.put("expiry_date","11/22") + record.put("fields", fields) recordsArray.put(record) records.put("records", recordsArray) - skyflow.insert(records, InsertOptions(),object : Callback - { - override fun onSuccess(responseBody: Any) { - } + + skyflow.insert(records, InsertOptions(), object : Callback { + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - assertEquals((exception as SkyflowError).message.toString(),SkyflowErrorCode.ELEMENT_EMPTY_TABLE_NAME.getMessage()) + val skyflowError = SkyflowError( + SkyflowErrorCode.EMPTY_TABLE_KEY, params = arrayOf("0") + ) + assertEquals(skyflowError.getErrorMessage(), (exception as SkyflowError).message) } - }) } @Test - fun testEmptyColumnName() - { + fun testEmptyColumnName() { val records = JSONObject() val recordsArray = JSONArray() + val record = JSONObject() record.put("table", "cards") + val fields = JSONObject() fields.put("", "xyz") fields.put("cardNumber", "41111111111") fields.put("expiry_date","11/22") record.put("fields", fields) + recordsArray.put(record) records.put("records", recordsArray) - skyflow.insert(records, InsertOptions(),object : Callback - { - override fun onSuccess(responseBody: Any) { - } + + skyflow.insert(records, InsertOptions(), object : Callback { + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - assertEquals((exception as SkyflowError).message.toString(),SkyflowErrorCode.EMPTY_COLUMN_NAME.getMessage()) + val skyflowError = SkyflowError( + SkyflowErrorCode.EMPTY_FIELD_IN_FIELDS, params = arrayOf("0") + ) + assertEquals(skyflowError.getErrorMessage(), (exception as SkyflowError).message) } - }) } @Test - fun testMissingTable() - { + fun testMissingTable() { val records = JSONObject() val recordsArray = JSONArray() val record = JSONObject() + val fields = JSONObject() - // fields.put("table", "cards") fields.put("cardNumber", "41111111111") fields.put("expiry_date","11/22") record.put("fields", fields) + recordsArray.put(record) records.put("records", recordsArray) - skyflow.insert(records, InsertOptions(),object : Callback - { - override fun onSuccess(responseBody: Any) { - } + + skyflow.insert(records, InsertOptions(), object : Callback { + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - assertEquals((exception as SkyflowError).message.toString(),SkyflowErrorCode.MISSING_TABLE_IN_ELEMENT.getMessage()) + val skyflowError = SkyflowError( + SkyflowErrorCode.TABLE_KEY_NOY_FOUND, params = arrayOf("0") + ) + assertEquals(skyflowError.getErrorMessage(), (exception as SkyflowError).message) } }) } @Test - fun testMissingFields() - { + fun testMissingFields() { val records = JSONObject() val recordsArray = JSONArray() val record = JSONObject() @@ -206,15 +213,15 @@ class InsertTest { //record.put("fields", fields) recordsArray.put(record) records.put("records", recordsArray) - skyflow.insert(records, InsertOptions(),object : Callback - { - override fun onSuccess(responseBody: Any) { - } + skyflow.insert(records, InsertOptions(), object : Callback { + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - assertEquals((exception as SkyflowError).message,SkyflowErrorCode.FIELDS_KEY_ERROR.getMessage()) + val skyflowError = SkyflowError( + SkyflowErrorCode.FIELDS_KEY_NOT_FOUND, params = arrayOf("0") + ) + assertEquals(skyflowError.getErrorMessage(), (exception as SkyflowError).message) } - }) } @@ -245,27 +252,33 @@ class InsertTest { } @Test - fun testInvalidTableType() - { + fun testInvalidTableType() { val records = JSONObject() val recordsArray = JSONArray() + val record = JSONObject() record.put("table", JSONObject()) + val fields = JSONObject() fields.put("cardNumber", "41111111111") fields.put("expiry_date","11/22") + record.put("fields", fields) recordsArray.put(record) records.put("records", recordsArray) - skyflow.insert(records, InsertOptions(),object : Callback - { - override fun onSuccess(responseBody: Any) { - } + + skyflow.insert(records, InsertOptions(), object : Callback { + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - assertEquals((exception as SkyflowError).message,SkyflowErrorCode.INVALID_TABLE_NAME.getMessage()) + val skyflowError = SkyflowError( + SkyflowErrorCode.INVALID_TABLE_NAME, params = arrayOf("0") + ) + assertEquals( + skyflowError.getErrorMessage(), + (exception as SkyflowError).getErrorMessage() + ) } - }) } diff --git a/Skyflow/src/main/kotlin/Skyflow/Client.kt b/Skyflow/src/main/kotlin/Skyflow/Client.kt index be26ee1..d465822 100644 --- a/Skyflow/src/main/kotlin/Skyflow/Client.kt +++ b/Skyflow/src/main/kotlin/Skyflow/Client.kt @@ -127,58 +127,94 @@ class Client internal constructor( } internal fun constructBodyForGetById(records: JSONObject): MutableList { - if(!records.has("records")) - throw SkyflowError(SkyflowErrorCode.RECORDS_KEY_NOT_FOUND,tag, configuration.options.logLevel) - else if(records.get("records") !is JSONArray) - throw SkyflowError(SkyflowErrorCode.INVALID_RECORDS,tag, configuration.options.logLevel) + if (!records.has("records")) + throw SkyflowError( + SkyflowErrorCode.RECORDS_KEY_NOT_FOUND, + tag, configuration.options.logLevel + ) + else if (records.get("records") !is JSONArray) + throw SkyflowError(SkyflowErrorCode.INVALID_RECORDS, tag, configuration.options.logLevel) val jsonArray = records.getJSONArray("records") - if(jsonArray.length() == 0) - throw SkyflowError(SkyflowErrorCode.EMPTY_RECORDS,tag, configuration.options.logLevel) + if (jsonArray.length() == 0) + throw SkyflowError(SkyflowErrorCode.EMPTY_RECORDS, tag, configuration.options.logLevel) var i = 0 val result = mutableListOf() while (i < jsonArray.length()) { val jsonObj = jsonArray.getJSONObject(i) - if(jsonObj == {}) - throw SkyflowError(SkyflowErrorCode.EMPTY_RECORDS, tag, configuration.options.logLevel) + if (jsonObj == {}) + throw SkyflowError( + SkyflowErrorCode.EMPTY_RECORD_OBJECT, tag, configuration.options.logLevel, + arrayOf("$i") + ) else if (!jsonObj.has("table")) { - throw SkyflowError(SkyflowErrorCode.MISSING_TABLE_KEY,tag, configuration.options.logLevel) - } else if (jsonObj.get("table") !is String) - throw SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME,tag, configuration.options.logLevel) - else if (!jsonObj.has("redaction")) { - throw SkyflowError(SkyflowErrorCode.REDACTION_KEY_ERROR,tag, configuration.options.logLevel) + throw SkyflowError( + SkyflowErrorCode.TABLE_KEY_NOY_FOUND, tag, configuration.options.logLevel, + arrayOf("$i") + ) + } else if (jsonObj.get("table") !is String) { + throw SkyflowError( + SkyflowErrorCode.INVALID_TABLE_NAME, tag, configuration.options.logLevel, + arrayOf("$i") + ) + } else if (!jsonObj.has("redaction")) { + throw SkyflowError( + SkyflowErrorCode.REDACTION_KEY_NOT_FOUND, tag, configuration.options.logLevel, + arrayOf("$i") + ) } else if (!jsonObj.has("ids")) { - throw SkyflowError(SkyflowErrorCode.MISSING_IDS,tag, configuration.options.logLevel) + throw SkyflowError( + SkyflowErrorCode.IDS_KEY_NOT_FOUND, tag, configuration.options.logLevel, + arrayOf("$i") + ) } else if (jsonObj.getString("table").isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY,tag, configuration.options.logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_TABLE_KEY, tag, configuration.options.logLevel, + arrayOf("$i") + ) } else if (jsonObj.getString("redaction").isEmpty()) { - throw SkyflowError(SkyflowErrorCode.MISSING_REDACTION_VALUE,tag, configuration.options.logLevel) - } else if (!(jsonObj.get("redaction").toString() - .equals("PLAIN_TEXT") || jsonObj.get("redaction") - .toString() - .equals("DEFAULT") || - jsonObj.get("redaction").toString() - .equals("MASKED") || jsonObj.get("redaction") - .toString() - .equals("REDACTED")) + throw SkyflowError( + SkyflowErrorCode.EMPTY_REDACTION_VALUE, tag, configuration.options.logLevel, + arrayOf("$i") + ) + } else if (!(jsonObj.get("redaction").toString() == "PLAIN_TEXT" || + jsonObj.get("redaction").toString() == "DEFAULT" || + jsonObj.get("redaction").toString() == "MASKED" || + jsonObj.get("redaction").toString() == "REDACTED") ) { - throw SkyflowError(SkyflowErrorCode.INVALID_REDACTION_TYPE,tag, configuration.options.logLevel) + throw SkyflowError( + SkyflowErrorCode.INVALID_REDACTION_TYPE, tag, configuration.options.logLevel, + arrayOf("$i") + ) } else { var skyflowIds = jsonObj.get("ids") try { skyflowIds = skyflowIds as ArrayList - } - catch (e:Exception) - { - throw SkyflowError(SkyflowErrorCode.INVALID_RECORD_IDS,tag, configuration.options.logLevel) + } catch (e: Exception) { + throw SkyflowError( + SkyflowErrorCode.INVALID_IDS, tag, configuration.options.logLevel, + arrayOf("$i") + ) } if (skyflowIds.isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_RECORD_IDS,tag, configuration.options.logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_RECORD_IDS, tag, configuration.options.logLevel, + arrayOf("$i") + ) } for (j in 0 until skyflowIds.size) { - if (skyflowIds.get(j).isEmpty()) - throw SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID,tag, configuration.options.logLevel) + if (skyflowIds[j].isEmpty()) { + throw SkyflowError( + SkyflowErrorCode.EMPTY_ID_IN_RECORD_IDS, + tag, configuration.options.logLevel, + arrayOf("$i") + ) + } } - val record = GetByIdRecord(skyflowIds, jsonObj.get("table").toString(), jsonObj.get("redaction").toString()) + val record = GetByIdRecord( + skyflowIds, + jsonObj.get("table").toString(), + jsonObj.get("redaction").toString() + ) result.add(record) } i++ diff --git a/Skyflow/src/main/kotlin/Skyflow/RevealContainer.kt b/Skyflow/src/main/kotlin/Skyflow/RevealContainer.kt index bc9cd62..5efb4e8 100644 --- a/Skyflow/src/main/kotlin/Skyflow/RevealContainer.kt +++ b/Skyflow/src/main/kotlin/Skyflow/RevealContainer.kt @@ -63,22 +63,22 @@ internal fun Container.validateElements() { val token = element.revealInput.token if (!checkIfElementsMounted(element)) { throw SkyflowError( - SkyflowErrorCode.ELEMENT_NOT_MOUNTED, - tag, - configuration.options.logLevel, + SkyflowErrorCode.ELEMENT_NOT_MOUNTED_REVEAL, tag, configuration.options.logLevel, arrayOf(element.revealInput.label) ) } if (element.isTokenNull) { - throw SkyflowError(SkyflowErrorCode.MISSING_TOKEN, tag, configuration.options.logLevel) + throw SkyflowError( + SkyflowErrorCode.TOKEN_KEY_NOT_FOUND_REVEAL, tag, configuration.options.logLevel, + ) } else if (token!!.isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID, tag, configuration.options.logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_TOKEN_REVEAL, tag, configuration.options.logLevel + ) } else if (element.isError) { throw SkyflowError( - SkyflowErrorCode.INVALID_INPUT, - tag, - configuration.options.logLevel, + SkyflowErrorCode.ERROR_STATE_REVEAL, tag, configuration.options.logLevel, arrayOf("${element.error.text}") ) } diff --git a/Skyflow/src/main/kotlin/Skyflow/SkyflowError.kt b/Skyflow/src/main/kotlin/Skyflow/SkyflowError.kt index 0e7280c..a7ebad8 100644 --- a/Skyflow/src/main/kotlin/Skyflow/SkyflowError.kt +++ b/Skyflow/src/main/kotlin/Skyflow/SkyflowError.kt @@ -17,7 +17,7 @@ class SkyflowError(val skyflowErrorCode: SkyflowErrorCode = SkyflowErrorCode.UNK if(logLevel != null) Logger.error(tag, logMessage, logLevel) this.internalMessage = logMessage - this.message = "Interface : $tag - $logMessage" + this.message = logMessage } fun setErrorCode(code:Int) { diff --git a/Skyflow/src/main/kotlin/Skyflow/SkyflowErrorCode.kt b/Skyflow/src/main/kotlin/Skyflow/SkyflowErrorCode.kt index ba8b818..a5bf26c 100644 --- a/Skyflow/src/main/kotlin/Skyflow/SkyflowErrorCode.kt +++ b/Skyflow/src/main/kotlin/Skyflow/SkyflowErrorCode.kt @@ -2,39 +2,37 @@ package Skyflow import Skyflow.core.Messages - -enum class SkyflowErrorCode(val code:Int, var message:String) -{ +enum class SkyflowErrorCode(val code:Int, var message:String) { INVALID_VAULT_ID(400, Messages.INVALID_VAULT_ID.message), INVALID_VAULT_URL(400,Messages.INVALID_VAULT_URL.message), EMPTY_VAULT_ID(400,Messages.EMPTY_VAULT_ID.message), EMPTY_VAULT_URL(400,Messages.EMPTY_VAULT_URL.message), INVALID_BEARER_TOKEN(400,Messages.INVALID_BEARER_TOKEN.message), + BEARER_TOKEN_REJECTED(400,Messages.BEARER_TOKEN_REJECTED.message), INVALID_TABLE_NAME(400,Messages.INVALID_TABLE_NAME.message), ELEMENT_EMPTY_TABLE_NAME(400,Messages.ELEMENT_EMPTY_TABLE_NAME.message), EMPTY_TABLE_KEY(400, Messages.EMPTY_TABLE_KEY.message), EMPTY_COLUMN_KEY(400,Messages.EMPTY_COLUMN_KEY.message), RECORDS_KEY_NOT_FOUND(400,Messages.RECORDS_KEY_NOT_FOUND.message), - ADDITIONAL_FIELDS_RECORDS_KEY_NOT_FOUND(400, Messages.ADDITION_RECORDS_KEY_KEY_NOT_FOUND.message), EMPTY_RECORDS(400,Messages.EMPTY_RECORDS.message), TABLE_KEY_ERROR(400,Messages.TABLE_KEY_ERROR.message), - FIELDS_KEY_ERROR(400,Messages.FIELDS_KEY_ERROR.message), + FIELDS_KEY_NOT_FOUND(400,Messages.FIELDS_KEY_NOT_FOUND.message), + EMPTY_FIELD_IN_FIELDS(400,Messages.EMPTY_FIELD_IN_FIELDS.message), INVALID_COLUMN_NAME(400,Messages.INVALID_COLUMN_NAME.message), EMPTY_COLUMN_NAME(400,Messages.EMPTY_COLUMN_NAME.message), INVALID_TOKEN_ID(400,Messages.INVALID_TOKEN_ID.message), // response is in success only, getting both successful and unsuccessful records - EMPTY_TOKEN_ID(400,Messages.EMPTY_TOKEN_ID.message), + EMPTY_TOKEN(400,Messages.EMPTY_TOKEN.message), ID_KEY_ERROR(400,Messages.ID_KEY_ERROR.message), - REDACTION_KEY_ERROR(400,Messages.REDACTION_KEY_ERROR.message), + REDACTION_KEY_NOT_FOUND(400,Messages.REDACTION_KEY_NOT_FOUND.message), INVALID_REDACTION_TYPE(400,Messages.INVALID_REDACTION_TYPE.message), INVALID_FIELD(400,Messages.INVALID_FIELD.message), - MISSING_TOKEN(400,Messages.MISSING_TOKEN.message), + TOKEN_KEY_NOT_FOUND(400,Messages.TOKEN_KEY_NOT_FOUND.message), MISSING_TOKEN_IN_CONNECTION_REQUEST(400, Messages.MISSING_TOKEN_IN_CONNECTION_REQUEST.message), - MISSING_IDS(404,Messages.MISSING_KEY_IDS.message), + IDS_KEY_NOT_FOUND(404,Messages.IDS_KEY_NOT_FOUND.message), EMPTY_RECORD_IDS(400,Messages.EMPTY_RECORD_IDS.message), - INVALID_RECORD_ID_TYPE(400,Messages.INVALID_RECORD_ID_TYPE.message), MISSING_TABLE_IN_ELEMENT(400,Messages.MISSING_TABLE_IN_ELEMENT.message), - MISSING_TABLE_KEY(400, Messages.MISSING_TABLE_KEY.message), + TABLE_KEY_NOY_FOUND(400, Messages.TABLE_KEY_NOT_FOUND.message), INVALID_RECORD_TABLE_VALUE(400,Messages.INVALID_RECORD_TABLE_VALUE.message), INVALID_CONNECTION_URL(400,Messages.INVALID_CONNECTION_URL.message), EMPTY_CONNECTION_URL(400,Messages.EMPTY_CONNECTION_URL.message), @@ -45,12 +43,13 @@ enum class SkyflowErrorCode(val code:Int, var message:String) UNKNOWN_ERROR(400,Messages.UNKNOWN_ERROR.message), TRANSACTION_ERROR(400,Messages.TRANSACTION_ERROR.message), CONNECTION_ERROR(400,Messages.CONNECTION_ERROR.message), - MISSING_REDACTION_VALUE(400,Messages.MISSING_REDACTION_VALUE.message), + EMPTY_REDACTION_VALUE(400,Messages.EMPTY_REDACTION_VALUE.message), ELEMENT_NOT_MOUNTED(400,Messages.ELEMENT_NOT_MOUNTED.message), DUPLICATE_COLUMN_FOUND(400,Messages.DUPLICATE_COLUMN_FOUND.message), DUPLICATE_ELEMENT_FOUND(400,Messages.DUPLICATE_ELEMENT_FOUND.message), INVALID_RECORDS(400,Messages.INVALID_RECORDS_TYPE.message), - INVALID_RECORD_IDS(400,Messages.INVALID_RECORD_IDS.message), + INVALID_IDS(400,Messages.INVALID_IDS.message), + EMPTY_ID_IN_RECORD_IDS(400, Messages.EMPTY_ID_IN_RECORD_IDS.message), MISSING_REDACTION(400,Messages.MISSING_REDACTION.message), EMPTY_KEY_IN_REQUEST_BODY(400,Messages.EMPTY_KEY_IN_REQUEST_BODY.message), EMPTY_KEY_IN_QUERY_PARAMS(400,Messages.EMPTY_KEY_IN_QUERY_PARAMS.message), @@ -86,15 +85,14 @@ enum class SkyflowErrorCode(val code:Int, var message:String) EMPTY_UPSERT_OPTIONS_ARRAY(400,Messages.EMPTY_UPSERT_OPTIONS_ARRAY.message), EMPTY_RECORD_OBJECT(400, Messages.EMPTY_RECORD_OBJECT.message), EMPTY_RECORD_IDS_IN_GET(400, Messages.EMPTY_RECORD_IDS_IN_GET.message), - INVALID_RECORD_IDS_TYPE(400, Messages.INVALID_RECORD_IDS_TYPE.message), - EMPTY_ID(400, Messages.EMPTY_ID.message), + INVALID_ID_IN_RECORD_IDS(400, Messages.INVALID_ID_IN_RECORD_IDS.message), REDACTION_WITH_TOKENS_NOT_SUPPORTED(400, Messages.REDACTION_WITH_TOKENS_NOT_SUPPORTED.message), TOKENS_NOT_SUPPORTED_WITH_COLUMN_DETAILS( 400, Messages.TOKENS_NOT_SUPPORTED_WITH_COLUMN_DETAILS.message ), - MISSING_RECORD_COLUMN_VALUES(400, Messages.MISSING_RECORD_COLUMN_VALUES.message), - MISSING_RECORD_COLUMN_NAME(400, Messages.MISSING_RECORD_COLUMN_NAME.message), + MISSING_RECORD_COLUMN_VALUES(400, Messages.RECORD_COLUMN_VALUES_NOT_FOUND.message), + MISSING_RECORD_COLUMN_NAME(400, Messages.RECORD_COLUMN_NAME_NOT_FOUND.message), INVALID_RECORD_COLUMN_NAME_TYPE(400,Messages.INVALID_RECORD_COLUMN_NAME_TYPE.message), EMPTY_RECORD_COLUMN_NAME(400, Messages.EMPTY_RECORD_COLUMN_NAME.message), INVALID_RECORD_COLUMN_VALUES_TYPE(400, Messages.INVALID_RECORD_COLUMN_VALUES_TYPE.message), @@ -106,20 +104,36 @@ enum class SkyflowErrorCode(val code:Int, var message:String) Messages.BOTH_IDS_AND_COLUMN_DETAILS_SPECIFIED.message ), NEITHER_IDS_NOR_COLUMN_DETAILS_SPECIFIED( - 400, - Messages.NEITHER_IDS_NOR_COLUMN_DETAILS_SPECIFIED.message + 400, Messages.NEITHER_IDS_NOR_COLUMN_DETAILS_SPECIFIED.message ), FAILED_TO_GET(400,Messages.FAILED_TO_GET.message), - MISMATCH_ELEMENT_COUNT_LAYOUT_SUM(400, Messages.MISMATCH_ELEMENT_COUNT_LAYOUT_SUM.message); + MISMATCH_ELEMENT_COUNT_LAYOUT_SUM(400, Messages.MISMATCH_ELEMENT_COUNT_LAYOUT_SUM.message), + + TOKEN_KEY_NOT_FOUND_REVEAL(400, Messages.TOKEN_KEY_NOT_FOUND_REVEAL.message), + EMPTY_TOKEN_REVEAL(400, Messages.EMPTY_TOKEN_REVEAL.message), + ELEMENT_NOT_MOUNTED_REVEAL(400, Messages.ELEMENT_NOT_MOUNTED_REVEAL.message), + ERROR_STATE_REVEAL(400, Messages.ERROR_STATE_REVEAL.message), + + ADDITIONAL_FIELDS_RECORDS_KEY_NOT_FOUND(400, Messages.ADDITIONAL_FIELDS_RECORDS_KEY_NOT_FOUND.message), + ADDITIONAL_FIELDS_INVALID_RECORDS(400,Messages.ADDITIONAL_FIELDS_INVALID_RECORDS_TYPE.message), + ADDITIONAL_FIELDS_EMPTY_RECORDS(400, Messages.ADDITIONAL_FIELDS_EMPTY_RECORDS.message), + ADDITIONAL_FIELDS_TABLE_KEY_NOT_FOUND(400, Messages.ADDITIONAL_FIELDS_TABLE_KEY_NOT_FOUND.message), + ADDITIONAL_FIELDS_EMPTY_TABLE_KEY(400, Messages.ADDITIONAL_FIELDS_EMPTY_TABLE_KEY.message), + ADDITIONAL_FIELDS_INVALID_TABLE_NAME(400, Messages.ADDITIONAL_FIELDS_INVALID_TABLE_NAME.message), + ADDITIONAL_FIELDS_FIELDS_KEY_NOT_FOUND(400, Messages.ADDITIONAL_FIELDS_FIELDS_KEY_NOT_FOUND.message), + ADDITIONAL_FIELDS_EMPTY_FIELDS(400, Messages.ADDITIONAL_FIELDS_EMPTY_FIELDS.message), + + EMPTY_TOKEN_ID(400, Messages.EMPTY_TOKEN_ID.message), + MISSING_TOKEN(400, Messages.MISSING_TOKEN.message) + ; + @JvmName("getCode1") - fun getCode() : Int - { + fun getCode() : Int { return this.code } @JvmName("getMessage1") - fun getMessage() : String - { + fun getMessage() : String { return this.message } } \ No newline at end of file diff --git a/Skyflow/src/main/kotlin/Skyflow/TextField.kt b/Skyflow/src/main/kotlin/Skyflow/TextField.kt index 56c546f..cf74ae3 100644 --- a/Skyflow/src/main/kotlin/Skyflow/TextField.kt +++ b/Skyflow/src/main/kotlin/Skyflow/TextField.kt @@ -100,10 +100,10 @@ class TextField @JvmOverloads constructor( var builtinValidationError = "" if (isRequired && str.isEmpty()) { builtinValidationError = "value is empty" - if(collectInput.label.isEmpty()) + if (collectInput.label.isEmpty()) setErrorText("Field is required") else - setErrorText(collectInput.label +" is required") + setErrorText("${collectInput.label} is required") return builtinValidationError } @@ -126,7 +126,7 @@ class TextField @JvmOverloads constructor( if (collectInput.label.isEmpty()) setErrorText("Invalid value") else - setErrorText("Invalid " + collectInput.label) + setErrorText("Invalid ${collectInput.label}") return builtinValidationError } } diff --git a/Skyflow/src/main/kotlin/Skyflow/collect/client/CollectRequestBody.kt b/Skyflow/src/main/kotlin/Skyflow/collect/client/CollectRequestBody.kt index 4c00bc4..6ff65f0 100644 --- a/Skyflow/src/main/kotlin/Skyflow/collect/client/CollectRequestBody.kt +++ b/Skyflow/src/main/kotlin/Skyflow/collect/client/CollectRequestBody.kt @@ -4,9 +4,6 @@ import Skyflow.* import Skyflow.collect.elements.validations.ElementValueMatchRule import org.json.JSONArray import org.json.JSONObject -import android.util.Log -import com.google.gson.JsonObject -import kotlin.Exception internal class CollectRequestBody { companion object { @@ -15,34 +12,34 @@ internal class CollectRequestBody { elements: MutableList, additionalFields: JSONObject?, logLevel: LogLevel - ) :String - { - val tableMap: HashMap> = HashMap() - val tableWithColumn : HashSet = HashSet() + ): String { + val tableMap: HashMap> = HashMap() + val tableWithColumn: HashSet = HashSet() for (element in elements) { - if (tableMap[(element.tableName)] != null){ - if(tableWithColumn.contains(element.tableName+element.columnName)) - { + if (tableMap[(element.tableName)] != null) { + if (tableWithColumn.contains(element.tableName + element.columnName)) { var hasElementValueMatchRule: Boolean = false - for(validation in element.collectInput.validations.rules) { - if(validation is ElementValueMatchRule) { + for (validation in element.collectInput.validations.rules) { + if (validation is ElementValueMatchRule) { hasElementValueMatchRule = true break; } } - if(!hasElementValueMatchRule) - throw SkyflowError(SkyflowErrorCode.DUPLICATE_COLUMN_FOUND, tag, logLevel, arrayOf(element.tableName,element.columnName)) + if (!hasElementValueMatchRule) + throw SkyflowError( + SkyflowErrorCode.DUPLICATE_COLUMN_FOUND, tag, logLevel, + arrayOf(element.tableName, element.columnName) + ) continue; } - tableWithColumn.add(element.tableName+element.columnName) - val obj = CollectRequestRecord(element.columnName,element.getValue()) + tableWithColumn.add(element.tableName + element.columnName) + val obj = CollectRequestRecord(element.columnName, element.getValue()) tableMap[(element.tableName)]!!.add(obj) - } - else{ - val obj = CollectRequestRecord(element.columnName,element.getValue()) + } else { + val obj = CollectRequestRecord(element.columnName, element.getValue()) val tempArray = mutableListOf() tempArray.add(obj) - tableWithColumn.add(element.tableName+element.columnName) + tableWithColumn.add(element.tableName + element.columnName) tableMap[(element.tableName)] = tempArray } } @@ -50,41 +47,66 @@ internal class CollectRequestBody { if (additionalFields != null) { if (additionalFields.has("records")) { if (additionalFields.get("records") !is JSONArray) - throw SkyflowError(SkyflowErrorCode.INVALID_RECORDS, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_INVALID_RECORDS, tag, logLevel + ) val records = additionalFields.getJSONArray("records") if (records.length() == 0) { - throw SkyflowError(SkyflowErrorCode.EMPTY_RECORDS, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_EMPTY_RECORDS, tag, logLevel + ) } var i = 0 while (i < records.length()) { val jsonobj = records.getJSONObject(i) if (!jsonobj.has("table")) - throw SkyflowError(SkyflowErrorCode.MISSING_TABLE_KEY, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_TABLE_KEY_NOT_FOUND, + tag, logLevel, arrayOf("$i") + ) else if (!jsonobj.has("fields")) - throw SkyflowError(SkyflowErrorCode.FIELDS_KEY_ERROR, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_FIELDS_KEY_NOT_FOUND, + tag, logLevel, arrayOf("$i") + ) else if (jsonobj.getJSONObject("fields").toString() == "{}") - throw SkyflowError(SkyflowErrorCode.EMPTY_FIELDS, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_EMPTY_FIELDS, tag, logLevel, + arrayOf("$i") + ) val tableName = jsonobj.get("table") if (tableName !is String) - throw SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_INVALID_TABLE_NAME, + tag, logLevel, arrayOf("$i") + ) if (tableName.isEmpty()) - throw SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_EMPTY_TABLE_KEY, tag, logLevel, + arrayOf("$i") + ) if (jsonobj.getJSONObject("fields").toString() != "{}") { val fields = jsonobj.getJSONObject("fields") val keys = fields.names() val fieldList = mutableListOf() for (j in 0 until keys!!.length()) { if (keys.getString(j).isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_COLUMN_NAME, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_COLUMN_NAME, tag, logLevel + ) } - val obj = CollectRequestRecord(keys.getString(j), fields.get(keys.getString(j)) + val obj = CollectRequestRecord( + keys.getString(j), fields.get(keys.getString(j)) ) fieldList.add(obj) } if (tableMap[tableName] != null) { for (k in 0 until fieldList.size) { if (tableWithColumn.contains(tableName + fieldList[k].columnName)) - throw SkyflowError(SkyflowErrorCode.DUPLICATE_COLUMN_FOUND, tag, logLevel, arrayOf(tableName, fieldList[k].columnName)) + throw SkyflowError( + SkyflowErrorCode.DUPLICATE_COLUMN_FOUND, tag, logLevel, + arrayOf(tableName, fieldList[k].columnName) + ) else tableWithColumn.add(tableName + fieldList[k].columnName) @@ -94,7 +116,12 @@ internal class CollectRequestBody { val tempArray = mutableListOf() for (k in 0 until fieldList.size) { if (tableWithColumn.contains(tableName + fieldList[k].columnName)) - throw SkyflowError(SkyflowErrorCode.DUPLICATE_COLUMN_FOUND, tag, logLevel, arrayOf(tableName, fieldList[k].columnName)) + throw SkyflowError( + SkyflowErrorCode.DUPLICATE_COLUMN_FOUND, + tag, + logLevel, + arrayOf(tableName, fieldList[k].columnName) + ) else tableWithColumn.add(tableName + fieldList[k].columnName) } @@ -105,15 +132,17 @@ internal class CollectRequestBody { i++ } } else - throw SkyflowError(SkyflowErrorCode.ADDITIONAL_FIELDS_RECORDS_KEY_NOT_FOUND, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_RECORDS_KEY_NOT_FOUND, tag, logLevel + ) } val recordsArray = JSONArray() val requestObject = JSONObject() - for ((key, value ) in tableMap){ + for ((key, value) in tableMap) { val recordObject = JSONObject() recordObject.put("table", key) val fieldsObject = JSONObject() - for (element in value){ + for (element in value) { createJSONKey(fieldsObject, element.columnName, element.value) } recordObject.put("fields", fieldsObject) @@ -123,19 +152,22 @@ internal class CollectRequestBody { return requestObject.toString() } - private fun createJSONKey(fieldsObject: JSONObject, columnName: String, value: Any){ + private fun createJSONKey(fieldsObject: JSONObject, columnName: String, value: Any) { val keys = columnName.split(".").toTypedArray() - if(fieldsObject.has(keys[0])){ - if(keys.size > 1){ - createJSONKey(fieldsObject.get(keys[0]) as JSONObject, keys.drop(1).joinToString("."), value) + if (fieldsObject.has(keys[0])) { + if (keys.size > 1) { + createJSONKey( + fieldsObject.get(keys[0]) as JSONObject, + keys.drop(1).joinToString("."), + value + ) } - }else{ - if(keys.size > 1){ + } else { + if (keys.size > 1) { val tempObject = JSONObject() fieldsObject.put(keys[0], tempObject) createJSONKey(tempObject, keys.drop(1).joinToString("."), value) - } - else { + } else { fieldsObject.put(keys[0], value) } } diff --git a/Skyflow/src/main/kotlin/Skyflow/core/APIClient.kt b/Skyflow/src/main/kotlin/Skyflow/core/APIClient.kt index 624b9fe..f037480 100644 --- a/Skyflow/src/main/kotlin/Skyflow/core/APIClient.kt +++ b/Skyflow/src/main/kotlin/Skyflow/core/APIClient.kt @@ -13,7 +13,6 @@ import Skyflow.soap.SoapApiCallback import Skyflow.soap.SoapConnectionConfig import Skyflow.soap.SoapValueCallback import Skyflow.utils.Utils -import com.skyflow_android.BuildConfig import org.json.JSONArray import org.json.JSONObject import java.io.UnsupportedEncodingException @@ -71,7 +70,7 @@ internal class APIClient( Logger.info(tag, Messages.BEARER_TOKEN_RECEIVED.getMessage(), logLevel) if (!isValidToken(responseBody.toString())) { val error = - SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel, arrayOf("Android", "${BuildConfig.SDK_VERSION}")) + SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel) callback.onFailure(error) } else { token = "Bearer $responseBody" @@ -86,7 +85,7 @@ internal class APIClient( logLevel ) val error = - SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel, arrayOf("Android", "${BuildConfig.SDK_VERSION}")) + SkyflowError(SkyflowErrorCode.BEARER_TOKEN_REJECTED, tag, logLevel) callback.onFailure(error) } }) @@ -94,7 +93,7 @@ internal class APIClient( callback.onSuccess(token) } } catch (e: Exception) { - val error = SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel, arrayOf("Android", "${BuildConfig.SDK_VERSION}")) + val error = SkyflowError(SkyflowErrorCode.INVALID_BEARER_TOKEN, tag, logLevel) callback.onFailure(error) } } @@ -155,33 +154,35 @@ internal class APIClient( if (!records.has("records")) { throw SkyflowError(SkyflowErrorCode.RECORDS_KEY_NOT_FOUND, tag, logLevel) } - if (records.get("records").toString().isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_RECORDS, tag, logLevel) - } if (records.get("records") !is JSONArray) { throw SkyflowError(SkyflowErrorCode.INVALID_RECORDS, tag, logLevel) } - if (!records.has("records")) - throw SkyflowError(SkyflowErrorCode.RECORDS_KEY_NOT_FOUND, tag, logLevel) - else if (records.get("records") !is JSONArray) - throw SkyflowError(SkyflowErrorCode.INVALID_RECORDS, tag, logLevel) - val jsonArray = records.getJSONArray("records") - if (jsonArray.length() == 0) + if (records.getJSONArray("records").length() == 0) { throw SkyflowError(SkyflowErrorCode.EMPTY_RECORDS, tag, logLevel) + } + val jsonArray = records.getJSONArray("records") val list = mutableListOf() var i = 0 while (i < jsonArray.length()) { val recordObject = jsonArray.getJSONObject(i) if (!recordObject.has("token")) { - throw SkyflowError(SkyflowErrorCode.MISSING_TOKEN, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.TOKEN_KEY_NOT_FOUND, tag, logLevel, arrayOf("$i") + ) } else if (recordObject.get("token").toString().isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_TOKEN, tag, logLevel, arrayOf("$i") + ) } else if (recordObject.has("redaction")) { val redaction = recordObject.get("redaction") if (redaction.toString().isEmpty()) { - throw SkyflowError(SkyflowErrorCode.MISSING_REDACTION_VALUE, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_REDACTION_VALUE, tag, logLevel, arrayOf("$i") + ) } else if (redaction !is RedactionType) { - throw SkyflowError(SkyflowErrorCode.INVALID_REDACTION_TYPE, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.INVALID_REDACTION_TYPE, tag, logLevel, arrayOf("$i") + ) } else { list.add( RevealRequestRecord( diff --git a/Skyflow/src/main/kotlin/Skyflow/core/Messages.kt b/Skyflow/src/main/kotlin/Skyflow/core/Messages.kt index 1065902..853ac27 100644 --- a/Skyflow/src/main/kotlin/Skyflow/core/Messages.kt +++ b/Skyflow/src/main/kotlin/Skyflow/core/Messages.kt @@ -1,6 +1,9 @@ package Skyflow.core import Skyflow.utils.Utils +import com.skyflow_android.BuildConfig + +private const val SDK_NAME_VERSION = "Android SDK v${BuildConfig.SDK_VERSION}" enum class Messages(val message: String) { INVALID_URL("Invalid client credentials. Expecting \"https://XYZ\" for vaultURL"), @@ -47,51 +50,114 @@ enum class Messages(val message: String) { GETTING_RECORDS_BY_ID_CALLED("retrieving records using skyflow ids"), INVOKE_CONNECTION_CALLED("invokeConnection method called"), - + // client config validations INVALID_VAULT_ID("vault id invalid cannot be found"), - INVALID_VAULT_URL("%s SDK v%s Initialization failed. Invalid client credentials. 'vaultURL' must be begin with 'https://'."), - EMPTY_VAULT_ID("%s SDK v%s Initialization failed. Invalid credentials. Specify a valid 'vaultID'."), - EMPTY_VAULT_URL("%s SDK v%s Initialization failed. Invalid credentials. Specify a valid 'vaultURL'."), - INVALID_BEARER_TOKEN("%s SDK v%s Initialization failed. Invalid client credentials. Specify a valid bearer token."), - INVALID_TABLE_NAME("%s SDK v%s Validation error. Invalid 'table' key in records at index %s. Specify a value of type string instead."), - ELEMENT_EMPTY_TABLE_NAME("element with type %s - Table key cannot be empty."), - EMPTY_TABLE_KEY("%s SDK v%s Validation error.'table' cannot be empty in records at index %s. Specify a non-empty value for 'table'."), - EMPTY_COLUMN_KEY("column key cannot be empty"), - RECORDS_KEY_NOT_FOUND("%s SDK v%s Validation error. Missing 'records' key. Provide a valid 'records' key."), - ADDITION_RECORDS_KEY_KEY_NOT_FOUND("%s SDK v%s Validation error.'records' key not found in additionalFields. Specify a 'records' key in additionalFields."), - EMPTY_RECORDS("%s SDK v%s Validation error. 'records' key cannot be empty. Provide a non-empty value instead."), + EMPTY_VAULT_ID("$SDK_NAME_VERSION Initialization failed. Invalid credentials. Specify a valid 'vaultID'."), + INVALID_VAULT_URL("$SDK_NAME_VERSION Initialization failed. Invalid client credentials. 'vaultURL' must be begin with 'https://'."), + EMPTY_VAULT_URL("$SDK_NAME_VERSION Initialization failed. Invalid credentials. Specify a valid 'vaultURL'."), + INVALID_BEARER_TOKEN("$SDK_NAME_VERSION Token generated from 'getBearerToken' callback function is invalid. Make sure the implementation of 'getBearerToken' is correct."), + BEARER_TOKEN_REJECTED("$SDK_NAME_VERSION 'getBearerToken' callback function call failed with rejected promise. Make sure the implementation of 'getBearerToken' is correct."), + + // record validations + RECORDS_KEY_NOT_FOUND("$SDK_NAME_VERSION Validation error. Missing 'records' key. Provide a valid 'records' key."), + EMPTY_RECORDS("$SDK_NAME_VERSION Validation error. 'records' key cannot be empty. Provide a non-empty value instead."), + INVALID_RECORDS_TYPE("$SDK_NAME_VERSION Validation error. Invalid 'records' key found. Specify a value of type array instead."), + EMPTY_RECORD_OBJECT("$SDK_NAME_VERSION Validation error. 'records' key cannot be an array of empty objects at index %s. Specify non-empty objects instead."), + + // ids validations + IDS_KEY_NOT_FOUND("$SDK_NAME_VERSION Validation error. Missing 'ids' key in records at index %s. Provide a valid 'ids' key."), + INVALID_IDS("$SDK_NAME_VERSION Validation error. Invalid 'ids' key found in records at index %s. Specify a value of type array instead."), + EMPTY_RECORD_IDS("$SDK_NAME_VERSION Validation error. 'ids' key cannot be an empty array in records at index %s. Make sure to provide at least one id in array."), + EMPTY_ID_IN_RECORD_IDS("$SDK_NAME_VERSION Validation error. 'id' cannot be empty in 'ids' array in 'records' at index %s. Specify non-empty values instead."), + INVALID_ID_IN_RECORD_IDS("$SDK_NAME_VERSION Validation error. Invalid 'id' found in 'ids' array in 'records' at index %s. Specify a value of type string instead."), + EMPTY_RECORD_IDS_IN_GET("$SDK_NAME_VERSION Validation error. 'ids' key cannot be an empty array in records at index %s. Make sure to provide at least one id in array."), + + // table validations + TABLE_KEY_NOT_FOUND("$SDK_NAME_VERSION Validation error.'table' key not found in records at index %s. Provide a valid 'table' key."), + EMPTY_TABLE_KEY("$SDK_NAME_VERSION Validation error.'table' cannot be empty in records at index %s. Specify a non-empty value instead."), + INVALID_TABLE_NAME("$SDK_NAME_VERSION Validation error. Invalid 'table' key in records at index %s. Specify a value of type string instead."), + + // redaction validations + REDACTION_KEY_NOT_FOUND("$SDK_NAME_VERSION Validation error. Missing 'redaction' key in records at index %s. Provide a valid 'redaction' key."), + EMPTY_REDACTION_VALUE("$SDK_NAME_VERSION Validation error. 'redaction' key cannot be empty in records at index %s. Specify a non-empty value instead."), + INVALID_REDACTION_TYPE("$SDK_NAME_VERSION Validation error. Invalid 'redaction' key in records at index %s. Specify a valid redaction type."), + + // special case validations - get + REDACTION_WITH_TOKENS_NOT_SUPPORTED("$SDK_NAME_VERSION Get failed. Redaction cannot be applied when 'tokens' are set to true in get options. Either remove redaction or set 'tokens' to false."), + TOKENS_NOT_SUPPORTED_WITH_COLUMN_DETAILS("$SDK_NAME_VERSION Validation error. 'columnName' and 'columnValues' cannot be used when 'tokens' are set to true in get options. Either set 'tokens' to false or use 'ids' instead."), + NEITHER_IDS_NOR_COLUMN_DETAILS_SPECIFIED("$SDK_NAME_VERSION Validation error. Both 'ids' or 'columnValues' keys are missing. Either provide 'ids' or 'columnValues' with 'columnName' to fetch records."), + BOTH_IDS_AND_COLUMN_DETAILS_SPECIFIED("$SDK_NAME_VERSION Validation error. 'ids' cannot be used when 'columnName' and 'columnValues' are passed in records at index %s. Either use 'ids' or 'columnName' and 'columnValues'."), + + // column name validations - get + RECORD_COLUMN_NAME_NOT_FOUND("$SDK_NAME_VERSION Validation error. Missing 'columnName' in records at index %s. Provide a valid 'columnName' key."), + EMPTY_RECORD_COLUMN_NAME("$SDK_NAME_VERSION Validation error. 'columnName' cannot be empty in records at index %s. Specify a non-empty value for instead."), + INVALID_RECORD_COLUMN_NAME_TYPE("$SDK_NAME_VERSION Validation error. Invalid 'columnName' found in records at index %s. Specify a value of type string instead."), + + // column values validations - get + RECORD_COLUMN_VALUES_NOT_FOUND("$SDK_NAME_VERSION Validation error. Missing 'columnValues' in records at index %s. Provide a valid 'columnValues' key."), + EMPTY_RECORD_COLUMN_VALUES("$SDK_NAME_VERSION Validation error. 'columnValues' cannot be an empty array in records at index %s. Make sure to provide at least one columnValue in array."), + INVALID_RECORD_COLUMN_VALUES_TYPE("$SDK_NAME_VERSION Validation error. Invalid 'columnValues' key found. Specify a value of type array instead."), + EMPTY_COLUMN_VALUE("$SDK_NAME_VERSION Validation error. 'columnValue' cannot be empty in 'columnValues' array in 'records' at index %s. Specify non-empty values instead."), + INVALID_COLUMN_VALUE_TYPE("$SDK_NAME_VERSION Validation error. Invalid 'columnValue' found in 'columnValues' array in 'records' at index %s. Specify a value of type string instead."), + + // token validations - detokenize + TOKEN_KEY_NOT_FOUND("$SDK_NAME_VERSION Validation error. Missing 'token' key in records at index %s. Provide a valid 'token' key."), + EMPTY_TOKEN("$SDK_NAME_VERSION Validation error. 'token' key cannot be empty in records at index %s. Specify a non-empty value instead."), + + // fields object validations - insert + FIELDS_KEY_NOT_FOUND("$SDK_NAME_VERSION Validation error. Missing 'fields' key in records at index %s. Provide a valid 'fields' key."), + EMPTY_FIELDS("$SDK_NAME_VERSION Validation error. Missing 'fields' key in records at index %s. Provide a valid 'fields' key."), + EMPTY_FIELD_IN_FIELDS("$SDK_NAME_VERSION Validation error. 'field' cannot be empty in 'fields' in 'records' at index %s. Specify non-empty values instead."), + + // upsert options validations - insert + EMPTY_UPSERT_OPTIONS_ARRAY("$SDK_NAME_VERSION Validation error. 'upsert' key cannot be an empty array in insert options. Make sure to add at least one table column object in upsert array."), + ALLOW_JSON_OBJECT_IN_UPSERT("$SDK_NAME_VERSION Validation error. Invalid value in upsert array at index %s in insert options. Specify objects with 'table' and 'column' keys instead."), + NO_TABLE_KEY_IN_UPSERT("$SDK_NAME_VERSION Validation error. Missing 'table' key in upsert array at index %s. Provide a valid 'table' key."), + NO_COLUMN_KEY_IN_UPSERT("$SDK_NAME_VERSION Validation error. Missing 'column' key in upsert array at index %s. Provide a valid 'column' key."), + INVALID_TABLE_IN_UPSERT_OPTION("$SDK_NAME_VERSION Validation error. Invalid 'table' key in upsert array at index %s. Specify a value of type string instead."), + INVALID_COLUMN_IN_UPSERT_OPTION("$SDK_NAME_VERSION Validation error. Invalid 'column' key in upsert array at index %s. Specify a value of type string instead."), + + // reveal validations + TOKEN_KEY_NOT_FOUND_REVEAL("$SDK_NAME_VERSION Validation error. Missing 'token' key for reveal element. Specify a valid value for token."), + EMPTY_TOKEN_REVEAL("$SDK_NAME_VERSION Validation error. 'token' key cannot be empty for reveal element. Specify a non-empty value instead."), + ELEMENT_NOT_MOUNTED_REVEAL("$SDK_NAME_VERSION Reveal failed. Make sure to mount all elements before invoking 'reveal' function."), + ERROR_STATE_REVEAL("$SDK_NAME_VERSION Reveal failed. 'setError' is invoked on one or more elements. Make sure to reset any custom errors on all elements before invoking 'reveal' function."), + + // collect validations + MISSING_TABLE_IN_ELEMENT("$SDK_NAME_VERSION Validation error. Missing 'table' key for %s collect element. Specify a valid value for 'table' key."), + MISSING_COLUMN("$SDK_NAME_VERSION Validation error. Missing 'column' key for %s collect element. Specify a valid value for 'column' key."), + ELEMENT_EMPTY_TABLE_NAME("$SDK_NAME_VERSION Validation error. 'table' cannot be empty for %s collect element. Specify a non-empty value for 'table'."), + EMPTY_COLUMN_NAME("$SDK_NAME_VERSION Validation error. 'column' cannot be empty for %s collect element. Specify a non-empty value for 'column'."), + DUPLICATE_COLUMN_FOUND("$SDK_NAME_VERSION Validation error. Duplicate for column %s is found for table %s. Please ensure each column within a record is unique."), + ELEMENT_NOT_MOUNTED("$SDK_NAME_VERSION Collect failed. Make sure all elements are mounted before calling 'collect' on the container."), + + // additional fields validations - collect + ADDITIONAL_FIELDS_RECORDS_KEY_NOT_FOUND("$SDK_NAME_VERSION Validation error.'records' key not found in additionalFields. Specify a 'records' key in additionalFields."), + ADDITIONAL_FIELDS_INVALID_RECORDS_TYPE("$SDK_NAME_VERSION Validation error.'records' must be an array within additionalFields."), + ADDITIONAL_FIELDS_EMPTY_RECORDS("$SDK_NAME_VERSION Validation error.'records' object cannot be empty within additionalFields. Specify a non-empty value instead."), + ADDITIONAL_FIELDS_TABLE_KEY_NOT_FOUND("$SDK_NAME_VERSION Validation error.'table' key not found in additionalFields record at index %s. Specify a 'table' key in additionalFields record."), + ADDITIONAL_FIELDS_INVALID_TABLE_NAME("$SDK_NAME_VERSION Validation error. Invalid 'table' key value in additionalFields record at index %s. Specify a value of type string for 'table' key."), + ADDITIONAL_FIELDS_EMPTY_TABLE_KEY("$SDK_NAME_VERSION Validation error.'table' field cannot be empty in additionalFields record at index %s. Specify a non-empty value instead."), + ADDITIONAL_FIELDS_FIELDS_KEY_NOT_FOUND("$SDK_NAME_VERSION Validation error.'fields' key not found in additionalFields record at index %s. Specify a 'fields' key in additionalFields record."), + ADDITIONAL_FIELDS_EMPTY_FIELDS("$SDK_NAME_VERSION Validation error.'fields' object cannot be empty in additionalFields record at index %s. Specify a non-empty value instead."), + TABLE_KEY_ERROR("key \'table\' is missing or payload is incorrectly formatted"), - FIELDS_KEY_ERROR("fields key is required"), INVALID_COLUMN_NAME("column with given name is not present in the vault"), - EMPTY_COLUMN_NAME("element with type %s - Column key is required."), INVALID_TOKEN_ID("token provided is invalid "), - EMPTY_TOKEN_ID("token key cannot be empty "), ID_KEY_ERROR("key \'id\' is missing in the payload provided"), - REDACTION_KEY_ERROR("redaction key is required"), - INVALID_REDACTION_TYPE("provided redaction type value doesn’t match with one of : \'plain_text\', \'redacted\' ,\'default\' or \'masked\'"), INVALID_FIELD("invalid field %s"), - MISSING_TOKEN("token key is required "), + EMPTY_COLUMN_KEY("column key cannot be empty"), MISSING_TOKEN_IN_CONNECTION_REQUEST("element for %s must have token"), - MISSING_KEY_IDS("ids key is required "), - EMPTY_RECORD_IDS("ids key cannot be empty"), - INVALID_RECORD_ID_TYPE("invalid type of records id"), - MISSING_TABLE_IN_ELEMENT("element with type %s - Table key is required."), - MISSING_TABLE_KEY("%s SDK v%s Validation error.'table' key not found in records at index %s. Specify a valid value for 'table' key."), INVALID_RECORD_TABLE_VALUE("invalid record table value"), INVALID_CONNECTION_URL("connectionURL %s is invalid "), INVALID_INPUT("%s"), REQUIRED_INPUTS_NOT_PROVIDED("required inputs are not provided"), - INVALID_EVENT_TYPE("%s SDK v%s Invalid event type. Specify a valid event type."), - INVALID_EVENT_LISTENER("%s SDK v%s Invalid event listener. Please specify a valid event listener."), + INVALID_EVENT_TYPE("$SDK_NAME_VERSION Invalid event type. Specify a valid event type."), + INVALID_EVENT_LISTENER("$SDK_NAME_VERSION Invalid event listener. Please specify a valid event listener."), UNKNOWN_ERROR("%s"), TRANSACTION_ERROR("an error occurred during transaction"), CONNECTION_ERROR("error while initializing the connection"), - MISSING_REDACTION_VALUE("missing redaction value"), - ELEMENT_NOT_MOUNTED("%s SDK v%s Collect failed. Make sure all elements are mounted before calling 'collect' on the container."), - DUPLICATE_COLUMN_FOUND("Duplicate element with %s and %s found in container"), - DUPLICATE_ELEMENT_FOUND("%s SDK v%s Mount failed. '%s' appeared multiple times in '%s'. Make sure each column in a record is unique."), - INVALID_RECORDS_TYPE("Key 'records' is of invalid type"), - INVALID_RECORD_IDS("ids are not valid"), + DUPLICATE_ELEMENT_FOUND("Duplicate Element found in response body"), MISSING_REDACTION("redaction is missing"), EMPTY_KEY_IN_REQUEST_BODY("empty key present in request body"), EMPTY_KEY_IN_QUERY_PARAMS("empty key present in query parameters"), @@ -101,12 +167,10 @@ enum class Messages(val message: String) { INVALID_FIELD_IN_QUERY_PARAMS("invalid data type %s present in query parameters"), INVALID_FIELD_IN_REQUEST_HEADER_PARAMS("invalid data type %s present in request header"), INVALID_FIELD_IN_REQUEST_BODY("invalid data type %s present in request body"), - FAILED_TO_REVEAL("%s SDK v%s Reveal failed. Some errors were encountered."), + FAILED_TO_REVEAL("$SDK_NAME_VERSION Reveal failed. Some errors were encountered."), EMPTY_CONNECTION_URL("Empty connection url is passed"), NOT_FOUND_IN_RESPONSE("%s is not found in response"), BAD_REQUEST("bad request"), - MISSING_COLUMN("element with type %s - Column key cannot be empty."), - EMPTY_FIELDS("%s SDK v%s Validation error. Missing 'fields' key in records at index %s. Provide a valid 'fields' key."), SERVER_ERROR("Server error %s"), EMPTY_REQUEST_XML("RequestXML is empty"), INVALID_REQUEST_XML("Invalid RequestXML in SoapConnection - %s"), @@ -118,39 +182,21 @@ enum class Messages(val message: String) { INVALID_ID_IN_RESPONSE_XML("Invalid elementId %s present in ResponseXML"), EMPTY_ID_IN_RESPONSE_XML("empty elementId present in ResponseXML"), DUPLICATE_ID_IN_RESPONSE_XML("duplicate Id present in ResponseXML"), - INVALID_FORMAT_REGEX("%s SDK v%s Validation error. Invalid value for 'regex' param found for regex in validations array at index %s. Provide a valid value regular expression for regex param."), + INVALID_FORMAT_REGEX("$SDK_NAME_VERSION Validation error. Invalid value for 'regex' param found for regex in validations array at index %s. Provide a valid value regular expression for regex param."), NOT_VALID_TOKENS("following tokens are not valid - %s"), - NO_TABLE_KEY_IN_UPSERT("\"table\" key is required in upsert options object at index %s"), - NO_COLUMN_KEY_IN_UPSERT("\"column\" key is required in upsert option at index %s"), - INVALID_TABLE_IN_UPSERT_OPTION("Invalid table in upsert object at index %s, table of type non empty string is required"), - INVALID_COLUMN_IN_UPSERT_OPTION("Invalid column upsert object at index %s, column of type non empty string is required"), - ALLOW_JSON_OBJECT_IN_UPSERT("upsert array should consists of json objects only"), - EMPTY_UPSERT_OPTIONS_ARRAY("upsert option cannot be an empty array, atleast one object of table and column is required."), - EMPTY_RECORD_OBJECT("record object can not be empty at index %s"), - EMPTY_RECORD_IDS_IN_GET("ids cannot be in record object at index %s"), - INVALID_RECORD_IDS_TYPE("invalid type of ids in record object at index %s"), - EMPTY_ID("empty value in ids"), - REDACTION_WITH_TOKENS_NOT_SUPPORTED("redaction cannot be used when tokens are true in options"), - TOKENS_NOT_SUPPORTED_WITH_COLUMN_DETAILS("tokens cannot be used when column details (name and values) are passed"), - MISSING_RECORD_COLUMN_VALUES("Column Values is required when Column Name is specified"), - MISSING_RECORD_COLUMN_NAME("Column Name is required when Column Values is specified"), - INVALID_RECORD_COLUMN_NAME_TYPE("Invalid Column Name type in record object at index %s. Must be String"), - EMPTY_RECORD_COLUMN_NAME("Column Name is empty in record object at index %s"), - INVALID_RECORD_COLUMN_VALUES_TYPE("Invalid Column Values type in record object at index %s. Must be JSONArray"), - INVALID_COLUMN_VALUE_TYPE("Invalid Column Value type. Must be String"), - EMPTY_RECORD_COLUMN_VALUES("Column Values cannot be empty in record object at index %s"), - EMPTY_COLUMN_VALUE("%s SDK v%s Validation error. 'column' key cannot be empty in records at index %s. Specify a non-empty value instead."), - BOTH_IDS_AND_COLUMN_DETAILS_SPECIFIED("Both skyflow ids and column details (name and values) are specified in record object at index %s"), - NEITHER_IDS_NOR_COLUMN_DETAILS_SPECIFIED("Neither skyflow ids nor column details (name and values) are specified in record object at index %s"), - FAILED_TO_GET("Failed to Get records"), + + EMPTY_TOKEN_ID("token key cannot be empty"), + MISSING_TOKEN("token key is required"), + + FAILED_TO_GET("$SDK_NAME_VERSION Get failed. Get request is rejected."), VALIDATE_INPUT_FORMAT_OPTIONS("Validated Input Format Options for %s"), - INPUT_FORMATTING_NOT_SUPPORTED("%s SDK v%s Mount failed. Format must be a non-empty string. Specify a valid format."), - INVALID_INPUT_TRANSLATION("%s SDK v%s Mount failed. Translation must be a non-empty object. Specify a valid translation."), + INPUT_FORMATTING_NOT_SUPPORTED("$SDK_NAME_VERSION Mount failed. Format must be a non-empty string. Specify a valid format."), + INVALID_INPUT_TRANSLATION("$SDK_NAME_VERSION Mount failed. Translation must be a non-empty object. Specify a valid translation."), EMPTY_INPUT_TRANSLATION("translation not passed. Switching to default translation %s"), - MISMATCH_ELEMENT_COUNT_LAYOUT_SUM("%s SDK v%s Mount failed. Invalid layout array values. Make sure all values in the layout array are positive numbers.") + MISMATCH_ELEMENT_COUNT_LAYOUT_SUM("$SDK_NAME_VERSION Mount failed. Invalid layout array values. Make sure all values in the layout array are positive numbers.") } fun Messages.getMessage(vararg values: String?): String { diff --git a/Skyflow/src/main/kotlin/Skyflow/utils/Utils.kt b/Skyflow/src/main/kotlin/Skyflow/utils/Utils.kt index 17d227e..82d6dfd 100644 --- a/Skyflow/src/main/kotlin/Skyflow/utils/Utils.kt +++ b/Skyflow/src/main/kotlin/Skyflow/utils/Utils.kt @@ -60,15 +60,25 @@ public class Utils { while (i < obj1.length()) { val jsonObj = obj1.getJSONObject(i) if (!jsonObj.has("table")) { - throw SkyflowError(SkyflowErrorCode.MISSING_TABLE_KEY, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.TABLE_KEY_NOY_FOUND, tag, logLevel, arrayOf("$i") + ) } else if (jsonObj.get("table") !is String) { - throw SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.INVALID_TABLE_NAME, tag, logLevel, arrayOf("$i") + ) } else if (jsonObj.get("table").toString().isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_TABLE_KEY, tag, logLevel, arrayOf("$i") + ) } else if (!jsonObj.has("fields")) { - throw SkyflowError(SkyflowErrorCode.FIELDS_KEY_ERROR, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.FIELDS_KEY_NOT_FOUND, tag, logLevel, arrayOf("$i") + ) } else if (jsonObj.getJSONObject("fields").toString().equals("{}")) { - throw SkyflowError(SkyflowErrorCode.EMPTY_FIELDS, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_FIELDS, tag, logLevel, arrayOf("$i") + ) } val map = HashMap() @@ -84,7 +94,10 @@ public class Utils { while (keys.hasNext()) { val key = keys.next() if (key.isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_COLUMN_KEY, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_FIELD_IN_FIELDS, tag, logLevel, + params = arrayOf("$i") + ) } } postPayload.add(map) @@ -139,9 +152,8 @@ public class Utils { for (index in 0..options.length() - 1) { if (options.get(index) !is JSONObject) { throw SkyflowError( - SkyflowErrorCode.ALLOW_JSON_OBJECT_IN_UPSERT, - tag, - logLevel + SkyflowErrorCode.ALLOW_JSON_OBJECT_IN_UPSERT, tag, logLevel, + arrayOf("$index") ) } if (!options.getJSONObject(index).has("table")) { @@ -152,9 +164,7 @@ public class Utils { } if (!options.getJSONObject(index).has("column")) { throw SkyflowError( - SkyflowErrorCode.NO_COLUMN_KEY_IN_UPSERT, - tag, - logLevel, + SkyflowErrorCode.NO_COLUMN_KEY_IN_UPSERT, tag, logLevel, arrayOf(index.toString()) ) } @@ -163,9 +173,7 @@ public class Utils { .toString().isEmpty() ) { throw SkyflowError( - SkyflowErrorCode.INVALID_TABLE_IN_UPSERT_OPTION, - tag, - logLevel, + SkyflowErrorCode.INVALID_TABLE_IN_UPSERT_OPTION, tag, logLevel, arrayOf(index.toString()) ) } @@ -174,9 +182,7 @@ public class Utils { .toString().isEmpty() ) { throw SkyflowError( - SkyflowErrorCode.INVALID_COLUMN_IN_UPSERT_OPTION, - tag, - logLevel, + SkyflowErrorCode.INVALID_COLUMN_IN_UPSERT_OPTION, tag, logLevel, arrayOf(index.toString()) ) } @@ -552,11 +558,17 @@ public class Utils { // checking for table if (!recordObject.has("table")) { - throw SkyflowError(SkyflowErrorCode.MISSING_TABLE_KEY, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.TABLE_KEY_NOY_FOUND, tag, logLevel, arrayOf(it.toString()) + ) } else if (recordObject.get("table") !is String) { - throw SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.INVALID_TABLE_NAME, tag, logLevel, arrayOf(it.toString()) + ) } else if (recordObject.get("table").toString().isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_TABLE_KEY, tag, logLevel, arrayOf(it.toString()) + ) } // checking for ids @@ -564,23 +576,25 @@ public class Utils { val ids = recordObject.get("ids") if (ids !is JSONArray) { throw SkyflowError( - SkyflowErrorCode.INVALID_RECORD_IDS_TYPE, tag, logLevel, - arrayOf(it.toString()) + SkyflowErrorCode.INVALID_IDS, tag, logLevel, arrayOf(it.toString()) ) } else if (ids.length() == 0) { throw SkyflowError( - SkyflowErrorCode.EMPTY_RECORD_IDS_IN_GET, tag, logLevel, - arrayOf(it.toString()) + SkyflowErrorCode.EMPTY_RECORD_IDS, tag, logLevel, arrayOf(it.toString()) ) } else { hasIds = true for (i in 0 until ids.length()) { if (ids[i] !is String) { throw SkyflowError( - SkyflowErrorCode.INVALID_RECORD_ID_TYPE, tag, logLevel + SkyflowErrorCode.INVALID_ID_IN_RECORD_IDS, tag, logLevel, + arrayOf(it.toString()) ) } else if (ids[i].toString().isEmpty()) { - throw SkyflowError(SkyflowErrorCode.EMPTY_ID, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.EMPTY_ID_IN_RECORD_IDS, tag, logLevel, + arrayOf(it.toString()) + ) } } } @@ -602,25 +616,34 @@ public class Utils { ) } else if (options?.tokens == false) { if (!hasRedaction) { - throw SkyflowError(SkyflowErrorCode.REDACTION_KEY_ERROR, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.REDACTION_KEY_NOT_FOUND, tag, logLevel, + arrayOf(it.toString()) + ) } else if (recordObject.get("redaction").toString().isEmpty()) { throw SkyflowError( - SkyflowErrorCode.MISSING_REDACTION_VALUE, - tag, logLevel + SkyflowErrorCode.EMPTY_REDACTION_VALUE, tag, logLevel, + arrayOf(it.toString()) ) } else if (recordObject.get("redaction") !is RedactionType) { throw SkyflowError( - SkyflowErrorCode.INVALID_REDACTION_TYPE, - tag, logLevel + SkyflowErrorCode.INVALID_REDACTION_TYPE, tag, logLevel, + arrayOf(it.toString()) ) } } // checking for column name and column values if (!hasColumnName && hasColumnValues) { - throw SkyflowError(SkyflowErrorCode.MISSING_RECORD_COLUMN_NAME, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.MISSING_RECORD_COLUMN_NAME, tag, logLevel, + arrayOf(it.toString()) + ) } else if (hasColumnName && !hasColumnValues) { - throw SkyflowError(SkyflowErrorCode.MISSING_RECORD_COLUMN_VALUES, tag, logLevel) + throw SkyflowError( + SkyflowErrorCode.MISSING_RECORD_COLUMN_VALUES, tag, logLevel, + arrayOf(it.toString()) + ) } else if (hasColumnName && hasColumnValues) { if (hasIds) { throw SkyflowError( @@ -657,11 +680,13 @@ public class Utils { for (i in 0 until columnValues.length()) { if (columnValues[i] !is String) { throw SkyflowError( - SkyflowErrorCode.INVALID_COLUMN_VALUE_TYPE, tag, logLevel + SkyflowErrorCode.INVALID_COLUMN_VALUE_TYPE, tag, logLevel, + arrayOf(it.toString()) ) } else if (columnValues[i].toString().isEmpty()) { throw SkyflowError( - SkyflowErrorCode.EMPTY_COLUMN_VALUE, tag, logLevel + SkyflowErrorCode.EMPTY_COLUMN_VALUE, tag, logLevel, + arrayOf(it.toString()) ) } } diff --git a/Skyflow/src/test/java/com/Skyflow/CollectTest.kt b/Skyflow/src/test/java/com/Skyflow/CollectTest.kt index b4ac9d5..6954bc0 100644 --- a/Skyflow/src/test/java/com/Skyflow/CollectTest.kt +++ b/Skyflow/src/test/java/com/Skyflow/CollectTest.kt @@ -1082,7 +1082,10 @@ class CollectTest { try { CollectRequestBody.createRequestBody(container.collectElements, records, LogLevel.ERROR) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY) + val skyflowError = SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_EMPTY_TABLE_KEY, + params = arrayOf("0") + ) TestCase.assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() @@ -1118,7 +1121,11 @@ class CollectTest { try { CollectRequestBody.createRequestBody(container.collectElements, records, LogLevel.ERROR) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TABLE_KEY) + val skyflowError = + SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_TABLE_KEY_NOT_FOUND, + params = arrayOf("0") + ) TestCase.assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() @@ -1155,7 +1162,11 @@ class CollectTest { try { CollectRequestBody.createRequestBody(container.collectElements, records, LogLevel.ERROR) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME) + val skyflowError = + SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_INVALID_TABLE_NAME, + params = arrayOf("0") + ) TestCase.assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() @@ -1191,7 +1202,11 @@ class CollectTest { try { CollectRequestBody.createRequestBody(container.collectElements, records, LogLevel.ERROR) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.FIELDS_KEY_ERROR) + val skyflowError = + SkyflowError( + SkyflowErrorCode.ADDITIONAL_FIELDS_FIELDS_KEY_NOT_FOUND, + params = arrayOf("0") + ) TestCase.assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() @@ -1226,7 +1241,8 @@ class CollectTest { try { CollectRequestBody.createRequestBody(container.collectElements, records, LogLevel.ERROR) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_FIELDS) + val skyflowError = + SkyflowError(SkyflowErrorCode.ADDITIONAL_FIELDS_EMPTY_FIELDS, params = arrayOf("0")) TestCase.assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() @@ -1255,7 +1271,7 @@ class CollectTest { try { CollectRequestBody.createRequestBody(container.collectElements, records, LogLevel.ERROR) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_RECORDS) + val skyflowError = SkyflowError(SkyflowErrorCode.ADDITIONAL_FIELDS_EMPTY_RECORDS) TestCase.assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() @@ -1283,7 +1299,7 @@ class CollectTest { try { CollectRequestBody.createRequestBody(container.collectElements, records, LogLevel.ERROR) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_RECORDS) + val skyflowError = SkyflowError(SkyflowErrorCode.ADDITIONAL_FIELDS_INVALID_RECORDS) TestCase.assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() diff --git a/Skyflow/src/test/java/com/Skyflow/DetokenizeTests.kt b/Skyflow/src/test/java/com/Skyflow/DetokenizeTests.kt index 30a8bdd..2c62e05 100644 --- a/Skyflow/src/test/java/com/Skyflow/DetokenizeTests.kt +++ b/Skyflow/src/test/java/com/Skyflow/DetokenizeTests.kt @@ -166,7 +166,8 @@ class DetokenizeTests { detokenizeRecordsArray.put(recordObj) detokenizeRecords.put("records", detokenizeRecordsArray) - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TOKEN, apiClientTag, logLevel) + val skyflowError = + SkyflowError(SkyflowErrorCode.TOKEN_KEY_NOT_FOUND, apiClientTag, logLevel, arrayOf("0")) try { apiClient.constructBodyForDetokenize(detokenizeRecords) @@ -190,7 +191,8 @@ class DetokenizeTests { detokenizeRecordsArray.put(recordObj) detokenizeRecords.put("records", detokenizeRecordsArray) - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID, apiClientTag, logLevel) + val skyflowError = + SkyflowError(SkyflowErrorCode.EMPTY_TOKEN, apiClientTag, logLevel, arrayOf("0")) try { apiClient.constructBodyForDetokenize(detokenizeRecords) diff --git a/Skyflow/src/test/java/com/Skyflow/GetTests.kt b/Skyflow/src/test/java/com/Skyflow/GetTests.kt index f607a7f..afe4411 100644 --- a/Skyflow/src/test/java/com/Skyflow/GetTests.kt +++ b/Skyflow/src/test/java/com/Skyflow/GetTests.kt @@ -217,7 +217,7 @@ class GetTests { getRecords.put("records", recordsArray) val skyflowError = SkyflowError( - SkyflowErrorCode.MISSING_TABLE_KEY, utilsTag, logLevel + SkyflowErrorCode.TABLE_KEY_NOY_FOUND, utilsTag, logLevel, arrayOf("0") ) try { @@ -231,7 +231,9 @@ class GetTests { fun testEmptyTableInRecordObject() { getRecord.put("table", String()) getRecords.put("records", recordsArray) - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY, utilsTag, logLevel) + val skyflowError = SkyflowError( + SkyflowErrorCode.EMPTY_TABLE_KEY, utilsTag, logLevel, arrayOf("0") + ) try { Utils.validateGetInputAndOptions(getRecords, GetOptions(), logLevel) @@ -244,7 +246,9 @@ class GetTests { fun testInvalidTableTypeInRecordObject() { getRecord.put("table", JSONObject()) getRecords.put("records", recordsArray) - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME, utilsTag, logLevel) + val skyflowError = SkyflowError( + SkyflowErrorCode.INVALID_TABLE_NAME, utilsTag, logLevel, arrayOf("0") + ) try { Utils.validateGetInputAndOptions(getRecords, GetOptions(), logLevel) @@ -273,7 +277,7 @@ class GetTests { getRecord.put("ids", JSONObject()) getRecords.put("records", recordsArray) val skyflowError = SkyflowError( - SkyflowErrorCode.INVALID_RECORD_IDS_TYPE, utilsTag, logLevel, arrayOf("0") + SkyflowErrorCode.INVALID_IDS, utilsTag, logLevel, arrayOf("0") ) try { @@ -287,7 +291,11 @@ class GetTests { fun testInvalidIdTypeInRecordObject() { getRecord.getJSONArray("ids").put(123) getRecords.put("records", recordsArray) - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_RECORD_ID_TYPE, utilsTag, logLevel) + val skyflowError = + SkyflowError( + SkyflowErrorCode.INVALID_ID_IN_RECORD_IDS, + utilsTag, logLevel, arrayOf("0") + ) try { Utils.validateGetInputAndOptions(getRecords, GetOptions(), logLevel) @@ -300,7 +308,8 @@ class GetTests { fun testEmptyIdValueInRecordObject() { getRecord.getJSONArray("ids").put(String()) getRecords.put("records", recordsArray) - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_ID, utilsTag, logLevel) + val skyflowError = + SkyflowError(SkyflowErrorCode.EMPTY_ID_IN_RECORD_IDS, utilsTag, logLevel, arrayOf("0")) try { Utils.validateGetInputAndOptions(getRecords, GetOptions(), logLevel) @@ -329,7 +338,7 @@ class GetTests { getRecord.put("redaction", String()) getRecords.put("records", recordsArray) val skyflowError = SkyflowError( - SkyflowErrorCode.MISSING_REDACTION_VALUE, utilsTag, logLevel, arrayOf("0") + SkyflowErrorCode.EMPTY_REDACTION_VALUE, utilsTag, logLevel, arrayOf("0") ) try { @@ -371,7 +380,7 @@ class GetTests { getRecord.remove("redaction") getRecords.put("records", recordsArray) val skyflowError = SkyflowError( - SkyflowErrorCode.REDACTION_KEY_ERROR, utilsTag, logLevel, arrayOf("0") + SkyflowErrorCode.REDACTION_KEY_NOT_FOUND, utilsTag, logLevel, arrayOf("0") ) try { @@ -398,7 +407,7 @@ class GetTests { getRecords.put("records", recordsArray) val skyflowError = SkyflowError( - SkyflowErrorCode.MISSING_RECORD_COLUMN_NAME, utilsTag, logLevel, arrayOf("0") + SkyflowErrorCode.MISSING_RECORD_COLUMN_NAME, utilsTag, logLevel, arrayOf("1") ) try { @@ -414,7 +423,7 @@ class GetTests { getRecords.put("records", recordsArray) val skyflowError = SkyflowError( - SkyflowErrorCode.MISSING_RECORD_COLUMN_VALUES, utilsTag, logLevel, arrayOf("0") + SkyflowErrorCode.MISSING_RECORD_COLUMN_VALUES, utilsTag, logLevel, arrayOf("1") ) try { @@ -528,7 +537,7 @@ class GetTests { getRecords.put("records", recordsArray) val skyflowError = SkyflowError( - SkyflowErrorCode.INVALID_COLUMN_VALUE_TYPE, utilsTag, logLevel + SkyflowErrorCode.INVALID_COLUMN_VALUE_TYPE, utilsTag, logLevel, arrayOf("1") ) try { @@ -543,7 +552,9 @@ class GetTests { getRecordColumnDetails.put("columnValues", JSONArray(arrayOf("value1", ""))) getRecords.put("records", recordsArray) - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_COLUMN_VALUE, utilsTag, logLevel) + val skyflowError = SkyflowError( + SkyflowErrorCode.EMPTY_COLUMN_VALUE, utilsTag, logLevel, arrayOf("1") + ) try { Utils.validateGetInputAndOptions(getRecords, GetOptions(), logLevel) @@ -684,8 +695,7 @@ class GetTests { override fun onFailure(exception: Any) { val expectedError = SkyflowError( - SkyflowErrorCode.UNKNOWN_ERROR, - getResponseTag, logLevel, params = arrayOf("Failed to Get records") + SkyflowErrorCode.FAILED_TO_GET, getResponseTag, logLevel, ) Assert.assertEquals( expectedError.getErrorMessage(), getErrorMessage(exception as JSONObject) diff --git a/Skyflow/src/test/java/com/Skyflow/InvokeConnectionTest.kt b/Skyflow/src/test/java/com/Skyflow/InvokeConnectionTest.kt index 898700c..55921aa 100644 --- a/Skyflow/src/test/java/com/Skyflow/InvokeConnectionTest.kt +++ b/Skyflow/src/test/java/com/Skyflow/InvokeConnectionTest.kt @@ -1177,7 +1177,7 @@ class InvokeConnectionTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TOKEN) + val skyflowError = SkyflowError(SkyflowErrorCode.TOKEN_KEY_NOT_FOUND) assertEquals(skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -1197,7 +1197,7 @@ class InvokeConnectionTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TOKEN) + val skyflowError = SkyflowError(SkyflowErrorCode.TOKEN_KEY_NOT_FOUND) assertEquals(skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -1217,7 +1217,7 @@ class InvokeConnectionTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID) + val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN) assertEquals(skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -1237,7 +1237,7 @@ class InvokeConnectionTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID) + val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN) assertEquals(skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -1257,7 +1257,7 @@ class InvokeConnectionTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID) + val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN) assertEquals(skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject)) } @@ -1277,7 +1277,7 @@ class InvokeConnectionTest { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID) + val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN) assertEquals(skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject)) } diff --git a/Skyflow/src/test/java/com/Skyflow/RevealTest.kt b/Skyflow/src/test/java/com/Skyflow/RevealTest.kt index 214ce3c..2d625c1 100644 --- a/Skyflow/src/test/java/com/Skyflow/RevealTest.kt +++ b/Skyflow/src/test/java/com/Skyflow/RevealTest.kt @@ -190,7 +190,7 @@ class RevealTest { override fun onFailure(exception: Any) { val skyflowError = SkyflowError( - SkyflowErrorCode.ELEMENT_NOT_MOUNTED, + SkyflowErrorCode.ELEMENT_NOT_MOUNTED_REVEAL, params = arrayOf(revealElement.label.text.toString()) ) assertEquals( @@ -219,14 +219,13 @@ class RevealTest { revealContainer.create(activity, revealInput, Skyflow.RevealElementOptions()) activity.addContentView(revealElement, layoutParams) revealContainer.reveal(object : Callback { - override fun onSuccess(responseBody: Any) { - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID) + val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_REVEAL) assertEquals( skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + UnitTests.getErrorMessage(exception as JSONObject, true) ) } }) @@ -254,7 +253,7 @@ class RevealTest { override fun onFailure(exception: Any) { assertEquals(revealElement.getValue(), "") - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TOKEN) + val skyflowError = SkyflowError(SkyflowErrorCode.TOKEN_KEY_NOT_FOUND_REVEAL) assertEquals( skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) @@ -287,7 +286,7 @@ class RevealTest { override fun onFailure(exception: Any) { assertEquals(revealElement.getValue(), "") - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TOKEN) + val skyflowError = SkyflowError(SkyflowErrorCode.TOKEN_KEY_NOT_FOUND_REVEAL) assertEquals( skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) @@ -468,10 +467,8 @@ class RevealTest { } override fun onFailure(exception: Any) { - val expectedError = SkyflowError( - SkyflowErrorCode.UNKNOWN_ERROR, - params = arrayOf("Failed to reveal") - ).getInternalErrorMessage() + val expectedError = SkyflowError(SkyflowErrorCode.FAILED_TO_REVEAL) + .getInternalErrorMessage() assertEquals(expectedError, UnitTests.getErrorMessage(exception as JSONObject)) } }, LogLevel.ERROR).insertResponse(null, false) @@ -589,11 +586,7 @@ class RevealTest { }, LogLevel.ERROR) val failedResponse = JSONArray() val resObj = JSONObject() - val skyflowError = SkyflowError( - SkyflowErrorCode.UNKNOWN_ERROR, - logLevel = LogLevel.ERROR, - params = arrayOf("unknown") - ) + val skyflowError = SkyflowError(SkyflowErrorCode.FAILED_TO_REVEAL) resObj.put("error", skyflowError) resObj.put("ids", "[\"123\",\"456\"]") failedResponse.put(resObj) @@ -604,10 +597,8 @@ class RevealTest { } override fun onFailure(exception: Any) { - val expectedError = SkyflowError( - SkyflowErrorCode.UNKNOWN_ERROR, - params = arrayOf("Failed to reveal") - ).getInternalErrorMessage() + val expectedError = SkyflowError(SkyflowErrorCode.FAILED_TO_REVEAL) + .getInternalErrorMessage() assertEquals(expectedError, UnitTests.getErrorMessage(exception as JSONObject)) } @@ -872,7 +863,7 @@ class RevealTest { override fun onFailure(exception: Any) { TestCase.assertEquals( - "{\"errors\":[{\"error\":\"Skyflow.SkyflowError: Interface : - Value string of type java.lang.String cannot be converted to JSONObject\"}]}", + "{\"errors\":[{\"error\":\"Skyflow.SkyflowError: Value string of type java.lang.String cannot be converted to JSONObject\"}]}", exception.toString() ) } diff --git a/Skyflow/src/test/java/com/Skyflow/UnitTests.kt b/Skyflow/src/test/java/com/Skyflow/UnitTests.kt index 81a475c..517c3ca 100644 --- a/Skyflow/src/test/java/com/Skyflow/UnitTests.kt +++ b/Skyflow/src/test/java/com/Skyflow/UnitTests.kt @@ -41,16 +41,13 @@ class UnitTests { fun setup() { MockKAnnotations.init(this) val configuration = Configuration( - "b359c43f1b844ff4bea0f098", - "https://vaulturl.com", - AccessTokenProvider() + "b359c43f1b844ff4bea0f098", "https://vaulturl.com", AccessTokenProvider() ) skyflow = Client(configuration) activityController = Robolectric.buildActivity(Activity::class.java).setup() activity = activityController.get() layoutParams = ViewGroup.LayoutParams( - ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.WRAP_CONTENT + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT ) } @@ -92,34 +89,27 @@ class UnitTests { revealRecordsArray.put(recordObj) revealRecords.put("records", revealRecordsArray) apiClient.get(revealRecords, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID) + val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject, true) ) } - }) revealRecords.put("records", "") apiClient.get(revealRecords, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_RECORDS) + val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_RECORDS) Assert.assertEquals( - skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) ) } - }) revealRecords.put("records", "{}") apiClient.get(revealRecords, object : Callback { @@ -130,8 +120,7 @@ class UnitTests { override fun onFailure(exception: Any) { val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_RECORDS) Assert.assertEquals( - skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) ) } @@ -156,10 +145,10 @@ class UnitTests { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TOKEN) + val skyflowError = + SkyflowError(SkyflowErrorCode.TOKEN_KEY_NOT_FOUND, params = arrayOf("0")) Assert.assertEquals( - skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) ) } @@ -186,8 +175,7 @@ class UnitTests { override fun onFailure(exception: Any) { val skyflowError = SkyflowError(SkyflowErrorCode.RECORDS_KEY_NOT_FOUND) Assert.assertEquals( - skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) ) } @@ -209,8 +197,7 @@ class UnitTests { override fun onFailure(exception: Any) { val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_RECORDS) Assert.assertEquals( - skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) ) } @@ -225,29 +212,23 @@ class UnitTests { val revealRecords = JSONObject() revealRecords.put("records", JSONArray()) apiClient.get(revealRecords, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_RECORDS) + val skyflowError = + SkyflowError(SkyflowErrorCode.EMPTY_RECORDS, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject, true) ) } - }) - } - @Test fun testEmptyVaultID() { val skyflowConfiguration = Skyflow.Configuration( - "", - "https://vaulturl.com", - AccessTokenProvider() + "", "https://vaulturl.com", AccessTokenProvider() ) val revealRecords = JSONObject() val revealRecordsArray = JSONArray() @@ -264,8 +245,7 @@ class UnitTests { override fun onFailure(exception: Any) { val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_VAULT_ID) Assert.assertEquals( - skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) ) } @@ -276,9 +256,7 @@ class UnitTests { @Test fun testEmptyVaultURL2() { val skyflowConfiguration = Skyflow.Configuration( - "vault_id", - "", - AccessTokenProvider() + "vault_id", "", AccessTokenProvider() ) val revealRecords = JSONObject() val revealRecordsArray = JSONArray() @@ -295,8 +273,7 @@ class UnitTests { override fun onFailure(exception: Any) { val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_VAULT_URL) Assert.assertEquals( - skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) ) } @@ -306,9 +283,7 @@ class UnitTests { @Test fun testInvalidVaultURL2() { val skyflowConfiguration = Skyflow.Configuration( - "vault_id", - "http://www.goog.com", - AccessTokenProvider() + "vault_id", "http://www.goog.com", AccessTokenProvider() ) val revealRecords = JSONObject() val revealRecordsArray = JSONArray() @@ -328,8 +303,7 @@ class UnitTests { params = arrayOf(skyflowConfiguration.vaultURL) ) Assert.assertEquals( - skyflowError.getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + skyflowError.getInternalErrorMessage(), getErrorMessage(exception as JSONObject) ) } @@ -348,27 +322,23 @@ class UnitTests { revealRecordsArray.put(recordObj) revealRecords.put("records", revealRecordsArray) apiClient.get(revealRecords, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { assertEquals( - SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID).getInternalErrorMessage(), - getErrorMessage(exception as JSONObject) + SkyflowError( + SkyflowErrorCode.EMPTY_TOKEN, params = arrayOf("0") + ).getInternalErrorMessage(), + getErrorMessage(exception as JSONObject, true) ) } - }) - } @Test fun testValidRequestForDetokenizeInClient() { val configuration = Configuration( - "12344", - "https://vaulturl.com", - AccessTokenProvider() + "12344", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val revealRecords = JSONObject() @@ -458,7 +428,8 @@ class UnitTests { val x = Utils.constructBatchRequestBody(records, InsertOptions(), LogLevel.ERROR) assertEquals(x.toString().trim(), JSONObject().toString().trim()) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TABLE_KEY, params = arrayOf()) + val skyflowError = + SkyflowError(SkyflowErrorCode.TABLE_KEY_NOY_FOUND, params = arrayOf("0")) assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() @@ -483,7 +454,8 @@ class UnitTests { val x = Utils.constructBatchRequestBody(records, InsertOptions(), LogLevel.ERROR) assertEquals(x.toString().trim(), JSONObject().toString().trim()) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME, params = arrayOf()) + val skyflowError = + SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME, params = arrayOf("0")) assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() @@ -508,13 +480,12 @@ class UnitTests { val x = Utils.constructBatchRequestBody(records, InsertOptions(), LogLevel.ERROR) assertEquals(x.toString().trim(), JSONObject().toString().trim()) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY, params = arrayOf()) + val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY, params = arrayOf("0")) assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() ) } - } @Test @@ -549,8 +520,7 @@ class UnitTests { val container = skyflow.container(ContainerType.COLLECT) val options = CollectElementOptions(false) val collectInput = CollectElementInput( - "cards", "card_number", - SkyflowElementType.CARD_NUMBER, placeholder = "card number" + "cards", "card_number", SkyflowElementType.CARD_NUMBER, placeholder = "card number" ) val card_number = container.create(activity, collectInput, options) as? TextField card_number!!.inputField.setText("4111") @@ -574,8 +544,7 @@ class UnitTests { val container = skyflow.container(ContainerType.COLLECT) val options = CollectElementOptions(false) val collectInput = CollectElementInput( - "cards", "card_number", - SkyflowElementType.CARD_NUMBER, placeholder = "card number" + "cards", "card_number", SkyflowElementType.CARD_NUMBER, placeholder = "card number" ) val card_number = container.create(activity, collectInput, options) as? TextField activity.addContentView(card_number, layoutParams) @@ -598,8 +567,7 @@ class UnitTests { val container = skyflow.container(ContainerType.COLLECT) val options = CollectElementOptions(true) val collectInput = CollectElementInput( - "cards", "card_number", - SkyflowElementType.CARD_NUMBER, placeholder = "card number" + "cards", "card_number", SkyflowElementType.CARD_NUMBER, placeholder = "card number" ) val card_number = container.create(activity, collectInput, options) as? TextField activity.addContentView(card_number, layoutParams) @@ -621,11 +589,7 @@ class UnitTests { fun testBearerTokenFunction() //success { val client = APIClient( - "1234", - "https://vaulturl.com", - APITokenProviderForSuccess(), - LogLevel.ERROR, - "" + "1234", "https://vaulturl.com", APITokenProviderForSuccess(), LogLevel.ERROR, "" ) client.getAccessToken(object : Callback { override fun onSuccess(responseBody: Any) { @@ -643,9 +607,7 @@ class UnitTests { @Test fun testInsertEmptyVaultID() { val skyflowConfiguration = Skyflow.Configuration( - "", - "https://vaulturl.com", - AccessTokenProvider() + "", "https://vaulturl.com", AccessTokenProvider() ) val records = JSONObject() val skyflowClient = Client(skyflowConfiguration) @@ -668,9 +630,7 @@ class UnitTests { @Test fun testInsertEmptyVaultURL() { val skyflowConfiguration = Skyflow.Configuration( - "vault_id", - "", - AccessTokenProvider() + "vault_id", "", AccessTokenProvider() ) val records = JSONObject() val skyflowClient = Client(skyflowConfiguration) @@ -692,9 +652,7 @@ class UnitTests { @Test fun testInsertInvalidVaultURL() { val skyflowConfiguration = Skyflow.Configuration( - "vault_id", - "http://www.goog.com", - AccessTokenProvider() + "vault_id", "http://www.goog.com", AccessTokenProvider() ) val records = JSONObject() val skyflowClient = Client(skyflowConfiguration) @@ -719,9 +677,7 @@ class UnitTests { @Test fun testValidRequestForInsert() { val skyflowConfiguration = Skyflow.Configuration( - "vault_id", - "https://www.google.com", - AccessTokenProvider() + "vault_id", "https://www.google.com", AccessTokenProvider() ) val records = JSONObject() val skyflowClient = Client(skyflowConfiguration) @@ -756,18 +712,16 @@ class UnitTests { recordsArray.put(record) records.put("records", recordsArray) apiClient.post(records, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY) + val skyflowError = + SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage() ) } - }, InsertOptions()) } @@ -809,9 +763,7 @@ class UnitTests { requestRecord.put("xxx", CheckBox(activity)) val url = "BuildConfig.GATEWAY_CVV_GEN_URL " // eg: url.../{cardNumber}/... val connectionRequestBody = ConnectionConfig( - connectionURL = url, - methodName = RequestMethod.POST, - requestBody = requestRecord + connectionURL = url, methodName = RequestMethod.POST, requestBody = requestRecord ) apiClient.invokeConnection(connectionRequestBody, object : Callback { @@ -839,9 +791,7 @@ class UnitTests { requestRecord.put("card_number", "41111") val url = "BuildConfig.GATEWAY_CVV_GEN_URL " // eg: url.../{cardNumber}/... val connectionRequestBody = ConnectionConfig( - connectionURL = url, - methodName = RequestMethod.POST, - requestBody = requestRecord + connectionURL = url, methodName = RequestMethod.POST, requestBody = requestRecord ) apiClient.invokeConnection(connectionRequestBody, object : Callback { @@ -874,8 +824,7 @@ class UnitTests { val container = skyflow.container(ContainerType.COLLECT) val collectInput = CollectElementInput( - "cards", "card_number", - SkyflowElementType.INPUT_FIELD, placeholder = "card number" + "cards", "card_number", SkyflowElementType.INPUT_FIELD, placeholder = "card number" ) val first_name = container.create(activity, collectInput) as TextField activity.addContentView(first_name, layoutParams) @@ -969,9 +918,7 @@ class UnitTests { @Test fun testNoRecords() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val records = JSONObject() @@ -995,9 +942,7 @@ class UnitTests { @Test fun testInvalidRecords() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val records = JSONObject() @@ -1021,35 +966,28 @@ class UnitTests { @Test fun testEmptyRecords() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val records = JSONObject() records.put("records", JSONArray()) client.getById(records, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_RECORDS) Assert.assertEquals( - UnitTests.getErrorMessage(exception as JSONObject), - skyflowError.getInternalErrorMessage() + skyflowError.getInternalErrorMessage(), + getErrorMessage(exception as JSONObject, true) ) } - }) } @Test fun testMissingIds() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1065,27 +1003,23 @@ class UnitTests { val records = JSONObject() records.put("records", recordsArray) client.getById(records, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_IDS) + val skyflowError = + SkyflowError(SkyflowErrorCode.IDS_KEY_NOT_FOUND, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - UnitTests.getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject) ) } - }) } @Test fun testEmptyIds() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1099,15 +1033,14 @@ class UnitTests { val records = JSONObject() records.put("records", recordsArray) client.getById(records, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_RECORD_IDS) + val skyflowError = + SkyflowError(SkyflowErrorCode.EMPTY_RECORD_IDS, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - UnitTests.getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject) ) } @@ -1118,9 +1051,7 @@ class UnitTests { @Test fun testEmptySkyflowId() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1135,27 +1066,23 @@ class UnitTests { val records = JSONObject() records.put("records", recordsArray) client.getById(records, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TOKEN_ID) + val skyflowError = + SkyflowError(SkyflowErrorCode.EMPTY_ID_IN_RECORD_IDS, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - UnitTests.getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject, true) ) } - }) } @Test fun testInvalidIds() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1168,27 +1095,22 @@ class UnitTests { val records = JSONObject() records.put("records", recordsArray) client.getById(records, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_RECORD_IDS) + val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_IDS, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - UnitTests.getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject) ) } - }) } @Test fun testMissingRedaction() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1206,13 +1128,13 @@ class UnitTests { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.REDACTION_KEY_ERROR) + val skyflowError = + SkyflowError(SkyflowErrorCode.REDACTION_KEY_NOT_FOUND, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - UnitTests.getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject) ) } - }) } @@ -1220,9 +1142,7 @@ class UnitTests { @Test fun testMissingTable() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1240,10 +1160,11 @@ class UnitTests { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_TABLE_KEY) + val skyflowError = + SkyflowError(SkyflowErrorCode.TABLE_KEY_NOY_FOUND, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - UnitTests.getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject) ) } @@ -1253,9 +1174,7 @@ class UnitTests { @Test fun testInvalidTableType() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1273,7 +1192,8 @@ class UnitTests { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME) + val skyflowError = + SkyflowError(SkyflowErrorCode.INVALID_TABLE_NAME, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), UnitTests.getErrorMessage(exception as JSONObject) @@ -1287,9 +1207,7 @@ class UnitTests { @Test fun testInvalidRedaction() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1305,18 +1223,16 @@ class UnitTests { val records = JSONObject() records.put("records", recordsArray) client.getById(records, object : Callback { - override fun onSuccess(responseBody: Any) { - - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.INVALID_REDACTION_TYPE) + val skyflowError = + SkyflowError(SkyflowErrorCode.INVALID_REDACTION_TYPE, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - UnitTests.getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject) ) } - }) } @@ -1324,9 +1240,7 @@ class UnitTests { @Test fun testEmptyRedaction() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1347,10 +1261,11 @@ class UnitTests { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.MISSING_REDACTION_VALUE) + val skyflowError = + SkyflowError(SkyflowErrorCode.EMPTY_REDACTION_VALUE, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - UnitTests.getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject, true) ) } @@ -1360,9 +1275,7 @@ class UnitTests { @Test fun testEmptyTableName() { val configuration = Configuration( - "1234", - "https://vaulturl.com", - AccessTokenProvider() + "1234", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1383,10 +1296,11 @@ class UnitTests { } override fun onFailure(exception: Any) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY) + val skyflowError = + SkyflowError(SkyflowErrorCode.EMPTY_TABLE_KEY, params = arrayOf("0")) Assert.assertEquals( skyflowError.getInternalErrorMessage(), - UnitTests.getErrorMessage(exception as JSONObject) + getErrorMessage(exception as JSONObject, true) ) } @@ -1396,9 +1310,7 @@ class UnitTests { @Test fun testInvalidVaultURL() { val configuration = Configuration( - "1234", - "http://sb1.aa51.vault.skyflowapis.tech>", - AccessTokenProvider() + "1234", "http://sb1.aa51.vault.skyflowapis.tech>", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1420,8 +1332,7 @@ class UnitTests { override fun onFailure(exception: Any) { val skyflowError = SkyflowError( - SkyflowErrorCode.INVALID_VAULT_URL, - params = arrayOf(configuration.vaultURL) + SkyflowErrorCode.INVALID_VAULT_URL, params = arrayOf(configuration.vaultURL) ) Assert.assertEquals( skyflowError.getInternalErrorMessage(), @@ -1435,9 +1346,7 @@ class UnitTests { @Test fun testEmptyVaultURL() { val configuration = Configuration( - "1234", - "", - AccessTokenProvider() + "1234", "", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1471,9 +1380,7 @@ class UnitTests { @Test fun testEmptyVaultId() { val configuration = Configuration( - "", - "https://vaulturl.com", - AccessTokenProvider() + "", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1508,9 +1415,7 @@ class UnitTests { @Test fun testValidRequestForGetById() { val configuration = Configuration( - "23456", - "https://vaulturl.com", - AccessTokenProvider() + "23456", "https://vaulturl.com", AccessTokenProvider() ) val client = Client(configuration) val recordsArray = JSONArray() @@ -1557,8 +1462,7 @@ class UnitTests { val container = skyflow.container(ContainerType.COLLECT) val options = CollectElementOptions(false) val collectInput = CollectElementInput( - "cards", "card_number", - SkyflowElementType.CARD_NUMBER, placeholder = "card number" + "cards", "card_number", SkyflowElementType.CARD_NUMBER, placeholder = "card number" ) val element = (container.create(activity, collectInput, options) as? TextField) element?.on(EventName.FOCUS) { state -> @@ -1578,8 +1482,7 @@ class UnitTests { val container = skyflow.container(ContainerType.COLLECT) val options = CollectElementOptions(false) val collectInput = CollectElementInput( - "cards", "card_number", - SkyflowElementType.CARD_NUMBER, placeholder = "card number" + "cards", "card_number", SkyflowElementType.CARD_NUMBER, placeholder = "card number" ) val element = (container.create(activity, collectInput, options) as? TextField) element?.on(EventName.BLUR) { state -> @@ -1599,8 +1502,7 @@ class UnitTests { val container = skyflow.container(ContainerType.COLLECT) val options = CollectElementOptions(false) val collectInput = CollectElementInput( - "cards", "card_number", - SkyflowElementType.CARD_NUMBER, placeholder = "card number" + "cards", "card_number", SkyflowElementType.CARD_NUMBER, placeholder = "card number" ) val element = (container.create(activity, collectInput, options) as? TextField) element?.on(EventName.READY) { state -> @@ -1649,14 +1551,15 @@ class UnitTests { val client = APIClient("1234", "https://vaulturl.com", APITokenProviderForFail(), LogLevel.ERROR) client.getAccessToken(object : Callback { - override fun onSuccess(responseBody: Any) { - } + override fun onSuccess(responseBody: Any) {} override fun onFailure(exception: Any) { - val errorMessage = "bearer token is invalid or expired" - assertEquals(errorMessage, (exception as SkyflowError).getInternalErrorMessage()) + val skyflowError = SkyflowError(SkyflowErrorCode.BEARER_TOKEN_REJECTED) + assertEquals( + skyflowError.getInternalErrorMessage(), + (exception as SkyflowError).getInternalErrorMessage() + ) } - }) } @@ -1668,14 +1571,13 @@ class UnitTests { } companion object { - fun getErrorMessage(error: JSONObject): String { + fun getErrorMessage(error: JSONObject, returnFull: Boolean? = false): String { val errors = error.getJSONArray("errors") val skyflowError = errors.getJSONObject(0).get("error") as SkyflowError val message = skyflowError.getInternalErrorMessage() - if (message.indexOf("-") != -1) - return message.substring(message.indexOf("-") + 2) - else - return message + return if (returnFull == true) message + else if (message.indexOf("-") != -1) message.substring(message.indexOf("-") + 2) + else message } } @@ -1683,7 +1585,8 @@ class UnitTests { fun testGetValueForLabel() { val revealContainer = skyflow.container(ContainerType.REVEAL) val cvv = revealContainer.create( - activity, RevealElementInput(label = "cvv", token = "1234"), + activity, + RevealElementInput(label = "cvv", token = "1234"), RevealElementOptions(formatRegex = "..$") ) activity.addContentView(cvv, layoutParams) @@ -1696,7 +1599,8 @@ class UnitTests { fun testSetValueForLabel() { val revealContainer = skyflow.container(ContainerType.REVEAL) val cvv = revealContainer.create( - activity, RevealElementInput(label = "cvv", token = "1234"), + activity, + RevealElementInput(label = "cvv", token = "1234"), RevealElementOptions(formatRegex = "^([0-9]{1})$", replaceText = "0$1") ) activity.addContentView(cvv, layoutParams) @@ -1708,7 +1612,8 @@ class UnitTests { fun testSetValueForLabel1() { val revealContainer = skyflow.container(ContainerType.REVEAL) val cvv = revealContainer.create( - activity, RevealElementInput(label = "cvv", token = "1234"), + activity, + RevealElementInput(label = "cvv", token = "1234"), RevealElementOptions(formatRegex = "^([0-9]{1})$", replaceText = "0$1") ) activity.addContentView(cvv, layoutParams) @@ -1720,7 +1625,8 @@ class UnitTests { fun testSetValueForLabelFailed() { val revealContainer = skyflow.container(ContainerType.REVEAL) val cvv = revealContainer.create( - activity, RevealElementInput(label = "cvv", token = "1234"), + activity, + RevealElementInput(label = "cvv", token = "1234"), RevealElementOptions(formatRegex = "^([0-9]{1})$", replaceText = "0$1") ) activity.addContentView(cvv, layoutParams) @@ -1733,7 +1639,8 @@ class UnitTests { { val revealContainer = skyflow.container(ContainerType.REVEAL) val cvv = revealContainer.create( - activity, RevealElementInput(label = "cvv", token = "1234"), + activity, + RevealElementInput(label = "cvv", token = "1234"), RevealElementOptions(replaceText = "0$1") ) activity.addContentView(cvv, layoutParams) @@ -1933,9 +1840,14 @@ class UnitTests { fun testNotJSONObjectInUpsert() { try { - assertEquals("", Utils.getUpsertColumn("cards", JSONArray().put("123"), LogLevel.DEBUG)) + assertEquals( + String(), + Utils.getUpsertColumn("cards", JSONArray().put("123"), LogLevel.DEBUG) + ) } catch (e: SkyflowError) { - assertEquals(Messages.ALLOW_JSON_OBJECT_IN_UPSERT.message, e.getInternalErrorMessage()) + val skyflowError = + SkyflowError(SkyflowErrorCode.ALLOW_JSON_OBJECT_IN_UPSERT, params = arrayOf("0")) + assertEquals(skyflowError.getInternalErrorMessage(), e.getInternalErrorMessage()) } } @@ -1943,8 +1855,7 @@ class UnitTests { fun testFetchMetrics() { val expectedMetrics = JSONObject() expectedMetrics.put( - "sdk_name_version", - "${BuildConfig.SDK_NAME}@${BuildConfig.SDK_VERSION}" + "sdk_name_version", "${BuildConfig.SDK_NAME}@${BuildConfig.SDK_VERSION}" ) expectedMetrics.put("sdk_client_device_model", "${Build.BRAND} ${Build.MODEL}") expectedMetrics.put("sdk_client_os_details", "android-${Build.VERSION.RELEASE}") From f8743475b755318ba3bb3a98e338f1cf4bb90b02 Mon Sep 17 00:00:00 2001 From: skyflow-vivek Date: Mon, 27 May 2024 16:25:24 +0530 Subject: [PATCH 3/3] OE-1156 Fixed failing unit tests --- Skyflow/src/test/java/com/Skyflow/UnitTests.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Skyflow/src/test/java/com/Skyflow/UnitTests.kt b/Skyflow/src/test/java/com/Skyflow/UnitTests.kt index 517c3ca..319a5a8 100644 --- a/Skyflow/src/test/java/com/Skyflow/UnitTests.kt +++ b/Skyflow/src/test/java/com/Skyflow/UnitTests.kt @@ -504,7 +504,9 @@ class UnitTests { val x = Utils.constructBatchRequestBody(records, InsertOptions(), LogLevel.ERROR) assertEquals(x.toString().trim(), JSONObject().toString().trim()) } catch (exception: Exception) { - val skyflowError = SkyflowError(SkyflowErrorCode.EMPTY_COLUMN_KEY, params = arrayOf()) + val skyflowError = SkyflowError( + SkyflowErrorCode.EMPTY_FIELD_IN_FIELDS, params = arrayOf("0") + ) assertEquals( skyflowError.getInternalErrorMessage(), (exception as SkyflowError).getInternalErrorMessage()