From a4c3ece227f1c57c2053af09a19c875cea213168 Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 14 Feb 2024 15:24:38 +0530 Subject: [PATCH 01/19] Implementation of VRP Authorization flow -Retrieval flow --- .../impl/DefaultConsentRetrievalStep.java | 20 +- .../authorize/utils/ConsentRetrievalUtil.java | 563 ++++++++++++++++-- .../impl/OBDefaultAuthServletImpl.java | 5 + .../authservlet/impl/util/Utils.java | 43 ++ .../common/ConsentExtensionConstants.java | 10 + 5 files changed, 569 insertions(+), 72 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java index a22af70d..f9b139f2 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java @@ -20,7 +20,6 @@ package com.wso2.openbanking.accelerator.consent.extensions.authorize.impl; import com.wso2.openbanking.accelerator.common.exception.ConsentManagementException; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.retrieval.ConsentRetrievalHandler; import com.wso2.openbanking.accelerator.consent.extensions.authorize.model.ConsentData; import com.wso2.openbanking.accelerator.consent.extensions.authorize.model.ConsentRetrievalStep; import com.wso2.openbanking.accelerator.consent.extensions.authorize.utils.ConsentRetrievalUtil; @@ -28,13 +27,13 @@ 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.common.factory.AcceleratorConsentExtensionFactory; import com.wso2.openbanking.accelerator.consent.extensions.internal.ConsentExtensionsDataHolder; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.AuthorizationResource; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl; import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; +import net.minidev.json.parser.ParseException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -44,7 +43,6 @@ public class DefaultConsentRetrievalStep implements ConsentRetrievalStep { private static final Log log = LogFactory.getLog(DefaultConsentRetrievalStep.class); - ConsentRetrievalHandler consentRetrievalHandler; @Override public void execute(ConsentData consentData, JSONObject jsonObject) throws ConsentException { @@ -66,7 +64,7 @@ public void execute(ConsentData consentData, JSONObject jsonObject) throws Conse "executed successfully before default consent persist step"); } String requestObject = ConsentRetrievalUtil.extractRequestObject(consentData.getSpQueryParams()); - consentId = ConsentRetrievalUtil.extractConsentId(requestObject); + consentId = ConsentRetrievalUtil.extractConsentId(requestObject); consentData.setConsentId(consentId); } ConsentResource consentResource = consentCoreService.getConsent(consentId, false); @@ -101,30 +99,30 @@ public void execute(ConsentData consentData, JSONObject jsonObject) throws Conse JSONArray accountsJSON = ConsentRetrievalUtil.appendDummyAccountID(); jsonObject.appendField(ConsentExtensionConstants.ACCOUNTS, accountsJSON); - } catch (ConsentException e) { JSONObject errorObj = (JSONObject) e.getPayload(); JSONArray errorList = (JSONArray) errorObj.get("Errors"); jsonObject.put(ConsentExtensionConstants.IS_ERROR, ((JSONObject) errorList.get(0)).getAsString("Message")); return; - } catch (ConsentManagementException e) { + } catch (ParseException | ConsentManagementException e) { throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, "Exception occurred while getting consent data"); } } + /** * Method to retrieve consent related data from the initiation payload. * @param consentResource - * @return + * @return consent * @throws ConsentException */ public JSONArray getConsentDataSet(ConsentResource consentResource) - throws ConsentException { + throws ConsentException, ConsentManagementException, ParseException { - String type = consentResource.getConsentType(); - consentRetrievalHandler = AcceleratorConsentExtensionFactory.getConsentRetrievalHandler(type); - return consentRetrievalHandler.getConsentDataSet(consentResource); + JSONArray consent; + consent = ConsentRetrievalUtil.getConsentData(consentResource); + return consent; } } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java index 8bc85ee1..9827d708 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.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.ResponseStatus; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; import net.minidev.json.parser.JSONParser; @@ -78,7 +79,6 @@ public static String extractConsentId(String requestObject) { String consentId = null; try { - // validate request object and get the payload String requestObjectPayload; String[] jwtTokenValues = requestObject.split("\\."); @@ -150,85 +150,101 @@ public static boolean validateExpiryDateTime(String expiryDate) throws ConsentEx /** * Method to add debtor account details to consent data to send it to the consent page. * - * @param initiation Initiation object from the request + * @param data Initiation object from the request * @param consentDataJSON Consent information object */ - public static void populateDebtorAccount(JSONObject initiation, JSONArray consentDataJSON) { + public static void populateDebtorAccount(JSONObject data, JSONArray consentDataJSON) { + if (consentDataJSON == null) { + // Initialize consentDataJSON if it's null + consentDataJSON = new JSONArray(); + } - if (initiation.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { - JSONObject debtorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.DEBTOR_ACC); - JSONArray debtorAccountArray = new JSONArray(); + if (data.containsKey(ConsentExtensionConstants.INITIATION)) { + JSONObject initiation = (JSONObject) data.get(ConsentExtensionConstants.INITIATION); - //Adding Debtor Account Scheme Name - if (debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { - debtorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); - } + // Check if initiation contains the debtor account information + if (initiation.containsKey(ConsentExtensionConstants.DEBTOR_ACC)) { + JSONObject debtorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.DEBTOR_ACC); + JSONArray debtorAccountArray = new JSONArray(); - //Adding Debtor Account Identification - if (debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { - debtorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); - } + // Adding Debtor Account Scheme Name + if (debtorAccount.containsKey(ConsentExtensionConstants.SCHEME_NAME)) { + debtorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); + } - //Adding Debtor Account Name - if (debtorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { - debtorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.NAME)); - } + // Adding Debtor Account Identification + if (debtorAccount.containsKey(ConsentExtensionConstants.IDENTIFICATION)) { + debtorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); + } - //Adding Debtor Account Secondary Identification - if (debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { - debtorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); - } + // Adding Debtor Account Name + if (debtorAccount.containsKey(ConsentExtensionConstants.NAME)) { + debtorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.NAME)); + } + // Adding Debtor Account Secondary Identification + if (debtorAccount.containsKey(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)) { + debtorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); + } - JSONObject jsonElementDebtor = new JSONObject(); - jsonElementDebtor.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.DEBTOR_ACC_TITLE); - jsonElementDebtor.appendField(ConsentExtensionConstants.DATA_SIMPLE, debtorAccountArray); - consentDataJSON.add(jsonElementDebtor); + JSONObject jsonElementDebtor = new JSONObject(); + jsonElementDebtor.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.DEBTOR_ACC_TITLE); + jsonElementDebtor.appendField(ConsentExtensionConstants.DATA_SIMPLE, debtorAccountArray); + consentDataJSON.add(jsonElementDebtor); + } } } /** * Method to add debtor account details to consent data to send it to the consent page. * - * @param initiation + * @param data * @param consentDataJSON */ - public static void populateCreditorAccount(JSONObject initiation, JSONArray consentDataJSON) { + public static void populateCreditorAccount(JSONObject data, JSONArray consentDataJSON) { - if (initiation.get(ConsentExtensionConstants.CREDITOR_ACC) != null) { - JSONObject creditorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.CREDITOR_ACC); - JSONArray creditorAccountArray = new JSONArray(); - //Adding Debtor Account Scheme Name - if (creditorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { - creditorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + - creditorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); - } - //Adding Debtor Account Identification - if (creditorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { - creditorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + - creditorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); - } - //Adding Debtor Account Name - if (creditorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { - creditorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + - creditorAccount.getAsString(ConsentExtensionConstants.NAME)); - } - //Adding Debtor Account Secondary Identification - if (creditorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { - creditorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + - creditorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); - } + if (consentDataJSON == null) { + consentDataJSON = new JSONArray(); + } + + if (data.containsKey(ConsentExtensionConstants.INITIATION)) { + JSONObject initiation = (JSONObject) data.get(ConsentExtensionConstants.INITIATION); - JSONObject jsonElementCreditor = new JSONObject(); - jsonElementCreditor.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CREDITOR_ACC_TITLE); - jsonElementCreditor.appendField(ConsentExtensionConstants.DATA_SIMPLE, creditorAccountArray); - consentDataJSON.add(jsonElementCreditor); + if (initiation.get(ConsentExtensionConstants.CREDITOR_ACC) != null) { + JSONObject creditorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.CREDITOR_ACC); + JSONArray creditorAccountArray = new JSONArray(); + //Adding Debtor Account Scheme Name + if (creditorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { + creditorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + + creditorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); + } + //Adding Debtor Account Identification + if (creditorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { + creditorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + + creditorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); + } + //Adding Debtor Account Name + if (creditorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { + creditorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + + creditorAccount.getAsString(ConsentExtensionConstants.NAME)); + } + //Adding Debtor Account Secondary Identification + if (creditorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { + creditorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + + creditorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); + } + + JSONObject jsonElementCreditor = new JSONObject(); + jsonElementCreditor.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CREDITOR_ACC_TITLE); + jsonElementCreditor.appendField(ConsentExtensionConstants.DATA_SIMPLE, creditorAccountArray); + consentDataJSON.add(jsonElementCreditor); + } } } @@ -256,4 +272,429 @@ public static JSONArray appendDummyAccountID() { return accountsJSON; } + + /** + * Method that invokes the relevant methods to populate data for each flow. + * + * @param consentResource Consent Resource parameter containing consent related information retrieved from database + * @return ConsentDataJson array + */ + public static JSONArray getConsentData(ConsentResource consentResource) throws ParseException { + + JSONArray consentDataJSON = getConsentRetrievalDataSet(consentResource); + String receiptString = consentResource.getReceipt(); + Object receiptJSON = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(receiptString); + + JSONObject receipt = (JSONObject) receiptJSON; + JSONObject data = (JSONObject) receipt.get(ConsentExtensionConstants.DATA); + JSONObject controlParameters = (JSONObject) data.get(ConsentExtensionConstants.CONTROL_PARAMETERS); + + + String type = consentResource.getConsentType(); + switch (type) { + case ConsentExtensionConstants.ACCOUNTS: + consentDataJSON = populateAccountData(data, consentDataJSON); + break; + case ConsentExtensionConstants.PAYMENTS: + consentDataJSON = populateSinglePaymentData(data, consentDataJSON); + // Adding Debtor Account + ConsentRetrievalUtil.populateDebtorAccount(data, consentDataJSON); + // Adding Creditor Account + ConsentRetrievalUtil.populateCreditorAccount(data, consentDataJSON); + break; + case ConsentExtensionConstants.FUNDSCONFIRMATIONS: + consentDataJSON = populateCofData(data, consentDataJSON); + break; + case ConsentExtensionConstants.VRP: + consentDataJSON = populateVRData(controlParameters, consentDataJSON); + break; + default: + break; + } + return consentDataJSON; + + } + + /** + * Method defined to retrieve the Payment consent related data in the authorization flow to send them to the + * consent page to get PSU consent. + * + * @param consentResource Consent Resource parameter containing consent related information retrieved from database + */ + public static JSONArray getConsentRetrievalDataSet(ConsentResource consentResource) { + + try { + String receiptString = consentResource.getReceipt(); + Object receiptJSON = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(receiptString); + + //Checking whether the request body is in JSON format + if (!(receiptJSON instanceof JSONObject)) { + log.error(ErrorConstants.NOT_JSON_OBJECT_ERROR); + throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.NOT_JSON_OBJECT_ERROR); + } + + //Checking whether the consent status is valid + if (!consentResource.getCurrentStatus().equals(ConsentExtensionConstants.AWAITING_AUTH_STATUS)) { + log.error(ErrorConstants.STATE_INVALID_ERROR); + //Currently throwing error as 400 response. Developer also have the option of appending a field IS_ERROR + // to the jsonObject and showing it to the user in the webapp. If so, the IS_ERROR have to be checked in + // any later steps. + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.STATE_INVALID_ERROR); + } + } catch (ParseException e) { + throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.CONSENT_RETRIEVAL_ERROR); + } + return null; + + } + + /** + * Populate Domestic and international Payment Details. + * + * @param data data request from the request + * @param consentDataJSON Consent information + */ + private static JSONArray populateSinglePaymentData(JSONObject data, JSONArray consentDataJSON) { + + if (consentDataJSON == null) { + // Initialize consentDataJSON if it's null + consentDataJSON = new JSONArray(); + } + + JSONArray paymentTypeArray = new JSONArray(); + JSONObject jsonElementPaymentType = new JSONObject(); + + if (data.containsKey(ConsentExtensionConstants.INITIATION)) { + JSONObject initiation = (JSONObject) data.get(ConsentExtensionConstants.INITIATION); + + if (initiation.containsKey(ConsentExtensionConstants.CURRENCY_OF_TRANSFER)) { + //For International Payments + //Adding Payment Type + paymentTypeArray.add(ConsentExtensionConstants.INTERNATIONAL_PAYMENTS); + + jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.PAYMENT_TYPE_TITLE); + jsonElementPaymentType.appendField(ConsentExtensionConstants.DATA_SIMPLE, paymentTypeArray); + consentDataJSON.add(jsonElementPaymentType); + + //Adding Currency Of Transfer + JSONArray currencyTransferArray = new JSONArray(); + currencyTransferArray.add(initiation.getAsString(ConsentExtensionConstants.CURRENCY_OF_TRANSFER)); + + JSONObject jsonElementCurTransfer = new JSONObject(); + jsonElementCurTransfer.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CURRENCY_OF_TRANSFER_TITLE); + jsonElementCurTransfer.appendField(ConsentExtensionConstants.DATA_SIMPLE, currencyTransferArray); + consentDataJSON.add(jsonElementCurTransfer); + } else { + //Adding Payment Type + paymentTypeArray.add(ConsentExtensionConstants.DOMESTIC_PAYMENTS); + + jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.PAYMENT_TYPE_TITLE); + jsonElementPaymentType.appendField(ConsentExtensionConstants.DATA_SIMPLE, paymentTypeArray); + consentDataJSON.add(jsonElementPaymentType); + } + + //Adding InstructionIdentification + JSONArray identificationArray = new JSONArray(); + identificationArray.add(initiation.getAsString(ConsentExtensionConstants.INSTRUCTION_IDENTIFICATION)); + + JSONObject jsonElementIdentification = new JSONObject(); + jsonElementIdentification.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.INSTRUCTION_IDENTIFICATION_TITLE); + jsonElementIdentification.appendField(ConsentExtensionConstants.DATA_SIMPLE, identificationArray); + consentDataJSON.add(jsonElementIdentification); + + //Adding EndToEndIdentification + JSONArray endToEndIdentificationArray = new JSONArray(); + endToEndIdentificationArray + .add(initiation.getAsString(ConsentExtensionConstants.END_TO_END_IDENTIFICATION)); + + JSONObject jsonElementEndToEndIdentification = new JSONObject(); + jsonElementEndToEndIdentification.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.END_TO_END_IDENTIFICATION_TITLE); + jsonElementEndToEndIdentification.appendField(ConsentExtensionConstants.DATA_SIMPLE, + endToEndIdentificationArray); + consentDataJSON.add(jsonElementEndToEndIdentification); + + //Adding InstructedAmount + JSONObject instructedAmount = (JSONObject) initiation.get(ConsentExtensionConstants.INSTRUCTED_AMOUNT); + JSONArray instructedAmountArray = new JSONArray(); + + + if (instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT_TITLE) != null) { + instructedAmountArray.add(ConsentExtensionConstants.AMOUNT_TITLE + " : " + + instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT)); + } + + if (instructedAmount.getAsString(ConsentExtensionConstants.CURRENCY) != null) { + instructedAmountArray.add(ConsentExtensionConstants.CURRENCY_TITLE + " : " + + instructedAmount.getAsString(ConsentExtensionConstants.CURRENCY)); + } + + JSONObject jsonElementInstructedAmount = new JSONObject(); + jsonElementInstructedAmount.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.INSTRUCTED_AMOUNT_TITLE); + jsonElementInstructedAmount.appendField(ConsentExtensionConstants.DATA_SIMPLE, instructedAmountArray); + consentDataJSON.add(jsonElementInstructedAmount); + } + + return consentDataJSON; + + } + + /** + * Populate account Details. + * + * @param data data request from the request + * @param consentDataJSON Consent information + */ + private static JSONArray populateAccountData(JSONObject data, JSONArray consentDataJSON) { + + if (consentDataJSON == null) { + consentDataJSON = new JSONArray(); + } + + //Adding Permissions + JSONArray permissions = (JSONArray) data.get(ConsentExtensionConstants.PERMISSIONS); + if (permissions != null) { + JSONObject jsonElementPermissions = new JSONObject(); + jsonElementPermissions.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.PERMISSIONS); + jsonElementPermissions.appendField(ConsentExtensionConstants.DATA_SIMPLE, permissions); + consentDataJSON.add(jsonElementPermissions); + } + + //Adding Expiration Date Time + String expirationDate = data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE); + if (expirationDate != null) { + if (!ConsentRetrievalUtil.validateExpiryDateTime(expirationDate)) { + log.error(ErrorConstants.CONSENT_EXPIRED); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONSENT_EXPIRED); + } + JSONArray expiryArray = new JSONArray(); + expiryArray.add(expirationDate); + + JSONObject jsonElementExpiry = new JSONObject(); + jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.EXPIRATION_DATE_TITLE); + jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); + consentDataJSON.add(jsonElementExpiry); + } + + //Adding Transaction From Date Time + String fromDateTime = data.getAsString(ConsentExtensionConstants.TRANSACTION_FROM_DATE); + if (fromDateTime != null) { + JSONArray fromDateTimeArray = new JSONArray(); + fromDateTimeArray.add(fromDateTime); + + JSONObject jsonElementFromDateTime = new JSONObject(); + jsonElementFromDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.TRANSACTION_FROM_DATE_TITLE); + jsonElementFromDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, fromDateTimeArray); + consentDataJSON.add(jsonElementFromDateTime); + } + + //Adding Transaction To Date Time + String toDateTime = data.getAsString(ConsentExtensionConstants.TRANSACTION_TO_DATE); + if (toDateTime != null) { + JSONArray toDateTimeArray = new JSONArray(); + toDateTimeArray.add(toDateTime); + + JSONObject jsonElementToDateTime = new JSONObject(); + jsonElementToDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.TRANSACTION_TO_DATE_TITLE); + jsonElementToDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, toDateTimeArray); + consentDataJSON.add(jsonElementToDateTime); + } + + return consentDataJSON; + + } + + /** + * Populate funds confirmation Details. + * + * @param data data request from the request + * @param consentDataJSON Consent information + */ + private static JSONArray populateCofData(JSONObject data, JSONArray consentDataJSON) { + + if (consentDataJSON == null) { + consentDataJSON = new JSONArray(); + } + + //Adding Expiration Date Time + if (data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE) != null) { + + if (!ConsentRetrievalUtil + .validateExpiryDateTime(data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE))) { + log.error(ErrorConstants.CONSENT_EXPIRED); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONSENT_EXPIRED); + } + + String expiry = data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE); + JSONArray expiryArray = new JSONArray(); + expiryArray.add(expiry); + + JSONObject jsonElementExpiry = new JSONObject(); + jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.EXPIRATION_DATE_TITLE); + jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); + consentDataJSON.add(jsonElementExpiry); + } else { + JSONArray expiryArray = new JSONArray(); + expiryArray.add(ConsentExtensionConstants.OPEN_ENDED_AUTHORIZATION); + + JSONObject jsonElementExpiry = new JSONObject(); + jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.EXPIRATION_DATE_TITLE); + jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); + consentDataJSON.add(jsonElementExpiry); + } + + //Adding Debtor Account + if (data.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { + JSONObject debtorAccount = (JSONObject) data.get(ConsentExtensionConstants.DEBTOR_ACC); + JSONArray debtorAccountArray = new JSONArray(); + //Adding Debtor Account Scheme Name + if (debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { + debtorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); + } + //Adding Debtor Account Identification + if (debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { + debtorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); + } + //Adding Debtor Account Name + if (debtorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { + debtorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.NAME)); + } + //Adding Debtor Account Secondary Identification + if (debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { + debtorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); + } + + JSONObject jsonElementDebtor = new JSONObject(); + jsonElementDebtor.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.DEBTOR_ACC_TITLE); + jsonElementDebtor.appendField(ConsentExtensionConstants.DATA_SIMPLE, debtorAccountArray); + consentDataJSON.add(jsonElementDebtor); + } + + return consentDataJSON; + + } + + + /** + * Populate VRP Details. + * + * @param controlParameters Control Parameters from the request + * @param consentDataJSON Consent information object + */ + private static JSONArray populateVRData(JSONObject controlParameters, JSONArray consentDataJSON) { + + if (consentDataJSON == null) { + consentDataJSON = new JSONArray(); + } + + JSONArray paymentTypeArray = new JSONArray(); + JSONObject jsonElementPaymentType = new JSONObject(); + + //Adding Payment Type + paymentTypeArray.add(ConsentExtensionConstants.DOMESTIC_VRP); + jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.PAYMENT_TYPE_TITLE); + jsonElementPaymentType.appendField(ConsentExtensionConstants.DATA_SIMPLE, paymentTypeArray); + consentDataJSON.add(jsonElementPaymentType); + + // Constructing jsonElementValidToDataTime + JSONObject jsonElementValidToDateTime = new JSONObject(); + jsonElementValidToDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_VALID_TO_DATE_TITLE); + JSONArray dateControlParameterArray = new JSONArray(); + dateControlParameterArray.add((controlParameters). + get(ConsentExtensionConstants.VALID_TO_DATE_TIME)); + jsonElementValidToDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, dateControlParameterArray); + + // Constructing jsonElementValidFromDataTime + JSONObject jsonElementValidFromDateTime = new JSONObject(); + jsonElementValidFromDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_VALID_FROM_DATE_TITLE); + JSONArray dateTimeControlParameterArray = new JSONArray(); + dateTimeControlParameterArray.add((controlParameters). + get(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); + jsonElementValidFromDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, dateTimeControlParameterArray); + + // Constructing jsonElementControlParameter + JSONObject jsonElementControlParameter = new JSONObject(); + jsonElementControlParameter.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_MAX_INDIVIDUAL_AMOUNT_TITLE); + JSONArray controlParameterArray = new JSONArray(); + + String formattedAmount = String.format("%s %s", + ((JSONObject) controlParameters.get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)) + .get(ConsentExtensionConstants.CURRENCY), + ((JSONObject) controlParameters.get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)) + .get(ConsentExtensionConstants.AMOUNT)); + controlParameterArray.add(formattedAmount); + jsonElementControlParameter.appendField(ConsentExtensionConstants.DATA_SIMPLE, controlParameterArray); + + // Constructing jsonElementPeriodAlignment + JSONObject jsonElementPeriodAlignment = new JSONObject(); + jsonElementPeriodAlignment.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_ALIGNMENT_TITLE); + String periodAlignment = (String) ((JSONObject) ((JSONArray) controlParameters + .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)) + .get(ConsentExtensionConstants.PERIOD_ALIGNMENT); + JSONArray periodAlignmentArray = new JSONArray(); + periodAlignmentArray.add(periodAlignment); + jsonElementPeriodAlignment.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodAlignmentArray); + + // Constructing jsonElementPeriodType + JSONObject jsonElementPeriodType = new JSONObject(); + jsonElementPeriodType.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_TYPE_TITLE); + String periodType = (String) ((JSONObject) ((JSONArray) controlParameters + .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)).get(ConsentExtensionConstants.PERIOD_TYPE); + JSONArray periodTypeArray = new JSONArray(); + periodTypeArray.add(periodType); + jsonElementPeriodType.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodTypeArray); + + // Constructing jsonElementPeriodicLimits + JSONObject jsonElementPeriodicLimits = new JSONObject(); + jsonElementPeriodicLimits.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_AMOUNT_TITLE + periodType); + JSONArray periodicLimitsArray = new JSONArray(); + + JSONObject periodicLimitsObject = (JSONObject) ((JSONArray) controlParameters + .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0); + + String currency = (String) periodicLimitsObject.get(ConsentExtensionConstants.CURRENCY); + String amount = (String) periodicLimitsObject.get(ConsentExtensionConstants.AMOUNT); + + // Concatenate currency and amount in the desired order + String formattedPeriodicAmount = String.format("%s %s", currency, amount); + + periodicLimitsArray.add(formattedPeriodicAmount); + + jsonElementPeriodicLimits.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodicLimitsArray); + + // Adding constructed JSON objects to consentDataJSON + consentDataJSON.add(jsonElementControlParameter); + consentDataJSON.add(jsonElementPeriodAlignment); + consentDataJSON.add(jsonElementPeriodicLimits); + consentDataJSON.add(jsonElementPeriodType); + consentDataJSON.add(jsonElementValidToDateTime); + consentDataJSON.add(jsonElementValidFromDateTime); + + return consentDataJSON; + + } } + diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java index e7679374..77aea95b 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java @@ -53,6 +53,8 @@ public Map updateRequestAttribute(HttpServletRequest request, JS return Utils.populatePaymentsData(request, dataSet); case ConsentExtensionConstants.FUNDSCONFIRMATIONS: return Utils.populateCoFData(request, dataSet); + case ConsentExtensionConstants.VRP: + return Utils.populateVRPDataRetrieval(request, dataSet); default: return new HashMap<>(); } @@ -91,8 +93,11 @@ public String getJSPPath() { if (jspPath.equalsIgnoreCase(ConsentExtensionConstants.ACCOUNTS)) { return "/ob_default.jsp"; + } else if (jspPath.equalsIgnoreCase(ConsentExtensionConstants.VRP)) { + return "/ob_default.jsp"; } else { return "/default_displayconsent.jsp"; } + } } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java index 2e025dfb..2ee28989 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java @@ -250,4 +250,47 @@ private static List> addAccList (JSONObject dataSet) { return accountData; } + /** + * Method to populate vrp data to be sent to consent page. + * + * @param request + * @param dataSet + * @return + */ + public static Map populateVRPDataRetrieval(HttpServletRequest request, JSONObject dataSet) { + + String selectedAccount = null; + Map returnMaps = new HashMap<>(); + + //Sets "data_requested" that contains the human-readable scope-requested information + JSONArray dataRequestedJsonArray = dataSet.getJSONArray(ConsentExtensionConstants.CONSENT_DATA); + Map> dataRequested = new LinkedHashMap<>(); + + for (int requestedDataIndex = 0; requestedDataIndex < dataRequestedJsonArray.length(); requestedDataIndex++) { + JSONObject dataObj = dataRequestedJsonArray.getJSONObject(requestedDataIndex); + String title = dataObj.getString(ConsentExtensionConstants.TITLE); + JSONArray dataArray = dataObj.getJSONArray(ConsentExtensionConstants.DATA_SIMPLE); + + ArrayList listData = new ArrayList<>(); + for (int dataIndex = 0; dataIndex < dataArray.length(); dataIndex++) { + listData.add(dataArray.getString(dataIndex)); + } + dataRequested.put(title, listData); + } + returnMaps.put(ConsentExtensionConstants.DATA_REQUESTED, dataRequested); + + //Assigning value of the "Debtor Account" key in the map to the variable "selectedAccount". + if (dataRequested.containsKey("Debtor Account")) { + selectedAccount = getDebtorAccFromConsentData(dataRequestedJsonArray); + } else { + // add accounts list + request.setAttribute(ConsentExtensionConstants.ACCOUNT_DATA, addAccList(dataSet)); + } + + request.setAttribute(ConsentExtensionConstants.SELECTED_ACCOUNT, selectedAccount); + request.setAttribute(ConsentExtensionConstants.CONSENT_TYPE, ConsentExtensionConstants.VRP); + + return returnMaps; + + } } 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 aaa9aa60..3ed34af1 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 @@ -198,4 +198,14 @@ public class ConsentExtensionConstants { 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"; + + // vrp authorization flow constants + public static final String DOMESTIC_VRP = "Domestic VRP"; + public static final String CONTROL_PARAMETER_MAX_INDIVIDUAL_AMOUNT_TITLE = "Maximum amount per payment"; + public static final String CONTROL_PARAMETER_VALID_TO_DATE_TITLE = "Valid to date and time"; + public static final String CONTROL_PARAMETER_PERIOD_ALIGNMENT_TITLE = "Period Alignment"; + public static final String CONTROL_PARAMETER_PERIOD_TYPE_TITLE = "Period Type"; + public static final Object CONTROL_PARAMETER_AMOUNT_TITLE = "Maximum payment amount per "; + public static final String VRP_ACCOUNT = "vrpAccount"; + public static final Object CONTROL_PARAMETER_VALID_FROM_DATE_TITLE = "Valid from date and time"; } From 1d39a87536cb86696a2111dd08414bbf7159e5ad Mon Sep 17 00:00:00 2001 From: kalpana Date: Fri, 16 Feb 2024 14:19:50 +0530 Subject: [PATCH 02/19] Unit test for authorize flow --- .../src/test/resources/testng.xml | 1 + 1 file changed, 1 insertion(+) 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 a3747971..206a067a 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 @@ -26,6 +26,7 @@ + From a6d2b0b08ae6c5d316e4d4456d92a2f08d8c3226 Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 21 Feb 2024 14:23:17 +0530 Subject: [PATCH 03/19] Resolving the comments for the last update of retrieval flow --- .../common/util/ErrorConstants.java | 2 + .../impl/DefaultConsentRetrievalStep.java | 5 +- .../authorize/utils/ConsentRetrievalUtil.java | 490 +++++++++--------- .../impl/OBDefaultAuthServletImpl.java | 5 +- 4 files changed, 241 insertions(+), 261 deletions(-) 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 628dc98c..44d685af 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 @@ -290,5 +290,7 @@ public class ErrorConstants { public static final String PATH_PERIOD_TYPE = "Data.ControlParameters.PeriodicLimits.PeriodType"; public static final String PATH_PERIOD_ALIGNMENT = "Data.ControlParameters.PeriodicLimits.PeriodAlignment"; + public static final String CONTROL_PARAMETERS_MISSING_ERROR = "Missing mandatory parameter the ControlParameters"; + public static final String DATA_OBJECT_MISSING_ERROR = "Missing mandatory parameter the Data "; } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java index f9b139f2..47950ff6 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java @@ -16,7 +16,6 @@ * under the License. */ - package com.wso2.openbanking.accelerator.consent.extensions.authorize.impl; import com.wso2.openbanking.accelerator.common.exception.ConsentManagementException; @@ -120,9 +119,7 @@ public void execute(ConsentData consentData, JSONObject jsonObject) throws Conse public JSONArray getConsentDataSet(ConsentResource consentResource) throws ConsentException, ConsentManagementException, ParseException { - JSONArray consent; - consent = ConsentRetrievalUtil.getConsentData(consentResource); - return consent; + return ConsentRetrievalUtil.getConsentData(consentResource); } } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java index 9827d708..93be7eb4 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java @@ -1,12 +1,12 @@ /** - * 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 * in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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 @@ -16,7 +16,6 @@ * under the License. */ - package com.wso2.openbanking.accelerator.consent.extensions.authorize.utils; @@ -146,51 +145,43 @@ public static boolean validateExpiryDateTime(String expiryDate) throws ConsentEx } } - /** * Method to add debtor account details to consent data to send it to the consent page. * - * @param data Initiation object from the request + * @param initiation Initiation object from the request * @param consentDataJSON Consent information object */ - public static void populateDebtorAccount(JSONObject data, JSONArray consentDataJSON) { - if (consentDataJSON == null) { - // Initialize consentDataJSON if it's null - consentDataJSON = new JSONArray(); - } + public static void populateDebtorAccount(JSONObject initiation, JSONArray consentDataJSON) { - if (data.containsKey(ConsentExtensionConstants.INITIATION)) { - JSONObject initiation = (JSONObject) data.get(ConsentExtensionConstants.INITIATION); - - // Check if initiation contains the debtor account information - if (initiation.containsKey(ConsentExtensionConstants.DEBTOR_ACC)) { + if (initiation.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { JSONObject debtorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.DEBTOR_ACC); JSONArray debtorAccountArray = new JSONArray(); - // Adding Debtor Account Scheme Name - if (debtorAccount.containsKey(ConsentExtensionConstants.SCHEME_NAME)) { + //Adding Debtor Account Scheme Name + if (debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { debtorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); } - // Adding Debtor Account Identification - if (debtorAccount.containsKey(ConsentExtensionConstants.IDENTIFICATION)) { + //Adding Debtor Account Identification + if (debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { debtorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); } - // Adding Debtor Account Name - if (debtorAccount.containsKey(ConsentExtensionConstants.NAME)) { + //Adding Debtor Account Name + if (debtorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { debtorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + debtorAccount.getAsString(ConsentExtensionConstants.NAME)); } - // Adding Debtor Account Secondary Identification - if (debtorAccount.containsKey(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)) { + //Adding Debtor Account Secondary Identification + if (debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { debtorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); } + JSONObject jsonElementDebtor = new JSONObject(); jsonElementDebtor.appendField(ConsentExtensionConstants.TITLE, ConsentExtensionConstants.DEBTOR_ACC_TITLE); @@ -198,56 +189,50 @@ public static void populateDebtorAccount(JSONObject data, JSONArray consentDataJ consentDataJSON.add(jsonElementDebtor); } } - } + /** * Method to add debtor account details to consent data to send it to the consent page. * - * @param data + * @param initiation * @param consentDataJSON */ - public static void populateCreditorAccount(JSONObject data, JSONArray consentDataJSON) { - - if (consentDataJSON == null) { - consentDataJSON = new JSONArray(); - } - - if (data.containsKey(ConsentExtensionConstants.INITIATION)) { - JSONObject initiation = (JSONObject) data.get(ConsentExtensionConstants.INITIATION); - - if (initiation.get(ConsentExtensionConstants.CREDITOR_ACC) != null) { - JSONObject creditorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.CREDITOR_ACC); - JSONArray creditorAccountArray = new JSONArray(); - //Adding Debtor Account Scheme Name - if (creditorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { - creditorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + - creditorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); - } - //Adding Debtor Account Identification - if (creditorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { - creditorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + - creditorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); - } - //Adding Debtor Account Name - if (creditorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { - creditorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + - creditorAccount.getAsString(ConsentExtensionConstants.NAME)); - } - //Adding Debtor Account Secondary Identification - if (creditorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { - creditorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + - creditorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); - } + public static void populateCreditorAccount(JSONObject initiation, JSONArray consentDataJSON) { - JSONObject jsonElementCreditor = new JSONObject(); - jsonElementCreditor.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CREDITOR_ACC_TITLE); - jsonElementCreditor.appendField(ConsentExtensionConstants.DATA_SIMPLE, creditorAccountArray); - consentDataJSON.add(jsonElementCreditor); + if (initiation.get(ConsentExtensionConstants.CREDITOR_ACC) != null) { + JSONObject creditorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.CREDITOR_ACC); + JSONArray creditorAccountArray = new JSONArray(); + //Adding Debtor Account Scheme Name + if (creditorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { + creditorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + + creditorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); + } + //Adding Debtor Account Identification + if (creditorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { + creditorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + + creditorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); + } + //Adding Debtor Account Name + if (creditorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { + creditorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + + creditorAccount.getAsString(ConsentExtensionConstants.NAME)); } + //Adding Debtor Account Secondary Identification + if (creditorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { + creditorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + + creditorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); + } + + JSONObject jsonElementCreditor = new JSONObject(); + jsonElementCreditor.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CREDITOR_ACC_TITLE); + jsonElementCreditor.appendField(ConsentExtensionConstants.DATA_SIMPLE, creditorAccountArray); + consentDataJSON.add(jsonElementCreditor); } } + + /** * Method to append Dummy data for Account ID. Ideally should be separate step calling accounts service * @@ -279,82 +264,82 @@ public static JSONArray appendDummyAccountID() { * @param consentResource Consent Resource parameter containing consent related information retrieved from database * @return ConsentDataJson array */ - public static JSONArray getConsentData(ConsentResource consentResource) throws ParseException { - - JSONArray consentDataJSON = getConsentRetrievalDataSet(consentResource); - String receiptString = consentResource.getReceipt(); - Object receiptJSON = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(receiptString); - - JSONObject receipt = (JSONObject) receiptJSON; - JSONObject data = (JSONObject) receipt.get(ConsentExtensionConstants.DATA); - JSONObject controlParameters = (JSONObject) data.get(ConsentExtensionConstants.CONTROL_PARAMETERS); - - - String type = consentResource.getConsentType(); - switch (type) { - case ConsentExtensionConstants.ACCOUNTS: - consentDataJSON = populateAccountData(data, consentDataJSON); - break; - case ConsentExtensionConstants.PAYMENTS: - consentDataJSON = populateSinglePaymentData(data, consentDataJSON); - // Adding Debtor Account - ConsentRetrievalUtil.populateDebtorAccount(data, consentDataJSON); - // Adding Creditor Account - ConsentRetrievalUtil.populateCreditorAccount(data, consentDataJSON); - break; - case ConsentExtensionConstants.FUNDSCONFIRMATIONS: - consentDataJSON = populateCofData(data, consentDataJSON); - break; - case ConsentExtensionConstants.VRP: - consentDataJSON = populateVRData(controlParameters, consentDataJSON); - break; - default: - break; - } - return consentDataJSON; - - } - - /** - * Method defined to retrieve the Payment consent related data in the authorization flow to send them to the - * consent page to get PSU consent. - * - * @param consentResource Consent Resource parameter containing consent related information retrieved from database - */ - public static JSONArray getConsentRetrievalDataSet(ConsentResource consentResource) { + public static JSONArray getConsentData(ConsentResource consentResource) { + JSONArray consentDataJSON; try { + consentDataJSON = new JSONArray(); String receiptString = consentResource.getReceipt(); Object receiptJSON = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(receiptString); - //Checking whether the request body is in JSON format + // Checking whether the request body is in JSON format if (!(receiptJSON instanceof JSONObject)) { log.error(ErrorConstants.NOT_JSON_OBJECT_ERROR); throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.NOT_JSON_OBJECT_ERROR); } - //Checking whether the consent status is valid + // Checking whether the consent status is valid if (!consentResource.getCurrentStatus().equals(ConsentExtensionConstants.AWAITING_AUTH_STATUS)) { log.error(ErrorConstants.STATE_INVALID_ERROR); - //Currently throwing error as 400 response. Developer also have the option of appending a field IS_ERROR - // to the jsonObject and showing it to the user in the webapp. If so, the IS_ERROR have to be checked in - // any later steps. + // Currently throwing an error as a 400 response. + // Developers have the option of appending a field IS_ERROR to the jsonObject + // and showing it to the user in the webapp. If so, the IS_ERROR has to be checked in any later steps. throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.STATE_INVALID_ERROR); } + + JSONObject receipt = (JSONObject) receiptJSON; + + // Checks if 'data' object is present in the receipt + if (receipt.containsKey(ConsentExtensionConstants.DATA)) { + JSONObject data = (JSONObject) receipt.get(ConsentExtensionConstants.DATA); + + String type = consentResource.getConsentType(); + switch (type) { + case ConsentExtensionConstants.ACCOUNTS: + consentDataJSON = populateAccountData(data, consentDataJSON); + break; + case ConsentExtensionConstants.PAYMENTS: + consentDataJSON = populatePaymentData(data, consentDataJSON); + break; + case ConsentExtensionConstants.FUNDSCONFIRMATIONS: + consentDataJSON = populateCofData(data, consentDataJSON); + break; + case ConsentExtensionConstants.VRP: + // Check if 'controlParameters' object is present in the 'data' + if (data.containsKey(ConsentExtensionConstants.CONTROL_PARAMETERS)) { + JSONObject controlParameters = (JSONObject) data. + get(ConsentExtensionConstants.CONTROL_PARAMETERS); + + populateVRPData(controlParameters, consentDataJSON); + } else { + log.error(ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, + ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); + } + break; + default: + break; + } + } else { + log.error(ErrorConstants.DATA_OBJECT_MISSING_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.DATA_OBJECT_MISSING_ERROR); + } + } catch (ParseException e) { throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.CONSENT_RETRIEVAL_ERROR); } - return null; + return consentDataJSON; } + /** * Populate Domestic and international Payment Details. * - * @param data data request from the request - * @param consentDataJSON Consent information + * @param data data request from the request + * @param consentDataJSON Consent information */ - private static JSONArray populateSinglePaymentData(JSONObject data, JSONArray consentDataJSON) { + private static JSONArray populatePaymentData(JSONObject data, JSONArray consentDataJSON) { if (consentDataJSON == null) { // Initialize consentDataJSON if it's null @@ -438,8 +423,13 @@ private static JSONArray populateSinglePaymentData(JSONObject data, JSONArray co ConsentExtensionConstants.INSTRUCTED_AMOUNT_TITLE); jsonElementInstructedAmount.appendField(ConsentExtensionConstants.DATA_SIMPLE, instructedAmountArray); consentDataJSON.add(jsonElementInstructedAmount); - } + // Adding Debtor Account + populateDebtorAccount(initiation, consentDataJSON); + // Adding Creditor Account + populateCreditorAccount(initiation, consentDataJSON); + + } return consentDataJSON; } @@ -447,8 +437,8 @@ private static JSONArray populateSinglePaymentData(JSONObject data, JSONArray co /** * Populate account Details. * - * @param data data request from the request - * @param consentDataJSON Consent information + * @param data data request from the request + * @param consentDataJSON Consent information */ private static JSONArray populateAccountData(JSONObject data, JSONArray consentDataJSON) { @@ -516,78 +506,51 @@ private static JSONArray populateAccountData(JSONObject data, JSONArray consentD /** * Populate funds confirmation Details. * - * @param data data request from the request - * @param consentDataJSON Consent information + * @param initiation data from the request + * @param consentDataJSON Consent information */ - private static JSONArray populateCofData(JSONObject data, JSONArray consentDataJSON) { + private static JSONArray populateCofData(JSONObject initiation, JSONArray consentDataJSON) { if (consentDataJSON == null) { consentDataJSON = new JSONArray(); } - //Adding Expiration Date Time - if (data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE) != null) { + //Adding Expiration Date Time + if (initiation.getAsString(ConsentExtensionConstants.EXPIRATION_DATE) != null) { - if (!ConsentRetrievalUtil - .validateExpiryDateTime(data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE))) { - log.error(ErrorConstants.CONSENT_EXPIRED); - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONSENT_EXPIRED); - } + if (!ConsentRetrievalUtil + .validateExpiryDateTime(initiation.getAsString(ConsentExtensionConstants.EXPIRATION_DATE))) { + log.error(ErrorConstants.CONSENT_EXPIRED); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONSENT_EXPIRED); + } - String expiry = data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE); - JSONArray expiryArray = new JSONArray(); - expiryArray.add(expiry); + String expiry = initiation.getAsString(ConsentExtensionConstants.EXPIRATION_DATE); + JSONArray expiryArray = new JSONArray(); + expiryArray.add(expiry); - JSONObject jsonElementExpiry = new JSONObject(); - jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.EXPIRATION_DATE_TITLE); - jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); - consentDataJSON.add(jsonElementExpiry); - } else { - JSONArray expiryArray = new JSONArray(); - expiryArray.add(ConsentExtensionConstants.OPEN_ENDED_AUTHORIZATION); + JSONObject jsonElementExpiry = new JSONObject(); + jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.EXPIRATION_DATE_TITLE); + jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); + consentDataJSON.add(jsonElementExpiry); + } else { + JSONArray expiryArray = new JSONArray(); + expiryArray.add(ConsentExtensionConstants.OPEN_ENDED_AUTHORIZATION); - JSONObject jsonElementExpiry = new JSONObject(); - jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.EXPIRATION_DATE_TITLE); - jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); - consentDataJSON.add(jsonElementExpiry); - } + JSONObject jsonElementExpiry = new JSONObject(); + jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.EXPIRATION_DATE_TITLE); + jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); + consentDataJSON.add(jsonElementExpiry); - //Adding Debtor Account - if (data.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { - JSONObject debtorAccount = (JSONObject) data.get(ConsentExtensionConstants.DEBTOR_ACC); - JSONArray debtorAccountArray = new JSONArray(); - //Adding Debtor Account Scheme Name - if (debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { - debtorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); - } - //Adding Debtor Account Identification - if (debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { - debtorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); - } - //Adding Debtor Account Name - if (debtorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { - debtorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.NAME)); - } - //Adding Debtor Account Secondary Identification - if (debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { - debtorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); } - JSONObject jsonElementDebtor = new JSONObject(); - jsonElementDebtor.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.DEBTOR_ACC_TITLE); - jsonElementDebtor.appendField(ConsentExtensionConstants.DATA_SIMPLE, debtorAccountArray); - consentDataJSON.add(jsonElementDebtor); + if (initiation.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { + //Adding Debtor Account + populateDebtorAccount(initiation, consentDataJSON); } return consentDataJSON; - } @@ -597,7 +560,7 @@ private static JSONArray populateCofData(JSONObject data, JSONArray consentDataJ * @param controlParameters Control Parameters from the request * @param consentDataJSON Consent information object */ - private static JSONArray populateVRData(JSONObject controlParameters, JSONArray consentDataJSON) { + private static JSONArray populateVRPData(JSONObject controlParameters, JSONArray consentDataJSON) { if (consentDataJSON == null) { consentDataJSON = new JSONArray(); @@ -613,85 +576,104 @@ private static JSONArray populateVRData(JSONObject controlParameters, JSONArray jsonElementPaymentType.appendField(ConsentExtensionConstants.DATA_SIMPLE, paymentTypeArray); consentDataJSON.add(jsonElementPaymentType); - // Constructing jsonElementValidToDataTime - JSONObject jsonElementValidToDateTime = new JSONObject(); - jsonElementValidToDateTime.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_VALID_TO_DATE_TITLE); - JSONArray dateControlParameterArray = new JSONArray(); - dateControlParameterArray.add((controlParameters). - get(ConsentExtensionConstants.VALID_TO_DATE_TIME)); - jsonElementValidToDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, dateControlParameterArray); - - // Constructing jsonElementValidFromDataTime - JSONObject jsonElementValidFromDateTime = new JSONObject(); - jsonElementValidFromDateTime.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_VALID_FROM_DATE_TITLE); - JSONArray dateTimeControlParameterArray = new JSONArray(); - dateTimeControlParameterArray.add((controlParameters). - get(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); - jsonElementValidFromDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, dateTimeControlParameterArray); - - // Constructing jsonElementControlParameter - JSONObject jsonElementControlParameter = new JSONObject(); - jsonElementControlParameter.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_MAX_INDIVIDUAL_AMOUNT_TITLE); - JSONArray controlParameterArray = new JSONArray(); - - String formattedAmount = String.format("%s %s", - ((JSONObject) controlParameters.get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)) - .get(ConsentExtensionConstants.CURRENCY), - ((JSONObject) controlParameters.get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)) - .get(ConsentExtensionConstants.AMOUNT)); - controlParameterArray.add(formattedAmount); - jsonElementControlParameter.appendField(ConsentExtensionConstants.DATA_SIMPLE, controlParameterArray); - - // Constructing jsonElementPeriodAlignment - JSONObject jsonElementPeriodAlignment = new JSONObject(); - jsonElementPeriodAlignment.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_ALIGNMENT_TITLE); - String periodAlignment = (String) ((JSONObject) ((JSONArray) controlParameters - .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)) - .get(ConsentExtensionConstants.PERIOD_ALIGNMENT); - JSONArray periodAlignmentArray = new JSONArray(); - periodAlignmentArray.add(periodAlignment); - jsonElementPeriodAlignment.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodAlignmentArray); - - // Constructing jsonElementPeriodType - JSONObject jsonElementPeriodType = new JSONObject(); - jsonElementPeriodType.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_TYPE_TITLE); - String periodType = (String) ((JSONObject) ((JSONArray) controlParameters - .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)).get(ConsentExtensionConstants.PERIOD_TYPE); - JSONArray periodTypeArray = new JSONArray(); - periodTypeArray.add(periodType); - jsonElementPeriodType.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodTypeArray); - - // Constructing jsonElementPeriodicLimits - JSONObject jsonElementPeriodicLimits = new JSONObject(); - jsonElementPeriodicLimits.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_AMOUNT_TITLE + periodType); - JSONArray periodicLimitsArray = new JSONArray(); - - JSONObject periodicLimitsObject = (JSONObject) ((JSONArray) controlParameters - .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0); - - String currency = (String) periodicLimitsObject.get(ConsentExtensionConstants.CURRENCY); - String amount = (String) periodicLimitsObject.get(ConsentExtensionConstants.AMOUNT); - - // Concatenate currency and amount in the desired order - String formattedPeriodicAmount = String.format("%s %s", currency, amount); - - periodicLimitsArray.add(formattedPeriodicAmount); - - jsonElementPeriodicLimits.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodicLimitsArray); - - // Adding constructed JSON objects to consentDataJSON - consentDataJSON.add(jsonElementControlParameter); - consentDataJSON.add(jsonElementPeriodAlignment); - consentDataJSON.add(jsonElementPeriodicLimits); - consentDataJSON.add(jsonElementPeriodType); - consentDataJSON.add(jsonElementValidToDateTime); - consentDataJSON.add(jsonElementValidFromDateTime); + String expirationDate = controlParameters.getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME); + if (expirationDate != null) { + // Constructing jsonElementValidToDataTime + JSONObject jsonElementValidToDateTime = new JSONObject(); + jsonElementValidToDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_VALID_TO_DATE_TITLE); + JSONArray dateControlParameterArray = new JSONArray(); + dateControlParameterArray.add((controlParameters). + get(ConsentExtensionConstants.VALID_TO_DATE_TIME)); + jsonElementValidToDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, dateControlParameterArray); + + consentDataJSON.add(jsonElementValidToDateTime); + } + + String expirationDates = controlParameters.getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME); + if (expirationDates != null) { + // Constructing jsonElementValidFromDataTime + JSONObject jsonElementValidFromDateTime = new JSONObject(); + jsonElementValidFromDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_VALID_FROM_DATE_TITLE); + JSONArray dateTimeControlParameterArray = new JSONArray(); + dateTimeControlParameterArray.add((controlParameters). + get(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); + jsonElementValidFromDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, + dateTimeControlParameterArray); + consentDataJSON.add(jsonElementValidFromDateTime); + } + + String maxAmount = controlParameters.getAsString(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT); + if (maxAmount != null) { + // Constructing jsonElementControlParameter + JSONObject jsonElementControlParameter = new JSONObject(); + jsonElementControlParameter.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_MAX_INDIVIDUAL_AMOUNT_TITLE); + JSONArray controlParameterArray = new JSONArray(); + + + JSONObject maximumIndividualAmount = ((JSONObject) controlParameters. + get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)); + + String formattedAmount = String.format("%s %s", + (maximumIndividualAmount.get(ConsentExtensionConstants.CURRENCY)), + (maximumIndividualAmount.get(ConsentExtensionConstants.AMOUNT))); + controlParameterArray.add(formattedAmount); + jsonElementControlParameter.appendField(ConsentExtensionConstants.DATA_SIMPLE, controlParameterArray); + + consentDataJSON.add(jsonElementControlParameter); + } + + // Constructing jsonElementPeriodAlignment + JSONObject jsonElementPeriodAlignment = new JSONObject(); + jsonElementPeriodAlignment.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_ALIGNMENT_TITLE); + String periodAlignment = (String) ((JSONObject) ((JSONArray) controlParameters + .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)) + .get(ConsentExtensionConstants.PERIOD_ALIGNMENT); + + if (periodAlignment != null) { + JSONArray periodAlignmentArray = new JSONArray(); + periodAlignmentArray.add(periodAlignment); + jsonElementPeriodAlignment.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodAlignmentArray); + consentDataJSON.add(jsonElementPeriodAlignment); + } + + // Constructing jsonElementPeriodType + JSONObject jsonElementPeriodType = new JSONObject(); + jsonElementPeriodType.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_TYPE_TITLE); + String periodType = (String) ((JSONObject) ((JSONArray) controlParameters + .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)).get(ConsentExtensionConstants.PERIOD_TYPE); + + if (periodType != null) { + JSONArray periodTypeArray = new JSONArray(); + periodTypeArray.add(periodType); + jsonElementPeriodType.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodTypeArray); + consentDataJSON.add(jsonElementPeriodType); + } + + // Constructing jsonElementPeriodicLimits + JSONObject jsonElementPeriodicLimits = new JSONObject(); + jsonElementPeriodicLimits.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_AMOUNT_TITLE + periodType); + JSONArray periodicLimitsArray = new JSONArray(); + + JSONObject periodicLimitsObject = (JSONObject) ((JSONArray) controlParameters + .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0); + + if (periodicLimitsObject != null) { + String currency = (String) periodicLimitsObject.get(ConsentExtensionConstants.CURRENCY); + String amount = (String) periodicLimitsObject.get(ConsentExtensionConstants.AMOUNT); + String formattedPeriodicAmount = String.format("%s %s", currency, amount); + + periodicLimitsArray.add(formattedPeriodicAmount); + + jsonElementPeriodicLimits.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodicLimitsArray); + consentDataJSON.add(jsonElementPeriodicLimits); + + } return consentDataJSON; diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java index 77aea95b..42838db2 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java @@ -91,9 +91,8 @@ public Map updateConsentMetaData(HttpServletRequest request) { @Override public String getJSPPath() { - if (jspPath.equalsIgnoreCase(ConsentExtensionConstants.ACCOUNTS)) { - return "/ob_default.jsp"; - } else if (jspPath.equalsIgnoreCase(ConsentExtensionConstants.VRP)) { + if (jspPath.equalsIgnoreCase(ConsentExtensionConstants.ACCOUNTS) || + jspPath.equalsIgnoreCase(ConsentExtensionConstants.VRP)) { return "/ob_default.jsp"; } else { return "/default_displayconsent.jsp"; From cf5221f859e25f6f558d7a1c891277b6ef9587ee Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 21 Feb 2024 14:31:43 +0530 Subject: [PATCH 04/19] Unit test for VRP Retrieval Flow --- .../flow/ConsentAuthorizeTestConstants.java | 322 +++++++++++++++ .../flow/ConsentExtensionDataProvider.java | 17 + .../flow/VRPConsentRetrievalStepTest.java | 291 +++++++++++++ .../flow/VRPConsentRetrievalUtilTest.java | 389 ++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 5 files changed, 1020 insertions(+) create mode 100644 open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentAuthorizeTestConstants.java create mode 100644 open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentExtensionDataProvider.java create mode 100644 open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java create mode 100644 open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentAuthorizeTestConstants.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentAuthorizeTestConstants.java new file mode 100644 index 00000000..eeebf157 --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentAuthorizeTestConstants.java @@ -0,0 +1,322 @@ +/** + * 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.authorize.vrp.retrieval.flow; + +import java.time.Instant; +import java.time.OffsetDateTime; + +/** + * Constant class for consent authorize tests. + */ +public class ConsentAuthorizeTestConstants { + public static final String INVALID_REQUEST_OBJECT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.aWF0.TIygRaBn7MUFR9Zzy3" + + "yu9K8uKVe8KXdAty0Ckrg2vFI"; + public static final String VALID_REQUEST_OBJECT = "eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkR3TUtkV01tajdQV2" + + "ludm9xZlF5WFZ6eVo2USJ9.eyJtYXhfYWdlIjo4NjQwMCwiYXVkIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6OTQ0Ni9vYXV0aDIvdG9rZW4iL" + + "CJzY29wZSI6Im9wZW5pZCBhY2NvdW50cyIsImlzcyI6InF3ZGZnaGpwbG1nZmRhYWhrZ2pvcGhuayIsImNsYWltcyI6eyJpZF90b2tlb" + + "iI6eyJhY3IiOnsidmFsdWVzIjpbInVybjpvcGVuYmFua2luZzpwc2QyOnNjYSIsInVybjpvcGVuYmFua2luZzpwc2QyOmNhIl0sImVzc" + + "2VudGlhbCI6dHJ1ZX0sIm9wZW5iYW5raW5nX2ludGVudF9pZCI6eyJ2YWx1ZSI6IjEyMzQ1Njc3NjU0MzIxMjM0MjM0IiwiZXNzZW50a" + + "WFsIjp0cnVlfX0sInVzZXJpbmZvIjp7Im9wZW5iYW5raW5nX2ludGVudF9pZCI6eyJ2YWx1ZSI6IjEyMzQ1Njc3NjU0MzIxMjM0MjM0I" + + "iwiZXNzZW50aWFsIjp0cnVlfX19LCJyZXNwb25zZV90eXBlIjoiY29kZSBpZF90b2tlbiIsInJlZGlyZWN0X3VyaSI6Imh0dHBzOi8vd" + + "3NvMi5jb20iLCJzdGF0ZSI6IllXbHpjRG96TVRRMiIsImV4cCI6MTY1MzcxNzQ3OCwibm9uY2UiOiJuLTBTNl9XekEyTSIsImNsaWVud" + + "F9pZCI6InF3ZGZnaGpwbG1nZmRhYWhrZ2pvcGhuayJ9.lOvcc81dqjqdv4dslB_Kg4K3TKd13UQWaUKl3dBiPPlnu9y-R84Xfx-bMMnH" + + "atYyW9hYWJcUlprIm_dqgFXauCSTgBz6-vacrXLzuaGtj07d-8bL_qta45qbpbKPTY2pnM_PXe7fzs4RMCGEoiRLRs7lJUBfIbV9GzlS" + + "pHkOZiOjiFxxeYm0cNpZRvXkZNd59_GLdW2kKmWaGQHpQ9Ci_QpQENRzF8KEV1QtNd3cK2DjL5tKSw824C6AmXp-PKfvhurqPaVkz5p-" + + "iPA6bRaNBPY4hj_nsZpfuCnE8-V7YXWXXzWbK3gWo_dMOV1CZcHS6KqP7DANqDEEP4LoN081uQ"; + + public static final OffsetDateTime EXP_DATE = OffsetDateTime.now().plusDays(50); + + public static final OffsetDateTime INVALID_EXP_DATE = OffsetDateTime.now().plusDays(0); + + public static final OffsetDateTime NULL_EXP_DATE = null; + public static final String VALID_INITIATION_OBJECT = "{\"Data\": {\"Permissions\": [\"ReadAccountsDetail\"," + + "\"ReadBalances\",\"ReadBeneficiariesDetail\",\"ReadDirectDebits\",\"ReadProducts\"," + + "\"ReadStandingOrdersDetail\",\"ReadTransactionsCredits\",\"ReadTransactionsDebits\"," + + "\"ReadTransactionsDetail\",\"ReadOffers\",\"ReadPAN\",\"ReadParty\",\"ReadPartyPSU\"," + + " \"ReadScheduledPaymentsDetail\",\"ReadStatementsDetail\"],\"ExpirationDateTime\": " + + "\"" + EXP_DATE + "\",\"TransactionFromDateTime\": \"2021-05-03T00:00:00+00:00\"," + + "\"TransactionToDateTime\": \"2021-12-03T00:00:00+00:00\"},\"Risk\": {}}"; + + public static final String INVALID_INITIATION_OBJECT = "{\"Data\": {\"Permissions\": [\"ReadAccountsDetail\"," + + "\"ReadBalances\",\"ReadBeneficiariesDetail\",\"ReadDirectDebits\",\"ReadProducts\"," + + "\"ReadStandingOrdersDetail\",\"ReadTransactionsCredits\",\"ReadTransactionsDebits\"," + + "\"ReadTransactionsDetail\",\"ReadOffers\",\"ReadPAN\",\"ReadParty\",\"ReadPartyPSU\"," + + " \"ReadScheduledPaymentsDetail\",\"ReadStatementsDetail\"],\"ExpirationDateTime\": " + + "\"" + INVALID_EXP_DATE + "\",\"TransactionFromDateTime\": \"2021-05-03T00:00:00+00:00\"," + + "\"TransactionToDateTime\": \"2021-12-03T00:00:00+00:00\"},\"Risk\": {}}"; + + + public static final String AWAITING_AUTH_STATUS = "awaitingAuthorisation"; + + public static final long CREATED_TIME = Instant.now().toEpochMilli(); + public static final String COF_RECEIPT = "{" + + " \"Data\": {" + + " \"DebtorAccount\": {" + + " \"SchemeName\": \"OB.IBAN\"," + + " \"Identification\": \"GB76LOYD30949301273801\"," + + " \"Name\": \"Andrea Smith\"," + + " \"SecondaryIdentification\": \"Roll 56988\"" + + " }," + + " \"ExpirationDateTime\": \"" + EXP_DATE + "\"" + + " }" + + "}"; + + public static final String INVALID_COF_RECEIPT = "{" + + " \"Data\": {" + + " \"DebtorAccount\": {" + + " \"SchemeName\": \"OB.IBAN\"," + + " \"Identification\": \"GB76LOYD30949301273801\"," + + " \"Name\": \"Andrea Smith\"," + + " \"SecondaryIdentification\": \"Roll 56988\"" + + " }," + + " \"ExpirationDateTime\": \"" + INVALID_EXP_DATE + "\"" + + " }" + + "}"; + + public static final String NULL_COF_RECEIPT = "{" + + " \"Data\": {" + + " \"DebtorAccount\": {" + + " \"SchemeName\": \"OB.IBAN\"," + + " \"Identification\": \"GB76LOYD30949301273801\"," + + " \"Name\": \"Andrea Smith\"," + + " \"SecondaryIdentification\": \"Roll 56988\"" + + " }," + + " \"ExpirationDateTime\": \"" + NULL_EXP_DATE + "\"" + + " }" + + "}"; + public static final String VRP_INITIATION = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"Yes\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2017-06-05T15:15:13+00:00\",\n" + + " \"ValidToDateTime\": \"2022-07-05T15:15:13+00:00\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"100.00\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"200.00\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Week\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {" + + " \"Name\": \"Andrea Smith\", " + + " \"SchemeName\": \"OB.SortCodeAccountNumber\", " + + " \"Identification\": \"30080012343456\", " + + " \"SecondaryIdentification\": \"30080012343456\"" + + " }," + + " \"CreditorAccount\": {" + + " \"Name\": \"Andrea Smith\", " + + " \"SchemeName\": \"OB.SortCodeAccountNumber\", " + + " \"Identification\": \"30080012343456\", " + + " \"SecondaryIdentification\": \"30080012343456\"" + + " }," + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + public static final String VRP_WITHOUT_CONTROLPARAMETERS = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"Yes\",\n" + + " \"\": {\n" + + " \"ValidFromDateTime\": \"2017-06-05T15:15:13+00:00\",\n" + + " \"ValidToDateTime\": \"2022-07-05T15:15:13+00:00\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"100.00\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"200.00\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Week\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {" + + " \"Name\": \"Andrea Smith\", " + + " \"SchemeName\": \"OB.SortCodeAccountNumber\", " + + " \"Identification\": \"30080012343456\", " + + " \"SecondaryIdentification\": \"30080012343456\"" + + " }," + + " \"CreditorAccount\": {" + + " \"Name\": \"Andrea Smith\", " + + " \"SchemeName\": \"OB.SortCodeAccountNumber\", " + + " \"Identification\": \"30080012343456\", " + + " \"SecondaryIdentification\": \"30080012343456\"" + + " }," + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + + public static final String VRP_WITHOUT_DATA = "{\n" + + " \"\": {\n" + + " \"ReadRefundAccount\": \"Yes\",\n" + + " \"ControlParameters\": {\n" + + " \"ValidFromDateTime\": \"2017-06-05T15:15:13+00:00\",\n" + + " \"ValidToDateTime\": \"2022-07-05T15:15:13+00:00\",\n" + + " \"MaximumIndividualAmount\": {\n" + + " \"Amount\": \"100.00\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"PeriodicLimits\": [\n" + + " {\n" + + " \"Amount\": \"200.00\",\n" + + " \"Currency\": \"GBP\",\n" + + " \"PeriodAlignment\": \"Consent\",\n" + + " \"PeriodType\": \"Week\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"DebtorAccount\": {" + + " \"Name\": \"Andrea Smith\", " + + " \"SchemeName\": \"OB.SortCodeAccountNumber\", " + + " \"Identification\": \"30080012343456\", " + + " \"SecondaryIdentification\": \"30080012343456\"" + + " }," + + " \"CreditorAccount\": {" + + " \"Name\": \"Andrea Smith\", " + + " \"SchemeName\": \"OB.SortCodeAccountNumber\", " + + " \"Identification\": \"30080012343456\", " + + " \"SecondaryIdentification\": \"30080012343456\"" + + " }," + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"Sweepco\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"PartyToParty\"\n" + + " }\n" + + "}"; + + static OffsetDateTime expirationInstant = OffsetDateTime.now().plusDays(50); + public static final String PAYMENT_INITIATION = "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"Yes\",\n" + + " \"Authorisation\": {\n" + + " \"AuthorisationType\": \"Any\",\n" + + " \"CompletionDateTime\": \"" + expirationInstant + "\"\n" + + " },\n" + + " \"Initiation\": {\n" + + " \"InstructionIdentification\": \"ACME412\",\n" + + " \"EndToEndIdentification\": \"FRESCO.21302.GFX.20\",\n" + + " \"InstructedAmount\": {\n" + + " \"Amount\": \"165\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"DebtorAccount\": {\n" + + "\"SchemeName\": \"OB.SortCodeAccountNumber\",\n" + + "\"Identification\": \"30080012343456\",\n" + + "\"Name\": \"Andrea Smith\",\n" + + "\"SecondaryIdentification\": \"30080012343456\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.SortCodeAccountNumber\",\n" + + " \"Identification\": \"08080021325698\",\n" + + " \"Name\": \"ACME Inc\",\n" + + " \"SecondaryIdentification\": \"0002\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"FRESCO-101\",\n" + + " \"Unstructured\": \"Internal ops code 5120101\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"EcommerceGoods\",\n" + + " \"MerchantCategoryCode\": \"5967\",\n" + + " \"MerchantCustomerIdentification\": \"053598653254\",\n" + + " \"DeliveryAddress\": {\n" + + " \"AddressLine\": [\n" + + " \"Flat 7\",\n" + + " \"Acacia Lodge\"\n" + + " ],\n" + + " \"StreetName\": \"Acacia Avenue\",\n" + + " \"BuildingNumber\": \"27\",\n" + + " \"PostCode\": \"GU31 2ZZ\",\n" + + " \"TownName\": \"Sparsholt\",\n" + + " \"CountySubDivision\": [\n" + + " \"Wessex\"\n" + + " ],\n" + + " \"Country\": \"UK\"\n" + + " }\n" + + " }\n" + + "}"; + public static final String INTERNATIONAL_PAYMENT_INITIATION = "" + + "{\n" + + " \"Data\": {\n" + + " \"ReadRefundAccount\": \"Yes\",\n" + + " \"Initiation\": {\n" + + " \"InstructionIdentification\": \"ACME412\",\n" + + " \"EndToEndIdentification\": \"FRESCO.21302.GFX.20\",\n" + + " \"InstructionPriority\": \"Normal\",\n" + + " \"CurrencyOfTransfer\": \"USD\",\n" + + " \"InstructedAmount\": {\n" + + " \"Amount\": \"165.88\",\n" + + " \"Currency\": \"GBP\"\n" + + " },\n" + + " \"DebtorAccount\": {\n" + + " \"SchemeName\": \"OB.SortCodeAccountNumber\",\n" + + " \"Identification\": \"30080012343456\",\n" + + " \"Name\": \"Andrea Smith\",\n" + + " \"SecondaryIdentification\": \"30080012343456\"\n" + + " },\n" + + " \"CreditorAccount\": {\n" + + " \"SchemeName\": \"OB.SortCodeAccountNumber\",\n" + + " \"Identification\": \"08080021325698\",\n" + + " \"Name\": \"ACME Inc\",\n" + + " \"SecondaryIdentification\": \"0002\"\n" + + " },\n" + + " \"RemittanceInformation\": {\n" + + " \"Reference\": \"FRESCO-101\",\n" + + " \"Unstructured\": \"Internal ops code 5120101\"\n" + + " },\n" + + " \"ExchangeRateInformation\": {\n" + + " \"UnitCurrency\": \"GBP\",\n" + + " \"RateType\": \"Actual\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"Risk\": {\n" + + " \"PaymentContextCode\": \"TransferToThirdParty\"\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/authorize/vrp/retrieval/flow/ConsentExtensionDataProvider.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentExtensionDataProvider.java new file mode 100644 index 00000000..7d1a739e --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentExtensionDataProvider.java @@ -0,0 +1,17 @@ +package com.wso2.openbanking.accelerator.consent.extensions.authorize.vrp.retrieval.flow; + +import org.testng.annotations.DataProvider; +/** + * Data Provider for Consent Executor Tests. + */ +public class ConsentExtensionDataProvider { + + @DataProvider(name = "PaymentConsentDataDataProvider") + Object[][] getPaymentConsentDataDataProvider() { + + return new Object[][]{ + {ConsentAuthorizeTestConstants.PAYMENT_INITIATION}, + {ConsentAuthorizeTestConstants.INTERNATIONAL_PAYMENT_INITIATION} + }; + } +} diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java new file mode 100644 index 00000000..aefacc40 --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java @@ -0,0 +1,291 @@ +/** + * 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.authorize.vrp.retrieval.flow; + +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.common.util.ErrorConstants; +import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.DefaultConsentRetrievalStep; +import com.wso2.openbanking.accelerator.consent.extensions.authorize.model.ConsentData; +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.utils.ConsentExtensionTestUtils; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.AuthorizationResource; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentFile; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; +import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl; +import net.minidev.json.JSONArray; +import net.minidev.json.JSONObject; +import net.minidev.json.parser.ParseException; +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.IObjectFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.ObjectFactory; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +/** + * Test class for Consent Retrieval Step. + */ +@PrepareForTest({OpenBankingConfigParser.class, OpenBankingConfigParser.class, ConsentServiceUtil.class}) +@PowerMockIgnore({"com.wso2.openbanking.accelerator.consent.extensions.common.*", "net.minidev.*", + "jdk.internal.reflect.*"}) +public class VRPConsentRetrievalStepTest extends PowerMockTestCase { + + private static DefaultConsentRetrievalStep defaultConsentRetrievalStep; + @Mock + private static ConsentData consentDataMock; + @Mock + private static ConsentResource consentResourceMock; + @Mock + private static AuthorizationResource authorizationResourceMock; + @Mock + ConsentFile consentFileMock; + @Mock + ConsentCoreServiceImpl consentCoreServiceMock; + @Mock + OpenBankingConfigParser openBankingConfigParserMock; + + private static Map configMap; + ArrayList authResources; + + @BeforeClass + public void initClass() { + + MockitoAnnotations.initMocks(this); + + defaultConsentRetrievalStep = new DefaultConsentRetrievalStep(); + consentDataMock = Mockito.mock(ConsentData.class); + consentResourceMock = Mockito.mock(ConsentResource.class); + authorizationResourceMock = Mockito.mock(AuthorizationResource.class); + consentFileMock = Mockito.mock(ConsentFile.class); + consentCoreServiceMock = Mockito.mock(ConsentCoreServiceImpl.class); + + configMap = new HashMap<>(); + openBankingConfigParserMock = Mockito.mock(OpenBankingConfigParser.class); + + + authResources = new ArrayList(); + } + + @BeforeMethod + public void initMethod() throws ReflectiveOperationException { + + //to execute util class initialization + new CarbonUtils(); + System.setProperty("some.property", "property.value"); + System.setProperty("carbon.home", "."); + ConsentExtensionTestUtils.injectEnvironmentVariable("CARBON_HOME", "."); + + OpenBankingConfigParser openBankingConfigParserMock = Mockito.mock(OpenBankingConfigParser.class); + Mockito.doReturn("jdbc/WSO2OB_DB").when(openBankingConfigParserMock).getDataSourceName(); + + PowerMockito.mockStatic(OpenBankingConfigParser.class); + PowerMockito.when(OpenBankingConfigParser.getInstance()).thenReturn(openBankingConfigParserMock); + + Mockito.doReturn(configMap).when(openBankingConfigParserMock).getConfiguration(); + + PowerMockito.mockStatic(OpenBankingConfigParser.class); + PowerMockito.when(OpenBankingConfigParser.getInstance()).thenReturn(openBankingConfigParserMock); + } + + @ObjectFactory + public IObjectFactory getObjectFactory() { + + return new org.powermock.modules.testng.PowerMockObjectFactory(); + } + + @Test + public void testGetConsentDataSetForNonRegulatory() { + + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(false).when(consentDataMock).isRegulatory(); + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + + Assert.assertTrue(jsonObject.isEmpty()); + } + + @Test + public void testConsentRetrievalWithEmptyConsentData() { + + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(true).when(consentDataMock).isRegulatory(); + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + + Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); + String errorMsg = (String) jsonObject.get(ConsentExtensionConstants.IS_ERROR); + Assert.assertFalse(errorMsg.contains(ErrorConstants.REQUEST_OBJ_EXTRACT_ERROR)); + } + + @Test + public void testConsentRetrievalWithNonJWTRequestObject() { + + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(true).when(consentDataMock).isRegulatory(); + Mockito.doReturn("request=qwertyuijhbvbn").when(consentDataMock).getSpQueryParams(); + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + + Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); + String errorMsg = (String) jsonObject.get(ConsentExtensionConstants.IS_ERROR); + Assert.assertTrue(errorMsg.contains(ErrorConstants.REQUEST_OBJ_NOT_SIGNED)); + } + + @Test + public void testConsentRetrievalWithInvalidRequestObject() { + + String request = "request=" + ConsentAuthorizeTestConstants.INVALID_REQUEST_OBJECT; + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(true).when(consentDataMock).isRegulatory(); + Mockito.doReturn(request).when(consentDataMock).getSpQueryParams(); + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + + Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); + String errorMsg = (String) jsonObject.get(ConsentExtensionConstants.IS_ERROR); + Assert.assertTrue(errorMsg.contains(ErrorConstants.NOT_JSON_PAYLOAD)); + } + + @Test + public void testConsentRetrievalWithValidRequestObject() throws ConsentManagementException { + + String request = "request=" + ConsentAuthorizeTestConstants.VALID_REQUEST_OBJECT; + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(true).when(consentDataMock).isRegulatory(); + Mockito.doReturn(request).when(consentDataMock).getSpQueryParams(); + + Mockito.doReturn(ConsentExtensionConstants.AUTHORIZED_STATUS).when(consentResourceMock).getCurrentStatus(); + Mockito.doReturn(ConsentExtensionConstants.ACCOUNTS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.VALID_INITIATION_OBJECT).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(consentResourceMock).when(consentCoreServiceMock) + .getConsent(Mockito.anyString(), Mockito.anyBoolean()); + Mockito.doReturn(ConsentExtensionConstants.AUTHORIZED_STATUS).when(authorizationResourceMock) + .getAuthorizationStatus(); + authResources.add(authorizationResourceMock); + Mockito.doReturn(authResources).when(consentCoreServiceMock) + .searchAuthorizations(Mockito.anyString()); + + PowerMockito.mockStatic(ConsentServiceUtil.class); + PowerMockito.when(ConsentServiceUtil.getConsentService()).thenReturn(consentCoreServiceMock); + + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + + Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); + } + + @Test + public void testGetConsentDataSetForAccounts() throws ConsentManagementException, ParseException { + + Mockito.doReturn(ConsentExtensionConstants.ACCOUNTS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.VALID_INITIATION_OBJECT).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray accountConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); + + Assert.assertNotNull(accountConsentData); + } + + + + @Test(dataProvider = "PaymentConsentDataDataProvider", dataProviderClass = ConsentExtensionDataProvider.class) + public void testGetConsentDataSetForPayments(String paymentReceipt) throws ConsentManagementException, + ParseException { + + Mockito.doReturn(configMap).when(openBankingConfigParserMock).getConfiguration(); + + PowerMockito.mockStatic(OpenBankingConfigParser.class); + PowerMockito.when(OpenBankingConfigParser.getInstance()).thenReturn(openBankingConfigParserMock); + + Mockito.doReturn(ConsentExtensionConstants.PAYMENTS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(paymentReceipt).when(consentResourceMock).getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.CREATED_TIME).when(consentResourceMock) + .getCreatedTime(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray paymentConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); + + Assert.assertNotNull(paymentConsentData); + } + + @Test + public void testGetConsentDataSetForFilePayments() throws ConsentManagementException, ParseException { + + Mockito.doReturn(configMap).when(openBankingConfigParserMock).getConfiguration(); + + PowerMockito.mockStatic(OpenBankingConfigParser.class); + PowerMockito.when(OpenBankingConfigParser.getInstance()).thenReturn(openBankingConfigParserMock); + + PowerMockito.mockStatic(ConsentServiceUtil.class); + PowerMockito.when(ConsentServiceUtil.getConsentService()).thenReturn(consentCoreServiceMock); + + Mockito.doReturn(ConsentExtensionConstants.PAYMENTS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.CREATED_TIME).when(consentResourceMock) + .getCreatedTime(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray paymentConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); + + Assert.assertNotNull(paymentConsentData); + } + + + @Test + public void testGetConsentDataSetForCOF() throws ConsentManagementException, ParseException { + + Mockito.doReturn(ConsentExtensionConstants.FUNDSCONFIRMATIONS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.COF_RECEIPT).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray cofConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); + + Assert.assertNotNull(cofConsentData); + } + + @Test + public void testGetConsentDataSetForVRP() throws ConsentManagementException, ParseException { + + Mockito.doReturn(ConsentExtensionConstants.VRP).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.VRP_INITIATION).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray cofConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); + + Assert.assertNotNull(cofConsentData); + } + + +} diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java new file mode 100644 index 00000000..c929e07c --- /dev/null +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java @@ -0,0 +1,389 @@ +/** + * 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.authorize.vrp.retrieval.flow; + +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.common.util.ErrorConstants; +import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.DefaultConsentRetrievalStep; +import com.wso2.openbanking.accelerator.consent.extensions.authorize.model.ConsentData; +import com.wso2.openbanking.accelerator.consent.extensions.authorize.utils.ConsentRetrievalUtil; +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.utils.ConsentExtensionTestUtils; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.AuthorizationResource; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; +import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl; +import net.minidev.json.JSONArray; +import net.minidev.json.JSONObject; +import org.mockito.InjectMocks; +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.IObjectFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.ObjectFactory; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import static com.wso2.openbanking.accelerator.consent.extensions.authorize.vrp.retrieval.flow.ConsentAuthorizeTestConstants.VRP_WITHOUT_DATA; +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 consentRetrievalUtil. + */ +@PowerMockIgnore({"com.wso2.openbanking.accelerator.consent.extensions.common.*", "net.minidev.*", + "jdk.internal.reflect.*"}) +@PrepareForTest({OpenBankingConfigParser.class, OpenBankingConfigParser.class, ConsentServiceUtil.class}) +public class VRPConsentRetrievalUtilTest extends PowerMockTestCase { + + @InjectMocks + private final DefaultConsentRetrievalStep defaultConsentRetrievalStep = new DefaultConsentRetrievalStep(); + private final ConsentRetrievalUtil consentRetrievalUtil = new ConsentRetrievalUtil(); + @Mock + private static ConsentData consentDataMock; + @Mock + private static ConsentResource consentResourceMock; + @Mock + private static AuthorizationResource authorizationResourceMock; + @Mock + ConsentCoreServiceImpl consentCoreServiceMock; + @Mock + OpenBankingConfigParser openBankingConfigParser; + @Mock + OpenBankingConfigParser openBankingConfigParse; + private static Map configMap; + ArrayList authResources; + + + @BeforeClass + public void initClass() { + MockitoAnnotations.initMocks(this); + consentDataMock = mock(ConsentData.class); + consentResourceMock = mock(ConsentResource.class); + authorizationResourceMock = mock(AuthorizationResource.class); + consentCoreServiceMock = mock(ConsentCoreServiceImpl.class); + + + configMap = new HashMap<>(); + + authResources = new ArrayList(); + } + + @BeforeClass + public void setUp() throws ReflectiveOperationException { + + MockitoAnnotations.initMocks(this); + new CarbonUtils(); + System.setProperty("some.property", "property.value"); + System.setProperty("carbon.home", "."); + ConsentExtensionTestUtils.injectEnvironmentVariable("CARBON_HOME", "."); + + consentResourceMock = mock(ConsentResource.class); + + } + + @BeforeMethod + public void initMethod() { + openBankingConfigParser = mock(OpenBankingConfigParser.class); + doReturn(configMap).when(openBankingConfigParser).getConfiguration(); + + PowerMockito.mockStatic(OpenBankingConfigParser.class); + when(OpenBankingConfigParser.getInstance()).thenReturn(openBankingConfigParser); + + } + + @ObjectFactory + public IObjectFactory getObjectFactory() { + return new org.powermock.modules.testng.PowerMockObjectFactory(); + } + + @Test + public void testGetConsentDataSetForNonRegulatory() { + + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(false).when(consentDataMock).isRegulatory(); + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + Assert.assertTrue(jsonObject.isEmpty()); + } + + @Test + public void testConsentRetrievalWithEmptyConsentData() { + + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(true).when(consentDataMock).isRegulatory(); + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + + Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); + String errorMsg = (String) jsonObject.get(ConsentExtensionConstants.IS_ERROR); + Assert.assertFalse(errorMsg.contains(ErrorConstants.REQUEST_OBJ_EXTRACT_ERROR)); + } + + @Test + public void testConsentRetrievalWithNonJWTRequestObject() { + + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(true).when(consentDataMock).isRegulatory(); + Mockito.doReturn("request=qwertyuijhbvbn").when(consentDataMock).getSpQueryParams(); + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + + Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); + String errorMsg = (String) jsonObject.get(ConsentExtensionConstants.IS_ERROR); + Assert.assertTrue(errorMsg.contains(ErrorConstants.REQUEST_OBJ_NOT_SIGNED)); + } + + @Test + public void testConsentRetrievalWithInvalidRequestObject() { + + String request = "request=" + ConsentAuthorizeTestConstants.INVALID_REQUEST_OBJECT; + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(true).when(consentDataMock).isRegulatory(); + Mockito.doReturn(request).when(consentDataMock).getSpQueryParams(); + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + + Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); + String errorMsg = (String) jsonObject.get(ConsentExtensionConstants.IS_ERROR); + Assert.assertTrue(errorMsg.contains(ErrorConstants.NOT_JSON_PAYLOAD)); + } + + @Test + public void testConsentRetrievalWithValidRequestObject() throws ConsentManagementException { + + String request = "request=" + ConsentAuthorizeTestConstants.VALID_REQUEST_OBJECT; + JSONObject jsonObject = new JSONObject(); + Mockito.doReturn(true).when(consentDataMock).isRegulatory(); + Mockito.doReturn(request).when(consentDataMock).getSpQueryParams(); + + Mockito.doReturn(ConsentExtensionConstants.AUTHORIZED_STATUS).when(consentResourceMock).getCurrentStatus(); + Mockito.doReturn(ConsentExtensionConstants.ACCOUNTS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.VALID_INITIATION_OBJECT).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(consentResourceMock).when(consentCoreServiceMock) + .getConsent(Mockito.anyString(), Mockito.anyBoolean()); + Mockito.doReturn(ConsentExtensionConstants.AUTHORIZED_STATUS).when(authorizationResourceMock) + .getAuthorizationStatus(); + authResources.add(authorizationResourceMock); + Mockito.doReturn(authResources).when(consentCoreServiceMock) + .searchAuthorizations(Mockito.anyString()); + + PowerMockito.mockStatic(ConsentServiceUtil.class); + PowerMockito.when(ConsentServiceUtil.getConsentService()).thenReturn(consentCoreServiceMock); + + defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); + + Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); + } + + @Test + public void testGetConsentDataSetForAccounts() { + + Mockito.doReturn(ConsentExtensionConstants.ACCOUNTS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.VALID_INITIATION_OBJECT).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray accountConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + + Assert.assertNotNull(accountConsentData); + } + + @Test(dataProvider = "PaymentConsentDataDataProvider", dataProviderClass = ConsentExtensionDataProvider.class) + public void testGetConsentDataSetForPayments(String paymentReceipt) { + + Mockito.doReturn(configMap).when(openBankingConfigParse).getConfiguration(); + + PowerMockito.mockStatic(OpenBankingConfigParser.class); + PowerMockito.when(OpenBankingConfigParser.getInstance()).thenReturn(openBankingConfigParse); + + Mockito.doReturn(ConsentExtensionConstants.PAYMENTS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(paymentReceipt).when(consentResourceMock).getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.CREATED_TIME).when(consentResourceMock) + .getCreatedTime(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray paymentConsentData = consentRetrievalUtil.getConsentData(consentResourceMock); + + Assert.assertNotNull(paymentConsentData); + } + + @Test + public void testGetConsentDataSetForCOF() { + + Mockito.doReturn(ConsentExtensionConstants.FUNDSCONFIRMATIONS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.COF_RECEIPT).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + + Assert.assertNotNull(cofConsentData); + } + + @Test + public void testGetConsentDataSetForVRPData() { + + Mockito.doReturn(ConsentExtensionConstants.VRP).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.VRP_INITIATION).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + + Assert.assertNotNull(vrpConsentData); + } + + @Test + public void testAppendDummyAccountID() { + + JSONArray result = ConsentRetrievalUtil.appendDummyAccountID(); + + Assert.assertNotNull(result); + + Assert.assertEquals(result.size(), 2); + + JSONObject accountOne = (JSONObject) result.get(0); + Assert.assertEquals(accountOne.get("account_id"), "12345"); + Assert.assertEquals(accountOne.get("display_name"), "Salary Saver Account"); + + JSONObject accountTwo = (JSONObject) result.get(1); + Assert.assertEquals(accountTwo.get("account_id"), "67890"); + Assert.assertEquals(accountTwo.get("display_name"), "Max Bonus Account"); + } + + @Test(expectedExceptions = ConsentException.class) + public void testConsentDataWithInvalidJson() { + // Arrange + String invalidJsonString = "Invalid JSON String"; + Mockito.when(consentResourceMock.getReceipt()).thenReturn(invalidJsonString); + + // Act + JSONArray consentDataJSON = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(consentDataJSON); + } + + @Test(expectedExceptions = ConsentException.class) + public void testGetConsentDataSetForVRPDataParameter() { + + Mockito.doReturn(ConsentExtensionConstants.VRP).when(consentResourceMock).getConsentType(); + Mockito.doReturn(VRP_WITHOUT_DATA).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + + Assert.assertNotNull(vrpConsentData); + + + Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); + String errorMsg = ConsentExtensionConstants.IS_ERROR; + Assert.assertTrue(errorMsg.contains(ErrorConstants.NOT_JSON_PAYLOAD)); + } + + @Test(expectedExceptions = ConsentException.class) + public void testGetConsentDataSetForVRPDataWithoutControlParameters() { + + Mockito.doReturn(ConsentExtensionConstants.VRP).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.VRP_WITHOUT_CONTROLPARAMETERS).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + + Assert.assertNotNull(vrpConsentData); + + + Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); + String errorMsg = ConsentExtensionConstants.IS_ERROR; + Assert.assertTrue(errorMsg.contains(ErrorConstants.NOT_JSON_PAYLOAD)); + } + + @Test(expectedExceptions = ConsentException.class) + public void testGetConsentDataSetForAccount() { + + Mockito.doReturn(ConsentExtensionConstants.ACCOUNTS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.INVALID_INITIATION_OBJECT).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray accountConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(accountConsentData); + + Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); + String errorMsg = ConsentExtensionConstants.IS_ERROR; + Assert.assertTrue(errorMsg.contains(ErrorConstants.CONSENT_EXPIRED)); + } + + @Test(expectedExceptions = ConsentException.class) + public void testGetConsentDataSetForCOFs() { + + Mockito.doReturn(ConsentExtensionConstants.FUNDSCONFIRMATIONS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.INVALID_COF_RECEIPT).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + + Assert.assertNotNull(cofConsentData); + Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); + String errorMsg = ConsentExtensionConstants.IS_ERROR; + Assert.assertTrue(errorMsg.contains(ErrorConstants.CONSENT_EXPIRED)); + } + + + @Test(expectedExceptions = ConsentException.class) + public void testGetConsentDataSetForCOFNull() { + + Mockito.doReturn(ConsentExtensionConstants.FUNDSCONFIRMATIONS).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.NULL_COF_RECEIPT).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + + Assert.assertNotNull(cofConsentData); + Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); + String errorMsg = ConsentExtensionConstants.IS_ERROR; + Assert.assertTrue(errorMsg.contains(ErrorConstants.CONSENT_EXPIRED)); + } + +} + + 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 206a067a..1cb92604 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 @@ -27,6 +27,7 @@ + From 7207d30ffae9bffa4a557c478273cb12dbfd6de4 Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 28 Feb 2024 10:41:24 +0530 Subject: [PATCH 05/19] Fixing the comments for the last update of Retrieval flow --- .../common/util/ErrorConstants.java | 2 +- .../authorize/utils/ConsentRetrievalUtil.java | 65 ++++++++++--------- 2 files changed, 34 insertions(+), 33 deletions(-) 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 44d685af..2c2f4a25 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 @@ -291,6 +291,6 @@ public class ErrorConstants { public static final String PATH_PERIOD_ALIGNMENT = "Data.ControlParameters.PeriodicLimits.PeriodAlignment"; public static final String CONTROL_PARAMETERS_MISSING_ERROR = "Missing mandatory parameter the ControlParameters"; - public static final String DATA_OBJECT_MISSING_ERROR = "Missing mandatory parameter the Data "; + public static final String DATA_OBJECT_MISSING_ERROR = "Missing mandatory parameter the Data"; } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java index 93be7eb4..43867e1f 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * Copyright (c) 2023, 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 @@ -152,45 +152,46 @@ public static boolean validateExpiryDateTime(String expiryDate) throws ConsentEx * @param consentDataJSON Consent information object */ public static void populateDebtorAccount(JSONObject initiation, JSONArray consentDataJSON) { + if (initiation.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { + JSONObject debtorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.DEBTOR_ACC); + JSONArray debtorAccountArray = new JSONArray(); - if (initiation.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { - JSONObject debtorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.DEBTOR_ACC); - JSONArray debtorAccountArray = new JSONArray(); - - //Adding Debtor Account Scheme Name - if (debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { - debtorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); - } + //Adding Debtor Account Scheme Name + if (debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { + debtorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); + } - //Adding Debtor Account Identification - if (debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { - debtorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); - } + //Adding Debtor Account Identification + if (debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { + debtorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); + } - //Adding Debtor Account Name - if (debtorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { - debtorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.NAME)); - } + //Adding Debtor Account Name + if (debtorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { + debtorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.NAME)); + } - //Adding Debtor Account Secondary Identification - if (debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { - debtorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); - } + //Adding Debtor Account Secondary Identification + if (debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { + debtorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + + debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); + } - JSONObject jsonElementDebtor = new JSONObject(); - jsonElementDebtor.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.DEBTOR_ACC_TITLE); - jsonElementDebtor.appendField(ConsentExtensionConstants.DATA_SIMPLE, debtorAccountArray); - consentDataJSON.add(jsonElementDebtor); - } + JSONObject jsonElementDebtor = new JSONObject(); + jsonElementDebtor.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.DEBTOR_ACC_TITLE); + jsonElementDebtor.appendField(ConsentExtensionConstants.DATA_SIMPLE, debtorAccountArray); + consentDataJSON.add(jsonElementDebtor); } + } + + /** * Method to add debtor account details to consent data to send it to the consent page. * @@ -198,7 +199,6 @@ public static void populateDebtorAccount(JSONObject initiation, JSONArray consen * @param consentDataJSON */ public static void populateCreditorAccount(JSONObject initiation, JSONArray consentDataJSON) { - if (initiation.get(ConsentExtensionConstants.CREDITOR_ACC) != null) { JSONObject creditorAccount = (JSONObject) initiation.get(ConsentExtensionConstants.CREDITOR_ACC); JSONArray creditorAccountArray = new JSONArray(); @@ -326,6 +326,7 @@ public static JSONArray getConsentData(ConsentResource consentResource) { } } catch (ParseException e) { + log.error(ErrorConstants.CONSENT_RETRIEVAL_ERROR); throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.CONSENT_RETRIEVAL_ERROR); } From fa29ae703302ebdc6b481a49775b983428e0180f Mon Sep 17 00:00:00 2001 From: kalpana Date: Tue, 12 Mar 2024 17:46:07 +0530 Subject: [PATCH 06/19] Fixing the changes after the code review --- .../common/util/ErrorConstants.java | 7 + .../impl/DefaultConsentRetrievalStep.java | 6 +- .../authorize/utils/ConsentRetrievalUtil.java | 723 +++++++++--------- .../impl/OBDefaultAuthServletImpl.java | 5 - .../authservlet/impl/util/Utils.java | 10 +- .../common/ConsentExtensionConstants.java | 1 - .../AcceleratorConsentExtensionFactory.java | 51 -- 7 files changed, 389 insertions(+), 414 deletions(-) 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 2c2f4a25..7f5c8625 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 @@ -290,7 +290,14 @@ public class ErrorConstants { public static final String PATH_PERIOD_TYPE = "Data.ControlParameters.PeriodicLimits.PeriodType"; public static final String PATH_PERIOD_ALIGNMENT = "Data.ControlParameters.PeriodicLimits.PeriodAlignment"; + // VRP Authorization flow public static final String CONTROL_PARAMETERS_MISSING_ERROR = "Missing mandatory parameter the ControlParameters"; public static final String DATA_OBJECT_MISSING_ERROR = "Missing mandatory parameter the Data"; + public static final String MAX_AMOUNT_NOT_JSON_OBJECT_ERROR = "Parameter Maximum Individual Amount is" + + "not of type JSONObject"; + public static final String NOT_JSON_ARRAY_ERROR = "Parameter PeriodicLimits is not a JSON Array"; + public static final String PERIOD_ALIGNMENT_NOT_STRING_ERROR = "Parameter Period Alignment is not a String"; + public static final String PERIOD_TYPE_NOT_STRING_ERROR = "Parameter Period Type is not a String"; + public static final String NOT_STRING_ERROR = "Parameter amount or currency is not a 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/authorize/impl/DefaultConsentRetrievalStep.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java index 47950ff6..496db846 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/DefaultConsentRetrievalStep.java @@ -32,7 +32,6 @@ import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl; import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; -import net.minidev.json.parser.ParseException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -104,7 +103,7 @@ public void execute(ConsentData consentData, JSONObject jsonObject) throws Conse jsonObject.put(ConsentExtensionConstants.IS_ERROR, ((JSONObject) errorList.get(0)).getAsString("Message")); return; - } catch (ParseException | ConsentManagementException e) { + } catch (ConsentManagementException e) { throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, "Exception occurred while getting consent data"); } @@ -116,8 +115,7 @@ public void execute(ConsentData consentData, JSONObject jsonObject) throws Conse * @return consent * @throws ConsentException */ - public JSONArray getConsentDataSet(ConsentResource consentResource) - throws ConsentException, ConsentManagementException, ParseException { + public JSONArray getConsentDataSet(ConsentResource consentResource) { return ConsentRetrievalUtil.getConsentData(consentResource); } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java index 43867e1f..db7547b3 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java @@ -58,7 +58,7 @@ public static String extractRequestObject(String spQueryParams) { for (String param : spQueries) { if (param.contains("request=")) { requestObject = (param.substring("request=".length())).replaceAll( - "\\r\\n|\\r|\\n|\\%20", ""); + "\\r\\n|\\r|\\n|%20", ""); } } if (requestObject != null) { @@ -184,7 +184,7 @@ public static void populateDebtorAccount(JSONObject initiation, JSONArray consen JSONObject jsonElementDebtor = new JSONObject(); jsonElementDebtor.appendField(ConsentExtensionConstants.TITLE, ConsentExtensionConstants.DEBTOR_ACC_TITLE); - jsonElementDebtor.appendField(ConsentExtensionConstants.DATA_SIMPLE, debtorAccountArray); + jsonElementDebtor.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), debtorAccountArray); consentDataJSON.add(jsonElementDebtor); } @@ -226,13 +226,13 @@ public static void populateCreditorAccount(JSONObject initiation, JSONArray cons JSONObject jsonElementCreditor = new JSONObject(); jsonElementCreditor.appendField(ConsentExtensionConstants.TITLE, ConsentExtensionConstants.CREDITOR_ACC_TITLE); - jsonElementCreditor.appendField(ConsentExtensionConstants.DATA_SIMPLE, creditorAccountArray); + jsonElementCreditor.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + creditorAccountArray); consentDataJSON.add(jsonElementCreditor); } } - /** * Method to append Dummy data for Account ID. Ideally should be separate step calling accounts service * @@ -258,263 +258,249 @@ public static JSONArray appendDummyAccountID() { } - /** - * Method that invokes the relevant methods to populate data for each flow. - * - * @param consentResource Consent Resource parameter containing consent related information retrieved from database - * @return ConsentDataJson array - */ - public static JSONArray getConsentData(ConsentResource consentResource) { - - JSONArray consentDataJSON; - try { - consentDataJSON = new JSONArray(); - String receiptString = consentResource.getReceipt(); - Object receiptJSON = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(receiptString); - - // Checking whether the request body is in JSON format - if (!(receiptJSON instanceof JSONObject)) { - log.error(ErrorConstants.NOT_JSON_OBJECT_ERROR); - throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.NOT_JSON_OBJECT_ERROR); - } - - // Checking whether the consent status is valid - if (!consentResource.getCurrentStatus().equals(ConsentExtensionConstants.AWAITING_AUTH_STATUS)) { - log.error(ErrorConstants.STATE_INVALID_ERROR); - // Currently throwing an error as a 400 response. - // Developers have the option of appending a field IS_ERROR to the jsonObject - // and showing it to the user in the webapp. If so, the IS_ERROR has to be checked in any later steps. - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.STATE_INVALID_ERROR); - } - - JSONObject receipt = (JSONObject) receiptJSON; - - // Checks if 'data' object is present in the receipt - if (receipt.containsKey(ConsentExtensionConstants.DATA)) { - JSONObject data = (JSONObject) receipt.get(ConsentExtensionConstants.DATA); - - String type = consentResource.getConsentType(); - switch (type) { - case ConsentExtensionConstants.ACCOUNTS: - consentDataJSON = populateAccountData(data, consentDataJSON); - break; - case ConsentExtensionConstants.PAYMENTS: - consentDataJSON = populatePaymentData(data, consentDataJSON); - break; - case ConsentExtensionConstants.FUNDSCONFIRMATIONS: - consentDataJSON = populateCofData(data, consentDataJSON); - break; - case ConsentExtensionConstants.VRP: - // Check if 'controlParameters' object is present in the 'data' - if (data.containsKey(ConsentExtensionConstants.CONTROL_PARAMETERS)) { - JSONObject controlParameters = (JSONObject) data. - get(ConsentExtensionConstants.CONTROL_PARAMETERS); - - populateVRPData(controlParameters, consentDataJSON); - } else { - log.error(ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); - throw new ConsentException(ResponseStatus.BAD_REQUEST, - ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); - } - break; - default: - break; + /** + * Method that consists the implementation for the validation of payload and the consent, + * this method also invokes the relevant methods to populate data for each flow. + * + * @param consentResource Consent Resource parameter containing consent related information retrieved + * from database + * @return ConsentDataJson array + */ + public static JSONArray getConsentData(ConsentResource consentResource) throws ConsentException { + + JSONArray consentDataJSON = new JSONArray(); + try { + + String receiptString = consentResource.getReceipt(); + Object receiptJSON = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(receiptString); + + // Checking whether the request body is in JSON format + if (!(receiptJSON instanceof JSONObject)) { + log.error(ErrorConstants.NOT_JSON_OBJECT_ERROR); + throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, + ErrorConstants.NOT_JSON_OBJECT_ERROR); } - } else { - log.error(ErrorConstants.DATA_OBJECT_MISSING_ERROR); - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.DATA_OBJECT_MISSING_ERROR); - } - } catch (ParseException e) { - log.error(ErrorConstants.CONSENT_RETRIEVAL_ERROR); - throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.CONSENT_RETRIEVAL_ERROR); - } - - return consentDataJSON; - } + if (!consentResource.getCurrentStatus().equals(ConsentExtensionConstants.AWAITING_AUTH_STATUS)) { + log.error(ErrorConstants.STATE_INVALID_ERROR); + // Currently throwing an error as a 400 response. + // Developers have the option of appending a field IS_ERROR to the jsonObject + // and showing it to the user in the webapp.If so,the IS_ERROR has to be checked in any later steps. + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.STATE_INVALID_ERROR); + } + JSONObject receipt = (JSONObject) receiptJSON; + + // Checks if 'data' object is present in the receipt + if (receipt.containsKey(ConsentExtensionConstants.DATA)) { + JSONObject data = (JSONObject) receipt.get(ConsentExtensionConstants.DATA); + + String type = consentResource.getConsentType(); + switch (type) { + case ConsentExtensionConstants.ACCOUNTS: + populateAccountData(data, consentDataJSON); + break; + case ConsentExtensionConstants.PAYMENTS: + populatePaymentData(data, consentDataJSON); + break; + case ConsentExtensionConstants.FUNDSCONFIRMATIONS: + populateCofData(data, consentDataJSON); + break; + case ConsentExtensionConstants.VRP: + populateVRPData(data, consentDataJSON); + break; + default: + break; + } + } else { + log.error(ErrorConstants.DATA_OBJECT_MISSING_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.DATA_OBJECT_MISSING_ERROR); + } - /** - * Populate Domestic and international Payment Details. - * - * @param data data request from the request - * @param consentDataJSON Consent information - */ - private static JSONArray populatePaymentData(JSONObject data, JSONArray consentDataJSON) { + } catch (ParseException e) { + log.error(ErrorConstants.CONSENT_RETRIEVAL_ERROR, e); + throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, + ErrorConstants.CONSENT_RETRIEVAL_ERROR); + } - if (consentDataJSON == null) { - // Initialize consentDataJSON if it's null - consentDataJSON = new JSONArray(); + return consentDataJSON; } - JSONArray paymentTypeArray = new JSONArray(); - JSONObject jsonElementPaymentType = new JSONObject(); - if (data.containsKey(ConsentExtensionConstants.INITIATION)) { - JSONObject initiation = (JSONObject) data.get(ConsentExtensionConstants.INITIATION); + /** + * Populate Domestic and international Payment Details. + * + * @param data data request from the request + * @param consentDataJSON Consent information + */ + private static void populatePaymentData(JSONObject data, JSONArray consentDataJSON) { + + JSONArray paymentTypeArray = new JSONArray(); + JSONObject jsonElementPaymentType = new JSONObject(); + + if (data.containsKey(ConsentExtensionConstants.INITIATION)) { + JSONObject initiation = (JSONObject) data.get(ConsentExtensionConstants.INITIATION); + + if (initiation.containsKey(ConsentExtensionConstants.CURRENCY_OF_TRANSFER)) { + //For International Payments + //Adding Payment Type + paymentTypeArray.add(ConsentExtensionConstants.INTERNATIONAL_PAYMENTS); + + jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.PAYMENT_TYPE_TITLE); + jsonElementPaymentType.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + paymentTypeArray); + consentDataJSON.add(jsonElementPaymentType); + + //Adding Currency Of Transfer + JSONArray currencyTransferArray = new JSONArray(); + currencyTransferArray.add(initiation.getAsString(ConsentExtensionConstants.CURRENCY_OF_TRANSFER)); + + JSONObject jsonElementCurTransfer = new JSONObject(); + jsonElementCurTransfer.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CURRENCY_OF_TRANSFER_TITLE); + jsonElementCurTransfer.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + currencyTransferArray); + consentDataJSON.add(jsonElementCurTransfer); + } else { + //Adding Payment Type + paymentTypeArray.add(ConsentExtensionConstants.DOMESTIC_PAYMENTS); + + jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.PAYMENT_TYPE_TITLE); + jsonElementPaymentType.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + paymentTypeArray); + consentDataJSON.add(jsonElementPaymentType); + } - if (initiation.containsKey(ConsentExtensionConstants.CURRENCY_OF_TRANSFER)) { - //For International Payments - //Adding Payment Type - paymentTypeArray.add(ConsentExtensionConstants.INTERNATIONAL_PAYMENTS); + //Adding InstructionIdentification + JSONArray identificationArray = new JSONArray(); + identificationArray.add(initiation.getAsString(ConsentExtensionConstants.INSTRUCTION_IDENTIFICATION)); + + JSONObject jsonElementIdentification = new JSONObject(); + jsonElementIdentification.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.INSTRUCTION_IDENTIFICATION_TITLE); + jsonElementIdentification.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + identificationArray); + consentDataJSON.add(jsonElementIdentification); + + //Adding EndToEndIdentification + JSONArray endToEndIdentificationArray = new JSONArray(); + endToEndIdentificationArray + .add(initiation.getAsString(ConsentExtensionConstants.END_TO_END_IDENTIFICATION)); + + JSONObject jsonElementEndToEndIdentification = new JSONObject(); + jsonElementEndToEndIdentification.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.END_TO_END_IDENTIFICATION_TITLE); + jsonElementEndToEndIdentification.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + endToEndIdentificationArray); + consentDataJSON.add(jsonElementEndToEndIdentification); + + //Adding InstructedAmount + JSONObject instructedAmount = (JSONObject) initiation.get(ConsentExtensionConstants.INSTRUCTED_AMOUNT); + JSONArray instructedAmountArray = new JSONArray(); + + + if (instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT_TITLE) != null) { + instructedAmountArray.add(ConsentExtensionConstants.AMOUNT_TITLE + " : " + + instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT)); + } - jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.PAYMENT_TYPE_TITLE); - jsonElementPaymentType.appendField(ConsentExtensionConstants.DATA_SIMPLE, paymentTypeArray); - consentDataJSON.add(jsonElementPaymentType); + if (instructedAmount.getAsString(ConsentExtensionConstants.CURRENCY) != null) { + instructedAmountArray.add(ConsentExtensionConstants.CURRENCY_TITLE + " : " + + instructedAmount.getAsString(ConsentExtensionConstants.CURRENCY)); + } - //Adding Currency Of Transfer - JSONArray currencyTransferArray = new JSONArray(); - currencyTransferArray.add(initiation.getAsString(ConsentExtensionConstants.CURRENCY_OF_TRANSFER)); + JSONObject jsonElementInstructedAmount = new JSONObject(); + jsonElementInstructedAmount.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.INSTRUCTED_AMOUNT_TITLE); + jsonElementInstructedAmount.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + instructedAmountArray); + consentDataJSON.add(jsonElementInstructedAmount); - JSONObject jsonElementCurTransfer = new JSONObject(); - jsonElementCurTransfer.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CURRENCY_OF_TRANSFER_TITLE); - jsonElementCurTransfer.appendField(ConsentExtensionConstants.DATA_SIMPLE, currencyTransferArray); - consentDataJSON.add(jsonElementCurTransfer); - } else { - //Adding Payment Type - paymentTypeArray.add(ConsentExtensionConstants.DOMESTIC_PAYMENTS); + // Adding Debtor Account + populateDebtorAccount(initiation, consentDataJSON); + // Adding Creditor Account + populateCreditorAccount(initiation, consentDataJSON); - jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.PAYMENT_TYPE_TITLE); - jsonElementPaymentType.appendField(ConsentExtensionConstants.DATA_SIMPLE, paymentTypeArray); - consentDataJSON.add(jsonElementPaymentType); } - //Adding InstructionIdentification - JSONArray identificationArray = new JSONArray(); - identificationArray.add(initiation.getAsString(ConsentExtensionConstants.INSTRUCTION_IDENTIFICATION)); - - JSONObject jsonElementIdentification = new JSONObject(); - jsonElementIdentification.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.INSTRUCTION_IDENTIFICATION_TITLE); - jsonElementIdentification.appendField(ConsentExtensionConstants.DATA_SIMPLE, identificationArray); - consentDataJSON.add(jsonElementIdentification); - - //Adding EndToEndIdentification - JSONArray endToEndIdentificationArray = new JSONArray(); - endToEndIdentificationArray - .add(initiation.getAsString(ConsentExtensionConstants.END_TO_END_IDENTIFICATION)); - - JSONObject jsonElementEndToEndIdentification = new JSONObject(); - jsonElementEndToEndIdentification.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.END_TO_END_IDENTIFICATION_TITLE); - jsonElementEndToEndIdentification.appendField(ConsentExtensionConstants.DATA_SIMPLE, - endToEndIdentificationArray); - consentDataJSON.add(jsonElementEndToEndIdentification); + } - //Adding InstructedAmount - JSONObject instructedAmount = (JSONObject) initiation.get(ConsentExtensionConstants.INSTRUCTED_AMOUNT); - JSONArray instructedAmountArray = new JSONArray(); + /** + * Populate account Details. + * + * @param data data request from the request + * @param consentDataJSON Consent information + */ + private static void populateAccountData(JSONObject data, JSONArray consentDataJSON) { + + //Adding Permissions + JSONArray permissions = (JSONArray) data.get(ConsentExtensionConstants.PERMISSIONS); + if (permissions != null) { + JSONObject jsonElementPermissions = new JSONObject(); + jsonElementPermissions.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.PERMISSIONS); + jsonElementPermissions.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + permissions); + consentDataJSON.add(jsonElementPermissions); + } + //Adding Expiration Date Time + String expirationDate = data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE); + if (expirationDate != null) { + if (!ConsentRetrievalUtil.validateExpiryDateTime(expirationDate)) { + log.error(ErrorConstants.CONSENT_EXPIRED); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONSENT_EXPIRED); + } + JSONArray expiryArray = new JSONArray(); + expiryArray.add(expirationDate); - if (instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT_TITLE) != null) { - instructedAmountArray.add(ConsentExtensionConstants.AMOUNT_TITLE + " : " + - instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT)); + JSONObject jsonElementExpiry = new JSONObject(); + jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.EXPIRATION_DATE_TITLE); + jsonElementExpiry.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + expiryArray); + consentDataJSON.add(jsonElementExpiry); } - if (instructedAmount.getAsString(ConsentExtensionConstants.CURRENCY) != null) { - instructedAmountArray.add(ConsentExtensionConstants.CURRENCY_TITLE + " : " + - instructedAmount.getAsString(ConsentExtensionConstants.CURRENCY)); + //Adding Transaction From Date Time + String fromDateTime = data.getAsString(ConsentExtensionConstants.TRANSACTION_FROM_DATE); + if (fromDateTime != null) { + JSONArray fromDateTimeArray = new JSONArray(); + fromDateTimeArray.add(fromDateTime); + + JSONObject jsonElementFromDateTime = new JSONObject(); + jsonElementFromDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.TRANSACTION_FROM_DATE_TITLE); + jsonElementFromDateTime.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + fromDateTimeArray); + consentDataJSON.add(jsonElementFromDateTime); } - JSONObject jsonElementInstructedAmount = new JSONObject(); - jsonElementInstructedAmount.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.INSTRUCTED_AMOUNT_TITLE); - jsonElementInstructedAmount.appendField(ConsentExtensionConstants.DATA_SIMPLE, instructedAmountArray); - consentDataJSON.add(jsonElementInstructedAmount); - - // Adding Debtor Account - populateDebtorAccount(initiation, consentDataJSON); - // Adding Creditor Account - populateCreditorAccount(initiation, consentDataJSON); - - } - return consentDataJSON; - - } - - /** - * Populate account Details. - * - * @param data data request from the request - * @param consentDataJSON Consent information - */ - private static JSONArray populateAccountData(JSONObject data, JSONArray consentDataJSON) { - - if (consentDataJSON == null) { - consentDataJSON = new JSONArray(); - } - - //Adding Permissions - JSONArray permissions = (JSONArray) data.get(ConsentExtensionConstants.PERMISSIONS); - if (permissions != null) { - JSONObject jsonElementPermissions = new JSONObject(); - jsonElementPermissions.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.PERMISSIONS); - jsonElementPermissions.appendField(ConsentExtensionConstants.DATA_SIMPLE, permissions); - consentDataJSON.add(jsonElementPermissions); - } - - //Adding Expiration Date Time - String expirationDate = data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE); - if (expirationDate != null) { - if (!ConsentRetrievalUtil.validateExpiryDateTime(expirationDate)) { - log.error(ErrorConstants.CONSENT_EXPIRED); - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONSENT_EXPIRED); + //Adding Transaction To Date Time + String toDateTime = data.getAsString(ConsentExtensionConstants.TRANSACTION_TO_DATE); + if (toDateTime != null) { + JSONArray toDateTimeArray = new JSONArray(); + toDateTimeArray.add(toDateTime); + + JSONObject jsonElementToDateTime = new JSONObject(); + jsonElementToDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.TRANSACTION_TO_DATE_TITLE); + jsonElementToDateTime.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + toDateTimeArray); + consentDataJSON.add(jsonElementToDateTime); } - JSONArray expiryArray = new JSONArray(); - expiryArray.add(expirationDate); - - JSONObject jsonElementExpiry = new JSONObject(); - jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.EXPIRATION_DATE_TITLE); - jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); - consentDataJSON.add(jsonElementExpiry); - } - //Adding Transaction From Date Time - String fromDateTime = data.getAsString(ConsentExtensionConstants.TRANSACTION_FROM_DATE); - if (fromDateTime != null) { - JSONArray fromDateTimeArray = new JSONArray(); - fromDateTimeArray.add(fromDateTime); - - JSONObject jsonElementFromDateTime = new JSONObject(); - jsonElementFromDateTime.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.TRANSACTION_FROM_DATE_TITLE); - jsonElementFromDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, fromDateTimeArray); - consentDataJSON.add(jsonElementFromDateTime); } - //Adding Transaction To Date Time - String toDateTime = data.getAsString(ConsentExtensionConstants.TRANSACTION_TO_DATE); - if (toDateTime != null) { - JSONArray toDateTimeArray = new JSONArray(); - toDateTimeArray.add(toDateTime); - - JSONObject jsonElementToDateTime = new JSONObject(); - jsonElementToDateTime.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.TRANSACTION_TO_DATE_TITLE); - jsonElementToDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, toDateTimeArray); - consentDataJSON.add(jsonElementToDateTime); - } - - return consentDataJSON; - - } - - /** - * Populate funds confirmation Details. - * - * @param initiation data from the request - * @param consentDataJSON Consent information - */ - private static JSONArray populateCofData(JSONObject initiation, JSONArray consentDataJSON) { - - if (consentDataJSON == null) { - consentDataJSON = new JSONArray(); - } + /** + * Populate funds confirmation Details. + * + * @param initiation data from the request + * @param consentDataJSON Consent information + */ + private static void populateCofData(JSONObject initiation, JSONArray consentDataJSON) { //Adding Expiration Date Time if (initiation.getAsString(ConsentExtensionConstants.EXPIRATION_DATE) != null) { @@ -532,7 +518,7 @@ private static JSONArray populateCofData(JSONObject initiation, JSONArray consen JSONObject jsonElementExpiry = new JSONObject(); jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, ConsentExtensionConstants.EXPIRATION_DATE_TITLE); - jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); + jsonElementExpiry.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), expiryArray); consentDataJSON.add(jsonElementExpiry); } else { JSONArray expiryArray = new JSONArray(); @@ -541,143 +527,184 @@ private static JSONArray populateCofData(JSONObject initiation, JSONArray consen JSONObject jsonElementExpiry = new JSONObject(); jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, ConsentExtensionConstants.EXPIRATION_DATE_TITLE); - jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); + jsonElementExpiry.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), expiryArray); consentDataJSON.add(jsonElementExpiry); - } - if (initiation.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { - //Adding Debtor Account - populateDebtorAccount(initiation, consentDataJSON); + if (initiation.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { + //Adding Debtor Account + populateDebtorAccount(initiation, consentDataJSON); + } } - return consentDataJSON; - } + /** + * Populate VRP Details. + * + * @param data Control Parameters from the request + * @param consentDataJSON Consent information object + */ + private static void populateVRPData(JSONObject data, JSONArray consentDataJSON) { - /** - * Populate VRP Details. - * - * @param controlParameters Control Parameters from the request - * @param consentDataJSON Consent information object - */ - private static JSONArray populateVRPData(JSONObject controlParameters, JSONArray consentDataJSON) { + if (data.containsKey(ConsentExtensionConstants.CONTROL_PARAMETERS)) { - if (consentDataJSON == null) { - consentDataJSON = new JSONArray(); - } + JSONObject controlParameters = (JSONObject) data. + get(ConsentExtensionConstants.CONTROL_PARAMETERS); - JSONArray paymentTypeArray = new JSONArray(); - JSONObject jsonElementPaymentType = new JSONObject(); - - //Adding Payment Type - paymentTypeArray.add(ConsentExtensionConstants.DOMESTIC_VRP); - jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.PAYMENT_TYPE_TITLE); - jsonElementPaymentType.appendField(ConsentExtensionConstants.DATA_SIMPLE, paymentTypeArray); - consentDataJSON.add(jsonElementPaymentType); - - String expirationDate = controlParameters.getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME); - if (expirationDate != null) { - // Constructing jsonElementValidToDataTime - JSONObject jsonElementValidToDateTime = new JSONObject(); - jsonElementValidToDateTime.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_VALID_TO_DATE_TITLE); - JSONArray dateControlParameterArray = new JSONArray(); - dateControlParameterArray.add((controlParameters). - get(ConsentExtensionConstants.VALID_TO_DATE_TIME)); - jsonElementValidToDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, dateControlParameterArray); - - consentDataJSON.add(jsonElementValidToDateTime); - } + //Adding Payment Type + JSONArray paymentTypeArray = new JSONArray(); + JSONObject jsonElementPaymentType = new JSONObject(); + paymentTypeArray.add(ConsentExtensionConstants.DOMESTIC_VRP); + jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.PAYMENT_TYPE_TITLE); + jsonElementPaymentType.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + paymentTypeArray); + consentDataJSON.add(jsonElementPaymentType); - String expirationDates = controlParameters.getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME); - if (expirationDates != null) { - // Constructing jsonElementValidFromDataTime - JSONObject jsonElementValidFromDateTime = new JSONObject(); - jsonElementValidFromDateTime.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_VALID_FROM_DATE_TITLE); - JSONArray dateTimeControlParameterArray = new JSONArray(); - dateTimeControlParameterArray.add((controlParameters). - get(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); - jsonElementValidFromDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, - dateTimeControlParameterArray); - consentDataJSON.add(jsonElementValidFromDateTime); - } + String validToDateTime = controlParameters.getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME); + if (validToDateTime != null) { + // Constructing jsonElementValidToDataTime + JSONObject jsonElementValidToDateTime = new JSONObject(); + jsonElementValidToDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_VALID_TO_DATE_TITLE); + JSONArray dateControlParameterArray = new JSONArray(); + dateControlParameterArray.add((controlParameters). + get(ConsentExtensionConstants.VALID_TO_DATE_TIME)); + jsonElementValidToDateTime.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + dateControlParameterArray); + + consentDataJSON.add(jsonElementValidToDateTime); + } - String maxAmount = controlParameters.getAsString(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT); - if (maxAmount != null) { - // Constructing jsonElementControlParameter - JSONObject jsonElementControlParameter = new JSONObject(); - jsonElementControlParameter.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_MAX_INDIVIDUAL_AMOUNT_TITLE); - JSONArray controlParameterArray = new JSONArray(); + String validFromDateTime = controlParameters.getAsString + (ConsentExtensionConstants.VALID_FROM_DATE_TIME); + if (validFromDateTime != null) { + // Constructing jsonElementValidFromDataTime + JSONObject jsonElementValidFromDateTime = new JSONObject(); + jsonElementValidFromDateTime.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_VALID_FROM_DATE_TITLE); + JSONArray dateTimeControlParameterArray = new JSONArray(); + dateTimeControlParameterArray.add((controlParameters). + get(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); + jsonElementValidFromDateTime.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + dateTimeControlParameterArray); + consentDataJSON.add(jsonElementValidFromDateTime); + } + Object maxAmount = controlParameters.get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT); - JSONObject maximumIndividualAmount = ((JSONObject) controlParameters. - get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)); + if (maxAmount instanceof JSONObject) { + JSONObject jsonElementControlParameter = new JSONObject(); + jsonElementControlParameter.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_MAX_INDIVIDUAL_AMOUNT_TITLE); + JSONArray controlParameterArray = new JSONArray(); - String formattedAmount = String.format("%s %s", - (maximumIndividualAmount.get(ConsentExtensionConstants.CURRENCY)), - (maximumIndividualAmount.get(ConsentExtensionConstants.AMOUNT))); - controlParameterArray.add(formattedAmount); - jsonElementControlParameter.appendField(ConsentExtensionConstants.DATA_SIMPLE, controlParameterArray); + JSONObject maximumIndividualAmount = (JSONObject) maxAmount; - consentDataJSON.add(jsonElementControlParameter); - } + String formattedAmount = String.format("%s %s", + maximumIndividualAmount.getAsString(ConsentExtensionConstants.CURRENCY), + maximumIndividualAmount.getAsString(ConsentExtensionConstants.AMOUNT)); + controlParameterArray.add(formattedAmount); + jsonElementControlParameter.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + controlParameterArray); - // Constructing jsonElementPeriodAlignment - JSONObject jsonElementPeriodAlignment = new JSONObject(); - jsonElementPeriodAlignment.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_ALIGNMENT_TITLE); - String periodAlignment = (String) ((JSONObject) ((JSONArray) controlParameters - .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)) - .get(ConsentExtensionConstants.PERIOD_ALIGNMENT); - - if (periodAlignment != null) { - JSONArray periodAlignmentArray = new JSONArray(); - periodAlignmentArray.add(periodAlignment); - jsonElementPeriodAlignment.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodAlignmentArray); - consentDataJSON.add(jsonElementPeriodAlignment); - } + consentDataJSON.add(jsonElementControlParameter); + } else { + log.error(ErrorConstants.MAX_AMOUNT_NOT_JSON_OBJECT_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, + ErrorConstants.MAX_AMOUNT_NOT_JSON_OBJECT_ERROR); + } - // Constructing jsonElementPeriodType - JSONObject jsonElementPeriodType = new JSONObject(); - jsonElementPeriodType.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_TYPE_TITLE); - String periodType = (String) ((JSONObject) ((JSONArray) controlParameters - .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)).get(ConsentExtensionConstants.PERIOD_TYPE); - - if (periodType != null) { - JSONArray periodTypeArray = new JSONArray(); - periodTypeArray.add(periodType); - jsonElementPeriodType.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodTypeArray); - consentDataJSON.add(jsonElementPeriodType); - } + Object periodicLimit = controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); - // Constructing jsonElementPeriodicLimits - JSONObject jsonElementPeriodicLimits = new JSONObject(); - jsonElementPeriodicLimits.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_AMOUNT_TITLE + periodType); - JSONArray periodicLimitsArray = new JSONArray(); + if (periodicLimit instanceof JSONArray) { + JSONArray periodicLimitsArrays = (JSONArray) periodicLimit; - JSONObject periodicLimitsObject = (JSONObject) ((JSONArray) controlParameters - .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0); + for (Object periodicLimitObject : periodicLimitsArrays) { + if (periodicLimitObject instanceof JSONObject) { + JSONObject jsonObject = (JSONObject) periodicLimitObject; - if (periodicLimitsObject != null) { - String currency = (String) periodicLimitsObject.get(ConsentExtensionConstants.CURRENCY); - String amount = (String) periodicLimitsObject.get(ConsentExtensionConstants.AMOUNT); - String formattedPeriodicAmount = String.format("%s %s", currency, amount); + Object periodAlignmentObject = jsonObject.get(ConsentExtensionConstants.PERIOD_ALIGNMENT); - periodicLimitsArray.add(formattedPeriodicAmount); + if (periodAlignmentObject instanceof String) { + // Constructing jsonElementPeriodAlignment + JSONObject jsonElementPeriodAlignment = new JSONObject(); + jsonElementPeriodAlignment.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_ALIGNMENT_TITLE); - jsonElementPeriodicLimits.appendField(ConsentExtensionConstants.DATA_SIMPLE, periodicLimitsArray); - consentDataJSON.add(jsonElementPeriodicLimits); + JSONArray periodAlignmentArray = new JSONArray(); + periodAlignmentArray.add(periodAlignmentObject); - } + jsonElementPeriodAlignment.appendField(StringUtils. + lowerCase(ConsentExtensionConstants.DATA), periodAlignmentArray); + consentDataJSON.add(jsonElementPeriodAlignment); + } else { + log.error(ErrorConstants.PERIOD_ALIGNMENT_NOT_STRING_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, + ErrorConstants.PERIOD_ALIGNMENT_NOT_STRING_ERROR); + } + + Object periodTypeObject = jsonObject.get(ConsentExtensionConstants.PERIOD_TYPE); + + if (periodTypeObject instanceof String) { + + JSONObject jsonElementPeriodType = new JSONObject(); + jsonElementPeriodType.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_PERIOD_TYPE_TITLE); + + JSONArray periodTypeArray = new JSONArray(); + periodTypeArray.add(periodTypeObject); + + jsonElementPeriodType.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + periodTypeArray); + + consentDataJSON.add(jsonElementPeriodType); + + } else { + log.error(ErrorConstants.PERIOD_TYPE_NOT_STRING_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, + ErrorConstants.PERIOD_TYPE_NOT_STRING_ERROR); + } + // Constructing jsonElementPeriodicLimitsAmountCurrency - periodicLimits amount and currency + Object amount = jsonObject.get(ConsentExtensionConstants.AMOUNT); + Object currency = jsonObject.get(ConsentExtensionConstants.CURRENCY); - return consentDataJSON; + if (amount instanceof String && currency instanceof String) { + String periodTypeString = (String) periodTypeObject; + JSONObject jsonElementPeriodicLimitsAmountCurrency = new JSONObject(); + jsonElementPeriodicLimitsAmountCurrency.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_AMOUNT_TITLE + + periodTypeString); + + JSONArray periodicLimitsArray = new JSONArray(); + + String amountString = (String) amount; + String currencyString = (String) currency; + + String formattedPeriodicAmount = String.format("%s %s", currencyString, amountString); + periodicLimitsArray.add(formattedPeriodicAmount); + + jsonElementPeriodicLimitsAmountCurrency.appendField(StringUtils. + lowerCase(ConsentExtensionConstants.DATA), periodicLimitsArray); + consentDataJSON.add(jsonElementPeriodicLimitsAmountCurrency); + + } else { + log.error(ErrorConstants.NOT_STRING_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, + ErrorConstants.NOT_STRING_ERROR); + } + } + } + } else { + log.error(ErrorConstants.NOT_JSON_ARRAY_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.NOT_JSON_ARRAY_ERROR); + } + } else { + log.error(ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONTROL_PARAMETERS_MISSING_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/authservlet/impl/OBDefaultAuthServletImpl.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java index 42838db2..54ea3034 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java @@ -74,11 +74,6 @@ public Map updateConsentData(HttpServletRequest request) { String[] accounts = request.getParameter("accounts[]").split(":"); returnMaps.put("accountIds", new JSONArray(accounts)); - returnMaps.put(ConsentExtensionConstants.PAYMENT_ACCOUNT, - request.getParameter(ConsentExtensionConstants.PAYMENT_ACCOUNT)); - returnMaps.put(ConsentExtensionConstants.COF_ACCOUNT, - request.getParameter(ConsentExtensionConstants.COF_ACCOUNT)); - return returnMaps; } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java index 2ee28989..b818c1f7 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java @@ -103,7 +103,7 @@ public static Map populateAccountsData(HttpServletRequest reques for (int requestedDataIndex = 0; requestedDataIndex < dataRequestedJsonArray.length(); requestedDataIndex++) { JSONObject dataObj = dataRequestedJsonArray.getJSONObject(requestedDataIndex); String title = dataObj.getString(ConsentExtensionConstants.TITLE); - JSONArray dataArray = dataObj.getJSONArray(ConsentExtensionConstants.DATA_SIMPLE); + JSONArray dataArray = dataObj.getJSONArray(StringUtils.lowerCase(ConsentExtensionConstants.DATA)); ArrayList listData = new ArrayList<>(); for (int dataIndex = 0; dataIndex < dataArray.length(); dataIndex++) { @@ -140,7 +140,7 @@ public static Map populatePaymentsData(HttpServletRequest reques for (int requestedDataIndex = 0; requestedDataIndex < dataRequestedJsonArray.length(); requestedDataIndex++) { JSONObject dataObj = dataRequestedJsonArray.getJSONObject(requestedDataIndex); String title = dataObj.getString(ConsentExtensionConstants.TITLE); - JSONArray dataArray = dataObj.getJSONArray(ConsentExtensionConstants.DATA_SIMPLE); + JSONArray dataArray = dataObj.getJSONArray(StringUtils.lowerCase(ConsentExtensionConstants.DATA)); ArrayList listData = new ArrayList<>(); for (int dataIndex = 0; dataIndex < dataArray.length(); dataIndex++) { @@ -182,7 +182,7 @@ public static Map populateCoFData(HttpServletRequest httpServlet for (int requestedDataIndex = 0; requestedDataIndex < dataRequestedJsonArray.length(); requestedDataIndex++) { JSONObject dataObj = dataRequestedJsonArray.getJSONObject(requestedDataIndex); String title = dataObj.getString(ConsentExtensionConstants.TITLE); - JSONArray dataArray = dataObj.getJSONArray(ConsentExtensionConstants.DATA_SIMPLE); + JSONArray dataArray = dataObj.getJSONArray(StringUtils.lowerCase(ConsentExtensionConstants.DATA)); ArrayList listData = new ArrayList<>(); for (int dataIndex = 0; dataIndex < dataArray.length(); dataIndex++) { @@ -217,7 +217,7 @@ public static String getDebtorAccFromConsentData(JSONArray consentDataObject) { String title = dataObj.getString(ConsentExtensionConstants.TITLE); if (ConsentExtensionConstants.DEBTOR_ACC_TITLE.equals(title)) { - JSONArray dataArray = dataObj.getJSONArray(ConsentExtensionConstants.DATA_SIMPLE); + JSONArray dataArray = dataObj.getJSONArray(StringUtils.lowerCase(ConsentExtensionConstants.DATA)); for (int dataIndex = 0; dataIndex < dataArray.length(); dataIndex++) { String data = (String) dataArray.get(dataIndex); @@ -269,7 +269,7 @@ public static Map populateVRPDataRetrieval(HttpServletRequest re for (int requestedDataIndex = 0; requestedDataIndex < dataRequestedJsonArray.length(); requestedDataIndex++) { JSONObject dataObj = dataRequestedJsonArray.getJSONObject(requestedDataIndex); String title = dataObj.getString(ConsentExtensionConstants.TITLE); - JSONArray dataArray = dataObj.getJSONArray(ConsentExtensionConstants.DATA_SIMPLE); + JSONArray dataArray = dataObj.getJSONArray(StringUtils.lowerCase(ConsentExtensionConstants.DATA)); ArrayList listData = new ArrayList<>(); for (int dataIndex = 0; dataIndex < dataArray.length(); dataIndex++) { 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 4b05690b..2f44a1a6 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 @@ -93,7 +93,6 @@ public class ConsentExtensionConstants { public static final String ACCOUNTS = "accounts"; public static final String CONSENT_DATA = "consentData"; public static final String TITLE = "title"; - 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"; 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 47a00f48..8d8672c9 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 @@ -17,14 +17,6 @@ */ package com.wso2.openbanking.accelerator.consent.extensions.common.factory; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.persist.AccountConsentPersistenceHandler; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.persist.CofConsentPersistenceHandler; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.persist.ConsentPersistenceHandler; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.persist.PaymentConsentPersistenceHandler; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.retrieval.AccountConsentRetrievalHandler; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.retrieval.CofConsentRetrievalHandler; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.retrieval.ConsentRetrievalHandler; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.retrieval.PaymentConsentRetrievalHandler; import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionConstants; import com.wso2.openbanking.accelerator.consent.extensions.manage.impl.AccountConsentManageRequestHandler; import com.wso2.openbanking.accelerator.consent.extensions.manage.impl.CofConsentRequestHandler; @@ -65,47 +57,4 @@ public static ConsentManageRequestHandler getConsentManageRequestValidator(Strin return consentManageRequestHandler; } - - /** - * Method to get the Consent Authorize Handler. - * - * @param type Type of the request - * @return ConsentAuthorizeHandler - */ - public static ConsentRetrievalHandler getConsentRetrievalHandler(String type) { - - ConsentRetrievalHandler consentRetrieveHandler = null; - - if (type.equalsIgnoreCase(ConsentExtensionConstants.ACCOUNTS)) { - consentRetrieveHandler = new AccountConsentRetrievalHandler(); - } else if (type.equalsIgnoreCase(ConsentExtensionConstants.PAYMENTS)) { - consentRetrieveHandler = new PaymentConsentRetrievalHandler(); - } else if (type.equalsIgnoreCase(ConsentExtensionConstants.FUNDSCONFIRMATIONS)) { - consentRetrieveHandler = new CofConsentRetrievalHandler(); - } - return consentRetrieveHandler; - - } - - /** - * Method to get the Consent Persistence Handler. - * - * @param type Type of the request - * @return ConsentPersistenceHandler - */ - public static ConsentPersistenceHandler getConsentPersistenceHandler(String type) { - - ConsentPersistenceHandler consentPersistenceHandler = null; - - if (ConsentExtensionConstants.ACCOUNTS.equalsIgnoreCase(type)) { - consentPersistenceHandler = new AccountConsentPersistenceHandler(); - } else if (ConsentExtensionConstants.PAYMENTS.equalsIgnoreCase(type)) { - consentPersistenceHandler = new PaymentConsentPersistenceHandler(); - } else if (ConsentExtensionConstants.FUNDSCONFIRMATIONS.equalsIgnoreCase(type)) { - consentPersistenceHandler = new CofConsentPersistenceHandler(); - } - return consentPersistenceHandler; - - } - } From 60a3cf31b74d8d403afc3f384a38b025df5ab085 Mon Sep 17 00:00:00 2001 From: kalpana Date: Tue, 12 Mar 2024 22:28:48 +0530 Subject: [PATCH 07/19] Resolving the comments for Retrieval flow --- .../authorize/utils/ConsentRetrievalUtil.java | 4 +-- .../flow/ConsentExtensionDataProvider.java | 18 +++++++++++ .../flow/VRPConsentRetrievalStepTest.java | 32 +++++++++---------- .../utils/ConsentExtensionTestUtils.java | 2 +- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java index db7547b3..c07d34e3 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2023, 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. @@ -187,8 +187,6 @@ public static void populateDebtorAccount(JSONObject initiation, JSONArray consen jsonElementDebtor.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), debtorAccountArray); consentDataJSON.add(jsonElementDebtor); } - - } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentExtensionDataProvider.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentExtensionDataProvider.java index 7d1a739e..eb9488d9 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentExtensionDataProvider.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/ConsentExtensionDataProvider.java @@ -1,6 +1,24 @@ +/** + * 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.authorize.vrp.retrieval.flow; import org.testng.annotations.DataProvider; + /** * Data Provider for Consent Executor Tests. */ diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java index aefacc40..2d4a9639 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java @@ -1,13 +1,13 @@ /** * 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 @@ -273,19 +273,19 @@ public void testGetConsentDataSetForCOF() throws ConsentManagementException, Par Assert.assertNotNull(cofConsentData); } - @Test - public void testGetConsentDataSetForVRP() throws ConsentManagementException, ParseException { - - Mockito.doReturn(ConsentExtensionConstants.VRP).when(consentResourceMock).getConsentType(); - Mockito.doReturn(ConsentAuthorizeTestConstants.VRP_INITIATION).when(consentResourceMock) - .getReceipt(); - Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) - .getCurrentStatus(); - - JSONArray cofConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); - - Assert.assertNotNull(cofConsentData); - } +// @Test +// public void testGetConsentDataSetForVRP() { +// +// Mockito.doReturn(ConsentExtensionConstants.VRP).when(consentResourceMock).getConsentType(); +// Mockito.doReturn(ConsentAuthorizeTestConstants.VRP_INITIATION).when(consentResourceMock) +// .getReceipt(); +// Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) +// .getCurrentStatus(); +// +// JSONArray cofConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); +// +// Assert.assertNotNull(cofConsentData); +// } } 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 index 7b3c383c..ca8c06aa 100644 --- 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 @@ -22,7 +22,7 @@ import java.util.Map; /** - * comment. + * Utils class for consent executor tests. */ public class ConsentExtensionTestUtils { From 5f6d81b3661ee8ee53abe50dcaa44f36453392c1 Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 13 Mar 2024 09:35:02 +0530 Subject: [PATCH 08/19] Resolving the comments for Retrieval flow --- .../flow/VRPConsentRetrievalStepTest.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java index 2d4a9639..9424536f 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java @@ -273,19 +273,19 @@ public void testGetConsentDataSetForCOF() throws ConsentManagementException, Par Assert.assertNotNull(cofConsentData); } -// @Test -// public void testGetConsentDataSetForVRP() { -// -// Mockito.doReturn(ConsentExtensionConstants.VRP).when(consentResourceMock).getConsentType(); -// Mockito.doReturn(ConsentAuthorizeTestConstants.VRP_INITIATION).when(consentResourceMock) -// .getReceipt(); -// Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) -// .getCurrentStatus(); -// -// JSONArray cofConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); -// -// Assert.assertNotNull(cofConsentData); -// } + @Test + public void testGetConsentDataSetForVRP() { + + Mockito.doReturn(ConsentExtensionConstants.VRP).when(consentResourceMock).getConsentType(); + Mockito.doReturn(ConsentAuthorizeTestConstants.VRP_INITIATION).when(consentResourceMock) + .getReceipt(); + Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) + .getCurrentStatus(); + + JSONArray cofConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); + + Assert.assertNotNull(cofConsentData); + } } From 4800926959c1fffca214e48f076e24a10fcb6d01 Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 13 Mar 2024 12:21:29 +0530 Subject: [PATCH 09/19] Removal of Retrieval handler classes present in the Retrieval Flow --- .../AccountConsentRetrievalHandler.java | 139 ------------- .../retrieval/CofConsentRetrievalHandler.java | 149 ------------- .../retrieval/ConsentRetrievalHandler.java | 43 ---- .../PaymentConsentRetrievalHandler.java | 195 ------------------ 4 files changed, 526 deletions(-) delete mode 100644 open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/AccountConsentRetrievalHandler.java delete mode 100644 open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/CofConsentRetrievalHandler.java delete mode 100644 open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/ConsentRetrievalHandler.java delete mode 100644 open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/PaymentConsentRetrievalHandler.java diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/AccountConsentRetrievalHandler.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/AccountConsentRetrievalHandler.java deleted file mode 100644 index b73be77f..00000000 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/AccountConsentRetrievalHandler.java +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Copyright (c) 2023, 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.authorize.impl.handler.retrieval; - - -import com.wso2.openbanking.accelerator.common.util.ErrorConstants; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.utils.ConsentRetrievalUtil; -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.ResponseStatus; -import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; -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.logging.Log; -import org.apache.commons.logging.LogFactory; - - -/** - * Class to handle Account Consent data retrieval for Authorize. - */ -public class AccountConsentRetrievalHandler implements ConsentRetrievalHandler { - - private static final Log log = LogFactory.getLog(AccountConsentRetrievalHandler.class); - - /** - * Method defined to retrieve the Account consent related data in the authorization flow to send them to the. - * consent page to get PSU consent - * - * @param consentResource Consent Resource parameter containing consent related information retrieved from database - * @return consentDataJSON - * @throws ConsentException - */ - @Override - public JSONArray getConsentDataSet(ConsentResource consentResource) - throws ConsentException { - - try { - String receiptString = consentResource.getReceipt(); - Object receiptJSON = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(receiptString); - - //Checking whether the request body is in JSON format - if (!(receiptJSON instanceof JSONObject)) { - log.error(ErrorConstants.NOT_JSON_OBJECT_ERROR); - throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.NOT_JSON_OBJECT_ERROR); - } - - //Checking whether the consent status is valid - if (!consentResource.getCurrentStatus().equals(ConsentExtensionConstants.AWAITING_AUTH_STATUS)) { - log.error(ErrorConstants.STATE_INVALID_ERROR); - //Currently throwing error as 400 response. Developer also have the option of appending a field IS_ERROR - // to the jsonObject and showing it to the user in the webapp. If so, the IS_ERROR have to be checked in - // any later steps. - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.STATE_INVALID_ERROR); - } - - JSONArray consentDataJSON = new JSONArray(); - - JSONObject receipt = (JSONObject) receiptJSON; - - //Adding Permissions - JSONObject data = (JSONObject) receipt.get(ConsentExtensionConstants.DATA); - JSONArray permissions = (JSONArray) data.get(ConsentExtensionConstants.PERMISSIONS); - JSONObject jsonElementPermissions = new JSONObject(); - jsonElementPermissions.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.PERMISSIONS); - jsonElementPermissions.appendField(ConsentExtensionConstants.DATA_SIMPLE, permissions); - consentDataJSON.add(jsonElementPermissions); - - //Adding Expiration Date Time - if (data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE) != null) { - - if (!ConsentRetrievalUtil - .validateExpiryDateTime(data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE))) { - log.error(ErrorConstants.CONSENT_EXPIRED); - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONSENT_EXPIRED); - } - String expiry = data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE); - JSONArray expiryArray = new JSONArray(); - expiryArray.add(expiry); - - JSONObject jsonElementExpiry = new JSONObject(); - jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.EXPIRATION_DATE_TITLE); - jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); - consentDataJSON.add(jsonElementExpiry); - } - - //Adding Transaction From Date Time - if (data.getAsString(ConsentExtensionConstants.TRANSACTION_FROM_DATE) != null) { - String fromDateTime = data.getAsString(ConsentExtensionConstants.TRANSACTION_FROM_DATE); - JSONArray fromDateTimeArray = new JSONArray(); - fromDateTimeArray.add(fromDateTime); - - JSONObject jsonElementFromDateTime = new JSONObject(); - jsonElementFromDateTime.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.TRANSACTION_FROM_DATE_TITLE); - jsonElementFromDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, fromDateTimeArray); - consentDataJSON.add(jsonElementFromDateTime); - } - - //Adding Transaction To Date Time - if (data.getAsString(ConsentExtensionConstants.TRANSACTION_TO_DATE) != null) { - String toDateTime = data.getAsString(ConsentExtensionConstants.TRANSACTION_TO_DATE); - JSONArray toDateTimeArray = new JSONArray(); - toDateTimeArray.add(toDateTime); - - JSONObject jsonElementToDateTime = new JSONObject(); - jsonElementToDateTime.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.TRANSACTION_TO_DATE_TITLE); - jsonElementToDateTime.appendField(ConsentExtensionConstants.DATA_SIMPLE, toDateTimeArray); - consentDataJSON.add(jsonElementToDateTime); - } - - return consentDataJSON; - } catch (ParseException e) { - log.error("Exception occurred while getting consent data. Caused by: ", e); - throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.CONSENT_RETRIEVAL_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/authorize/impl/handler/retrieval/CofConsentRetrievalHandler.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/CofConsentRetrievalHandler.java deleted file mode 100644 index 73f1017c..00000000 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/CofConsentRetrievalHandler.java +++ /dev/null @@ -1,149 +0,0 @@ -/** - * Copyright (c) 2023, 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.authorize.impl.handler.retrieval; - -import com.wso2.openbanking.accelerator.common.util.ErrorConstants; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.utils.ConsentRetrievalUtil; -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.ResponseStatus; -import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; -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.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Class to handle Confirmation of Funds Consent data retrieval for Authorize. - */ -public class CofConsentRetrievalHandler implements ConsentRetrievalHandler { - - private static final Log log = LogFactory.getLog(CofConsentRetrievalHandler.class); - - /** - * Method defined to retrieve the COF consent related data in the authorization flow to send them to the - * consent page to get PSU consent. - * - * @param consentResource Consent Resource parameter containing consent related information retrieved from database - * @return - * @throws ConsentException - */ - @Override - public JSONArray getConsentDataSet(ConsentResource consentResource) throws ConsentException { - - try { - String receiptString = consentResource.getReceipt(); - Object receiptJSON = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(receiptString); - - //Checking whether the request body is in JSON format - if (!(receiptJSON instanceof JSONObject)) { - log.error(ErrorConstants.NOT_JSON_OBJECT_ERROR); - throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.NOT_JSON_OBJECT_ERROR); - } - - //Checking whether the consent status is valid - if (!consentResource.getCurrentStatus().equals(ConsentExtensionConstants.AWAITING_AUTH_STATUS) && - !consentResource.getCurrentStatus().equals(ConsentExtensionConstants.AUTHORIZED_STATUS)) { - log.error(ErrorConstants.STATE_INVALID_ERROR); - //Currently throwing error as 400 response. Developer also have the option of appending a field IS_ERROR - // to the jsonObject and showing it to the user in the webapp. If so, the IS_ERROR have to be checked in - // any later steps. - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.STATE_INVALID_ERROR); - } - - JSONArray consentDataJSON = new JSONArray(); - JSONObject receipt = (JSONObject) receiptJSON; - JSONObject data = (JSONObject) receipt.get(ConsentExtensionConstants.DATA); - - //Adding Expiration Date Time - if (data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE) != null) { - - if (!ConsentRetrievalUtil - .validateExpiryDateTime(data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE))) { - log.error(ErrorConstants.CONSENT_EXPIRED); - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONSENT_EXPIRED); - } - - String expiry = data.getAsString(ConsentExtensionConstants.EXPIRATION_DATE); - JSONArray expiryArray = new JSONArray(); - expiryArray.add(expiry); - - JSONObject jsonElementExpiry = new JSONObject(); - jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.EXPIRATION_DATE_TITLE); - jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); - consentDataJSON.add(jsonElementExpiry); - } else { - JSONArray expiryArray = new JSONArray(); - expiryArray.add(ConsentExtensionConstants.OPEN_ENDED_AUTHORIZATION); - - JSONObject jsonElementExpiry = new JSONObject(); - jsonElementExpiry.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.EXPIRATION_DATE_TITLE); - jsonElementExpiry.appendField(ConsentExtensionConstants.DATA_SIMPLE, expiryArray); - consentDataJSON.add(jsonElementExpiry); - } - - //Adding Debtor Account - if (data.get(ConsentExtensionConstants.DEBTOR_ACC) != null) { - JSONObject debtorAccount = (JSONObject) data.get(ConsentExtensionConstants.DEBTOR_ACC); - JSONArray debtorAccountArray = new JSONArray(); - //Adding Debtor Account Scheme Name - if (debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME) != null) { - debtorAccountArray.add(ConsentExtensionConstants.SCHEME_NAME_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.SCHEME_NAME)); - } - //Adding Debtor Account Identification - if (debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION) != null) { - debtorAccountArray.add(ConsentExtensionConstants.IDENTIFICATION_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.IDENTIFICATION)); - } - //Adding Debtor Account Name - if (debtorAccount.getAsString(ConsentExtensionConstants.NAME) != null) { - debtorAccountArray.add(ConsentExtensionConstants.NAME_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.NAME)); - } - //Adding Debtor Account Secondary Identification - if (debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION) != null) { - debtorAccountArray.add(ConsentExtensionConstants.SECONDARY_IDENTIFICATION_TITLE + " : " + - debtorAccount.getAsString(ConsentExtensionConstants.SECONDARY_IDENTIFICATION)); - } - - JSONObject jsonElementDebtor = new JSONObject(); - jsonElementDebtor.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.DEBTOR_ACC_TITLE); - jsonElementDebtor.appendField(ConsentExtensionConstants.DATA_SIMPLE, debtorAccountArray); - consentDataJSON.add(jsonElementDebtor); - } - - if (log.isDebugEnabled()) { - log.debug("Returned consent data to get the PSU consent " + consentDataJSON); - } - - return consentDataJSON; - } catch (ParseException e) { - log.error("Exception occurred while getting consent data. Caused by : ", e); - throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.CONSENT_RETRIEVAL_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/authorize/impl/handler/retrieval/ConsentRetrievalHandler.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/ConsentRetrievalHandler.java deleted file mode 100644 index 2cb0375b..00000000 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/ConsentRetrievalHandler.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright (c) 2023, 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.authorize.impl.handler.retrieval; - - -import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentException; -import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; -import net.minidev.json.JSONArray; - - -/** - * Interface to handle Consent data retrieval for Authorize. - */ -public interface ConsentRetrievalHandler { - - /** - * Abstract method defined to retrieve the consent related data in the authorization flow to send them to the - * consent page to get PSU consent. - * - * @param consentResource Consent Resource parameter containing consent related information retrieved from database - * @return - * @throws ConsentException - */ - JSONArray getConsentDataSet(ConsentResource consentResource) throws ConsentException; - -} diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/PaymentConsentRetrievalHandler.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/PaymentConsentRetrievalHandler.java deleted file mode 100644 index cff4e09e..00000000 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/impl/handler/retrieval/PaymentConsentRetrievalHandler.java +++ /dev/null @@ -1,195 +0,0 @@ -/** - * Copyright (c) 2023, 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.authorize.impl.handler.retrieval; - -import com.wso2.openbanking.accelerator.common.exception.ConsentManagementException; -import com.wso2.openbanking.accelerator.common.util.ErrorConstants; -import com.wso2.openbanking.accelerator.consent.extensions.authorize.utils.ConsentRetrievalUtil; -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.mgt.dao.models.ConsentResource; -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.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Class to handle Payment Consent data retrieval for Authorize. - */ -public class PaymentConsentRetrievalHandler implements ConsentRetrievalHandler { - - private static final Log log = LogFactory.getLog(PaymentConsentRetrievalHandler.class); - - - /** - * Method defined to retrieve the Payment consent related data in the authorization flow to send them to the. - * consent page to get PSU consent - * - * @param consentResource Consent Resource parameter containing consent related information retrieved from database - * @return - * @throws ConsentException - */ - @Override - public JSONArray getConsentDataSet(ConsentResource consentResource) { - - try { - String receiptString = consentResource.getReceipt(); - Object receiptJSON = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(receiptString); - - //Checking whether the request body is in JSON format - if (!(receiptJSON instanceof JSONObject)) { - log.error(ErrorConstants.NOT_JSON_OBJECT_ERROR); - throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.NOT_JSON_OBJECT_ERROR); - } - - //Checking whether the consent status is valid - if (!consentResource.getCurrentStatus().equals(ConsentExtensionConstants.AWAITING_AUTH_STATUS)) { - log.error(ErrorConstants.STATE_INVALID_ERROR); - //Currently throwing error as 400 response. Developer also have the option of appending a field IS_ERROR - // to the jsonObject and showing it to the user in the webapp. If so, the IS_ERROR have to be checked in - // any later steps. - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.STATE_INVALID_ERROR); - } - - JSONArray consentDataJSON = new JSONArray(); - JSONObject receipt = (JSONObject) receiptJSON; - JSONObject data = (JSONObject) receipt.get(ConsentExtensionConstants.DATA); - JSONObject initiation = (JSONObject) data.get(ConsentExtensionConstants.INITIATION); - - // Rejecting consent if cut off time is elapsed and the policy is REJECT - // Updating the consent status to "Reject" if the above condition is true - if (ConsentExtensionUtils.shouldSubmissionRequestBeRejected(ConsentExtensionUtils - .convertToISO8601(consentResource.getCreatedTime()))) { - boolean success = ConsentServiceUtil.getConsentService() - .revokeConsent(consentResource.getConsentID(), ConsentExtensionConstants.REJECTED_STATUS); - if (!success) { - log.error(ErrorConstants.AUTH_TOKEN_REVOKE_ERROR); - throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, - ErrorConstants.AUTH_TOKEN_REVOKE_ERROR); - } - log.error(ErrorConstants.AUTH_CUT_OFF_DATE_ELAPSED); - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.AUTH_CUT_OFF_DATE_ELAPSED); - } - - consentDataJSON = populateSinglePaymentData(initiation, consentDataJSON); - //Adding Debtor Account - ConsentRetrievalUtil.populateDebtorAccount(initiation, consentDataJSON); - - //Adding Creditor Account - ConsentRetrievalUtil.populateCreditorAccount(initiation, consentDataJSON); - - return consentDataJSON; - } catch (ParseException | ConsentManagementException e) { - throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.CONSENT_RETRIEVAL_ERROR); - } - } - - /** - * Populate Domestic and international Payment Details. - * - * @param data Initiation request from th request - * @param consentDataJSON Consent information - */ - private static JSONArray populateSinglePaymentData(JSONObject data, JSONArray consentDataJSON) { - - JSONArray paymentTypeArray = new JSONArray(); - JSONObject jsonElementPaymentType = new JSONObject(); - - if (data.containsKey(ConsentExtensionConstants.CURRENCY_OF_TRANSFER)) { - //For International Payments - //Adding Payment Type - paymentTypeArray.add(ConsentExtensionConstants.INTERNATIONAL_PAYMENTS); - - jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.PAYMENT_TYPE_TITLE); - jsonElementPaymentType.appendField(ConsentExtensionConstants.DATA_SIMPLE, paymentTypeArray); - consentDataJSON.add(jsonElementPaymentType); - - //Adding Currency Of Transfer - JSONArray currencyTransferArray = new JSONArray(); - currencyTransferArray.add(data.getAsString(ConsentExtensionConstants.CURRENCY_OF_TRANSFER)); - - JSONObject jsonElementCurTransfer = new JSONObject(); - jsonElementCurTransfer.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CURRENCY_OF_TRANSFER_TITLE); - jsonElementCurTransfer.appendField(ConsentExtensionConstants.DATA_SIMPLE, currencyTransferArray); - consentDataJSON.add(jsonElementCurTransfer); - } else { - //Adding Payment Type - paymentTypeArray.add(ConsentExtensionConstants.DOMESTIC_PAYMENTS); - - jsonElementPaymentType.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.PAYMENT_TYPE_TITLE); - jsonElementPaymentType.appendField(ConsentExtensionConstants.DATA_SIMPLE, paymentTypeArray); - consentDataJSON.add(jsonElementPaymentType); - } - - //Adding InstructionIdentification - JSONArray identificationArray = new JSONArray(); - identificationArray.add(data.getAsString(ConsentExtensionConstants.INSTRUCTION_IDENTIFICATION)); - - JSONObject jsonElementIdentification = new JSONObject(); - jsonElementIdentification.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.INSTRUCTION_IDENTIFICATION_TITLE); - jsonElementIdentification.appendField(ConsentExtensionConstants.DATA_SIMPLE, identificationArray); - consentDataJSON.add(jsonElementIdentification); - - //Adding EndToEndIdentification - JSONArray endToEndIdentificationArray = new JSONArray(); - endToEndIdentificationArray - .add(data.getAsString(ConsentExtensionConstants.END_TO_END_IDENTIFICATION)); - - JSONObject jsonElementEndToEndIdentification = new JSONObject(); - jsonElementEndToEndIdentification.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.END_TO_END_IDENTIFICATION_TITLE); - jsonElementEndToEndIdentification.appendField(ConsentExtensionConstants.DATA_SIMPLE, - endToEndIdentificationArray); - consentDataJSON.add(jsonElementEndToEndIdentification); - - //Adding InstructedAmount - JSONObject instructedAmount = (JSONObject) data.get(ConsentExtensionConstants.INSTRUCTED_AMOUNT); - JSONArray instructedAmountArray = new JSONArray(); - - if (instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT_TITLE) != null) { - instructedAmountArray.add(ConsentExtensionConstants.AMOUNT_TITLE + " : " + - instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT)); - } - - if (instructedAmount.getAsString(ConsentExtensionConstants.CURRENCY) != null) { - instructedAmountArray.add(ConsentExtensionConstants.CURRENCY_TITLE + " : " + - instructedAmount.getAsString(ConsentExtensionConstants.CURRENCY)); - } - - JSONObject jsonElementInstructedAmount = new JSONObject(); - jsonElementInstructedAmount.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.INSTRUCTED_AMOUNT_TITLE); - jsonElementInstructedAmount.appendField(ConsentExtensionConstants.DATA_SIMPLE, instructedAmountArray); - consentDataJSON.add(jsonElementInstructedAmount); - - return consentDataJSON; - } - -} - From 01f2f082523407a64722c7ecaf927ccfcbb2d15f Mon Sep 17 00:00:00 2001 From: kalpana Date: Thu, 14 Mar 2024 09:13:16 +0530 Subject: [PATCH 10/19] Resolving the comments --- .../authorize/utils/ConsentRetrievalUtil.java | 56 +++++++++---------- .../AcceleratorConsentExtensionFactory.java | 24 ++++++++ 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java index c07d34e3..7d8c67ed 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java @@ -58,7 +58,7 @@ public static String extractRequestObject(String spQueryParams) { for (String param : spQueries) { if (param.contains("request=")) { requestObject = (param.substring("request=".length())).replaceAll( - "\\r\\n|\\r|\\n|%20", ""); + "\\r\\n|\\r|\\n|\\%20", ""); } } if (requestObject != null) { @@ -261,10 +261,10 @@ public static JSONArray appendDummyAccountID() { * this method also invokes the relevant methods to populate data for each flow. * * @param consentResource Consent Resource parameter containing consent related information retrieved - * from database + * from database. * @return ConsentDataJson array */ - public static JSONArray getConsentData(ConsentResource consentResource) throws ConsentException { + public static JSONArray getConsentData(ConsentResource consentResource) throws ConsentException { JSONArray consentDataJSON = new JSONArray(); try { @@ -314,17 +314,14 @@ public static JSONArray getConsentData(ConsentResource consentResource) throws log.error(ErrorConstants.DATA_OBJECT_MISSING_ERROR); throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.DATA_OBJECT_MISSING_ERROR); } - } catch (ParseException e) { log.error(ErrorConstants.CONSENT_RETRIEVAL_ERROR, e); throw new ConsentException(ResponseStatus.INTERNAL_SERVER_ERROR, ErrorConstants.CONSENT_RETRIEVAL_ERROR); } - return consentDataJSON; } - /** * Populate Domestic and international Payment Details. * @@ -544,7 +541,11 @@ private static void populateCofData(JSONObject initiation, JSONArray consentData */ private static void populateVRPData(JSONObject data, JSONArray consentDataJSON) { - if (data.containsKey(ConsentExtensionConstants.CONTROL_PARAMETERS)) { + if (!data.containsKey(ConsentExtensionConstants.CONTROL_PARAMETERS)) { + log.error(ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, + ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); + } else { JSONObject controlParameters = (JSONObject) data. get(ConsentExtensionConstants.CONTROL_PARAMETERS); @@ -591,27 +592,27 @@ private static void populateVRPData(JSONObject data, JSONArray consentDataJSON) Object maxAmount = controlParameters.get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT); - if (maxAmount instanceof JSONObject) { - JSONObject jsonElementControlParameter = new JSONObject(); - jsonElementControlParameter.appendField(ConsentExtensionConstants.TITLE, - ConsentExtensionConstants.CONTROL_PARAMETER_MAX_INDIVIDUAL_AMOUNT_TITLE); - JSONArray controlParameterArray = new JSONArray(); + if (maxAmount instanceof JSONObject) { + JSONObject jsonElementControlParameter = new JSONObject(); + jsonElementControlParameter.appendField(ConsentExtensionConstants.TITLE, + ConsentExtensionConstants.CONTROL_PARAMETER_MAX_INDIVIDUAL_AMOUNT_TITLE); + JSONArray controlParameterArray = new JSONArray(); - JSONObject maximumIndividualAmount = (JSONObject) maxAmount; + JSONObject maximumIndividualAmount = (JSONObject) maxAmount; - String formattedAmount = String.format("%s %s", - maximumIndividualAmount.getAsString(ConsentExtensionConstants.CURRENCY), - maximumIndividualAmount.getAsString(ConsentExtensionConstants.AMOUNT)); - controlParameterArray.add(formattedAmount); - jsonElementControlParameter.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), - controlParameterArray); + String formattedAmount = String.format("%s %s", + maximumIndividualAmount.getAsString(ConsentExtensionConstants.CURRENCY), + maximumIndividualAmount.getAsString(ConsentExtensionConstants.AMOUNT)); + controlParameterArray.add(formattedAmount); + jsonElementControlParameter.appendField(StringUtils.lowerCase(ConsentExtensionConstants.DATA), + controlParameterArray); - consentDataJSON.add(jsonElementControlParameter); - } else { - log.error(ErrorConstants.MAX_AMOUNT_NOT_JSON_OBJECT_ERROR); - throw new ConsentException(ResponseStatus.BAD_REQUEST, - ErrorConstants.MAX_AMOUNT_NOT_JSON_OBJECT_ERROR); - } + consentDataJSON.add(jsonElementControlParameter); + } else { + log.error(ErrorConstants.MAX_AMOUNT_NOT_JSON_OBJECT_ERROR); + throw new ConsentException(ResponseStatus.BAD_REQUEST, + ErrorConstants.MAX_AMOUNT_NOT_JSON_OBJECT_ERROR); + } Object periodicLimit = controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); @@ -698,11 +699,6 @@ private static void populateVRPData(JSONObject data, JSONArray consentDataJSON) log.error(ErrorConstants.NOT_JSON_ARRAY_ERROR); throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.NOT_JSON_ARRAY_ERROR); } - } else { - log.error(ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); - throw new ConsentException(ResponseStatus.BAD_REQUEST, ErrorConstants.CONTROL_PARAMETERS_MISSING_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/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 8d8672c9..7ca7e3a5 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 @@ -17,6 +17,10 @@ */ package com.wso2.openbanking.accelerator.consent.extensions.common.factory; +import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.persist.AccountConsentPersistenceHandler; +import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.persist.CofConsentPersistenceHandler; +import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.persist.ConsentPersistenceHandler; +import com.wso2.openbanking.accelerator.consent.extensions.authorize.impl.handler.persist.PaymentConsentPersistenceHandler; import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionConstants; import com.wso2.openbanking.accelerator.consent.extensions.manage.impl.AccountConsentManageRequestHandler; import com.wso2.openbanking.accelerator.consent.extensions.manage.impl.CofConsentRequestHandler; @@ -57,4 +61,24 @@ public static ConsentManageRequestHandler getConsentManageRequestValidator(Strin return consentManageRequestHandler; } + + /** + * Method to get the Consent Persistence Handler. + * + * @param type Type of the request + * @return ConsentPersistenceHandler + */ + public static ConsentPersistenceHandler getConsentPersistenceHandler(String type) { + ConsentPersistenceHandler consentPersistenceHandler = null; + + if (ConsentExtensionConstants.ACCOUNTS.equalsIgnoreCase(type)) { + consentPersistenceHandler = new AccountConsentPersistenceHandler(); + } else if (ConsentExtensionConstants.PAYMENTS.equalsIgnoreCase(type)) { + consentPersistenceHandler = new PaymentConsentPersistenceHandler(); + } else if (ConsentExtensionConstants.FUNDSCONFIRMATIONS.equalsIgnoreCase(type)) { + consentPersistenceHandler = new CofConsentPersistenceHandler(); + } + return consentPersistenceHandler; + + } } From 02c291de93a1343aaec0c580675f0b4c8c0a9248 Mon Sep 17 00:00:00 2001 From: kalpana Date: Thu, 14 Mar 2024 09:27:24 +0530 Subject: [PATCH 11/19] Resolving the comments --- .../vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java index c929e07c..3c441a6d 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java @@ -1,13 +1,13 @@ /** * 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 @@ -92,10 +92,7 @@ public void initClass() { consentResourceMock = mock(ConsentResource.class); authorizationResourceMock = mock(AuthorizationResource.class); consentCoreServiceMock = mock(ConsentCoreServiceImpl.class); - - configMap = new HashMap<>(); - authResources = new ArrayList(); } From 4e698572deefe80a2c673a341dcb15d5de2856eb Mon Sep 17 00:00:00 2001 From: kalpana Date: Thu, 14 Mar 2024 11:06:45 +0530 Subject: [PATCH 12/19] Resolving the comments --- .../flow/VRPConsentRetrievalUtilTest.java | 31 ++++--------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java index 3c441a6d..cbceea24 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java @@ -229,7 +229,7 @@ public void testGetConsentDataSetForPayments(String paymentReceipt) { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray paymentConsentData = consentRetrievalUtil.getConsentData(consentResourceMock); + JSONArray paymentConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(paymentConsentData); } @@ -303,11 +303,7 @@ public void testGetConsentDataSetForVRPDataParameter() { JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(vrpConsentData); - - - Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); - String errorMsg = ConsentExtensionConstants.IS_ERROR; - Assert.assertTrue(errorMsg.contains(ErrorConstants.NOT_JSON_PAYLOAD)); + Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.DATA_OBJECT_MISSING_ERROR); } @Test(expectedExceptions = ConsentException.class) @@ -322,11 +318,7 @@ public void testGetConsentDataSetForVRPDataWithoutControlParameters() { JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(vrpConsentData); - - - Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); - String errorMsg = ConsentExtensionConstants.IS_ERROR; - Assert.assertTrue(errorMsg.contains(ErrorConstants.NOT_JSON_PAYLOAD)); + Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); } @Test(expectedExceptions = ConsentException.class) @@ -340,10 +332,7 @@ public void testGetConsentDataSetForAccount() { JSONArray accountConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(accountConsentData); - - Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); - String errorMsg = ConsentExtensionConstants.IS_ERROR; - Assert.assertTrue(errorMsg.contains(ErrorConstants.CONSENT_EXPIRED)); + Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.CONSENT_EXPIRED); } @Test(expectedExceptions = ConsentException.class) @@ -358,12 +347,9 @@ public void testGetConsentDataSetForCOFs() { JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(cofConsentData); - Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); - String errorMsg = ConsentExtensionConstants.IS_ERROR; - Assert.assertTrue(errorMsg.contains(ErrorConstants.CONSENT_EXPIRED)); + Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.CONSENT_EXPIRED); } - @Test(expectedExceptions = ConsentException.class) public void testGetConsentDataSetForCOFNull() { @@ -376,11 +362,6 @@ public void testGetConsentDataSetForCOFNull() { JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(cofConsentData); - Assert.assertNotNull(ConsentExtensionConstants.IS_ERROR); - String errorMsg = ConsentExtensionConstants.IS_ERROR; - Assert.assertTrue(errorMsg.contains(ErrorConstants.CONSENT_EXPIRED)); + Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.CONSENT_EXPIRED); } - } - - From 77827810ca2b39152316176ac2acd338d6df23bf Mon Sep 17 00:00:00 2001 From: kalpana Date: Fri, 15 Mar 2024 15:25:24 +0530 Subject: [PATCH 13/19] Resolving the comments for the unit test --- .../flow/VRPConsentRetrievalUtilTest.java | 135 +++++++++++++----- 1 file changed, 100 insertions(+), 35 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java index cbceea24..c6196cf1 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java @@ -28,12 +28,15 @@ 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.manage.validator.VRPConsentRequestValidator; +import com.wso2.openbanking.accelerator.consent.extensions.manage.vrp.VRPTestConstants; import com.wso2.openbanking.accelerator.consent.extensions.utils.ConsentExtensionTestUtils; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.AuthorizationResource; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl; import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; +import net.minidev.json.JSONValue; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; @@ -49,6 +52,7 @@ import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -57,6 +61,8 @@ import static org.mockito.Mockito.mock; import static org.powermock.api.mockito.PowerMockito.doReturn; import static org.powermock.api.mockito.PowerMockito.when; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; /** * Test class for consentRetrievalUtil. @@ -130,7 +136,7 @@ public void testGetConsentDataSetForNonRegulatory() { JSONObject jsonObject = new JSONObject(); Mockito.doReturn(false).when(consentDataMock).isRegulatory(); defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); - Assert.assertTrue(jsonObject.isEmpty()); + assertTrue(jsonObject.isEmpty()); } @Test @@ -169,7 +175,7 @@ public void testConsentRetrievalWithInvalidRequestObject() { Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); String errorMsg = (String) jsonObject.get(ConsentExtensionConstants.IS_ERROR); - Assert.assertTrue(errorMsg.contains(ErrorConstants.NOT_JSON_PAYLOAD)); + assertTrue(errorMsg.contains(ErrorConstants.NOT_JSON_PAYLOAD)); } @Test @@ -202,7 +208,6 @@ public void testConsentRetrievalWithValidRequestObject() throws ConsentManagemen @Test public void testGetConsentDataSetForAccounts() { - Mockito.doReturn(ConsentExtensionConstants.ACCOUNTS).when(consentResourceMock).getConsentType(); Mockito.doReturn(ConsentAuthorizeTestConstants.VALID_INITIATION_OBJECT).when(consentResourceMock) .getReceipt(); @@ -256,10 +261,8 @@ public void testGetConsentDataSetForVRPData() { .getReceipt(); Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - - JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - - Assert.assertNotNull(vrpConsentData); + JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(vrpConsentData); } @Test @@ -269,26 +272,36 @@ public void testAppendDummyAccountID() { Assert.assertNotNull(result); - Assert.assertEquals(result.size(), 2); + assertEquals(result.size(), 2); JSONObject accountOne = (JSONObject) result.get(0); - Assert.assertEquals(accountOne.get("account_id"), "12345"); - Assert.assertEquals(accountOne.get("display_name"), "Salary Saver Account"); + assertEquals(accountOne.get("account_id"), "12345"); + assertEquals(accountOne.get("display_name"), "Salary Saver Account"); JSONObject accountTwo = (JSONObject) result.get(1); - Assert.assertEquals(accountTwo.get("account_id"), "67890"); - Assert.assertEquals(accountTwo.get("display_name"), "Max Bonus Account"); + assertEquals(accountTwo.get("account_id"), "67890"); + assertEquals(accountTwo.get("display_name"), "Max Bonus Account"); } @Test(expectedExceptions = ConsentException.class) public void testConsentDataWithInvalidJson() { - // Arrange String invalidJsonString = "Invalid JSON String"; Mockito.when(consentResourceMock.getReceipt()).thenReturn(invalidJsonString); - // Act - JSONArray consentDataJSON = ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(consentDataJSON); + try { + JSONArray consentDataJSON = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(consentDataJSON); + } catch (ConsentException e) { + // Parse the error message from the exception + JSONObject errorPayload = e.getPayload(); + JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); + JSONObject firstError = (JSONObject) errorsArray.get(0); + String errorMessage = (String) firstError.get("Message"); + + // Verify the error message + Assert.assertEquals(errorMessage, "Payload is not a JSON object"); + throw e; + } } @Test(expectedExceptions = ConsentException.class) @@ -300,10 +313,20 @@ public void testGetConsentDataSetForVRPDataParameter() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - - Assert.assertNotNull(vrpConsentData); - Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.DATA_OBJECT_MISSING_ERROR); + try { + JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(vrpConsentData); + } catch (ConsentException e) { + // Parse the error message from the exception + JSONObject errorPayload = e.getPayload(); + JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); + JSONObject firstError = (JSONObject) errorsArray.get(0); + String errorMessage = (String) firstError.get("Message"); + + // Verify the error message + Assert.assertEquals(errorMessage, "Missing mandatory parameter the Data"); + throw e; + } } @Test(expectedExceptions = ConsentException.class) @@ -315,10 +338,20 @@ public void testGetConsentDataSetForVRPDataWithoutControlParameters() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - - Assert.assertNotNull(vrpConsentData); - Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.CONTROL_PARAMETERS_MISSING_ERROR); + try { + JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(vrpConsentData); + } catch (ConsentException e) { + // Parse the error message from the exception + JSONObject errorPayload = e.getPayload(); + JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); + JSONObject firstError = (JSONObject) errorsArray.get(0); + String errorMessage = (String) firstError.get("Message"); + + // Verify the error message + Assert.assertEquals(errorMessage, "Missing mandatory parameter the ControlParameters"); + throw e; + } } @Test(expectedExceptions = ConsentException.class) @@ -330,9 +363,20 @@ public void testGetConsentDataSetForAccount() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray accountConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(accountConsentData); - Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.CONSENT_EXPIRED); + try { + JSONArray accountConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(accountConsentData); + } catch (ConsentException e) { + // Parse the error message from the exception + JSONObject errorPayload = e.getPayload(); + JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); + JSONObject firstError = (JSONObject) errorsArray.get(0); + String errorMessage = (String) firstError.get("Message"); + + // Verify the error message + Assert.assertEquals(errorMessage, "Provided consent is expired"); + throw e; + } } @Test(expectedExceptions = ConsentException.class) @@ -344,10 +388,20 @@ public void testGetConsentDataSetForCOFs() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - - Assert.assertNotNull(cofConsentData); - Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.CONSENT_EXPIRED); + try { + JSONArray cofConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(cofConsentData); + } catch (ConsentException e) { + // Parse the error message from the exception + JSONObject errorPayload = e.getPayload(); + JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); + JSONObject firstError = (JSONObject) errorsArray.get(0); + String errorMessage = (String) firstError.get("Message"); + + // Verify the error message + Assert.assertEquals(errorMessage, "Provided consent is expired"); + throw e; + } } @Test(expectedExceptions = ConsentException.class) @@ -359,9 +413,20 @@ public void testGetConsentDataSetForCOFNull() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - - Assert.assertNotNull(cofConsentData); - Assert.assertEquals(ConsentExtensionConstants.IS_ERROR, ErrorConstants.CONSENT_EXPIRED); + try { + JSONArray cofConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(cofConsentData); + } catch (ConsentException e) { + // Parse the error message from the exception + JSONObject errorPayload = e.getPayload(); + JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); + JSONObject firstError = (JSONObject) errorsArray.get(0); + String errorMessage = (String) firstError.get("Message"); + + // Verify the error message + Assert.assertEquals(errorMessage, "Error occurred while retrieving the account initiation" + + " request details"); + throw e; + } } } From 460b4c07cb45dfed1d482ffab7c1574b27ce7602 Mon Sep 17 00:00:00 2001 From: kalpana Date: Mon, 18 Mar 2024 15:48:09 +0530 Subject: [PATCH 14/19] Resolving the comments for the unit test --- .../authservlet/impl/util/Utils.java | 10 +- .../flow/VRPConsentRetrievalStepTest.java | 16 +-- .../flow/VRPConsentRetrievalUtilTest.java | 100 +----------------- 3 files changed, 10 insertions(+), 116 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java index b818c1f7..d1d66edd 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/util/Utils.java @@ -262,12 +262,12 @@ public static Map populateVRPDataRetrieval(HttpServletRequest re String selectedAccount = null; Map returnMaps = new HashMap<>(); - //Sets "data_requested" that contains the human-readable scope-requested information - JSONArray dataRequestedJsonArray = dataSet.getJSONArray(ConsentExtensionConstants.CONSENT_DATA); + // Populates "consentDataArray" with the scope information in a readable format + JSONArray consentDataArray = dataSet.getJSONArray(ConsentExtensionConstants.CONSENT_DATA); Map> dataRequested = new LinkedHashMap<>(); - for (int requestedDataIndex = 0; requestedDataIndex < dataRequestedJsonArray.length(); requestedDataIndex++) { - JSONObject dataObj = dataRequestedJsonArray.getJSONObject(requestedDataIndex); + for (int requestedDataIndex = 0; requestedDataIndex < consentDataArray.length(); requestedDataIndex++) { + JSONObject dataObj = consentDataArray.getJSONObject(requestedDataIndex); String title = dataObj.getString(ConsentExtensionConstants.TITLE); JSONArray dataArray = dataObj.getJSONArray(StringUtils.lowerCase(ConsentExtensionConstants.DATA)); @@ -281,7 +281,7 @@ public static Map populateVRPDataRetrieval(HttpServletRequest re //Assigning value of the "Debtor Account" key in the map to the variable "selectedAccount". if (dataRequested.containsKey("Debtor Account")) { - selectedAccount = getDebtorAccFromConsentData(dataRequestedJsonArray); + selectedAccount = getDebtorAccFromConsentData(consentDataArray); } else { // add accounts list request.setAttribute(ConsentExtensionConstants.ACCOUNT_DATA, addAccList(dataSet)); diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java index 9424536f..7a0a0307 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java @@ -90,8 +90,6 @@ public void initClass() { configMap = new HashMap<>(); openBankingConfigParserMock = Mockito.mock(OpenBankingConfigParser.class); - - authResources = new ArrayList(); } @@ -195,12 +193,11 @@ public void testConsentRetrievalWithValidRequestObject() throws ConsentManagemen PowerMockito.when(ConsentServiceUtil.getConsentService()).thenReturn(consentCoreServiceMock); defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); - Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); } @Test - public void testGetConsentDataSetForAccounts() throws ConsentManagementException, ParseException { + public void testGetConsentDataSetForAccounts() { Mockito.doReturn(ConsentExtensionConstants.ACCOUNTS).when(consentResourceMock).getConsentType(); Mockito.doReturn(ConsentAuthorizeTestConstants.VALID_INITIATION_OBJECT).when(consentResourceMock) @@ -209,7 +206,6 @@ public void testGetConsentDataSetForAccounts() throws ConsentManagementException .getCurrentStatus(); JSONArray accountConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); - Assert.assertNotNull(accountConsentData); } @@ -232,12 +228,11 @@ public void testGetConsentDataSetForPayments(String paymentReceipt) throws Conse .getCurrentStatus(); JSONArray paymentConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); - Assert.assertNotNull(paymentConsentData); } @Test - public void testGetConsentDataSetForFilePayments() throws ConsentManagementException, ParseException { + public void testGetConsentDataSetForFilePayments() { Mockito.doReturn(configMap).when(openBankingConfigParserMock).getConfiguration(); @@ -254,13 +249,12 @@ public void testGetConsentDataSetForFilePayments() throws ConsentManagementExcep .getCurrentStatus(); JSONArray paymentConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); - Assert.assertNotNull(paymentConsentData); } @Test - public void testGetConsentDataSetForCOF() throws ConsentManagementException, ParseException { + public void testGetConsentDataSetForCOF(){ Mockito.doReturn(ConsentExtensionConstants.FUNDSCONFIRMATIONS).when(consentResourceMock).getConsentType(); Mockito.doReturn(ConsentAuthorizeTestConstants.COF_RECEIPT).when(consentResourceMock) @@ -269,7 +263,6 @@ public void testGetConsentDataSetForCOF() throws ConsentManagementException, Par .getCurrentStatus(); JSONArray cofConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); - Assert.assertNotNull(cofConsentData); } @@ -283,9 +276,6 @@ public void testGetConsentDataSetForVRP() { .getCurrentStatus(); JSONArray cofConsentData = defaultConsentRetrievalStep.getConsentDataSet(consentResourceMock); - Assert.assertNotNull(cofConsentData); } - - } diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java index c6196cf1..dd0ac7e4 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java @@ -202,7 +202,6 @@ public void testConsentRetrievalWithValidRequestObject() throws ConsentManagemen PowerMockito.when(ConsentServiceUtil.getConsentService()).thenReturn(consentCoreServiceMock); defaultConsentRetrievalStep.execute(consentDataMock, jsonObject); - Assert.assertNotNull(jsonObject.get(ConsentExtensionConstants.IS_ERROR)); } @@ -215,7 +214,6 @@ public void testGetConsentDataSetForAccounts() { .getCurrentStatus(); JSONArray accountConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(accountConsentData); } @@ -235,7 +233,6 @@ public void testGetConsentDataSetForPayments(String paymentReceipt) { .getCurrentStatus(); JSONArray paymentConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(paymentConsentData); } @@ -249,7 +246,6 @@ public void testGetConsentDataSetForCOF() { .getCurrentStatus(); JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(cofConsentData); } @@ -265,43 +261,13 @@ public void testGetConsentDataSetForVRPData() { Assert.assertNotNull(vrpConsentData); } - @Test - public void testAppendDummyAccountID() { - - JSONArray result = ConsentRetrievalUtil.appendDummyAccountID(); - - Assert.assertNotNull(result); - - assertEquals(result.size(), 2); - - JSONObject accountOne = (JSONObject) result.get(0); - assertEquals(accountOne.get("account_id"), "12345"); - assertEquals(accountOne.get("display_name"), "Salary Saver Account"); - - JSONObject accountTwo = (JSONObject) result.get(1); - assertEquals(accountTwo.get("account_id"), "67890"); - assertEquals(accountTwo.get("display_name"), "Max Bonus Account"); - } - @Test(expectedExceptions = ConsentException.class) public void testConsentDataWithInvalidJson() { String invalidJsonString = "Invalid JSON String"; Mockito.when(consentResourceMock.getReceipt()).thenReturn(invalidJsonString); - try { JSONArray consentDataJSON = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(consentDataJSON); - } catch (ConsentException e) { - // Parse the error message from the exception - JSONObject errorPayload = e.getPayload(); - JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); - JSONObject firstError = (JSONObject) errorsArray.get(0); - String errorMessage = (String) firstError.get("Message"); - - // Verify the error message - Assert.assertEquals(errorMessage, "Payload is not a JSON object"); - throw e; - } } @Test(expectedExceptions = ConsentException.class) @@ -312,21 +278,8 @@ public void testGetConsentDataSetForVRPDataParameter() { .getReceipt(); Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - - try { JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(vrpConsentData); - } catch (ConsentException e) { - // Parse the error message from the exception - JSONObject errorPayload = e.getPayload(); - JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); - JSONObject firstError = (JSONObject) errorsArray.get(0); - String errorMessage = (String) firstError.get("Message"); - - // Verify the error message - Assert.assertEquals(errorMessage, "Missing mandatory parameter the Data"); - throw e; - } } @Test(expectedExceptions = ConsentException.class) @@ -338,20 +291,8 @@ public void testGetConsentDataSetForVRPDataWithoutControlParameters() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - try { JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(vrpConsentData); - } catch (ConsentException e) { - // Parse the error message from the exception - JSONObject errorPayload = e.getPayload(); - JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); - JSONObject firstError = (JSONObject) errorsArray.get(0); - String errorMessage = (String) firstError.get("Message"); - - // Verify the error message - Assert.assertEquals(errorMessage, "Missing mandatory parameter the ControlParameters"); - throw e; - } } @Test(expectedExceptions = ConsentException.class) @@ -363,20 +304,8 @@ public void testGetConsentDataSetForAccount() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - try { JSONArray accountConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(accountConsentData); - } catch (ConsentException e) { - // Parse the error message from the exception - JSONObject errorPayload = e.getPayload(); - JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); - JSONObject firstError = (JSONObject) errorsArray.get(0); - String errorMessage = (String) firstError.get("Message"); - - // Verify the error message - Assert.assertEquals(errorMessage, "Provided consent is expired"); - throw e; - } } @Test(expectedExceptions = ConsentException.class) @@ -388,20 +317,8 @@ public void testGetConsentDataSetForCOFs() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - try { JSONArray cofConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(cofConsentData); - } catch (ConsentException e) { - // Parse the error message from the exception - JSONObject errorPayload = e.getPayload(); - JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); - JSONObject firstError = (JSONObject) errorsArray.get(0); - String errorMessage = (String) firstError.get("Message"); - - // Verify the error message - Assert.assertEquals(errorMessage, "Provided consent is expired"); - throw e; - } } @Test(expectedExceptions = ConsentException.class) @@ -413,20 +330,7 @@ public void testGetConsentDataSetForCOFNull() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - try { - JSONArray cofConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(cofConsentData); - } catch (ConsentException e) { - // Parse the error message from the exception - JSONObject errorPayload = e.getPayload(); - JSONArray errorsArray = (JSONArray) errorPayload.get("Errors"); - JSONObject firstError = (JSONObject) errorsArray.get(0); - String errorMessage = (String) firstError.get("Message"); - - // Verify the error message - Assert.assertEquals(errorMessage, "Error occurred while retrieving the account initiation" + - " request details"); - throw e; - } + JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(cofConsentData); } } From 20e081720f3eeb4d1118d795f7fcd524806e5093 Mon Sep 17 00:00:00 2001 From: kalpana Date: Tue, 19 Mar 2024 11:51:05 +0530 Subject: [PATCH 15/19] Resolving the comments for the unit test --- .../extensions/authorize/utils/ConsentRetrievalUtil.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java index 7d8c67ed..0d5af23b 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/utils/ConsentRetrievalUtil.java @@ -279,7 +279,7 @@ public static JSONArray getConsentData(ConsentResource consentResource) throws C ErrorConstants.NOT_JSON_OBJECT_ERROR); } - if (!consentResource.getCurrentStatus().equals(ConsentExtensionConstants.AWAITING_AUTH_STATUS)) { + if (!ConsentExtensionConstants.AWAITING_AUTH_STATUS.equals(consentResource.getCurrentStatus())) { log.error(ErrorConstants.STATE_INVALID_ERROR); // Currently throwing an error as a 400 response. // Developers have the option of appending a field IS_ERROR to the jsonObject @@ -395,7 +395,6 @@ private static void populatePaymentData(JSONObject data, JSONArray consentDataJS JSONObject instructedAmount = (JSONObject) initiation.get(ConsentExtensionConstants.INSTRUCTED_AMOUNT); JSONArray instructedAmountArray = new JSONArray(); - if (instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT_TITLE) != null) { instructedAmountArray.add(ConsentExtensionConstants.AMOUNT_TITLE + " : " + instructedAmount.getAsString(ConsentExtensionConstants.AMOUNT)); @@ -419,7 +418,6 @@ private static void populatePaymentData(JSONObject data, JSONArray consentDataJS populateCreditorAccount(initiation, consentDataJSON); } - } /** @@ -486,7 +484,6 @@ private static void populateAccountData(JSONObject data, JSONArray consentDataJS toDateTimeArray); consentDataJSON.add(jsonElementToDateTime); } - } /** @@ -532,7 +529,6 @@ private static void populateCofData(JSONObject initiation, JSONArray consentData } } - /** * Populate VRP Details. * From f8affecc149ac999303f6d0775d1cb4e4501b506 Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 20 Mar 2024 10:11:33 +0530 Subject: [PATCH 16/19] Resolving the comments for the unit test --- .../flow/VRPConsentRetrievalUtilTest.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java index dd0ac7e4..65b09f5c 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java @@ -257,8 +257,8 @@ public void testGetConsentDataSetForVRPData() { .getReceipt(); Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(vrpConsentData); + JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(vrpConsentData); } @Test(expectedExceptions = ConsentException.class) @@ -266,8 +266,8 @@ public void testConsentDataWithInvalidJson() { String invalidJsonString = "Invalid JSON String"; Mockito.when(consentResourceMock.getReceipt()).thenReturn(invalidJsonString); - JSONArray consentDataJSON = ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(consentDataJSON); + JSONArray consentDataJSON = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(consentDataJSON); } @Test(expectedExceptions = ConsentException.class) @@ -278,8 +278,8 @@ public void testGetConsentDataSetForVRPDataParameter() { .getReceipt(); Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(vrpConsentData); + JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(vrpConsentData); } @Test(expectedExceptions = ConsentException.class) @@ -291,8 +291,8 @@ public void testGetConsentDataSetForVRPDataWithoutControlParameters() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(vrpConsentData); + JSONArray vrpConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(vrpConsentData); } @Test(expectedExceptions = ConsentException.class) @@ -304,8 +304,8 @@ public void testGetConsentDataSetForAccount() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray accountConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(accountConsentData); + JSONArray accountConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(accountConsentData); } @Test(expectedExceptions = ConsentException.class) @@ -317,8 +317,8 @@ public void testGetConsentDataSetForCOFs() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray cofConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); - Assert.assertNotNull(cofConsentData); + JSONArray cofConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); + Assert.assertNotNull(cofConsentData); } @Test(expectedExceptions = ConsentException.class) From 38efb6c35e8e85c910931190bcafa5f3ccd3c83e Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 20 Mar 2024 14:40:18 +0530 Subject: [PATCH 17/19] Resolving the comments --- .../extensions/authservlet/impl/OBDefaultAuthServletImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java index 54ea3034..6e33d788 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/authservlet/impl/OBDefaultAuthServletImpl.java @@ -92,6 +92,5 @@ public String getJSPPath() { } else { return "/default_displayconsent.jsp"; } - } } From e3d9f0c69e4dd09ef5c4d2c092500e36caab9d71 Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 20 Mar 2024 15:05:23 +0530 Subject: [PATCH 18/19] Committing the testng file --- .../src/test/resources/testng.xml | 3 --- 1 file changed, 3 deletions(-) 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 c0d30881..8a2c69a3 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 @@ -26,12 +26,9 @@ -<<<<<<<<< Temporary merge branch 1 -========= ->>>>>>>>> Temporary merge branch 2 From 013455d2b96b4e4cb4c0fcfe063df119ea828ad9 Mon Sep 17 00:00:00 2001 From: kalpana Date: Wed, 20 Mar 2024 16:53:06 +0530 Subject: [PATCH 19/19] commit --- .../vrp/retrieval/flow/VRPConsentRetrievalStepTest.java | 2 +- .../vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java index 7a0a0307..7637538c 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalStepTest.java @@ -254,7 +254,7 @@ public void testGetConsentDataSetForFilePayments() { @Test - public void testGetConsentDataSetForCOF(){ + public void testGetConsentDataSetForCOF() { Mockito.doReturn(ConsentExtensionConstants.FUNDSCONFIRMATIONS).when(consentResourceMock).getConsentType(); Mockito.doReturn(ConsentAuthorizeTestConstants.COF_RECEIPT).when(consentResourceMock) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java index 65b09f5c..f17e088d 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/test/java/com/wso2/openbanking/accelerator/consent/extensions/authorize/vrp/retrieval/flow/VRPConsentRetrievalUtilTest.java @@ -28,15 +28,12 @@ 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.manage.validator.VRPConsentRequestValidator; -import com.wso2.openbanking.accelerator.consent.extensions.manage.vrp.VRPTestConstants; import com.wso2.openbanking.accelerator.consent.extensions.utils.ConsentExtensionTestUtils; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.AuthorizationResource; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl; import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; -import net.minidev.json.JSONValue; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; @@ -52,7 +49,6 @@ import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test; -import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -61,7 +57,6 @@ import static org.mockito.Mockito.mock; import static org.powermock.api.mockito.PowerMockito.doReturn; import static org.powermock.api.mockito.PowerMockito.when; -import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; /** @@ -304,7 +299,7 @@ public void testGetConsentDataSetForAccount() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray accountConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); + JSONArray accountConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(accountConsentData); } @@ -317,7 +312,7 @@ public void testGetConsentDataSetForCOFs() { Mockito.doReturn(ConsentAuthorizeTestConstants.AWAITING_AUTH_STATUS).when(consentResourceMock) .getCurrentStatus(); - JSONArray cofConsentData= ConsentRetrievalUtil.getConsentData(consentResourceMock); + JSONArray cofConsentData = ConsentRetrievalUtil.getConsentData(consentResourceMock); Assert.assertNotNull(cofConsentData); }