diff --git a/open-banking-accelerator/accelerators/ob-is/carbon-home/repository/resources/conf/templates/repository/conf/open-banking.xml.j2 b/open-banking-accelerator/accelerators/ob-is/carbon-home/repository/resources/conf/templates/repository/conf/open-banking.xml.j2 index f9c86194..a4c75488 100644 --- a/open-banking-accelerator/accelerators/ob-is/carbon-home/repository/resources/conf/templates/repository/conf/open-banking.xml.j2 +++ b/open-banking-accelerator/accelerators/ob-is/carbon-home/repository/resources/conf/templates/repository/conf/open-banking.xml.j2 @@ -206,6 +206,11 @@ {% else %} https://localhost:8243/open-banking/{version}/cbpii/ {% endif %} + {% if open_banking.consent.vrp_consent_self_link is defined %} + {{open_banking.consent.vrp_consent_self_link}} + {% else %} + https://localhost:8243/open-banking/{version}/vrp/ + {% endif %} {% if open_banking.consent.data_retention.enabled is defined %} {{open_banking.consent.data_retention.enabled}} diff --git a/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/AnalyticsLogsUtils.java b/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/AnalyticsLogsUtils.java index 736ba448..72c9fc03 100644 --- a/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/AnalyticsLogsUtils.java +++ b/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/AnalyticsLogsUtils.java @@ -27,7 +27,7 @@ import java.util.Map; /** - * Open Banking common utility class to publish analytics logs + * Open Banking common utility class to publish analytics logs. */ public class AnalyticsLogsUtils { @@ -36,7 +36,7 @@ public class AnalyticsLogsUtils { private static final String DATA_PROCESSING_ERROR = "Error occurred while processing the analytics dataset"; /** - * Method to add analytics logs to the OB analytics log file + * Method to add analytics logs to the OB analytics log file. * * @param logFile Name of the logger which is used to log analytics data to the log file * @param dataStream Name of the data stream to which the data belongs diff --git a/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/ErrorConstants.java b/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/ErrorConstants.java index 7f3ce859..628dc98c 100644 --- a/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/ErrorConstants.java +++ b/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/ErrorConstants.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations * under the License. */ + package com.wso2.openbanking.accelerator.common.util; /** @@ -48,12 +49,19 @@ public class ErrorConstants { public static final String PATH_CREDIT_ACCOUNT_IDENTIFICATION = "Data.Initiation.CreditorAccount.Identification"; public static final String PATH_CREDIT_ACCOUNT_SCHEME = "Data.Initiation.CreditorAccount.SchemeName"; - public static final String PATH_INVALID = "Request path invalid"; public static final String PAYLOAD_INVALID = "Consent validation failed due to invalid initiation payload"; public static final String NOT_JSON_OBJECT_ERROR = "Payload is not a JSON object"; public static final String PAYLOAD_FORMAT_ERROR = "Request Payload is not in correct JSON format"; + public static final String PAYLOAD_FORMAT_ERROR_VALID_TO_DATE = "Invalid valid_to_date parameter in the payload" + + "for valid to date"; + public static final String PAYLOAD_FORMAT_ERROR_DEBTOR_ACC = "Parameter Debtor Account does not exists "; + public static final String PAYLOAD_FORMAT_ERROR_CREDITOR_ACC = "Parameter Creditor Account " + + "does not exists "; public static final String INVALID_REQ_PAYLOAD = "Invalid request payload"; + public static final String INVALID_REQ_PAYLOAD_INITIATION = "Invalid request payload in initiation key"; + public static final String INVALID_REQ_PAYLOAD_CONTROL_PARAMETERS = "Invalid request payload in " + + "control parameter key"; public static final String MISSING_DEBTOR_ACC_SCHEME_NAME = "Mandatory parameter Debtor Account Scheme Name does " + "not exists"; public static final String MISSING_DEBTOR_ACC_IDENTIFICATION = "Mandatory parameter Debtor Account Identification" + @@ -108,6 +116,7 @@ public class ErrorConstants { public static final String PATH_CONSENT_ID = "Data.Initiation.Consent-id"; public static final String PATH_DATA = "Data"; public static final String PATH_INITIATION = "Data.Initiation"; + public static final String PATH_CONTROL_PARAMETERS = "Data.ControlParameters"; public static final String PATH_RISK = "Data.Risk"; public static final String PATH_URL = "Data.Url"; public static final String PATH_EXPIRATION_DATE = "Data.Expiration-Date"; @@ -120,7 +129,6 @@ public class ErrorConstants { public static final String STATE_INVALID_ERROR = "Consent not in authorizable state"; public static final String DATE_PARSE_MSG = "Parsed OffsetDateTime: %s, current OffsetDateTime: %s"; public static final String EXP_DATE_PARSE_ERROR = "Error occurred while parsing the expiration date. "; - public static final String ACC_CONSENT_RETRIEVAL_ERROR = "Error occurred while retrieving the account initiation" + " request details"; public static final String CONSENT_EXPIRED = "Provided consent is expired"; @@ -210,6 +218,7 @@ public class ErrorConstants { public static final String RISK_MISMATCH = "RISK Does Not Match.:" + ErrorConstants.PATH_RISK; public static final String RISK_NOT_FOUND = "RISK is not found or empty in the request.:" + ErrorConstants.PATH_RISK; + public static final String INVALID_URI_ERROR = "Path requested is invalid. :" + ErrorConstants.PATH_URL; public static final String COF_CONSENT_STATE_INVALID = "Confirmation of Funds validation failed due to invalid" + " consent state.:" + ErrorConstants.PATH_STATUS; @@ -221,9 +230,65 @@ public class ErrorConstants { " retrieval request"; public static final String INVALID_CONSENT_ID = "Invalid Consent Id found in the request"; public static final String CONSENT_ID_NOT_FOUND = "Consent ID not available in consent data"; - public static final String FIELD_INVALID_DATE = "OB.Field.InvalidDate"; public static final String EXPIRED_DATE_ERROR = "The ExpirationDateTime value has to be a future date."; + // VRP error constants + + public static final String VRP_INITIATION_HANDLE_ERROR = "Error occurred while handling the VRP " + + "initiation request"; + + public static final String VRP_INITIATION_RETRIEVAL_ERROR = "Error occurred while handling the VRP initiation" + + " retrieval request"; + public static final String PAYLOAD_FORMAT_ERROR_VALID_FROM_DATE = "Request Payload is not in correct JSON format" + + " for valid from date"; + public static final String INVALID_VALID_TO_DATE_TIME = "Invalid date time format in validToDateTime"; + public static final String INVALID_VALID_FROM_DATE_TIME = "Invalid date time format in validFromDateTime"; + public static final String PAYLOAD_FORMAT_ERROR_CONTROL_PARAMETER = "Request Payload is not in correct JSON " + + "format for control parameter key"; + public static final String PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_AMOUNT = "Invalid maximum individual amount"; + public static final String MISSING_MAXIMUM_INDIVIDUAL_AMOUNT = "Missing mandatory parameter Maximum Individual" + + " Amount"; + public static final String PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_CURRENCY = "Invalid maximum individual amount" + + "currency"; + public static final String PAYLOAD_FORMAT_ERROR_INITIATION = "Missing mandatory parameter Initiation" + + " in the payload"; + public static final String PAYLOAD_FORMAT_ERROR_RISK = "Mandatory parameter Risk does not exists" + + " in the payload"; + public static final String INVALID_PERIOD_TYPE = "Invalid value for period type in PeriodicLimits"; + public static final String INVALID_PARAMETER = "Parameter passed in is null , " + + "empty or not a JSONObject"; + public static final String INVALID_CLIENT_ID_MATCH = "Consent validation failed due to client ID mismatch"; + public static final String INVALID_DATE_TIME_FORMAT = "Date and Time is not in correct JSON " + + "ISO-8601 date-time format"; + public static final String MISSING_DATE_TIME_FORMAT = "The value is empty or the value is not a string"; + public static final String MISSING_VALID_TO_DATE_TIME = "Missing parameter ValidToDateTime"; + public static final String MISSING_VALID_FROM_DATE_TIME = "Missing parameter ValidFromDateTime"; + public static final String INVALID_PARAMETER_PERIODIC_LIMITS = "Parameter passed in is null , " + + "empty or not a JSONArray"; + public static final String MISSING_PERIOD_LIMITS = "Mandatory parameter " + + "periodic limits is missing in the payload"; + public static final String MISSING_PERIOD_TYPE = "Missing required parameter Period type"; + public static final String PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS_PERIOD_TYPE = "Value of period type is empty or " + + "the value passed in is not a string"; + public static final String PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS_ALIGNMENT = "Value of periodic alignment is empty" + + " or the value passed in is not a string"; + public static final String MISSING_PERIOD_ALIGNMENT = "Missing periodic alignment in periodic limits"; + public static final String INVALID_PERIOD_ALIGNMENT = "Invalid value for period alignment in PeriodicLimits"; + public static final String INVALID_PARAMETER_MESSAGE = "Parameter '%s' passed in is null, empty, or not a %s"; + public static final String DATE_INVALID_PARAMETER_MESSAGE = "Invalid date-time range for ValidToDateTime "; + + + + // vrp path parameters + public static final String PATH_VALID_TO_DATE = "Data.ControlParameters.ValidToDateTime"; + public static final String PATH_VALID_FROM_DATE = "Data.ControlParameters.ValidFromDateTime"; + public static final String PATH_MAXIMUM_INDIVIDUAL_AMOUNT = "Data.ControlParameters.MaximumIndividualAmount"; + public static final String PATH_PERIOD_LIMIT = "Data.ControlParameters.PeriodicLimits"; + public static final String PATH_PERIOD_LIMIT_AMOUNT = "Data.ControlParameters.PeriodicLimits.Amount"; + public static final String PATH_PERIOD_LIMIT_CURRENCY = "Data.ControlParameters.PeriodicLimits.Currency"; + public static final String PATH_PERIOD_TYPE = "Data.ControlParameters.PeriodicLimits.PeriodType"; + public static final String PATH_PERIOD_ALIGNMENT = "Data.ControlParameters.PeriodicLimits.PeriodAlignment"; + } diff --git a/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/OpenBankingUtils.java b/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/OpenBankingUtils.java index d8c68a9a..3c57ce21 100644 --- a/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/OpenBankingUtils.java +++ b/open-banking-accelerator/components/com.wso2.openbanking.accelerator.common/src/main/java/com/wso2/openbanking/accelerator/common/util/OpenBankingUtils.java @@ -57,7 +57,7 @@ public static Object getClassInstanceFromFQN(String classpath) { } /** - * Extract software_environment (SANDBOX or PRODUCTION) from SSA + * Extract software_environment (SANDBOX or PRODUCTION) from SSA. * * @param softwareStatement software statement (jwt) extracted from request payload * @return software_environment @@ -103,7 +103,7 @@ public static boolean isPublishableDisputeData(int statusCode) { } /** - * Method to reduce string length + * Method to reduce string length. * * @param input and maxLength for dispute data * @return String diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/common/ConsentExtensionConstants.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/common/ConsentExtensionConstants.java index 6d02d829..53292e0c 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/common/ConsentExtensionConstants.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/common/ConsentExtensionConstants.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -16,7 +16,6 @@ * under the License. */ package com.wso2.openbanking.accelerator.consent.extensions.common; -; /** * Constant class for consent extension module. @@ -48,6 +47,8 @@ public class ConsentExtensionConstants { public static final String HTTP_CODE = "httpCode"; public static final String ERRORS = "errors"; public static final String PAYMENTS = "payments"; + public static final String VRP = "vrp"; + public static final String DATA = "Data"; public static final String INITIATION = "Initiation"; public static final String STATUS = "Status"; @@ -95,7 +96,6 @@ public class ConsentExtensionConstants { public static final String DATA_SIMPLE = "data"; public static final String DEBTOR_ACCOUNT_ID = "AccountId"; public static final String ACCOUNT_ID = "account_id"; - public static final String DATA_REQUESTED = "data_requested"; public static final String PAYMENT_ACCOUNT = "paymentAccount"; public static final String COF_ACCOUNT = "cofAccount"; @@ -108,7 +108,6 @@ public class ConsentExtensionConstants { public static final String OPENBANKING_INTENT_ID = "openbanking_intent_id"; public static final String VALUE = "value"; public static final String AUTHORIZED_STATUS = "authorised"; - public static final String EXPIRATION_DATE = "ExpirationDateTime"; public static final String EXPIRATION_DATE_TITLE = "Expiration Date Time"; public static final String INSTRUCTED_AMOUNT_TITLE = "Instructed Amount"; @@ -153,19 +152,50 @@ public class ConsentExtensionConstants { public static final String ACCOUNTS_SELF_LINK = "Consent.AccountAPIURL"; public static final String PAYMENT_SELF_LINK = "Consent.PaymentAPIURL"; public static final String COF_SELF_LINK = "Consent.FundsConfirmationAPIURL"; + public static final String VRP_SELF_LINK = "Consent.VRPAPIURL"; public static final String REVOKED_STATUS = "revoked"; - public static final String DISPLAY_NAME = "display_name"; public static final String ACCOUNT_DATA = "account_data"; public static final String SELECTED_ACCOUNT = "selectedAccount"; public static final String PAYMENT_COF_PATH = "funds-confirmation"; - public static final String AWAITING_UPLOAD_STATUS = "awaitingUpload"; - public static final String OB_REVOKED_STATUS = "Revoked"; public static final String OB_REJECTED_STATUS = "Rejected"; public static final String OB_AUTHORIZED_STATUS = "Authorised"; public static final String OB_AWAITING_AUTH_STATUS = "AwaitingAuthorisation"; public static final String OB_AWAITING_UPLOAD_STATUS = "AwaitingUpload"; + //VRP Constants + public static final String VRP_CONSENT_PATH = "domestic-vrp-consents"; + public static final String VRP_PAYMENT = "vrp-payment"; + public static final String PAID_AMOUNT = "paid-amount"; + public static final String LAST_PAYMENT_DATE = "last-payment-date"; + public static final String AUTH_TYPE_AUTHORIZATION = "authorization"; + public static final String CONTROL_PARAMETERS = "ControlParameters"; + public static final String MAXIMUM_INDIVIDUAL_AMOUNT = "MaximumIndividualAmount"; + public static final String MAXIMUM_INDIVIDUAL_AMOUNT_CURRENCY = "MaximumIndividualAmount.Amount.Currency"; + public static final String PERIODIC_LIMITS = "PeriodicLimits"; + public static final String PERIOD_AMOUNT_LIMIT = "Amount"; + public static final String PERIOD_LIMIT_CURRENCY = "PeriodicLimits.Currency"; + + //vrp period alignment + public static final String PERIOD_ALIGNMENT = "PeriodAlignment"; + + // vrp periodic alignment types + public static final String CONSENT = "Consent"; + public static final String CALENDAR = "Calendar"; + + //vrp periodicLimits + public static final String PERIOD_TYPE = "PeriodType"; + + //vrp periodic types + public static final String DAY = "Day"; + public static final String WEEK = "Week"; + public static final String FORTNIGHT = "Fortnight"; + public static final String MONTH = "Month"; + public static final String HALF_YEAR = "Half-year"; + public static final String YEAR = "Year"; + public static final String VALID_TO_DATE_TIME = "ValidToDateTime"; + public static final String VALID_FROM_DATE_TIME = "ValidFromDateTime"; + public static final String VRP_RESPONSE_PROCESS_PATH = "vrp-response-process"; } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/common/factory/AcceleratorConsentExtensionFactory.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/common/factory/AcceleratorConsentExtensionFactory.java index ef849604..47a00f48 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/common/factory/AcceleratorConsentExtensionFactory.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/common/factory/AcceleratorConsentExtensionFactory.java @@ -30,6 +30,7 @@ import com.wso2.openbanking.accelerator.consent.extensions.manage.impl.CofConsentRequestHandler; import com.wso2.openbanking.accelerator.consent.extensions.manage.impl.ConsentManageRequestHandler; import com.wso2.openbanking.accelerator.consent.extensions.manage.impl.PaymentConsentRequestHandler; +import com.wso2.openbanking.accelerator.consent.extensions.manage.impl.VRPConsentRequestHandler; /** * Factory class to get the class based in request type. @@ -55,6 +56,9 @@ public static ConsentManageRequestHandler getConsentManageRequestValidator(Strin case ConsentExtensionConstants.PAYMENT_CONSENT_PATH: consentManageRequestHandler = new PaymentConsentRequestHandler(); break; + case ConsentExtensionConstants.VRP_CONSENT_PATH: + consentManageRequestHandler = new VRPConsentRequestHandler(); + break; default: return null; } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/impl/VRPConsentRequestHandler.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/impl/VRPConsentRequestHandler.java new file mode 100644 index 00000000..485164ec --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/impl/VRPConsentRequestHandler.java @@ -0,0 +1,231 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.wso2.openbanking.accelerator.consent.extensions.manage.impl; + +import com.wso2.openbanking.accelerator.common.exception.ConsentManagementException; +import com.wso2.openbanking.accelerator.common.util.ErrorConstants; +import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentException; +import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionConstants; +import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentServiceUtil; +import com.wso2.openbanking.accelerator.consent.extensions.common.ResponseStatus; +import com.wso2.openbanking.accelerator.consent.extensions.manage.model.ConsentManageData; +import com.wso2.openbanking.accelerator.consent.extensions.manage.validator.VRPConsentRequestValidator; +import com.wso2.openbanking.accelerator.consent.extensions.util.ConsentManageUtil; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.DetailedConsentResource; +import net.minidev.json.JSONArray; +import net.minidev.json.JSONObject; +import net.minidev.json.parser.JSONParser; +import net.minidev.json.parser.ParseException; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.HashMap; +import java.util.Map; + +import static com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionConstants.AUTH_TYPE_AUTHORIZATION; +import static com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionConstants.CREATED_STATUS; + +/** + * Consent Manage request handler class for VRP Payment Request Validation. + */ +public class VRPConsentRequestHandler implements ConsentManageRequestHandler { + + private static final Log log = LogFactory.getLog(VRPConsentRequestHandler.class); + + + /** + * This method is responsible for processing a Variable Recurring Payment Consent Manage POST request. + * It validates the payment request, checks for the existence of an idempotency key. + * + * @param consentManageData Object + */ + @Override + public void handleConsentManagePost(ConsentManageData consentManageData) { + + try { + Object request = consentManageData.getPayload(); + + JSONObject validationResponse = VRPConsentRequestValidator.validateVRPPayload(request); + + if (!(Boolean.parseBoolean(validationResponse.getAsString(ConsentExtensionConstants.IS_VALID)))) { + log.error(validationResponse.get(ConsentExtensionConstants.ERRORS)); + throw new ConsentException((ResponseStatus) validationResponse + .get(ConsentExtensionConstants.HTTP_CODE), + String.valueOf(validationResponse.get(ConsentExtensionConstants.ERRORS))); + } + + if (StringUtils.isEmpty(consentManageData.getHeaders() + .get(ConsentExtensionConstants.X_IDEMPOTENCY_KEY))) { + log.error(ErrorConstants.IDEMPOTENCY_KEY_NOT_FOUND); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.IDEMPOTENCY_KEY_NOT_FOUND); + } + + //Handle payment initiation flows + handlePaymentPost(consentManageData, request); + + } catch (ConsentManagementException e) { + log.error("Error occurred while handling the initiation request", e); + throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, + ErrorConstants.VRP_INITIATION_HANDLE_ERROR); + } + } + + /** + * This method is responsible for handling the GET request for retrieving consent initiation details. + * It validates the consent ID, checks if the consent exists,verifies if the consent belongs to the + * client making the request. + * + * @param consentManageData Object + */ + @Override + public void handleConsentManageGet(ConsentManageData consentManageData) { + + String consentId = consentManageData.getRequestPath().split("/")[1]; + if (ConsentManageUtil.isConsentIdValid(consentId)) { + try { + ConsentResource consent = ConsentServiceUtil.getConsentService().getConsent(consentId, + false); + // Check whether the client id is matching + if (!consent.getClientID().equals(consentManageData.getClientId())) { + // Throws the error if the client Ids mismatch + if (log.isDebugEnabled()) { + log.debug(String.format("ClientIds missmatch. " + + "Retrieved client id: %s, ConsentmanageData client id: %s", + consent.getClientID(), consentManageData.getClientId())); + } + + throw new ConsentException(ResponseStatus.BAD_REQUEST, + "Invalid client id passed"); + } + + JSONObject receiptJSON = (JSONObject) new JSONParser(JSONParser.MODE_PERMISSIVE). + parse(consent.getReceipt()); + consentManageData.setResponsePayload(ConsentManageUtil + .getInitiationRetrievalResponse(receiptJSON, consent, consentManageData, + ConsentExtensionConstants.VRP)); + consentManageData.setResponseStatus(ResponseStatus.OK); + } catch (ConsentManagementException | ParseException e) { + log.error(ErrorConstants.INVALID_CLIENT_ID_MATCH, e); + throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, + ErrorConstants.VRP_INITIATION_RETRIEVAL_ERROR); + } + } else { + log.error(ErrorConstants.INVALID_CONSENT_ID); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.INVALID_CONSENT_ID); + } + } + + /** + * Handles the DELETE request for revoking or deleting a consent. + * + * @param consentManageData Object containing request details + */ + @Override + public void handleConsentManageDelete(ConsentManageData consentManageData) { + + ConsentManageUtil.handleConsentManageDelete(consentManageData); + } + + /** + * Method to handle Variable Recurring Payment POST requests. + * This private method processes Variable Recurring Payment POST requests, creating a new consent + * based on the provided request payload. It performs the following actions: + * - Creates a DetailedConsentResource representing the consent initiation. + * - Stores consent attributes, including the idempotency key. + * - Constructs the response payload containing initiation details and sets appropriate headers. + * - Sets the response status to Created. + * + * @param consentManageData Object containing request details, including client ID, request payload, headers. + * @param request Object + */ + public void handlePaymentPost(ConsentManageData consentManageData, Object request) + throws ConsentManagementException { + + if (request instanceof JSONObject) { + JSONObject requestObject = (JSONObject) request; + + // Create a ConsentResource representing the requested consent + ConsentResource requestedConsent = new ConsentResource(consentManageData.getClientId(), + requestObject.toJSONString(), ConsentExtensionConstants.VRP, + ConsentExtensionConstants.AWAITING_AUTH_STATUS); + + // Create the consent + DetailedConsentResource createdConsent = ConsentServiceUtil.getConsentService() + .createAuthorizableConsent(requestedConsent, null, + CREATED_STATUS, AUTH_TYPE_AUTHORIZATION, true); + + //Set consent attributes for storing + Map consentAttributes = new HashMap<>(); + consentAttributes.put(ConsentExtensionConstants.IDEMPOTENCY_KEY, consentManageData.getHeaders() + .get(ConsentExtensionConstants.X_IDEMPOTENCY_KEY)); + + consentManageData.setResponsePayload(ConsentManageUtil.getInitiationResponse(requestObject, createdConsent, + consentManageData, ConsentExtensionConstants.VRP)); + + //Set Control Parameters as consent attributes to store + JSONObject controlParameters = (JSONObject) ((JSONObject) ((JSONObject) consentManageData.getPayload()) + .get(ConsentExtensionConstants.DATA)).get(ConsentExtensionConstants.CONTROL_PARAMETERS); + + consentAttributes.put(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT, + ((JSONObject) (controlParameters) + .get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)) + .get(ConsentExtensionConstants.AMOUNT).toString()); + + consentAttributes.put(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT_CURRENCY, + ((JSONObject) (controlParameters) + .get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)) + .get(ConsentExtensionConstants.CURRENCY).toString()); + + consentAttributes.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, ((JSONObject) ((JSONArray) + (controlParameters).get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)) + .get(ConsentExtensionConstants.PERIOD_ALIGNMENT).toString()); + //TODO: Improve the logic of storing the PERIODIC_LIMITS and rest of VRP parameters + + consentAttributes.put(ConsentExtensionConstants.PERIOD_TYPE, + ((JSONObject) ((JSONArray) (controlParameters) + .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)).get(ConsentExtensionConstants.PERIOD_TYPE) + .toString()); + + consentAttributes.put(ConsentExtensionConstants.PERIOD_AMOUNT_LIMIT, ((JSONObject) + ((JSONArray) (controlParameters).get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)) + .get(ConsentExtensionConstants.PERIOD_AMOUNT_LIMIT).toString()); + + consentAttributes.put(ConsentExtensionConstants.PERIOD_LIMIT_CURRENCY, ((JSONObject) + ((JSONArray) (controlParameters).get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)) + .get(ConsentExtensionConstants.CURRENCY).toString()); + + //Store consent attributes + ConsentServiceUtil.getConsentService().storeConsentAttributes(createdConsent.getConsentID(), + consentAttributes); + + // Get request headers + Map headers = consentManageData.getHeaders(); + + consentManageData.setResponseHeader(ConsentExtensionConstants.X_IDEMPOTENCY_KEY, + headers.get(ConsentExtensionConstants.X_IDEMPOTENCY_KEY)); + consentManageData.setResponseStatus(ResponseStatus.CREATED); + + } else { + log.error("Invalid request type. Expected JSONObject."); + throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, + ErrorConstants.PAYLOAD_FORMAT_ERROR); + } + } +} diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/validator/VRPConsentRequestValidator.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/validator/VRPConsentRequestValidator.java new file mode 100644 index 00000000..14e2c1c3 --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/validator/VRPConsentRequestValidator.java @@ -0,0 +1,767 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + *

+ * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.wso2.openbanking.accelerator.consent.extensions.manage.validator; + +import com.wso2.openbanking.accelerator.common.util.ErrorConstants; +import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionConstants; +import com.wso2.openbanking.accelerator.consent.extensions.util.ConsentManageUtil; +import net.minidev.json.JSONArray; +import net.minidev.json.JSONObject; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.Iterator; + + +/** + * Consent Manage validator class for Variable Recurring Payment Request Validation. + */ +public class VRPConsentRequestValidator { + + private static final Log log = LogFactory.getLog(VRPConsentRequestValidator.class); + + /** + * Method to validate a variable recurring payment request. + * This method performs validation on the variable recurring payment request. + * It checks the validity of the data body, the initiation payload, control parameters, + * and ensures that the risk information is present. If any validation fails, the method returns a detailed + * validation response indicating the error. If all validations pass, the returned validation response + * indicates that the initiation request is valid. + * + * @param request The initiation object containing the variable recurring payment initiation request. + * @return A validation response object indicating whether the initiation request is valid. + */ + public static JSONObject validateVRPPayload(Object request) { + + JSONObject validationResponse = new JSONObject(); + validationResponse.put(ConsentExtensionConstants.IS_VALID, false); + + //Get the request payload from the ConsentManageData + if (!(request instanceof JSONObject)) { + log.error(ErrorConstants.PAYLOAD_FORMAT_ERROR); + return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR); + } + + JSONObject requestBody = (JSONObject) request; + //Check request body is valid and not empty + JSONObject dataValidationResult = ConsentManageUtil.validateInitiationDataBody(requestBody); + + if (!(Boolean.parseBoolean(dataValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { + return dataValidationResult; + } + + //Check consent initiation is valid and not empty + JSONObject initiationValidationResult = VRPConsentRequestValidator.validateConsentInitiation(requestBody); + + if (!(Boolean.parseBoolean(initiationValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { + return initiationValidationResult; + } + + JSONObject controlParameterValidationResult = VRPConsentRequestValidator. + validateConsentControlParameters(requestBody); + + if (!(Boolean.parseBoolean(controlParameterValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return controlParameterValidationResult; + } + + JSONObject riskValidationResult = VRPConsentRequestValidator.validateConsentRisk(requestBody); + + if (!(Boolean.parseBoolean(riskValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { + return riskValidationResult; + } + + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Method to validate control parameters for variable recurring payments. + * This method performs validation on the control parameters for variable recurring payments. + * It checks the validity of maximum individual amount, requested execution date-time, and periodic limits. + * If any validation fails, the method returns a detailed validation response indicating the error. + * If all validations pass, the returned validation response indicates that the control parameters are valid. + * + * @param controlParameters The initiation object containing control parameters for variable recurring payments. + * @return A validation response object indicating whether the control parameters are valid. + */ + public static JSONObject validateControlParameters(JSONObject controlParameters) { + JSONObject validationResponse = new JSONObject(); + + JSONObject maximumIndividualAmountResult = validateMaximumIndividualAmount(controlParameters); + if (!(Boolean.parseBoolean(maximumIndividualAmountResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { + return maximumIndividualAmountResult; + } + + JSONObject maximumIndividualAmountCurrencyValidationResult = validateMaximumIndividualAmountCurrency + (controlParameters); + if (!(Boolean.parseBoolean(maximumIndividualAmountCurrencyValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return maximumIndividualAmountCurrencyValidationResult; + } + + JSONObject parameterDateTimeValidationResult = validateParameterDateTime(controlParameters); + if (!(Boolean.parseBoolean(parameterDateTimeValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return parameterDateTimeValidationResult; + } + + // Validate Periodic Limits + JSONObject periodicLimitsValidationResult = validatePeriodicLimits(controlParameters); + if (!(Boolean.parseBoolean(periodicLimitsValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { + return periodicLimitsValidationResult; + } + + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Checks whether the given object is a valid JSONArray. + * This method verifies if the provided object is not null and is an instance of JSONArray. + * It is commonly used to validate whether a given object represents a valid JSON array. + * + * @param value The object to be checked for being a valid JSONArray. + */ + public static boolean isValidJSONArray(Object value) { + String errorMessage = String.format(ErrorConstants.INVALID_PARAMETER_MESSAGE, "periodic limit", + "JSONObject"); + return value instanceof JSONArray; + } + + /** + * Validates the Maximum Individual Amount in the control parameters of a consent request. + * + * @param controlParameters The JSON object representing the control parameters of the consent request. + * @return A JSON object containing the validation response. + */ + public static JSONObject validateMaximumIndividualAmount(JSONObject controlParameters) { + + JSONObject validationResponse = new JSONObject(); + + //Validate Maximum individual amount in control parameters + if (controlParameters.containsKey(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)) { + + Object maximumIndividualAmount = controlParameters. + get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT); + + String errorMessage = String.format(ErrorConstants.INVALID_PARAMETER_MESSAGE, + "maximum individual amount", "JSONObject"); + + // Check if the control parameter is valid + if (!isValidJSONObject(maximumIndividualAmount)) { + return ConsentManageUtil.getValidationResponse(errorMessage); + } + + JSONObject maximumIndividualAmountResult = validateJsonObjectKey((JSONObject) maximumIndividualAmount, + ConsentExtensionConstants.AMOUNT, String.class); + if (!(Boolean.parseBoolean(maximumIndividualAmountResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return maximumIndividualAmountResult; + } + } else { + log.error(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT); + return ConsentManageUtil.getValidationResponse(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT); + + } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Validates the Currency in Maximum Individual Amount in the control parameters of a consent request. + * + * @param controlParameters The JSON object representing the control parameters of the consent request. + * @return A JSON object containing the validation response. + */ + public static JSONObject validateMaximumIndividualAmountCurrency(JSONObject controlParameters) { + JSONObject validationResponse = new JSONObject(); + Object maximumIndividualAmount = controlParameters. + get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT); + + JSONObject maximumIndividualAmountValidationResult = validateJsonObjectKey((JSONObject) maximumIndividualAmount, + ConsentExtensionConstants.CURRENCY, String.class); + if (!(Boolean.parseBoolean(maximumIndividualAmountValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return maximumIndividualAmountValidationResult; + } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Method to validate variable recurring payment periodic limits. + * This method validates the periodic limits specified in the control parameters for variable recurring payments. + * It checks if the provided JSON array of periodic limits is valid and then iterates through each limit + * to ensure that required fields such as amount, currency, period alignment, and period type are present and + * meet the specified criteria. + * + * @param controlParameters Initiation Object containing periodic limits + * @return validation response object indicating whether the provided periodic limits are valid + */ + public static JSONObject validatePeriodicLimits(JSONObject controlParameters) { + JSONObject validationResponse = new JSONObject(); + + // Check if the periodic limits key is present + if (controlParameters.containsKey(ConsentExtensionConstants.PERIODIC_LIMITS)) { + + // Retrieve the periodic limits from the control parameters + Object periodicLimit = controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); + + // Check if the control parameter is a valid JSON array + if (!isValidJSONArray(periodicLimit)) { + return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_PARAMETER_PERIODIC_LIMITS); + } + + JSONArray periodicLimits = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); + Iterator parameters = periodicLimits.iterator(); + + while (parameters.hasNext()) { + JSONObject limit = (JSONObject) parameters.next(); + + JSONObject amountValidationResult = validateAmountPeriodicLimit(controlParameters); + if (!(Boolean.parseBoolean(amountValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return amountValidationResult; + } + + JSONObject currencyValidationResult = validateCurrencyPeriodicLimit(controlParameters); + if (!(Boolean.parseBoolean(currencyValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return currencyValidationResult; + } + + JSONObject periodAlignmentValidationResult = validatePeriodAlignment(limit); + if (!(Boolean.parseBoolean(periodAlignmentValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return periodAlignmentValidationResult; + } + + JSONObject periodTypeValidationResult = validatePeriodType(limit); + if (!(Boolean.parseBoolean(periodTypeValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return periodTypeValidationResult; + } + } + } else { + // If periodic limits key is missing, return an error + log.error(ErrorConstants.MISSING_PERIOD_LIMITS); + return ConsentManageUtil.getValidationResponse(ErrorConstants.MISSING_PERIOD_LIMITS); + } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Validates the Currency in periodic limits in the control parameters of a consent request. + * + * @param controlParameters The JSON object representing the control parameters of the consent request. + * @return A JSON object containing the validation response. + */ + public static JSONObject validateCurrencyPeriodicLimit(JSONObject controlParameters) { + + JSONObject validationResponse = new JSONObject(); + + JSONArray periodicLimits = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); + JSONObject currencyValidationResponse = validateAmountCurrencyPeriodicLimits((JSONArray) periodicLimits, + ConsentExtensionConstants.CURRENCY, String.class); + if (!(Boolean.parseBoolean(currencyValidationResponse. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return currencyValidationResponse; + } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Validates the Amount in periodic limits in the control parameters of a consent request. + * + * @param controlParameters The JSON object representing the control parameters of the consent request. + * @return A JSON object containing the validation response. + */ + public static JSONObject validateAmountPeriodicLimit(JSONObject controlParameters) { + + JSONObject validationResponse = new JSONObject(); + + JSONArray periodicLimits = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); + + JSONObject amountValidationResponse = validateAmountCurrencyPeriodicLimits((JSONArray) periodicLimits, + ConsentExtensionConstants.AMOUNT, String.class); + if (!(Boolean.parseBoolean(amountValidationResponse. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return amountValidationResponse; + } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Validates the date-time parameters in the control parameters of a consent request. + * + * @param controlParameters The JSON object representing the control parameters of the consent request. + * @return A JSON object containing the validation response. If the date-time parameters are valid, + * it sets the "IS_VALID" field to true; otherwise, it contains an error response. + */ + public static JSONObject validateParameterDateTime(JSONObject controlParameters) { + JSONObject validationResponse = new JSONObject(); + + if (controlParameters.containsKey(ConsentExtensionConstants.VALID_TO_DATE_TIME)) { + + if (!ConsentManageUtil.isValid8601(controlParameters + .getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME))) { + log.error(" Date and Time is not in valid ISO 8601 format"); + return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_VALID_TO_DATE_TIME); + } + + Object validToDateTimeRetrieval = controlParameters.get(ConsentExtensionConstants.VALID_TO_DATE_TIME); + JSONObject validToDateTimeValidationResponse = isValidDateTimeObject(validToDateTimeRetrieval); + if (!(Boolean.parseBoolean(validToDateTimeValidationResponse. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return validToDateTimeValidationResponse; + } + + String validToDateTimeString = controlParameters.getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME); + OffsetDateTime validToDateTime = OffsetDateTime.parse(validToDateTimeString); + + if (controlParameters.containsKey(ConsentExtensionConstants.VALID_FROM_DATE_TIME)) { + + if (!ConsentManageUtil.isValid8601(controlParameters + .getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME))) { + log.error("Date and Time is not in valid ISO 8601 format"); + return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_VALID_FROM_DATE_TIME); + } + + + Object validFromDateTimeRetrieval = controlParameters.get + (ConsentExtensionConstants.VALID_FROM_DATE_TIME); + JSONObject validFromDateTimeValidationResponse = isValidDateTimeObject(validFromDateTimeRetrieval); + if (!(Boolean.parseBoolean(validFromDateTimeValidationResponse. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return validFromDateTimeValidationResponse; + } + + String validFromoDateTimeString = controlParameters.getAsString + (ConsentExtensionConstants.VALID_FROM_DATE_TIME); + + OffsetDateTime validFromDateTime = OffsetDateTime.parse(validFromoDateTimeString); + OffsetDateTime currentDateTime = OffsetDateTime.now(validToDateTime.getOffset()); + + // If ValidToDateTime is older than current date OR ValidToDateTime is older than ValidFromDateTime, + // return error + if (!validFromDateTime.isBefore(currentDateTime) || !currentDateTime.isBefore(validToDateTime)) { + log.error(String.format("Invalid date-time range, " + + "validToDateTime: %s, validFromDateTime: %s, currentDateTime: %s", + validToDateTime, validFromDateTime, currentDateTime)); + + String errorMessage = String.format(ErrorConstants.DATE_INVALID_PARAMETER_MESSAGE); + + return ConsentManageUtil.getValidationResponse(errorMessage); + } + } else { + log.error("validFromDateTime parameter is missing in the payload"); + return ConsentManageUtil.getValidationResponse(ErrorConstants.MISSING_VALID_FROM_DATE_TIME); + } + } else { + log.error("Missing validToDateTime parameter is missing in the payload"); + return ConsentManageUtil.getValidationResponse(ErrorConstants.MISSING_VALID_TO_DATE_TIME); + } + + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Validator class to validate the payload of a variable recurring payment initiation. + * This method performs validation on the initiation payload for a variable recurring payment. + * It checks and validates the debtor account and creditor account information if present in the payload. + * If any validation fails, it returns a JSON object with details about the validation error. + * If the initiation payload passes all validations, the returned JSON object indicates a valid initiation. + * + * @param initiation The JSON object representing the variable recurring payment initiation payload. + * @return validationResponse + */ + public static JSONObject validateVRPInitiationPayload(JSONObject initiation) { + + JSONObject validationResponse = new JSONObject(); + + //Validate DebtorAccount + if (initiation.containsKey(ConsentExtensionConstants.DEBTOR_ACC)) { + + Object debtorAccount = initiation.get(ConsentExtensionConstants.DEBTOR_ACC); + + if (!isValidJSONObject(debtorAccount)) { + String errorMessage = String.format(ErrorConstants.INVALID_PARAMETER_MESSAGE, + "debtor account", "JSONObject"); + + return ConsentManageUtil.getValidationResponse(errorMessage); + } + + JSONObject validationResult = ConsentManageUtil.validateDebtorAccount((JSONObject) debtorAccount); + + if (!(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { + log.error(validationResult.get(ConsentExtensionConstants.ERRORS)); + return validationResult; + } + + } else { + log.error(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC); + return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC); + } + + //Validate CreditorAccount + if (initiation.containsKey(ConsentExtensionConstants.CREDITOR_ACC)) { + + Object creditorAccount = initiation.get(ConsentExtensionConstants.CREDITOR_ACC); + + if (!isValidJSONObject(creditorAccount)) { + String errorMessage = String.format(ErrorConstants.INVALID_PARAMETER_MESSAGE, + "creditor account", "JSONObject"); + + return ConsentManageUtil.getValidationResponse(errorMessage); + } + + JSONObject validationResult = ConsentManageUtil.validateCreditorAccount((JSONObject) creditorAccount); + + if (!(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { + log.error(validationResult.get(ConsentExtensionConstants.ERRORS)); + return validationResult; + } + + } else { + log.error(ErrorConstants.PAYLOAD_FORMAT_ERROR_CREDITOR_ACC); + return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR); + } + + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + + /** + * Validates the presence of a specified key in a JSONObject (either the amount or the currency) + * and checks if the associated value is a non-empty string. + * + * @param parentObj The JSONObject to be validated. + * @param key The key to be checked for presence in the parentObj. + * @param expectedType The expected type of the value associated with the key. + * @return true if the specified key is present in the parentObj and the associated value is a + * non-empty string. + */ + public static JSONObject validateJsonObjectKey(JSONObject parentObj, String key, Class expectedType) { + JSONObject validationResponse = new JSONObject(); + + if (parentObj != null) { + if (parentObj.containsKey(key)) { + Object value = parentObj.get(key); + + if (expectedType.isInstance(value)) { + if (value instanceof String && !((String) value).isEmpty()) { + if ("Amount".equals(key)) { + // For the "amount" key, try parsing as Double allowing letters + if (isDouble((String) value)) { + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } else { + String errorMessage = "The value of '" + key + "' is not a valid number"; + return ConsentManageUtil.getValidationResponse(errorMessage); + } + } else { + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + } else { + String errorMessage = "The value of '" + key + "' is not a " + expectedType.getSimpleName() + + " or the value is empty"; + return ConsentManageUtil.getValidationResponse(errorMessage); + } + } else { + String errorMessage = "The value of '" + key + "' is not of type " + expectedType.getSimpleName(); + return ConsentManageUtil.getValidationResponse(errorMessage); + } + } else { + String errorMessage = "Mandatory parameter '" + key + "' is not present in payload"; + return ConsentManageUtil.getValidationResponse(errorMessage); + } + } else { + String errorMessage = "parameter passed in is null"; + return ConsentManageUtil.getValidationResponse(errorMessage); + } + } + + /** + * Validates the presence of a specified key in a JSONArray (either the amount or the currency) + * in periodiclimits and checks if the associated value is a non-empty string. + * + * @param parentArray The JSONObject to be validated. + * @param key The key to be checked for presence in the parentObj. + * @param expectedType The expected type of the value associated with the key. + * @return A JSONObject containing validation results for the entire array. + */ + public static JSONObject validateAmountCurrencyPeriodicLimits(JSONArray parentArray, String key, + Class expectedType) { + JSONObject validationResponse = new JSONObject(); + + if (parentArray != null) { + for (Object obj : parentArray) { + if (obj instanceof JSONObject) { + + JSONObject jsonObject = (JSONObject) obj; + JSONObject elementValidationResult = validateJsonObjectKey(jsonObject, key, expectedType); + + if (!(Boolean.parseBoolean(elementValidationResult.getAsString + (ConsentExtensionConstants.IS_VALID)))) { + return elementValidationResult; + } + } + } + } else { + String errorMessage = "parameter passed in is null"; + return ConsentManageUtil.getValidationResponse(errorMessage); + } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + + /** + Checks if a given string can be parsed into a double value. + @param str The string to be checked. + @return True if the string can be parsed into a double value, false otherwise. + */ + private static boolean isDouble(String str) { + try { + Double.parseDouble(str); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + /** + * Validates the consent initiation payload in the VRP request. + * + * @param request The JSONObject representing the VRP request. + * @return A JSONObject containing the validation response. + */ + public static JSONObject validateConsentInitiation(JSONObject request) { + + JSONObject validationResponse = new JSONObject(); + + JSONObject requestBody = (JSONObject) request; + JSONObject data = (JSONObject) requestBody.get(ConsentExtensionConstants.DATA); + + //Validate initiation in the VRP payload + if (data.containsKey(ConsentExtensionConstants.INITIATION)) { + + Object initiation = data.get(ConsentExtensionConstants.INITIATION); + + if (!isValidJSONObject(initiation)) { + String errorMessage = String.format(ErrorConstants.INVALID_PARAMETER_MESSAGE, + "initiation", "JSONObject"); + + return ConsentManageUtil.getValidationResponse(errorMessage); + } + + JSONObject initiationValidationResult = VRPConsentRequestValidator + .validateVRPInitiationPayload((JSONObject) initiation); + + if (!(Boolean.parseBoolean(initiationValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { + return initiationValidationResult; + } + } else { + log.error(ErrorConstants.PAYLOAD_FORMAT_ERROR_INITIATION); + return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR_INITIATION); + } + + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Validates the consent control parameters in the VRP request payload. + * + * @param request The JSONObject representing the VRP request. + * @return A JSONObject containing the validation response. + */ + public static JSONObject validateConsentControlParameters(JSONObject request) { + + JSONObject validationResponse = new JSONObject(); + + JSONObject requestBody = (JSONObject) request; + JSONObject data = (JSONObject) requestBody.get(ConsentExtensionConstants.DATA); + + //Validate the ControlParameter in the payload + if (data.containsKey(ConsentExtensionConstants.CONTROL_PARAMETERS)) { + + Object controlParameters = data.get(ConsentExtensionConstants.CONTROL_PARAMETERS); + + if (!isValidJSONObject(controlParameters)) { + String errorMessage = String.format(ErrorConstants.INVALID_PARAMETER_MESSAGE, + "control parameters", "JSONObject"); + + return ConsentManageUtil.getValidationResponse(errorMessage); + } + + JSONObject controlParameterValidationResult = + VRPConsentRequestValidator.validateControlParameters((JSONObject) + data.get(ConsentExtensionConstants.CONTROL_PARAMETERS)); + + if (!(Boolean.parseBoolean(controlParameterValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return controlParameterValidationResult; + } + } else { + log.error(ErrorConstants.PAYLOAD_FORMAT_ERROR_CONTROL_PARAMETER); + return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR_CONTROL_PARAMETER); + } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Validates the risk information in the VRP request payload. + * + * @param request The JSONObject representing the VRP request. + * @return A JSONObject containing the validation response. + */ + public static JSONObject validateConsentRisk(JSONObject request) { + + JSONObject validationResponse = new JSONObject(); + + JSONObject requestBody = (JSONObject) request; + JSONObject data = (JSONObject) requestBody.get(ConsentExtensionConstants.DATA); + + // Check Risk key is mandatory + if (!requestBody.containsKey(ConsentExtensionConstants.RISK) || + !(requestBody.get(ConsentExtensionConstants.RISK) instanceof JSONObject + || ((JSONObject) requestBody.get(ConsentExtensionConstants.DATA)).isEmpty())) { + log.error(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK); + return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK); + } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } + + /** + * Validates the periodic alignment in the VRP request payload. + * + * @param limit The JSONObject representing the VRP request. + * @return A JSONObject containing the validation response. + */ + public static JSONObject validatePeriodAlignment(JSONObject limit) { + JSONObject validationResponse = new JSONObject(); + + if (limit.containsKey(ConsentExtensionConstants.PERIOD_ALIGNMENT)) { + Object periodAlignmentObj = limit.get(ConsentExtensionConstants.PERIOD_ALIGNMENT); + + if (periodAlignmentObj instanceof String && !((String) periodAlignmentObj).isEmpty()) { + String periodAlignment = (String) periodAlignmentObj; + + if (ConsentExtensionConstants.CONSENT.equals(periodAlignment) || + ConsentExtensionConstants.CALENDAR.equals(periodAlignment)) { + + validationResponse.put("isValid", true); + validationResponse.put("periodAlignment", periodAlignment); + } else { + return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_PERIOD_ALIGNMENT); + } + } else { + return ConsentManageUtil.getValidationResponse(ErrorConstants. + PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS_ALIGNMENT); + } + } else { + return ConsentManageUtil.getValidationResponse(ErrorConstants.MISSING_PERIOD_ALIGNMENT); + } + return validationResponse; + } + + /** + * Validates the periodic type in the VRP request payload. + * + * @param limit The JSONObject representing the VRP request. + * @return A JSONObject containing the validation response. + */ + public static JSONObject validatePeriodType(JSONObject limit) { + JSONObject validationResponse = new JSONObject(); + + if (limit.containsKey(ConsentExtensionConstants.PERIOD_TYPE)) { + Object periodTypeObj = limit.get(ConsentExtensionConstants.PERIOD_TYPE); + + if (periodTypeObj instanceof String && !((String) periodTypeObj).isEmpty()) { + String periodType = (String) periodTypeObj; + + if (ConsentExtensionConstants.DAY.equals(periodType) || + ConsentExtensionConstants.WEEK.equals(periodType) || + ConsentExtensionConstants.FORTNIGHT.equals(periodType) || + ConsentExtensionConstants.MONTH.equals(periodType) || + ConsentExtensionConstants.HALF_YEAR.equals(periodType) || + ConsentExtensionConstants.YEAR.equals(periodType)) { + + validationResponse.put("isValid", true); + validationResponse.put("periodAlignment", periodType); + } else { + return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_PERIOD_TYPE); + } + } else { + return ConsentManageUtil.getValidationResponse(ErrorConstants. + PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS_PERIOD_TYPE); + } + } else { + return ConsentManageUtil.getValidationResponse(ErrorConstants.MISSING_PERIOD_TYPE); + } + return validationResponse; + } + + /** + * Checks if the given Object is a JSONObject and the JSONObject is non-empty . + * + * @param value The Object to be validated. + * @return true if the object is a non-null and non-empty JSONObject. + */ + public static boolean isValidJSONObject(Object value) { + return value instanceof JSONObject && !((JSONObject) value).isEmpty(); + } + + /** + * Checks if the given object is a valid date-time string and it is non empty. + * + * @param value The object to be checked for a valid date-time format. + * @return True if the object is a non-empty string in ISO date-time format, false otherwise. + */ + private static final DateTimeFormatter dateTimeFormat = DateTimeFormatter.ISO_DATE_TIME; + + public static JSONObject isValidDateTimeObject(Object value) { + JSONObject validationResponse = new JSONObject(); + + if (value instanceof String && !((String) value).isEmpty()) { + try { + String dateTimeString = (String) value; + dateTimeFormat.parse(dateTimeString); + } catch (DateTimeParseException e) { + return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_DATE_TIME_FORMAT); + } + } else { + return ConsentManageUtil.getValidationResponse(ErrorConstants.MISSING_DATE_TIME_FORMAT); + } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; + } +} diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/util/ConsentManageUtil.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/util/ConsentManageUtil.java index 5506c011..695afcb4 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/util/ConsentManageUtil.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/util/ConsentManageUtil.java @@ -24,6 +24,7 @@ import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentException; import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionConstants; import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionUtils; +import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentServiceUtil; import com.wso2.openbanking.accelerator.consent.extensions.common.ResponseStatus; import com.wso2.openbanking.accelerator.consent.extensions.internal.ConsentExtensionsDataHolder; import com.wso2.openbanking.accelerator.consent.extensions.manage.model.ConsentManageData; @@ -54,14 +55,16 @@ public class ConsentManageUtil { /** * Check whether valid Data object is provided. * - * @param initiation Data object in initiation payload + * @param initiationRequestbody Data object in initiation payload * @return whether the Data object is valid */ - public static JSONObject validateInitiationDataBody(JSONObject initiation) { + public static JSONObject validateInitiationDataBody(JSONObject initiationRequestbody) { JSONObject validationResponse = new JSONObject(); - if (!initiation.containsKey(ConsentExtensionConstants.DATA) || !(initiation.get(ConsentExtensionConstants.DATA) - instanceof JSONObject)) { + if (!initiationRequestbody.containsKey(ConsentExtensionConstants.DATA) || !(initiationRequestbody. + get(ConsentExtensionConstants.DATA) + instanceof JSONObject) || ((JSONObject) initiationRequestbody.get(ConsentExtensionConstants.DATA)) + .isEmpty()) { log.error(ErrorConstants.PAYLOAD_FORMAT_ERROR); return ConsentManageUtil.getValidationResponse(ErrorConstants.RESOURCE_INVALID_FORMAT, ErrorConstants.PAYLOAD_FORMAT_ERROR, ErrorConstants.PATH_REQUEST_BODY); @@ -88,6 +91,22 @@ public static JSONObject getValidationResponse(String errorCode, String errorMes return validationResponse; } + /** + * Method to construct the consent manage validation response for vrp. + * + * @param errorMessage Error Message + * + * @return + */ + public static JSONObject getValidationResponse(String errorMessage) { + JSONObject validationResponse = new JSONObject(); + + validationResponse.put(ConsentExtensionConstants.IS_VALID, false); + validationResponse.put(ConsentExtensionConstants.HTTP_CODE, ResponseStatus.BAD_REQUEST); + validationResponse.put(ConsentExtensionConstants.ERRORS, errorMessage); + return validationResponse; + } + /** * Method to validate debtor account. * @@ -320,8 +339,7 @@ public static void handleConsentManageDelete(ConsentManageData consentManageData Boolean shouldRevokeTokens; if (ConsentManageUtil.isConsentIdValid(consentId)) { try { - - ConsentResource consentResource = ConsentExtensionsDataHolder.getInstance().getConsentCoreService() + ConsentResource consentResource = ConsentServiceUtil.getConsentService() .getConsent(consentId, false); if (!consentResource.getClientID().equals(consentManageData.getClientId())) { @@ -497,7 +515,6 @@ public static boolean validateMaxInstructedAmount(String instructedAmount) { */ public static JSONObject getInitiationResponse(JSONObject response, DetailedConsentResource createdConsent, ConsentManageData consentManageData, String type) { - JSONObject dataObject = (JSONObject) response.get(ConsentExtensionConstants.DATA); dataObject.appendField(ConsentExtensionConstants.CONSENT_ID, createdConsent.getConsentID()); dataObject.appendField("CreationDateTime", convertEpochDateTime(createdConsent.getCreatedTime())); @@ -585,6 +602,9 @@ public static String constructSelfLink(String consentId, ConsentManageData conse } else if (ConsentExtensionConstants.FUNDSCONFIRMATIONS.equals(type)) { baseUrl = (String) parser.getConfiguration().get( ConsentExtensionConstants.COF_SELF_LINK); + } else if (ConsentExtensionConstants.VRP.equals(type)) { + baseUrl = (String) parser.getConfiguration().get( + ConsentExtensionConstants.VRP_SELF_LINK); } String requestPath = consentManageData.getRequestPath(); @@ -620,5 +640,19 @@ public static boolean isConsentExpirationTimeValid(String expDateVal) { return false; } } + /** + * Validate whether the date is a valid ISO 8601 format. + * @param dateValue + * @return + */ + public static boolean isValid8601(String dateValue) { + try { + OffsetDateTime.parse(dateValue); + return true; + } catch (DateTimeParseException e) { + return false; + } + } + } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/manage/vrp/VRPConsentHandlerTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/manage/vrp/VRPConsentHandlerTest.java new file mode 100644 index 00000000..48e8ab94 --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/manage/vrp/VRPConsentHandlerTest.java @@ -0,0 +1,149 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.wso2.openbanking.accelerator.consent.extensions.manage.vrp; + +import com.wso2.openbanking.accelerator.common.config.OpenBankingConfigParser; +import com.wso2.openbanking.accelerator.common.exception.ConsentManagementException; +import com.wso2.openbanking.accelerator.common.util.CarbonUtils; +import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentException; +import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentServiceUtil; +import com.wso2.openbanking.accelerator.consent.extensions.internal.ConsentExtensionsDataHolder; +import com.wso2.openbanking.accelerator.consent.extensions.manage.impl.VRPConsentRequestHandler; +import com.wso2.openbanking.accelerator.consent.extensions.manage.model.ConsentManageData; +import com.wso2.openbanking.accelerator.consent.extensions.utils.ConsentExtensionTestUtils; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; +import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.IObjectFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.ObjectFactory; +import org.testng.annotations.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.powermock.api.mockito.PowerMockito.doReturn; +import static org.powermock.api.mockito.PowerMockito.when; + +/** + * Test class for VRPConsentRequestHandler. + */ +@PowerMockIgnore({"jdk.internal.reflect.*", +"com.wso2.openbanking.accelerator.consent.extensions.common.*"}) +@PrepareForTest({OpenBankingConfigParser.class, ConsentServiceUtil.class, + ConsentExtensionsDataHolder.class}) +public class VRPConsentHandlerTest extends PowerMockTestCase { + + @InjectMocks + private final VRPConsentRequestHandler handler = new VRPConsentRequestHandler(); + + @Mock + private ConsentManageData consentManageData; + + @Mock + OpenBankingConfigParser openBankingConfigParser; + + @Mock + ConsentCoreServiceImpl consentCoreServiceImpl; + + private static Map configMap; + + + @ObjectFactory + public IObjectFactory getObjectFactory() { + + return new org.powermock.modules.testng.PowerMockObjectFactory(); + } + + @BeforeClass + public void setUp() throws ReflectiveOperationException { + MockitoAnnotations.initMocks(this); + + configMap = new HashMap<>(); + + new CarbonUtils(); + System.setProperty("some.property", "property.value"); + System.setProperty("carbon.home", "."); + ConsentExtensionTestUtils.injectEnvironmentVariable("CARBON_HOME", "."); + + consentManageData = mock(ConsentManageData.class); + } + + @BeforeMethod + public void initMethod() { + + openBankingConfigParser = mock(OpenBankingConfigParser.class); + doReturn(configMap).when(openBankingConfigParser).getConfiguration(); + + PowerMockito.mockStatic(OpenBankingConfigParser.class); + when(OpenBankingConfigParser.getInstance()).thenReturn(openBankingConfigParser); + + } + + @Test(expectedExceptions = ConsentException.class) + public void testHandleConsentManageGetWithValidConsentIdAndMatchingClientId() throws ConsentManagementException { + UUID consentIdUUID = UUID.randomUUID(); + doReturn("vrp-consent/".concat(consentIdUUID.toString())).when(consentManageData).getRequestPath(); + ConsentResource consent = mock(ConsentResource.class); + doReturn("5678").when(consent).getClientID(); + + consentCoreServiceImpl = mock(ConsentCoreServiceImpl.class); + doReturn(consent).when(consentCoreServiceImpl).getConsent(anyString(), anyBoolean()); + + PowerMockito.mockStatic(ConsentServiceUtil.class); + when(ConsentServiceUtil.getConsentService()).thenReturn(consentCoreServiceImpl); + + String expectedClientId = "matchingClientId"; + doReturn(expectedClientId).when(consentManageData).getClientId(); + + handler.handleConsentManageGet(consentManageData); + } + + + @Test(expectedExceptions = ConsentException.class) + public void testHandleConsentManageDeleteWithValidConsent() throws ConsentManagementException { + + UUID consentIdUUID = UUID.randomUUID(); + doReturn("vrp-consent/".concat(consentIdUUID.toString())).when(consentManageData).getRequestPath(); + ConsentResource consent = mock(ConsentResource.class); + doReturn("5678").when(consent).getClientID(); + + consentCoreServiceImpl = mock(ConsentCoreServiceImpl.class); + doReturn(consent).when(consentCoreServiceImpl).getConsent(anyString(), anyBoolean()); + + PowerMockito.mockStatic(ConsentServiceUtil.class); + when(ConsentServiceUtil.getConsentService()).thenReturn(consentCoreServiceImpl); + + String expectedClientId = "6788"; + doReturn(expectedClientId).when(consentManageData).getClientId(); + + handler.handleConsentManageDelete(consentManageData); + } +} diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/manage/vrp/VRPConsentRequestValidatorTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/manage/vrp/VRPConsentRequestValidatorTest.java new file mode 100644 index 00000000..12ec982a --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/manage/vrp/VRPConsentRequestValidatorTest.java @@ -0,0 +1,2009 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + *

+ * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.wso2.openbanking.accelerator.consent.extensions.manage.vrp; + +import com.wso2.openbanking.accelerator.common.config.OpenBankingConfigParser; +import com.wso2.openbanking.accelerator.common.util.CarbonUtils; +import com.wso2.openbanking.accelerator.common.util.ErrorConstants; +import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionConstants; +import com.wso2.openbanking.accelerator.consent.extensions.manage.model.ConsentManageData; +import com.wso2.openbanking.accelerator.consent.extensions.manage.validator.VRPConsentRequestValidator; +import com.wso2.openbanking.accelerator.consent.extensions.util.ConsentManageUtil; +import com.wso2.openbanking.accelerator.consent.extensions.utils.ConsentExtensionTestUtils; +import net.minidev.json.JSONArray; +import net.minidev.json.JSONObject; +import net.minidev.json.JSONValue; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.mockito.Mockito.mock; +import static org.testng.Assert.assertTrue; +import static org.testng.AssertJUnit.assertFalse; + +/** + * Test class for VRPConsentRequestValidator. + */ +@PowerMockIgnore({"jdk.internal.reflect.*"}) +@PrepareForTest({OpenBankingConfigParser.class}) +public class VRPConsentRequestValidatorTest extends PowerMockTestCase { + + @Mock + private ConsentManageData consentManageData; + + @Mock + OpenBankingConfigParser openBankingConfigParser; + + private static Map configMap; + + @BeforeClass + public void setUp() throws ReflectiveOperationException { + MockitoAnnotations.initMocks(this); + + configMap = new HashMap<>(); + //to execute util class initialization + new CarbonUtils(); + System.setProperty("some.property", "property.value"); + System.setProperty("carbon.home", "."); + ConsentExtensionTestUtils.injectEnvironmentVariable("CARBON_HOME", "."); + + consentManageData = mock(ConsentManageData.class); + } + + @BeforeMethod + public void initMethod() { + + openBankingConfigParser = mock(OpenBankingConfigParser.class); + Mockito.doReturn(configMap).when(openBankingConfigParser).getConfiguration(); + + PowerMockito.mockStatic(OpenBankingConfigParser.class); + PowerMockito.when(OpenBankingConfigParser.getInstance()).thenReturn(openBankingConfigParser); + } + + @Test + public void testVrpPayload() { + + VRPConsentRequestValidator handler = new VRPConsentRequestValidator(); + JSONObject response = handler.validateVRPPayload("payload"); + Assert.assertFalse((boolean) response.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR, response.get(ConsentExtensionConstants.ERRORS)); + + } + + @Test + public void testVrpEmptyPayload() { + + JSONObject response = VRPConsentRequestValidator.validateVRPPayload(""); + Assert.assertFalse((boolean) response.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR, response.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiation() { + + String initiationPayloads = VRPTestConstants.vrpInitiationPayloadWithoutData; + JSONObject response = VRPConsentRequestValidator.validateVRPPayload(JSONValue. + parse(initiationPayloads)); + Assert.assertFalse((boolean) response.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR, response.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpControlParameters() { + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_CONTROL_PARAMETERS; + JSONObject response = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + Assert.assertFalse((boolean) response.get(ConsentExtensionConstants.IS_VALID)); + } + + @Test + public void testVrpEmptyData() { + String initiationPayloads = VRPTestConstants.vrpInitiationPayloadWithStringData; + JSONObject response = VRPConsentRequestValidator.validateVRPPayload(JSONValue. + parse(initiationPayloads)); + Assert.assertFalse((boolean) response.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR, response.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpDataIsJsonObject() { + String initiationPayloads = VRPTestConstants.vrpInitiationPayloadWithOutJsonObject; + JSONObject response = VRPConsentRequestValidator.validateVRPPayload(JSONValue. + parse(initiationPayloads)); + Assert.assertFalse((boolean) response.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR, response.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutControlParameterKey() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_CONTROL_PARAMETERS; + JSONObject result = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + JSONObject result2 = VRPConsentRequestValidator.validateMaximumIndividualAmount((JSONObject) JSONValue. + parse(initiationPayloads)); + JSONObject result3 = VRPConsentRequestValidator. + validateMaximumIndividualAmountCurrency((JSONObject) JSONValue.parse(initiationPayloads)); + + Assert.assertTrue(true); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result3.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyWithCurrencyKeys() { + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("Currency", "USD"); + + JSONArray jsonArray = new JSONArray(); + jsonArray.add(jsonObject); + + JSONObject result = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(jsonArray, "Currency", String.class); + Assert.assertTrue((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + } + + @Test + public void testValidateAmountCurrencyWithInvalidKey() { + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("InvalidKey", "USD"); + + JSONArray jsonArray = new JSONArray(); + jsonArray.add(jsonObject); + + JSONObject result = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(jsonArray, "Currency", String.class); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutPeriodicLimitCurrency() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_PERIODIC_LIMIT_CURRENCY; + JSONObject results = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + JSONObject result = VRPConsentRequestValidator.validateCurrencyPeriodicLimit((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) results.get(ConsentExtensionConstants.IS_VALID)); + assertTrue(true); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + result.get(ConsentExtensionConstants.ERRORS)); + + } + + @Test + public void testVrpInitiationPayloadWithoutPeriodicLimitAmount() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_PERIODIC_LIMIT_AMOUNT; + JSONObject results = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + JSONObject result = VRPConsentRequestValidator.validateMaximumIndividualAmountCurrency((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) results.get(ConsentExtensionConstants.IS_VALID)); + assertTrue(true); + + } + + @Test + public void testValidateAmountCurrencyPeriodicLimitsWithInvalidValue() { + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("Currency", 123); + + JSONArray jsonArray = new JSONArray(); + jsonArray.add(jsonObject); + + JSONObject result = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(jsonArray, "Currency", String.class); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimitsWithInvalidKey() { + + JSONArray testData = new JSONArray(); + JSONObject limit = new JSONObject(); + limit.put("anotherKey", "USD"); + testData.add(limit); + + JSONObject result = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData, "currency", String.class); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'currency' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testValidationFailureForCurrency() { + + JSONObject limit = new JSONObject(); + limit.put(ConsentExtensionConstants.CURRENCY, 123); + + JSONArray periodicLimits = new JSONArray(); + periodicLimits.add(limit); + + JSONObject result = VRPConsentRequestValidator.validateAmountCurrencyPeriodicLimits(periodicLimits, + ConsentExtensionConstants.CURRENCY, String.class); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("The value of 'Currency' is not of type String", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimitsWithCurrencyKey() { + + // Test case 2: Invalid currency key (empty value) + JSONArray testData2 = new JSONArray(); + JSONObject limit2 = new JSONObject(); + limit2.put("currency", ""); + testData2.add(limit2); + + JSONObject result2 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData2, "0", String.class); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + JSONArray testData3 = new JSONArray(); + + JSONObject result3 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData3, "0", String.class); + Assert.assertTrue((boolean) result3.get(ConsentExtensionConstants.IS_VALID)); + + JSONObject result4 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(null, "currency", String.class); + Assert.assertFalse((boolean) result4.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + result4.get(ConsentExtensionConstants.ERRORS)); + + + } + + @Test + public void testVrpInitiationPayloadWithoutRisk() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_RISK; + JSONObject result = VRPConsentRequestValidator.validateConsentRisk((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testIsValidObjectDebAcc() { + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT; + JSONObject result = VRPConsentRequestValidator.validateVRPInitiationPayload((JSONObject) JSONValue. + parse(initiationPayloads)); + + // Test case 3: Non-JSONObject value + String nonJsonObject = "not a JSONObject"; + Assert.assertFalse(VRPConsentRequestValidator.isValidJSONObject(nonJsonObject), + ConsentExtensionConstants.IS_VALID); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + + // Test case 4: Null value + Object nullValue = null; + Assert.assertFalse(VRPConsentRequestValidator.isValidJSONObject(nullValue), + ConsentExtensionConstants.IS_VALID); + + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testIsValidObjectDebtorAcc() { + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT; + JSONObject result = VRPConsentRequestValidator.validateVRPInitiationPayload((JSONObject) JSONValue. + parse(initiationPayloads)); + + // Test case 3: Non-JSONObject value + String nonJsonObject = "not a JSONObject"; + Assert.assertFalse(VRPConsentRequestValidator.isValidJSONObject(nonJsonObject), + ConsentExtensionConstants.IS_VALID); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + // Test case 4: Null value + Object nullValue = null; + Assert.assertFalse(VRPConsentRequestValidator.isValidJSONObject(nullValue), + ConsentExtensionConstants.IS_VALID); + } + + @Test + public void testVrpInitiationPayloadWithoutDebtorAcc() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT; + JSONObject result = VRPConsentRequestValidator.validateVRPInitiationPayload((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutCreditAcc() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_CREDITOR_ACCOUNT; + JSONObject result = VRPConsentRequestValidator.validateConsentInitiation((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutCreditorAcc() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_CREDITOR_ACCOUNT; + JSONObject result = VRPConsentRequestValidator.validateVRPInitiationPayload((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutSchemeName() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT_SCHEME_NAME; + JSONObject result = ConsentManageUtil.validateDebtorAccount((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_DEBTOR_ACC_SCHEME_NAME, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateDebtorAccount_InvalidSchemeName() { + JSONObject debtorAccount = new JSONObject(); + debtorAccount.put(ConsentExtensionConstants.SCHEME_NAME, ""); + debtorAccount.put(ConsentExtensionConstants.IDENTIFICATION, "ValidIdentification"); + debtorAccount.put(ConsentExtensionConstants.NAME, "ValidName"); + + JSONObject result = ConsentManageUtil.validateDebtorAccount(debtorAccount); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(result.get(ConsentExtensionConstants.ERRORS), + ErrorConstants.MISSING_DEBTOR_ACC_SCHEME_NAME); + } + + @Test + public void testVrpInitiationPayloadWithoutIdentification() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT_IDENTIFICATION; + JSONObject result = VRPConsentRequestValidator.validateVRPInitiationPayload((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + } + + @Test + public void testVrpInitiationPayloadCreditorAccWithoutSchemeName() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_CREDITOR_ACCOUNT_SCHEME_NAME; + JSONObject result = VRPConsentRequestValidator.validateVRPPayload((JSONValue. + parse(initiationPayloads))); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + + } + + @Test + public void testVrpInitiationPayloadCreditorAccWithoutIdentification() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_CREDITOR_ACCOUNT_IDENTIFICATION; + JSONObject result = VRPConsentRequestValidator.validateVRPPayload(JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + + } + + @Test + public void testValidatePeriodicLimits() { + + JSONObject invalidLimit = new JSONObject(); + invalidLimit.put("someKey", "someValue"); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodicLimits(invalidLimit); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_LIMITS, + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimitsWithValidKey() { + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("Currency", "USD"); + + JSONArray jsonArray = new JSONArray(); + jsonArray.add(jsonObject); + + JSONObject result = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(jsonArray, "Currency", String.class); + Assert.assertTrue(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimitsWithInvalidKeys() { + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("InvalidKey", "USD"); + + JSONArray jsonArray = new JSONArray(); + jsonArray.add(jsonObject); + + JSONObject result = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(jsonArray, "Currency", String.class); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimitsWithEmptyArray() { + + JSONArray jsonArray = new JSONArray(); + + JSONObject result = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(jsonArray, "Currency", String.class); + + Assert.assertTrue(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(null, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimitsWithNullArray() { + + JSONObject result = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(null, "Currency", String.class); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("parameter passed in is null", + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testVrpPeriodicTypeJsonArray() { + + Object invalidObject = "Not a JSONArray"; + boolean isValidInvalidObject = VRPConsentRequestValidator.isValidJSONArray(invalidObject); + + // Test case 2: Missing period type key + JSONObject missingKeyObject = new JSONObject(); + JSONObject result2 = VRPConsentRequestValidator.validatePeriodType(missingKeyObject); + Assert.assertFalse(Boolean.parseBoolean(result2.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_TYPE, result2.get(ConsentExtensionConstants.ERRORS)); + + // Test case 3: Null period type + JSONObject nullPeriodTypeObject = new JSONObject(); + nullPeriodTypeObject.put(ConsentExtensionConstants.PERIOD_TYPE, null); + JSONObject result3 = VRPConsentRequestValidator.validatePeriodType(nullPeriodTypeObject); + Assert.assertFalse(Boolean.parseBoolean(result3.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_TYPE, result2.get(ConsentExtensionConstants.ERRORS)); + + // Test case 4: Empty period type + JSONObject emptyPeriodTypeObject = new JSONObject(); + emptyPeriodTypeObject.put(ConsentExtensionConstants.PERIOD_TYPE, ""); + JSONObject result4 = VRPConsentRequestValidator.validatePeriodType(emptyPeriodTypeObject); + Assert.assertFalse(Boolean.parseBoolean(result4.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_TYPE, result2.get(ConsentExtensionConstants.ERRORS)); + + // Test case 5: Invalid period type + JSONObject invalidPeriodTypeObject = new JSONObject(); + invalidPeriodTypeObject.put(ConsentExtensionConstants.PERIOD_TYPE, "InvalidType"); + JSONObject result5 = VRPConsentRequestValidator.validatePeriodType(invalidPeriodTypeObject); + Assert.assertFalse(Boolean.parseBoolean(result5.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_TYPE, result2.get(ConsentExtensionConstants.ERRORS)); + + Assert.assertFalse(isValidInvalidObject, ConsentExtensionConstants.IS_VALID); + } + + @Test + public void testDataContainsKey_InitiationNotPresent() { + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_INITIATION; + JSONObject result = VRPConsentRequestValidator.validateConsentInitiation((JSONObject) JSONValue. + parse(initiationPayloads)); + + boolean containsKey = result.containsKey(ConsentExtensionConstants.INITIATION); + Assert.assertFalse(containsKey, ConsentExtensionConstants.IS_VALID); + Assert.assertEquals("Missing mandatory parameter Initiation in the payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testDataContainsKey_ControlParametersNotPresent() { + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_CONTROL_PARAMETERS; + JSONObject result = VRPConsentRequestValidator.validateConsentControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean containsKey = result.containsKey(ConsentExtensionConstants.CONTROL_PARAMETERS); + Assert.assertFalse(containsKey, ConsentExtensionConstants.IS_VALID); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_CONTROL_PARAMETER, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadMaximumIndividualAmountNotJsonObject() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITH_EMPTY_MAX_INDIVIDUAL_AMOUNT; + String date = VRPTestConstants.METADATA_VRP_WITHOUT_VALID_FROM_DATE; + String date2 = VRPTestConstants.METADATA_VRP_WITHOUT_VALID_TO_DATE; + + JSONObject result = VRPConsentRequestValidator.validateMaximumIndividualAmount((JSONObject) JSONValue. + parse(initiationPayloads)); + JSONObject results = VRPConsentRequestValidator.validateMaximumIndividualAmountCurrency((JSONObject) JSONValue. + parse(initiationPayloads)); + JSONObject result2 = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(date)); + JSONObject result3 = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(date)); + + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + + + boolean isValidNonJSONObjects = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObjects, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) results.get(ConsentExtensionConstants.IS_VALID)); + + boolean obj2 = VRPConsentRequestValidator.isValidJSONObject(date); + Assert.assertFalse(obj2, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + + boolean obj3 = VRPConsentRequestValidator.isValidJSONObject(date2); + Assert.assertFalse(obj3, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result3.get(ConsentExtensionConstants.IS_VALID)); + } + + @Test + public void testVrpInitiationPayloadDebAcc() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_DEBTOR_ACC; + JSONObject result = VRPConsentRequestValidator.validateConsentInitiation((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadDebAccs() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_DEB_ACC; + JSONObject result = VRPConsentRequestValidator.validateVRPPayload(JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Parameter 'debtor account' passed in is null, empty, or not a JSONObject", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationMax() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITH_EMPTY_MAX_INDIVIDUAL_AMOUNT; + JSONObject result = VRPConsentRequestValidator.validateVRPPayload(JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR, result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadValidateDebAcc() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_DEB_ACC; + JSONObject result = VRPConsentRequestValidator.validateVRPInitiationPayload((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadCreditorAcc() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_CREDITOR_ACC; + JSONObject result = VRPConsentRequestValidator.validateVRPInitiationPayload((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadCreditorAccs() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_CREDITOR_ACC; + JSONObject result = VRPConsentRequestValidator.validateVRPPayload(JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + + Assert.assertFalse(isValidNonJSONObject); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Parameter 'creditor account' passed in is null, empty, or not a JSONObject", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadDebtorAccs() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_DEB_ACC; + JSONObject result = VRPConsentRequestValidator.validateVRPPayload(JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + + Assert.assertFalse(isValidNonJSONObject); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + + Assert.assertEquals("Parameter 'debtor account' passed in is null, empty, or not a JSONObject", + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testIsValidObject_NegativeScenarios() { + + String nonJSONObject = "Not a JSONObject"; + JSONObject validInitiationObject = new JSONObject(); + + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(nonJSONObject); + boolean isValidNonJSONObject1 = VRPConsentRequestValidator.isValidJSONObject(validInitiationObject); + Assert.assertFalse(isValidNonJSONObject1, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + } + + @Test + public void testVrpInitiationPayloadInitiationNotJsonObject() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_EMPTY_INITIATION; + JSONObject result = VRPConsentRequestValidator.validateConsentInitiation((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Parameter 'initiation' passed in is null, empty, or not a JSONObject", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadMaximumIndividualNotJsonObject() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITH_INVALID_MAX_INDIVIDUAL_AMOUNT; + JSONObject result = VRPConsentRequestValidator.validateMaximumIndividualAmount((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadCurrencyNotJsonObject() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITH_INVALID_MAX_INDIVIDUAL_AMOUNT; + JSONObject result = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadControlParametersNotJsonObject() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITH_EMPTY_CONTROL_PARAMETERS; + JSONObject result = VRPConsentRequestValidator.validateConsentControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Parameter 'control parameters' passed in is null, empty, or not a JSONObject", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutDate() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITH_INVALID_VALID_FROM_DATETIME; + JSONObject result = VRPConsentRequestValidator.validateParameterDateTime((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_VALID_TO_DATE_TIME, + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testVrpInitiationPayloadWithoutValidToDate() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_VALID_TO_DATE; + JSONObject result = VRPConsentRequestValidator.validateParameterDateTime((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_VALID_TO_DATE_TIME, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadMaximumIndividualAmountIsJsonObject() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITH_EMPTY_MAX_INDIVIDUAL_AMOUNT; + JSONObject result = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT, + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testIsValidDateTimeObjectNegativeScenarios() { + // Test case 1: Empty string + String emptyString = ""; + JSONObject resultEmptyString = VRPConsentRequestValidator.isValidDateTimeObject(emptyString); + Assert.assertFalse((boolean) resultEmptyString.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_DATE_TIME_FORMAT, + resultEmptyString.get(ConsentExtensionConstants.ERRORS)); + + // Test case 2: Null value + Object nullValue = null; + boolean resultNullValue = false; + Assert.assertFalse(resultNullValue, "Expected false for a null value"); + + // Test case 3: Non-string value + Object nonStringValue = 123; // Assuming an integer, but could be any non-string type + JSONObject resultNonStringValue = VRPConsentRequestValidator.isValidDateTimeObject(nonStringValue); + Assert.assertFalse((boolean) resultNonStringValue.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_DATE_TIME_FORMAT, + resultNonStringValue.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimits() { + + // Test case 2: Key is null + JSONArray testData2 = new JSONArray(); + JSONObject result2 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData2, null, String.class); + Assert.assertTrue((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(null, + result2.get(ConsentExtensionConstants.ERRORS)); + + // Test case 3: ParentObj is null + JSONObject result3 = VRPConsentRequestValidator.validateAmountCurrencyPeriodicLimits(null, "0", String.class); + Assert.assertTrue((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(null, + result2.get(ConsentExtensionConstants.ERRORS)); + + // Test case 4: Key is not present in parentObj + JSONArray testData4 = new JSONArray(); + JSONObject result4 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData4, "nonExistentKey", String.class); + Assert.assertTrue((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(null, + result2.get(ConsentExtensionConstants.ERRORS)); + + // Test case 5: Value is an empty String + JSONArray testData5 = new JSONArray(); + testData5.add(""); + JSONObject result5 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData5, "0", String.class); + Assert.assertTrue((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(null, + result2.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testValidateKeyAndNonEmptyStringValue() { + + // Test case 2: Key is null + JSONArray testData2 = new JSONArray(); + JSONObject result2 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData2, null, String.class); + Assert.assertTrue((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(null, + result2.get(ConsentExtensionConstants.ERRORS)); + + // Test case 3: ParentObj is null + JSONObject result3 = VRPConsentRequestValidator.validateAmountCurrencyPeriodicLimits(null, "0", String.class); + Assert.assertFalse((boolean) result3.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + result3.get(ConsentExtensionConstants.ERRORS)); + + // Test case 4: Key is not present in parentObj + JSONArray testData4 = new JSONArray(); + JSONObject result4 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData4, "nonExistentKey", String.class); + Assert.assertTrue((boolean) result4.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(null, + result4.get(ConsentExtensionConstants.ERRORS)); + + // Test case 5: Value is an empty String + JSONArray testData5 = new JSONArray(); + testData5.add(""); + JSONObject result5 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData5, "0", String.class); + Assert.assertTrue((boolean) result5.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(null, + result5.get(ConsentExtensionConstants.ERRORS)); + + + // Test case 7: Value is not a String + JSONArray testData7 = new JSONArray(); + testData7.add(123); // Assuming the value should be a String, but it's an integer in this case + JSONObject result7 = VRPConsentRequestValidator. + validateAmountCurrencyPeriodicLimits(testData7, "0", String.class); + Assert.assertTrue((boolean) result7.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(null, + result7.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testInvalidPeriodicLimitsFormat() { + + JSONObject controlParameters = new JSONObject(); + controlParameters.put(ConsentExtensionConstants.PERIODIC_LIMITS, "invalid-format"); + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodicLimits(controlParameters); + + Assert.assertFalse((boolean) validationResult.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.INVALID_PARAMETER_PERIODIC_LIMITS, + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testInvalidMaxAmountFormat() { + + JSONObject controlParameters = new JSONObject(); + controlParameters.put(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT, "invalid-format"); + JSONObject validationResult = VRPConsentRequestValidator.validateMaximumIndividualAmount(controlParameters); + + Assert.assertFalse((boolean) validationResult.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Parameter 'maximum individual amount' passed in is null, empty, or not a JSONObject", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testInvalidMaxAmountFormatPeriodicLimit() { + + JSONObject controlParameters = new JSONObject(); + controlParameters.put(ConsentExtensionConstants.PERIODIC_LIMITS, "invalid-format"); + JSONObject validationResults = VRPConsentRequestValidator. + validateControlParameters(controlParameters); + JSONObject validationResult = VRPConsentRequestValidator. + validateMaximumIndividualAmountCurrency(controlParameters); + + Assert.assertFalse((boolean) validationResults.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) validationResult.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testInvalidMaxAmountFormats() { + + JSONObject controlParameters = new JSONObject(); + controlParameters.put(ConsentExtensionConstants.PERIODIC_LIMITS, "invalid-format"); + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodicLimits(controlParameters); + + Assert.assertFalse((boolean) validationResult.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.INVALID_PARAMETER_PERIODIC_LIMITS, + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testInvalidJSONObject() { + + String invalidJSONObject = "not a JSON object"; + boolean isValid = VRPConsentRequestValidator.isValidJSONObject(invalidJSONObject); + + Assert.assertFalse(isValid); + } + + @Test + public void testEmptyJSONObject() { + JSONObject emptyJSONObject = new JSONObject(); + + boolean isValid = VRPConsentRequestValidator.isValidJSONObject(emptyJSONObject); + Assert.assertFalse(isValid); + } + + @Test + public void testInvalidPeriodicAlignment() { + // Arrange + JSONObject invalidLimit = new JSONObject(); + invalidLimit.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, "InvalidAlignment"); + + JSONObject isValid = VRPConsentRequestValidator.validatePeriodicLimits(invalidLimit); + + Assert.assertFalse((boolean) isValid.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_LIMITS, + isValid.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testInvalidDateTimeRange() { + + JSONObject controlParameters = new JSONObject(); + controlParameters.put(ConsentExtensionConstants.VALID_FROM_DATE_TIME, "2023-01-01T00:00:00Z"); + controlParameters.put(ConsentExtensionConstants.VALID_TO_DATE_TIME, "2022-01-01T00:00:00Z"); + + boolean hasValidFromDate = controlParameters.containsKey(ConsentExtensionConstants.VALID_FROM_DATE_TIME); + boolean hasValidToDate = controlParameters.containsKey(ConsentExtensionConstants.VALID_TO_DATE_TIME); + + + assertTrue(hasValidFromDate && hasValidToDate); + } + + @Test + public void testVrpInitiationPayloadWithoutControlParameterss() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_CURRENCY; + JSONObject result = VRPConsentRequestValidator.validateMaximumIndividualAmount((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Missing mandatory parameter Maximum Individual Amount", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutPeriodicType() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_PERIODIC_TYPE; + JSONObject result = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + JSONObject result2 = VRPConsentRequestValidator.validatePeriodicLimits((JSONObject) JSONValue. + parse(initiationPayloads)); + JSONObject result3 = VRPConsentRequestValidator.validatePeriodType((JSONObject) JSONValue. + parse(initiationPayloads)); + + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT, + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testValidationFailureForNullCurrencyKey() { + + JSONArray periodicLimits = new JSONArray(); + periodicLimits.add(new JSONObject()); + + JSONObject result = VRPConsentRequestValidator.validateAmountCurrencyPeriodicLimits(periodicLimits, + ConsentExtensionConstants.CURRENCY, String.class); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutPeriodicTypeCurrency() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_PERIODIC_TYPE_CURRENCY; + JSONObject result = VRPConsentRequestValidator.validateControlParameters((JSONObject) JSONValue. + parse(initiationPayloads)); + JSONObject result2 = VRPConsentRequestValidator.validatePeriodicLimits((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT, + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testValidationFailureForMissingKey() { + + JSONArray periodicLimits = new JSONArray(); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("otherKey", "someValue"); + periodicLimits.add(jsonObject); + + + JSONObject result = VRPConsentRequestValidator.validateAmountCurrencyPeriodicLimits(periodicLimits, + ConsentExtensionConstants.CURRENCY, String.class); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testValidateControlParameters() { + + JSONObject controlParameters = new JSONObject(); + + JSONObject result = VRPConsentRequestValidator.validateControlParameters(controlParameters); + + assertTrue(result.containsKey(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT, + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testValidateAmountCurrencyPeriodicLimits_Invalid() { + + JSONObject controlParameters = new JSONObject(); + JSONArray periodicLimits = new JSONArray(); + + JSONObject invalidPeriodicLimit = new JSONObject(); + periodicLimits.add(invalidPeriodicLimit); + + controlParameters.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimits); + + JSONObject result = VRPConsentRequestValidator.validateCurrencyPeriodicLimit(controlParameters); + + assertTrue(result.containsKey(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimit_WithErrors() { + + JSONObject controlParameters = new JSONObject(); + JSONArray periodicLimits = new JSONArray(); + + JSONObject invalidPeriodicLimit = new JSONObject(); + periodicLimits.add(invalidPeriodicLimit); + + controlParameters.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimits); + + JSONObject periodicLimitType = VRPConsentRequestValidator. + validateCurrencyPeriodicLimit(controlParameters); + + Assert.assertFalse((boolean) periodicLimitType.get(ConsentExtensionConstants.IS_VALID)); + + assertTrue(periodicLimitType.containsKey(ConsentExtensionConstants.ERRORS)); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + periodicLimitType.get(ConsentExtensionConstants.ERRORS)); + + } + + @Test + public void testValidateConsentRisk_ValidRequest() { + + JSONObject validRequest = new JSONObject(); + JSONObject data = new JSONObject(); + data.put("someKey", "someValue"); + + validRequest.put(ConsentExtensionConstants.DATA, data); + validRequest.put(ConsentExtensionConstants.RISK, new JSONObject()); + + JSONObject validationResponse = VRPConsentRequestValidator.validateConsentRisk(validRequest); + + assertTrue((boolean) validationResponse.get(ConsentExtensionConstants.IS_VALID)); + } + + @Test + public void testValidateConsentControlParameters_InvalidControlParameters() { + + JSONObject invalidControlParametersObject = new JSONObject(); + invalidControlParametersObject.put("invalidParam", "value"); + + JSONObject invalidDataObject = new JSONObject(); + invalidDataObject.put(ConsentExtensionConstants.CONTROL_PARAMETERS, invalidControlParametersObject); + + + JSONObject invalidRequestObject = new JSONObject(); + invalidRequestObject.put(ConsentExtensionConstants.DATA, invalidDataObject); + + JSONObject validationResult = VRPConsentRequestValidator.validateConsentControlParameters(invalidRequestObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT, + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicLimits_Valid() { + + JSONObject controlParametersObject = new JSONObject(); + JSONArray periodicLimitsArray = new JSONArray(); + + JSONObject periodicLimit1 = new JSONObject(); + periodicLimit1.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, "ALIGNMENT1"); + periodicLimit1.put(ConsentExtensionConstants.PERIOD_TYPE, "TYPE1"); + + JSONObject periodicLimit2 = new JSONObject(); + periodicLimit2.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, "ALIGNMENT2"); + periodicLimit2.put(ConsentExtensionConstants.PERIOD_TYPE, "TYPE2"); + + periodicLimitsArray.add(periodicLimit1); + periodicLimitsArray.add(periodicLimit2); + + controlParametersObject.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimitsArray); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodicLimits(controlParametersObject); + JSONObject validationResults = VRPConsentRequestValidator.validatePeriodAlignment(controlParametersObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertFalse(Boolean.parseBoolean(validationResults.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_ALIGNMENT, + validationResults.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicLimits_InvalidFormat() { + JSONObject controlParametersObject = new JSONObject(); + controlParametersObject.put(ConsentExtensionConstants.PERIODIC_LIMITS, "InvalidFormat"); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodicLimits(controlParametersObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.INVALID_PARAMETER_PERIODIC_LIMITS, + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicLimits_MissingPeriodLimits() { + + JSONObject controlParametersObject = new JSONObject(); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodicLimits(controlParametersObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_LIMITS, + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimit_Valid() { + + JSONObject controlParametersObject = new JSONObject(); + JSONArray periodicLimitsArray = new JSONArray(); + + JSONObject periodicLimit1 = new JSONObject(); + periodicLimit1.put(ConsentExtensionConstants.CURRENCY, "USD"); + + JSONObject periodicLimit2 = new JSONObject(); + periodicLimit2.put(ConsentExtensionConstants.CURRENCY, "EUR"); + + periodicLimitsArray.add(periodicLimit1); + periodicLimitsArray.add(periodicLimit2); + + controlParametersObject.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimitsArray); + + JSONObject validationResult = VRPConsentRequestValidator. + validateAmountPeriodicLimit(controlParametersObject); + + assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Mandatory parameter 'Amount' is not present in payload", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimit_MissingCurrency() { + JSONObject controlParametersObject = new JSONObject(); + JSONArray periodicLimitsArray = new JSONArray(); + + JSONObject periodicLimit1 = new JSONObject(); + periodicLimitsArray.add(periodicLimit1); + + controlParametersObject.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimitsArray); + + JSONObject validationResult = VRPConsentRequestValidator. + validateAmountPeriodicLimit(controlParametersObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Mandatory parameter 'Amount' is not present in payload", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicType_Valid() { + JSONObject periodicLimitObject = new JSONObject(); + periodicLimitObject.put(ConsentExtensionConstants.PERIOD_TYPE, ConsentExtensionConstants.MONTH); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodType(periodicLimitObject); + + Assert.assertTrue(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + } + + @Test + public void testValidatePeriodicType_InvalidType() { + JSONObject periodicLimitObject = new JSONObject(); + periodicLimitObject.put(ConsentExtensionConstants.PERIOD_TYPE, "InvalidType"); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodType(periodicLimitObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.INVALID_PERIOD_TYPE, + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicType_MissingType() { + + JSONObject periodicLimitObject = new JSONObject(); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodType(periodicLimitObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_TYPE, + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicType_EmptyType() { + + JSONObject periodicLimitObject = new JSONObject(); + periodicLimitObject.put(ConsentExtensionConstants.PERIOD_TYPE, ""); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodType(periodicLimitObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Value of period type is empty or the value passed in is not a string", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicType_NullType() { + JSONObject periodicLimitObject = new JSONObject(); + periodicLimitObject.put(ConsentExtensionConstants.PERIOD_TYPE, null); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodType(periodicLimitObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Value of period type is empty or the value passed in is not a string", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testYourMethod_ValidPeriodicType() { + + JSONObject controlParameters = new JSONObject(); + JSONArray periodicLimits = new JSONArray(); + + JSONObject periodicLimit = new JSONObject(); + periodicLimit.put(ConsentExtensionConstants.PERIOD_TYPE, ConsentExtensionConstants.MONTH); + periodicLimits.add(periodicLimit); + + controlParameters.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimits); + + JSONObject result = VRPConsentRequestValidator.validateAmountPeriodicLimit(controlParameters); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Mandatory parameter 'Amount' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + + } + + @Test + public void testYourMethod_InvalidPeriodicType() { + + JSONObject controlParameters = new JSONObject(); + JSONArray periodicLimits = new JSONArray(); + + JSONObject periodicLimit = new JSONObject(); + periodicLimit.put(ConsentExtensionConstants.PERIOD_TYPE, "InvalidType"); + periodicLimits.add(periodicLimit); + + controlParameters.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimits); + + JSONObject result = VRPConsentRequestValidator.validateCurrencyPeriodicLimit(controlParameters); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testYourMethod_MissingPeriodicType() { + + JSONObject controlParameters = new JSONObject(); + + JSONObject result = VRPConsentRequestValidator.validatePeriodicLimits(controlParameters); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_LIMITS, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutDebtorAccs() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT; + JSONObject result = VRPConsentRequestValidator.validateConsentInitiation((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateConsentRisk_InvalidRequest() { + JSONObject requestBody = new JSONObject(); + JSONObject data = new JSONObject(); + data.put("key1", "value1"); + requestBody.put("data", data); + + JSONObject validationResult = VRPConsentRequestValidator.validateVRPPayload(requestBody); + JSONObject validationResults = VRPConsentRequestValidator.validateConsentRisk(requestBody); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertFalse(Boolean.parseBoolean(validationResults.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK, + validationResults.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutDeAcc() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT; + JSONObject result = VRPConsentRequestValidator.validateVRPInitiationPayload((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadDAccWithoutSchemeName() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT_SCHEME_NAME; + JSONObject result = VRPConsentRequestValidator.validateVRPPayload((JSONValue. + parse(initiationPayloads))); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_DEBTOR_ACC_SCHEME_NAME, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadDAccWithoutIdentification() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT_IDENTIFICATION; + JSONObject result = VRPConsentRequestValidator.validateVRPPayload(JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_DEBTOR_ACC_IDENTIFICATION, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadDAcc() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_DEBTOR_ACC; + JSONObject result = VRPConsentRequestValidator.validateVRPInitiationPayload((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutDAcc() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_DEBTOR_ACCOUNT; + JSONObject result = VRPConsentRequestValidator.validateConsentInitiation((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_DEBTOR_ACC, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateMaximumIndividualAmountCurrency_InvalidAmountCurrency() { + JSONObject controlParameters = new JSONObject(); + JSONObject maximumIndividualAmount = new JSONObject(); + maximumIndividualAmount.put("InvalidKey", "USD"); + controlParameters.put(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT, maximumIndividualAmount); + + JSONObject result = VRPConsentRequestValidator.validateMaximumIndividualAmountCurrency(controlParameters); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateMaximumIndividualAmountCurrency_MissingCurrency() { + + JSONObject controlParameters = new JSONObject(); + JSONObject maximumIndividualAmount = new JSONObject(); + + controlParameters.put(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT, maximumIndividualAmount); + + JSONObject result = VRPConsentRequestValidator.validateMaximumIndividualAmountCurrency(controlParameters); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodAlignmentInvalidValue() { + + JSONObject limit = new JSONObject(); + limit.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, "InvalidValue"); + + JSONObject result = VRPConsentRequestValidator.validatePeriodAlignment(limit); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(result.get(ConsentExtensionConstants.ERRORS), + ErrorConstants.INVALID_PERIOD_ALIGNMENT); + } + + @Test + public void testValidatePeriodAlignmentMissingKey() { + + JSONObject limit = new JSONObject(); + + JSONObject result = VRPConsentRequestValidator.validatePeriodAlignment(limit); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(result.get(ConsentExtensionConstants.ERRORS), + ErrorConstants.MISSING_PERIOD_ALIGNMENT); + } + + + @Test + public void testValidatePeriodicAlignment_EmptyType() { + + JSONObject periodicLimitObject = new JSONObject(); + periodicLimitObject.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, ""); + + JSONObject result = VRPConsentRequestValidator.validatePeriodType(periodicLimitObject); + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_TYPE, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicAlignment() { + // Test case 1: Valid periodic type + JSONObject validLimitObject = new JSONObject(); + validLimitObject.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, ConsentExtensionConstants.DAY); + JSONObject result1 = VRPConsentRequestValidator.validatePeriodAlignment(validLimitObject); + Assert.assertFalse((boolean) result1.get(ConsentExtensionConstants.IS_VALID)); + + // Test case 2: Missing period type key + JSONObject missingKeyObject = new JSONObject(); + JSONObject result2 = VRPConsentRequestValidator.validatePeriodAlignment(missingKeyObject); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + + // Test case 3: Null period type + JSONObject nullPeriodTypeObject = new JSONObject(); + nullPeriodTypeObject.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, null); + JSONObject result3 = VRPConsentRequestValidator.validatePeriodAlignment(nullPeriodTypeObject); + Assert.assertFalse((boolean) result3.get(ConsentExtensionConstants.IS_VALID)); + + // Test case 4: Empty period type + JSONObject emptyPeriodTypeObject = new JSONObject(); + emptyPeriodTypeObject.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, ""); + JSONObject result4 = VRPConsentRequestValidator.validatePeriodAlignment(emptyPeriodTypeObject); + Assert.assertFalse((boolean) result4.get(ConsentExtensionConstants.IS_VALID)); + + // Test case 5: Invalid period type + JSONObject invalidPeriodTypeObject = new JSONObject(); + invalidPeriodTypeObject.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, "InvalidType"); + JSONObject result5 = VRPConsentRequestValidator.validatePeriodAlignment(invalidPeriodTypeObject); + Assert.assertFalse((boolean) result5.get(ConsentExtensionConstants.IS_VALID)); + + Assert.assertEquals("Invalid value for period alignment in PeriodicLimits", + result5.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicAlignment_Valid() { + JSONObject periodicLimitObject = new JSONObject(); + periodicLimitObject.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, ConsentExtensionConstants.CONSENT); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodAlignment(periodicLimitObject); + + Assert.assertTrue(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + + } + + @Test + public void testValidatePeriodicAlignment_InvalidType() { + JSONObject periodicLimitObject = new JSONObject(); + periodicLimitObject.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, "InvalidType"); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodAlignment(periodicLimitObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Invalid value for period alignment in PeriodicLimits", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicAlignment_MissingType() { + + JSONObject periodicLimitObject = new JSONObject(); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodAlignment(periodicLimitObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.MISSING_PERIOD_ALIGNMENT, + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicAlignments_EmptyType() { + + JSONObject periodicLimitObject = new JSONObject(); + periodicLimitObject.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, ""); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodAlignment(periodicLimitObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Value of periodic alignment is empty or the value passed in is not a string", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicAlignment_NullType() { + JSONObject periodicLimitObject = new JSONObject(); + periodicLimitObject.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, null); + + JSONObject validationResult = VRPConsentRequestValidator.validatePeriodAlignment(periodicLimitObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Value of periodic alignment is empty or the value passed in is not a string", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimits_Valid() { + + JSONObject controlParametersObject = new JSONObject(); + JSONArray periodicLimitsArray = new JSONArray(); + + JSONObject periodicLimit1 = new JSONObject(); + periodicLimit1.put(ConsentExtensionConstants.CURRENCY, "USD"); + + JSONObject periodicLimit2 = new JSONObject(); + periodicLimit2.put(ConsentExtensionConstants.CURRENCY, "EUR"); + + periodicLimitsArray.add(periodicLimit1); + periodicLimitsArray.add(periodicLimit2); + + controlParametersObject.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimitsArray); + + JSONObject validationResult = VRPConsentRequestValidator. + validateCurrencyPeriodicLimit(controlParametersObject); + + + } + + + @Test + public void testValidateAmountCurrencyPeriodicLimitS_MissingCurrency() { + JSONObject controlParametersObject = new JSONObject(); + JSONArray periodicLimitsArray = new JSONArray(); + + JSONObject periodicLimit1 = new JSONObject(); + periodicLimitsArray.add(periodicLimit1); + + controlParametersObject.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimitsArray); + + JSONObject validationResult = VRPConsentRequestValidator. + validateCurrencyPeriodicLimit(controlParametersObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + validationResult.get(ConsentExtensionConstants.ERRORS)); + + } + + @Test + public void testYourMethod_ValidPeriodicTypes() { + + JSONObject controlParameters = new JSONObject(); + JSONArray periodicLimits = new JSONArray(); + + JSONObject periodicLimit = new JSONObject(); + periodicLimit.put(ConsentExtensionConstants.PERIOD_TYPE, ConsentExtensionConstants.MONTH); + periodicLimits.add(periodicLimit); + + controlParameters.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimits); + + JSONObject result = VRPConsentRequestValidator.validateCurrencyPeriodicLimit(controlParameters); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + } + + @Test + public void testValidateAmountCurrencyPeriodicLimits_MissingCurrency() { + JSONObject controlParametersObject = new JSONObject(); + JSONArray periodicLimitsArray = new JSONArray(); + + JSONObject periodicLimit1 = new JSONObject(); + periodicLimitsArray.add(periodicLimit1); + + controlParametersObject.put(ConsentExtensionConstants.PERIODIC_LIMITS, periodicLimitsArray); + + JSONObject validationResult = VRPConsentRequestValidator. + validateCurrencyPeriodicLimit(controlParametersObject); + + Assert.assertFalse(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("Mandatory parameter 'Currency' is not present in payload", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadMaximumIndividualAmountCurrencyNotJsonObject() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITH_EMPTY_MAX_INDIVIDUAL_AMOUNT; + + JSONObject results = VRPConsentRequestValidator.validateMaximumIndividualAmountCurrency((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + + boolean isValidNonJSONObjects = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObjects, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) results.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + results.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadMaximumIndividualCurrencyNotJsonObject() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITH_INVALID_MAX_INDIVIDUAL_AMOUNT; + JSONObject result = VRPConsentRequestValidator.validateMaximumIndividualAmountCurrency((JSONObject) JSONValue. + parse(initiationPayloads)); + boolean isValidNonJSONObject = VRPConsentRequestValidator.isValidJSONObject(initiationPayloads); + Assert.assertFalse(isValidNonJSONObject, (ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testInvalidMaxAmountCurrencyFormatPeriodicLimit() { + + JSONObject controlParameters = new JSONObject(); + controlParameters.put(ConsentExtensionConstants.PERIODIC_LIMITS, "invalid-format"); + JSONObject validationResults = VRPConsentRequestValidator. + validateControlParameters(controlParameters); + JSONObject validationResult = VRPConsentRequestValidator. + validateMaximumIndividualAmountCurrency(controlParameters); + + Assert.assertFalse((boolean) validationResults.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertFalse((boolean) validationResult.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testInvalidCurrencyKey_MissingKeys() { + + JSONObject maximumIndividualAmount = new JSONObject(); + + JSONObject validationResults = VRPConsentRequestValidator. + validateJsonObjectKey(maximumIndividualAmount, ConsentExtensionConstants.CURRENCY, String.class); + + Assert.assertFalse((Boolean) validationResults.get(ConsentExtensionConstants.IS_VALID)); + + JSONObject parentObj = new JSONObject(); + JSONObject validationResult = VRPConsentRequestValidator.validateMaximumIndividualAmountCurrency(parentObj); + + Assert.assertFalse((Boolean) validationResult.get(ConsentExtensionConstants.IS_VALID)); + JSONObject isValid = VRPConsentRequestValidator.validateJsonObjectKey(parentObj, "Currency", String.class); + + Assert.assertFalse((Boolean) isValid.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + validationResult.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutControlParameterCurrency() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_CURRENCY; + JSONObject result = VRPConsentRequestValidator.validateMaximumIndividualAmountCurrency((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testVrpInitiationPayloadWithoutControlParameter() { + + String initiationPayloads = VRPTestConstants.METADATA_VRP_WITHOUT_CURRENCY; + JSONObject result = VRPConsentRequestValidator.validateMaximumIndividualAmountCurrency((JSONObject) JSONValue. + parse(initiationPayloads)); + + Assert.assertFalse((boolean) result.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + result.get(ConsentExtensionConstants.ERRORS)); + } + + + @Test + public void testWithEmptyDate() { + + String initiationPayloads = VRPTestConstants.vrpInitiationPayloadWithoutDate; + JSONObject response = VRPConsentRequestValidator.validateParameterDateTime((JSONObject) JSONValue. + parse(initiationPayloads)); + Assert.assertFalse((boolean) response.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals(ErrorConstants.MISSING_VALID_TO_DATE_TIME, response.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateConsentRisk() { + + JSONObject requestBody = new JSONObject(); + JSONObject data = new JSONObject(); + JSONObject risk = new JSONObject(); + + data.put(ConsentExtensionConstants.RISK, risk); + requestBody.put(ConsentExtensionConstants.DATA, data); + + JSONObject result = VRPConsentRequestValidator.validateConsentRisk(requestBody); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateConsentRiskInvalidFormat() { + JSONObject requestBody = new JSONObject(); + requestBody.put("invalidKey", "invalidValue"); + + JSONObject result = VRPConsentRequestValidator.validateConsentRisk(requestBody); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateConsentRiskMissingRiskKey() { + + JSONObject requestBody = new JSONObject(); + JSONObject data = new JSONObject(); + requestBody.put(ConsentExtensionConstants.DATA, data); + + JSONObject result = VRPConsentRequestValidator.validateConsentRisk(requestBody); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateConsentRiskWithDataEmpty() { + + JSONObject requestBody = new JSONObject(); + requestBody.put(ConsentExtensionConstants.DATA, new JSONObject()); + + JSONObject result = VRPConsentRequestValidator.validateConsentRisk(requestBody); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateConsentRiskWithDataNotPresent() { + + JSONObject requestBody = new JSONObject(); + + JSONObject result = VRPConsentRequestValidator.validateConsentRisk(requestBody); + + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateConsentRiskWithDataNotEmpty() { + JSONObject requestBody = new JSONObject(); + JSONObject data = new JSONObject(); + data.put("someKey", "someValue"); + requestBody.put(ConsentExtensionConstants.DATA, data); + + JSONObject result = VRPConsentRequestValidator.validateConsentRisk(requestBody); + Assert.assertFalse(Boolean.parseBoolean(result.getAsString(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals(ErrorConstants.PAYLOAD_FORMAT_ERROR_RISK, + result.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateCurrencyWithoutAmountKeyAndEmptyString() { + + // Test case 1: parentObj is null + JSONObject result1 = VRPConsentRequestValidator. + validateJsonObjectKey(null, "Currency", String.class); + Assert.assertFalse((boolean) result1.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + result1.get(ConsentExtensionConstants.ERRORS)); + + // Test case 2: Key is not present in parentObj + JSONObject result2 = VRPConsentRequestValidator. + validateJsonObjectKey(new JSONObject(), "nonExistentKey", String.class); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'nonExistentKey' is not present in payload", + result2.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyWithCurrencyKey() { + + // Test case 3: Invalid currency key (missing key) + JSONObject testData3 = new JSONObject(); + + JSONObject result3 = VRPConsentRequestValidator. + validateJsonObjectKey(testData3, "currency", String.class); + Assert.assertFalse((boolean) result3.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'currency' is not present in payload", + result3.get(ConsentExtensionConstants.ERRORS)); + + // Test case 4: Invalid currency key (null parentObj) + JSONObject result4 = VRPConsentRequestValidator. + validateJsonObjectKey(null, "currency", String.class); + Assert.assertFalse((boolean) result4.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'currency' is not present in payload", + result3.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidatePeriodicType() { + // Test case 1: Valid periodic type + JSONObject validLimitObject = new JSONObject(); + validLimitObject.put(ConsentExtensionConstants.PERIOD_TYPE, ConsentExtensionConstants.DAY); + JSONObject result1 = VRPConsentRequestValidator.validatePeriodType(validLimitObject); + Assert.assertTrue((boolean) result1.get(ConsentExtensionConstants.IS_VALID)); + + + // Test case 2: Missing period type key + JSONObject missingKeyObject = new JSONObject(); + JSONObject result2 = VRPConsentRequestValidator.validatePeriodType(missingKeyObject); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Missing required parameter Period type", + result2.get(ConsentExtensionConstants.ERRORS)); + + // Test case 3: Null period type + JSONObject nullPeriodTypeObject = new JSONObject(); + nullPeriodTypeObject.put(ConsentExtensionConstants.PERIOD_TYPE, null); + JSONObject result3 = VRPConsentRequestValidator.validatePeriodType(nullPeriodTypeObject); + Assert.assertFalse((boolean) result3.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Missing required parameter Period type", + result2.get(ConsentExtensionConstants.ERRORS)); + + + // Test case 4: Empty period type + JSONObject emptyPeriodTypeObject = new JSONObject(); + emptyPeriodTypeObject.put(ConsentExtensionConstants.PERIOD_TYPE, ""); + JSONObject result4 = VRPConsentRequestValidator.validatePeriodType(emptyPeriodTypeObject); + Assert.assertFalse((boolean) result4.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Missing required parameter Period type", + result2.get(ConsentExtensionConstants.ERRORS)); + + + // Test case 5: Invalid period type + JSONObject invalidPeriodTypeObject = new JSONObject(); + invalidPeriodTypeObject.put(ConsentExtensionConstants.PERIOD_TYPE, "InvalidType"); + JSONObject result5 = VRPConsentRequestValidator.validatePeriodType(invalidPeriodTypeObject); + Assert.assertFalse((boolean) result5.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Missing required parameter Period type", + result2.get(ConsentExtensionConstants.ERRORS)); + } + + @Test + public void testValidateAmountCurrencyWithoutCurrentKeyAndEmptyString() { + // Test case 1: parentObj is null + JSONObject result1 = VRPConsentRequestValidator. + validateJsonObjectKey(null, "Currency", String.class); + Assert.assertFalse(((boolean) result1.get(ConsentExtensionConstants.IS_VALID))); + Assert.assertEquals("parameter passed in is null", + result1.get(ConsentExtensionConstants.ERRORS)); + + // Test case 2: Key is not present in parentObj + JSONObject result2 = VRPConsentRequestValidator. + validateJsonObjectKey(new JSONObject(), "nonExistentKey", String.class); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'nonExistentKey' is not present in payload", + result2.get(ConsentExtensionConstants.ERRORS)); + + } + + @Test + public void testValidateAmountCurrencyWithoutAmountKeyAndEmptyString() { + + // Test case 1: parentObj is null + JSONObject result1 = VRPConsentRequestValidator. + validateJsonObjectKey(null, "Amount", String.class); + Assert.assertFalse((boolean) result1.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("parameter passed in is null", + result1.get(ConsentExtensionConstants.ERRORS)); + + // Test case 2: Key is not present in parentObj + JSONObject result2 = VRPConsentRequestValidator. + validateJsonObjectKey(new JSONObject(), "nonExistentKey", String.class); + Assert.assertFalse((boolean) result2.get(ConsentExtensionConstants.IS_VALID)); + Assert.assertEquals("Mandatory parameter 'nonExistentKey' is not present in payload", + result2.get(ConsentExtensionConstants.ERRORS)); + } +} diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/manage/vrp/VRPTestConstants.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/manage/vrp/VRPTestConstants.java new file mode 100644 index 00000000..f8c8535e --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/manage/vrp/VRPTestConstants.java @@ -0,0 +1,1122 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.wso2.openbanking.accelerator.consent.extensions.manage.vrp; + +/** + * Constant class for consent manage tests. + */ +public class VRPTestConstants { + + public static String vrpInitiationPayloadWithoutData = "{\n" + + " \"\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static String vrpInitiationPayloadWithoutDate = "{\n" + + " \"\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"\",\n" + + " \"ValidToDateTime\": null, // Set to null instead of an empty string\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static String vrpInitiationPayloadWithStringData = "{\n" + + " \"\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static String vrpInitiationPayloadWithOutJsonObject = "{\n" + + " \"\": { }" + + ",\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_CREDITOR_ACCOUNT = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_DEBTOR_ACCOUNT = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + ; + + + public static final String METADATA_VRP_DEBTOR_ACCOUNT_SCHEME_NAME = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_CREDITOR_ACCOUNT_SCHEME_NAME = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_DEBTOR_ACCOUNT_IDENTIFICATION = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_CREDITOR_ACCOUNT_IDENTIFICATION = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_WITHOUT_INITIATION = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_WITHOUT_CONTROL_PARAMETERS = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_WITHOUT_CURRENCY = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_WITHOUT_PERIODIC_LIMIT_CURRENCY = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_WITHOUT_PERIODIC_LIMIT_AMOUNT = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_WITHOUT_PERIODIC_TYPE = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_WITHOUT_PERIODIC_TYPE_CURRENCY = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"\": \"\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodicType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_WITHOUT_VALID_TO_DATE = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_WITH_INVALID_VALID_FROM_DATETIME = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"\": \"2023-09-12T\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_WITH_INVALID_MAX_INDIVIDUAL_AMOUNT = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": \"\",\n" + // Empty string for MaximumIndividualAmount + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_WITHOUT_RISK = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_WITH_EMPTY_CONTROL_PARAMETERS = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": \"\",\n" + // Empty string for ControlParameters + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_EMPTY_INITIATION = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": \"\",\n" + // Empty string for Initiation + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_WITH_EMPTY_MAX_INDIVIDUAL_AMOUNT = "{\n" + + " \"\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": \"\",\n" + // Empty string for MaximumIndividualAmount + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_WITHOUT_VALID_FROM_DATE = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String METADATA_VRP_WITHOUT_DEB_ACC = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": \"\",\n" + // Change DebtorAccount to an empty string + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_WITHOUT_DEBTOR_ACC = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"\": \"\",\n" + // Change DebtorAccount to an empty string + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30949330000010\",\n" + + " \"SecondaryIdentification\": \"Roll 90210\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String METADATA_VRP_WITHOUT_CREDITOR_ACC = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"true\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2023-09-12T12:43:07.956Z\",\n" + + " \"ValidToDateTime\": \"2024-05-12T12:43:07.956Z\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"9\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"1000\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Half-year\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.IBAN\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Marcus Sweepimus\"\n" + + " },\n" + + " \"CreditorAccount\": \"\", // Change CreditorAccount to an empty string\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/utils/ConsentExtensionTestUtils.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/utils/ConsentExtensionTestUtils.java new file mode 100644 index 00000000..7b3c383c --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/utils/ConsentExtensionTestUtils.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.wso2.openbanking.accelerator.consent.extensions.utils; + +import java.lang.reflect.Field; +import java.util.Map; + +/** + * comment. + */ +public class ConsentExtensionTestUtils { + + public static void injectEnvironmentVariable(String key, String value) + throws ReflectiveOperationException { + + Class processEnvironment = Class.forName("java.lang.ProcessEnvironment"); + + Field unmodifiableMapField = getAccessibleField(processEnvironment, "theUnmodifiableEnvironment"); + Object unmodifiableMap = unmodifiableMapField.get(null); + injectIntoUnmodifiableMap(key, value, unmodifiableMap); + + Field mapField = getAccessibleField(processEnvironment, "theEnvironment"); + Map map = (Map) mapField.get(null); + map.put(key, value); + } + + private static Field getAccessibleField(Class clazz, String fieldName) + throws NoSuchFieldException { + + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + return field; + } + + private static void injectIntoUnmodifiableMap(String key, String value, Object map) + throws ReflectiveOperationException { + + Class unmodifiableMap = Class.forName("java.util.Collections$UnmodifiableMap"); + Field field = getAccessibleField(unmodifiableMap, "m"); + Object obj = field.get(map); + ((Map) obj).put(key, value); + } +} diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/resources/testng.xml b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/resources/testng.xml index 405a2646..a3747971 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/resources/testng.xml +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/resources/testng.xml @@ -24,6 +24,8 @@ + +