From d29df110c6be1c6aff8ed40473519f0331cdffbc Mon Sep 17 00:00:00 2001 From: kalpana Date: Thu, 11 Jan 2024 13:20:36 +0530 Subject: [PATCH] VRP initiation flow implementation --- .../common/util/ErrorConstants.java | 44 +- .../common/ConsentExtensionConstants.java | 7 +- .../manage/impl/VRPConsentRequestHandler.java | 8 +- .../validator/VRPConsentRequestValidator.java | 667 ++++++++++++------ 4 files changed, 470 insertions(+), 256 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 bcda5b32..5c9ae0ec 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 @@ -62,8 +62,13 @@ public class ErrorConstants { public static final String PAYLOAD_FORMAT_ERROR_VALID_FROM_DATE = "Request Payload is not in correct JSON format" + " for valid from date"; public static final String PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS = "Invalid periodic limits"; + public static final String PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS_ALIGNMENT = "Value is empty or the value passed " + + "in is not a string"; + public static final String PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS_PERIOD_TYPE = "Value of period type is empty or " + + "the value passed in is not a string"; public static final String PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_AMOUNT = "Invalid maximum individual amount"; + public static final String MISSING_MAXIMUM_INDIVIDUAL_AMOUNT = "Missing parameter maximum individual amount"; public static final String PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_CURRENCY = "Invalid maximum individual amount" + "currency"; public static final String PAYLOAD_FORMAT_ERROR_INITIATION = "Request Payload is not in correct JSON format" + @@ -248,37 +253,16 @@ public class ErrorConstants { public static final String FIELD_INVALID_DATE = "OB.Field.InvalidDate"; public static final String EXPIRED_DATE_ERROR = "The ExpirationDateTime value has to be a future date."; // VRP error constants - public static final String MAXIMUM_INDIVIDUAL_AMOUNT_IS_MISSING = "Mandatory parameter MaximumIndividualAmount" + - " Amount is missing in the payload."; - public static final String MAXIMUM_INDIVIDUAL_AMOUNT_CURRENCY_IS_MISSING = "Mandatory parameter" + - "Currency in MaximumIndividualAmount is missing in the payload"; - public static final String INVALID_AMOUNT = "Missing required parameter Amount"; - public static final String INVALID_CURRENCY = "Missing required parameter Currency"; public static final String INVALID_PERIOD_ALIGNMENT = "Invalid value for period alignment in PeriodicLimits"; + public static final String INVALID_PERIOD_TYPE = "Invalid value for period type in PeriodicLimits"; public static final String MISSING_PERIOD_TYPE = "Missing required parameter Period type"; - public static final String INVALID_VALID_TO_DATE = "Valid to Date specified in the request is invalid"; - - // new error constants - - public static final String INVALID_INITIATION_PAYLOAD = "Consent validation failed due to invalid" + - " initiation payload"; public static final String INVALID_PARAMETER = "Parameter passed in is null , " + "empty or not a JSONObject"; public static final String INVALID_CLIENT_ID_MATCH = "Consent validation failed due to client ID mismatch"; - public static final String INVALID_PARAMETER_DEBTOR_ACC = "Parameter passed in is null , " + - "empty or not a JSONObject in debtor account"; - public static final String INVALID_PARAMETER_CREDITOR_ACC = "Parameter passed in is null , " + - "empty or not a JSONObject in debtor account"; - public static final String INVALID_PARAMETER_AMOUNT = "Parameter passed in is null , " + - "empty or not a JSONObject in amount"; - public static final String INVALID_PARAMETER_INITIATION = "Parameter passed in is null , " + - "empty or not a JSONObject in initiation"; - public static final String INVALID_PARAMETER_CONTROL_PARAMETERS = "Parameter passed in is null , " + - "empty or not a JSONObject in control parameter"; - public static final String INVALID_PARAMETER_MAXIMUM_INDIVIDUAL_AMOUNT = "Parameter passed in is null , " + - "empty or not a JSONObject in maximum individual amount"; public static final String INVALID_DATE_TIME_FORMAT = "Date and Time is not in correct JSON " + "ISO-8601 date-time format"; + public static final String MISSING_DATE_TIME_FORMAT = "The value is empty or the value is not a string"; + public static final String MISSING_DATE_TIME = "Missing parameter"; public static final String INVALID_PARAMETER_PERIODIC_LIMITS = "Parameter passed in is null , " + "empty or not a JSONArray"; public static final String MISSING_PERIOD_LIMITS = "Mandatory parameter " + @@ -287,18 +271,14 @@ public class ErrorConstants { public static final String PATH_VALID_TO_DATE = "Data.ControlParameters.ValidToDateTime"; public static final String PATH_VALID_FROM_DATE = "Data.ControlParameters.ValidFromDateTime"; public static final String PATH_MAXIMUM_INDIVIDUAL_AMOUNT = "Data.ControlParameters.MaximumIndividualAmount"; - public static final String PATH_MAXIMUM_INDIVIDUAL_AMOUNT_AMOUNT = "Data.ControlParameters." + - "MaximumIndividualAmount.Amount"; - public static final String PATH_MAXIMUM_INDIVIDUAL_AMOUNT_CURRENCY = "Data.ControlParameters." + - "MaximumIndividualAmount.Currency"; + public static final String PATH_PERIOD_LIMIT = "Data.ControlParameters.PeriodicLimits"; public static final String PATH_PERIOD_LIMIT_AMOUNT = "Data.ControlParameters.PeriodicLimits.Amount"; public static final String PATH_PERIOD_LIMIT_CURRENCY = "Data.ControlParameters.PeriodicLimits.Currency"; public static final String PATH_PERIOD_TYPE = "Data.ControlParameters.PeriodicLimits.PeriodType"; - public static final String PATH_PERIOD_ALIGNMENT = "Data.ControlParameters.PeriodLimits.PeriodAlignment"; - public static final String PERIODIC_LIMIT_CURRENCY_IS_MISSING = "Mandatory parameter Currency in Periodic limits" + - " is missing in the payload"; - public static final String MISSING_PERIOD_ALIGNMENT = "Missing periodic alignment"; + public static final String PATH_PERIOD_ALIGNMENT = "Data.ControlParameters.PeriodicLimits.PeriodAlignment"; + + public static final String MISSING_PERIOD_ALIGNMENT = "Missing periodic alignment in periodic limits"; public static final String INVALID_PARAMETER_MESSAGE = "Parameter '%s' passed in is null, empty, or not a %s"; 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 ae8944ab..620eb0b0 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 @@ -16,7 +16,6 @@ * under the License. */ package com.wso2.openbanking.accelerator.consent.extensions.common; -; /** * Constant class for consent extension module. @@ -173,12 +172,12 @@ public class ConsentExtensionConstants { public static final String AUTH_TYPE_AUTHORIZATION = "authorization"; public static final String CONTROL_PARAMETERS = "ControlParameters"; public static final String MAXIMUM_INDIVIDUAL_AMOUNT = "MaximumIndividualAmount"; - public static final String PERIOD_ALIGNMENT = "PeriodicAlignment"; + public static final String PERIOD_ALIGNMENT = "PeriodAlignment"; public static final String PERIODIC_LIMITS = "PeriodicLimits"; public static final String PERIOD_TYPE = "PeriodType"; public static final String PERIOD_AMOUNT_LIMIT = "Amount"; - public static final String CONSENT = "consent-periodicAlignment"; - public static final String CALENDER = "calender-periodicAlignment"; + public static final String CONSENT = "Consent"; + public static final String CALENDAR = "Calendar"; public static final String DAY = "Day"; public static final String WEEK = "Week"; public static final String FORTNIGHT = "Fortnight"; diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/impl/VRPConsentRequestHandler.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/impl/VRPConsentRequestHandler.java index 59bc549a..bd2fcdff 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/impl/VRPConsentRequestHandler.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/impl/VRPConsentRequestHandler.java @@ -53,6 +53,8 @@ public class VRPConsentRequestHandler implements ConsentManageRequestHandler { /** * This method is responsible for processing a Variable Recurring Payment Consent Manage POST request. * It validates the payment request, checks for the existence of an idempotency key. + * + * @param consentManageData Object */ @Override public void handleConsentManagePost(ConsentManageData consentManageData) { @@ -152,6 +154,7 @@ public void handleConsentManageDelete(ConsentManageData consentManageData) { * - Sets the response status to Created. * * @param consentManageData Object containing request details, including client ID, request payload, headers. + * @param request Object */ public void handlePaymentPost(ConsentManageData consentManageData, Object request) throws ConsentManagementException { @@ -189,9 +192,10 @@ public void handlePaymentPost(ConsentManageData consentManageData, Object reques get(ConsentExtensionConstants.AMOUNT).toString()); consentAttributes.put(ConsentExtensionConstants.PERIOD_ALIGNMENT, ((JSONObject) ((JSONArray) (controlParameters).get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)) - //TODO: Improve the logic of storing the PERIODIC_LIMITS .get(ConsentExtensionConstants.PERIOD_ALIGNMENT).toString()); - consentAttributes.put(ConsentExtensionConstants.PERIOD_TYPE, ((JSONObject) ((JSONArray) (controlParameters) + //TODO: Improve the logic of storing the PERIODIC_LIMITS + consentAttributes.put(ConsentExtensionConstants.PERIOD_TYPE, + ((JSONObject) ((JSONArray) (controlParameters) .get(ConsentExtensionConstants.PERIODIC_LIMITS)).get(0)).get(ConsentExtensionConstants.PERIOD_TYPE) .toString()); consentAttributes.put(ConsentExtensionConstants.PERIOD_AMOUNT_LIMIT, ((JSONObject) diff --git a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/validator/VRPConsentRequestValidator.java b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/validator/VRPConsentRequestValidator.java index a8dd719f..1a0a3769 100644 --- a/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/validator/VRPConsentRequestValidator.java +++ b/open-banking-accelerator/components/consent-management/com.wso2.openbanking.accelerator.consent.extensions/src/main/java/com/wso2/openbanking/accelerator/consent/extensions/manage/validator/VRPConsentRequestValidator.java @@ -28,8 +28,8 @@ import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; -import java.util.Arrays; -import java.util.List; +import java.util.Iterator; + /** * Consent Manage validator class for Variable Recurring Payment Request Validation. @@ -66,7 +66,7 @@ public static JSONObject validateVRPPayload(Object request) { JSONObject dataValidationResult = ConsentManageUtil.validateInitiationDataBody(requestBody); if (!(Boolean.parseBoolean(dataValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(dataValidationResult.get(ConsentExtensionConstants.ERRORS)); + // log.error(dataValidationResult.get(ConsentExtensionConstants.ERRORS)); return dataValidationResult; } @@ -74,22 +74,23 @@ public static JSONObject validateVRPPayload(Object request) { JSONObject initiationValidationResult = VRPConsentRequestValidator.validateConsentInitiation(requestBody); if (!(Boolean.parseBoolean(initiationValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(initiationValidationResult.get(ConsentExtensionConstants.ERRORS)); + //log.error(initiationValidationResult.get(ConsentExtensionConstants.ERRORS)); return initiationValidationResult; } JSONObject controlParameterValidationResult = VRPConsentRequestValidator. validateConsentControlParameters(requestBody); - if (!(Boolean.parseBoolean(controlParameterValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(controlParameterValidationResult.get(ConsentExtensionConstants.ERRORS)); + if (!(Boolean.parseBoolean(controlParameterValidationResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + // log.error(controlParameterValidationResult.get(ConsentExtensionConstants.ERRORS)); return controlParameterValidationResult; } JSONObject riskValidationResult = VRPConsentRequestValidator.validateConsentRisk(requestBody); if (!(Boolean.parseBoolean(riskValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(riskValidationResult.get(ConsentExtensionConstants.ERRORS)); + //log.error(riskValidationResult.get(ConsentExtensionConstants.ERRORS)); return riskValidationResult; } @@ -97,39 +98,6 @@ public static JSONObject validateVRPPayload(Object request) { return validationResponse; } - /** - * Checks if the given Object is a JSONObject and the JSONObject is non-empty . - * - * @param value The Object to be validated. - * @return true if the object is a non-null and non-empty JSONObject. - */ - public static boolean isValidJSONObject(Object value) { - return value instanceof JSONObject && !((JSONObject) value).isEmpty(); - } - - /** - * Checks if the given object is a valid date-time string and it is non empty. - * - * @param value The object to be checked for a valid date-time format. - * @return True if the object is a non-empty string in ISO date-time format, false otherwise. - */ - public static boolean isValidDateTimeObject(Object value) { - - final DateTimeFormatter dateTimeFormat = DateTimeFormatter.ISO_DATE_TIME; - - if (value instanceof String && !((String) value).isEmpty()) { - try { - dateTimeFormat.parse((String) value); - return true; - } catch (DateTimeParseException e) { - log.error("Invalid date-time format: %d", e); - return false; - } - } else { - log.debug("date-time not a string or it's value is empty"); - return false; - } - } /** * Method to validate control parameters for variable recurring payments. @@ -142,36 +110,39 @@ public static boolean isValidDateTimeObject(Object value) { * @return A validation response object indicating whether the control parameters are valid. */ public static JSONObject validateControlParameters(JSONObject controlParameters) { - JSONObject controlParameterValidationResponse = new JSONObject(); + JSONObject validationResponse = new JSONObject(); JSONObject maximumIndividualAmountResult = validateMaximumIndividualAmount(controlParameters); if (!(Boolean.parseBoolean(maximumIndividualAmountResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(maximumIndividualAmountResult.get(ConsentExtensionConstants.ERRORS)); return maximumIndividualAmountResult; } - JSONObject maximumIndividualAmountResult2 = validateMaximumIndividualAmountCurrency(controlParameters); - if (!(Boolean.parseBoolean(maximumIndividualAmountResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(maximumIndividualAmountResult.get(ConsentExtensionConstants.ERRORS)); - return maximumIndividualAmountResult; + JSONObject maximumIndividualAmountCurrencyResult = validateMaximumIndividualAmountCurrency(controlParameters); + if (!(Boolean.parseBoolean(maximumIndividualAmountCurrencyResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return maximumIndividualAmountCurrencyResult; } JSONObject parameterDateTimeValidationResponse = validateParameterDateTime(controlParameters); if (!(Boolean.parseBoolean(parameterDateTimeValidationResponse. getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(parameterDateTimeValidationResponse.get(ConsentExtensionConstants.ERRORS)); return parameterDateTimeValidationResponse; } +// JSONObject parameterDateTimeValidationResponses = validateParameterDateTimes(controlParameters); +// if (!(Boolean.parseBoolean(parameterDateTimeValidationResponses. +// getAsString(ConsentExtensionConstants.IS_VALID)))) { +// return parameterDateTimeValidationResponses; +// } + // Validate Periodic Limits JSONObject periodicLimitsValidationResult = validatePeriodicLimits(controlParameters); if (!(Boolean.parseBoolean(periodicLimitsValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(ErrorConstants.PAYLOAD_INVALID); return periodicLimitsValidationResult; } - controlParameterValidationResponse.put(ConsentExtensionConstants.IS_VALID, true); - return controlParameterValidationResponse; + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; } /** @@ -212,17 +183,17 @@ public static JSONObject validateMaximumIndividualAmount(JSONObject controlParam ErrorConstants.PATH_MAXIMUM_INDIVIDUAL_AMOUNT); } - if (!validateJsonObjectKey((JSONObject) maximumIndividualAmount, ConsentExtensionConstants.AMOUNT)) { - return ConsentManageUtil.getValidationResponse(ErrorConstants. - PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_AMOUNT, - ErrorConstants.MAXIMUM_INDIVIDUAL_AMOUNT_IS_MISSING, - ErrorConstants.PATH_MAXIMUM_INDIVIDUAL_AMOUNT); + JSONObject maximumIndividualAmountResult = validateJsonObjectKey((JSONObject) maximumIndividualAmount, + ConsentExtensionConstants.AMOUNT); + if (!(Boolean.parseBoolean(maximumIndividualAmountResult. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + // log.error(maximumIndividualAmountResult.get(ConsentExtensionConstants.ERRORS)); + return maximumIndividualAmountResult; } - } else { - log.error(ErrorConstants.PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_AMOUNT); + log.error(ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT); return ConsentManageUtil.getValidationResponse(ErrorConstants.RESOURCE_INVALID_FORMAT, - ErrorConstants.PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_AMOUNT, ErrorConstants.PATH_REQUEST_BODY); + ErrorConstants.MISSING_MAXIMUM_INDIVIDUAL_AMOUNT, ErrorConstants.PATH_REQUEST_BODY); } validationResponse.put(ConsentExtensionConstants.IS_VALID, true); @@ -241,16 +212,18 @@ public static JSONObject validateMaximumIndividualAmountCurrency(JSONObject cont Object maximumIndividualAmount = controlParameters. get(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT); - if (!validateJsonObjectKey((JSONObject) maximumIndividualAmount, ConsentExtensionConstants.CURRENCY)) { - return ConsentManageUtil.getValidationResponse(ErrorConstants. - PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_AMOUNT, - ErrorConstants.MAXIMUM_INDIVIDUAL_AMOUNT_CURRENCY_IS_MISSING, - ErrorConstants.PATH_MAXIMUM_INDIVIDUAL_AMOUNT_CURRENCY); + + JSONObject maximumIndividualAmountResult = validateJsonObjectKey((JSONObject) maximumIndividualAmount, + ConsentExtensionConstants.CURRENCY); + if (!(Boolean.parseBoolean(maximumIndividualAmountResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { + // log.error(maximumIndividualAmountResult.get(ConsentExtensionConstants.ERRORS)); + return maximumIndividualAmountResult; } validationResponse.put(ConsentExtensionConstants.IS_VALID, true); return validationResponse; } + /** * Method to validate variable recurring payment periodic limits. * This method validates the periodic limits specified in the control parameters for variable recurring payments. @@ -280,45 +253,37 @@ public static JSONObject validatePeriodicLimits(JSONObject controlParameters) { ErrorConstants.PATH_PERIOD_LIMIT); } - // Retrieve the JSON array of periodic limits - // JSONArray periodicLimits = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); + JSONArray periodicLimits = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); + Iterator it = periodicLimits.iterator(); - // Iterate through each periodic limit - // Iterator periodicLimitIterator = periodicLimits.iterator(); + while (it.hasNext()) { + JSONObject limit = (JSONObject) it.next(); - // while (periodicLimitIterator.hasNext()) { - // JSONObject limit = (JSONObject) periodicLimitIterator.next(); - - JSONObject periodicLimitType = validateAmountCurrencyPeriodicLimit - (controlParameters); - if (!(Boolean.parseBoolean(periodicLimitType. + JSONObject amount = validateAmountPeriodicLimit(controlParameters); + if (!(Boolean.parseBoolean(amount. getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(periodicLimitType.get(ConsentExtensionConstants.ERRORS)); - return periodicLimitType; + return amount; } - JSONObject maximumIndividualAmountCurrencyPeriodicLimit = validateMaximumIndividualAmountCurrency - (controlParameters); - if (!(Boolean.parseBoolean(maximumIndividualAmountCurrencyPeriodicLimit. + JSONObject currency = validateCurrencyPeriodicLimit(controlParameters); + if (!(Boolean.parseBoolean(currency. getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(maximumIndividualAmountCurrencyPeriodicLimit.get(ConsentExtensionConstants.ERRORS)); - return maximumIndividualAmountCurrencyPeriodicLimit; + return currency; } - JSONObject periodicAlignment = validatePeriodicAlignments(controlParameters); - if (!(Boolean.parseBoolean(periodicAlignment. + JSONObject validationResults = validatePeriodAlignment(limit); + if (!(Boolean.parseBoolean(validationResults. getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(periodicAlignment.get(ConsentExtensionConstants.ERRORS)); - return periodicAlignment; + return validationResults; } - JSONObject periodicType = validatePeriodicTypes(controlParameters); - if (!(Boolean.parseBoolean(periodicType. + JSONObject periodType = validatePeriodType(limit); + if (!(Boolean.parseBoolean(periodType. getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(periodicType.get(ConsentExtensionConstants.ERRORS)); - return periodicType; + return periodType; } - } else { + } + } else { // If periodic limits key is missing, return an error log.error(ErrorConstants.MISSING_PERIOD_LIMITS); return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, @@ -335,69 +300,45 @@ public static JSONObject validatePeriodicLimits(JSONObject controlParameters) { * @param controlParameters The JSON object representing the control parameters of the consent request. * @return A JSON object containing the validation response. */ - public static JSONObject validateAmountCurrencyPeriodicLimit(JSONObject controlParameters) { + public static JSONObject validateCurrencyPeriodicLimit(JSONObject controlParameters) { JSONObject validationResponse = new JSONObject(); JSONArray periodicLimits = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); - - if (!validateAmountCurrencyPeriodicLimits((JSONArray) periodicLimits, - ConsentExtensionConstants.CURRENCY)) { - return ConsentManageUtil.getValidationResponse(ErrorConstants. - PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_CURRENCY, - ErrorConstants.PERIODIC_LIMIT_CURRENCY_IS_MISSING, - ErrorConstants.PATH_MAXIMUM_INDIVIDUAL_AMOUNT); + JSONObject currency = validateAmountCurrencyPeriodicLimits((JSONArray) periodicLimits, + ConsentExtensionConstants.CURRENCY); + if (!(Boolean.parseBoolean(currency. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return currency; } validationResponse.put(ConsentExtensionConstants.IS_VALID, true); return validationResponse; } - /** - * Validates the periodicType in periodic limits in the control parameters of a consent request. - * - * @param controlParameters The JSON object representing the control parameters of the consent request. - * @return A JSONArray containing the validation response. - */ - public static JSONObject validatePeriodicTypes(JSONObject controlParameters) { - - JSONObject validationResponse = new JSONObject(); - - JSONArray periodicTypes = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); - JSONArray periodicType = (JSONArray) periodicTypes.get(Integer.parseInt(ConsentExtensionConstants.PERIOD_TYPE)); - - if (!validatePeriodicType((JSONArray) periodicType)) { - return ConsentManageUtil.getValidationResponse(ErrorConstants. - PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS, - ErrorConstants.MISSING_PERIOD_TYPE, - ErrorConstants.PATH_PERIOD_TYPE); - } - validationResponse.put(ConsentExtensionConstants.IS_VALID, true); - return validationResponse; - } /** - * Validates the periodicAlignment in periodic limits in the control parameters of a consent request. + * Validates the Amount in periodic limits in the control parameters of a consent request. * * @param controlParameters The JSON object representing the control parameters of the consent request. - * @return A JSONArray containing the validation response. + * @return A JSON object containing the validation response. */ - public static JSONObject validatePeriodicAlignments(JSONObject controlParameters) { + public static JSONObject validateAmountPeriodicLimit(JSONObject controlParameters) { JSONObject validationResponse = new JSONObject(); - JSONArray periodicAlignments = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); - JSONArray periodicAlignment = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIOD_ALIGNMENT); + JSONArray periodicLimits = (JSONArray) controlParameters.get(ConsentExtensionConstants.PERIODIC_LIMITS); - if (!validatePeriodicAlignment((JSONArray) periodicAlignment)) { - return ConsentManageUtil.getValidationResponse(ErrorConstants. - PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS, - ErrorConstants.MISSING_PERIOD_ALIGNMENT, - ErrorConstants.PATH_PERIOD_TYPE); + JSONObject amount = validateAmountCurrencyPeriodicLimits((JSONArray) periodicLimits, + ConsentExtensionConstants.AMOUNT); + if (!(Boolean.parseBoolean(amount. + getAsString(ConsentExtensionConstants.IS_VALID)))) { + return amount; } validationResponse.put(ConsentExtensionConstants.IS_VALID, true); return validationResponse; } + /** * Validates the date-time parameters in the control parameters of a consent request. * @@ -405,63 +346,256 @@ public static JSONObject validatePeriodicAlignments(JSONObject controlParameters * @return A JSON object containing the validation response. If the date-time parameters are valid, * it sets the "IS_VALID" field to true; otherwise, it contains an error response. */ + public static JSONObject validateParameterDateTime(JSONObject controlParameters) { JSONObject validationResponse = new JSONObject(); - if (controlParameters.containsKey(ConsentExtensionConstants.VALID_TO_DATE_TIME)) { + // Check if ValidToDateTime and ValidFromDateTime keys are present + if (controlParameters.containsKey(ConsentExtensionConstants.VALID_TO_DATE_TIME) + && controlParameters.containsKey(ConsentExtensionConstants.VALID_FROM_DATE_TIME)) { - // Retrieve the validDateTime from the control parameters + // Get and validate ValidToDateTime Object validateToDateTime = controlParameters.get(ConsentExtensionConstants.VALID_TO_DATE_TIME); - - if (!isValidDateTimeObject(validateToDateTime)) { - return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR_VALID_TO_DATE, - ErrorConstants.INVALID_DATE_TIME_FORMAT, - ErrorConstants.PATH_VALID_TO_DATE); - } - - OffsetDateTime validToDateTime = OffsetDateTime.parse(controlParameters. - getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME)); - OffsetDateTime validFromDateTime = OffsetDateTime.parse(controlParameters. - getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); - OffsetDateTime currentDateTime = OffsetDateTime.now(validToDateTime.getOffset()); - - //If the ValidToDAte is older than current date OR currentDate is older than ValidFromDAte, return error - if (!validFromDateTime.isBefore(currentDateTime) || !currentDateTime.isBefore(validToDateTime)) { - log.error(String.format("Invalid date-time range, validToDateTime : %s , validFromDateTime : %s " + - "and currentDateTime : %s ", validToDateTime, validFromDateTime, currentDateTime)); - return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, - ErrorConstants.INVALID_VALID_TO_DATE, ErrorConstants.PATH_VALID_TO_DATE); + JSONObject toDateTimeValidation = isValidDateTimeObject(validateToDateTime); + if (!(Boolean.parseBoolean(toDateTimeValidation.getAsString(ConsentExtensionConstants.IS_VALID)))) { + return toDateTimeValidation; } - } - - if (controlParameters.containsKey(ConsentExtensionConstants.VALID_FROM_DATE_TIME)) { - // Retrieve the validDateTime from the control parameters + // Get and validate ValidFromDateTime Object validateFromDateTime = controlParameters.get(ConsentExtensionConstants.VALID_FROM_DATE_TIME); - - if (!isValidDateTimeObject(validateFromDateTime)) { - return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR_VALID_FROM_DATE, - ErrorConstants.INVALID_DATE_TIME_FORMAT, - ErrorConstants.PATH_VALID_TO_DATE); + JSONObject fromDateTimeValidation = isValidDateTimeObject(validateFromDateTime); + if (!(Boolean.parseBoolean(fromDateTimeValidation.getAsString(ConsentExtensionConstants.IS_VALID)))) { + return fromDateTimeValidation; } - OffsetDateTime validToDateTime = OffsetDateTime.parse(controlParameters. - getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME)); - OffsetDateTime validFromDateTime = OffsetDateTime.parse(controlParameters. - getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); + String validTo = controlParameters.getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME); + String validFrom = controlParameters.getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME); + + OffsetDateTime validToDateTime = OffsetDateTime.parse(validTo); + OffsetDateTime validFromDateTime = OffsetDateTime.parse(validFrom); OffsetDateTime currentDateTime = OffsetDateTime.now(validToDateTime.getOffset()); - //If the ValidToDAte is older than current date OR currentDate is older than ValidFromDAte, return error + // If ValidToDateTime is older than current date OR ValidToDateTime is + // older than ValidFromDateTime, return error if (!validFromDateTime.isBefore(currentDateTime) || !currentDateTime.isBefore(validToDateTime)) { - log.debug("Invalid date-time range."); + log.error(String.format("Invalid date-time range, " + + "validToDateTime: %s, validFromDateTime: %s, currentDateTime: %s", + validToDateTime, validFromDateTime, currentDateTime)); return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, - ErrorConstants.INVALID_VALID_TO_DATE, ErrorConstants.PATH_VALID_FROM_DATE); + ErrorConstants.INVALID_REQ_PAYLOAD, ErrorConstants.PATH_VALID_TO_DATE); } + } else { + // If ValidToDateTime or ValidFromDateTime keys are not present, return error + return ConsentManageUtil.getValidationResponse(ErrorConstants.FIELD_INVALID, + ErrorConstants.MISSING_DATE_TIME, ErrorConstants.PATH_VALID_TO_DATE); } + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); return validationResponse; } +// public static JSONObject validateParameterDateTime(JSONObject controlParameters) { +// JSONObject validationResponse = new JSONObject(); +// +// if (controlParameters.containsKey(ConsentExtensionConstants.VALID_TO_DATE_TIME)) { +// +// Object validateToDateTime = controlParameters.get(ConsentExtensionConstants.VALID_TO_DATE_TIME); +// JSONObject date = isValidDateTimeObject(validateToDateTime); +// if (!(Boolean.parseBoolean(date.getAsString(ConsentExtensionConstants.IS_VALID)))) { +// // log.error(currency.get(ConsentExtensionConstants.ERRORS)); +// return date; +// } +// +// String validTo = controlParameters.getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME); +// String validFrom = controlParameters.getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME); +// +// OffsetDateTime validToDateTime = OffsetDateTime.parse(validTo); +// OffsetDateTime validFromDateTime = OffsetDateTime.parse(validFrom); +// OffsetDateTime currentDateTime = OffsetDateTime.now(validToDateTime.getOffset()); +// +// //If the ValidToDAte is older than current date OR ValidToDAte is older than ValidFromDAte, return error +// if (!validFromDateTime.isBefore(currentDateTime) || !currentDateTime.isBefore(validToDateTime)) { +// log.error(String.format("Invalid date-time range, " + +// "validToDateTime: %s, validFromDateTime: %s, currentDateTime: %s", +// validToDateTime, validFromDateTime, currentDateTime)); +// return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, +// ErrorConstants.INVALID_REQ_PAYLOAD, ErrorConstants.PATH_VALID_TO_DATE); +// } +// } else { +// return ConsentManageUtil.getValidationResponse(ErrorConstants.FIELD_INVALID, +// ErrorConstants.INVALID_VALID_TO_DATE, ErrorConstants.PATH_VALID_TO_DATE); +// } +// +// validationResponse.put(ConsentExtensionConstants.IS_VALID, true); +// return validationResponse; +// } + +// public static JSONObject validateParameterDateTimes(JSONObject controlParameters) { +// JSONObject validationResponse = new JSONObject(); +// +// if (controlParameters.containsKey(ConsentExtensionConstants.VALID_FROM_DATE_TIME)) { +// +// Object validateFromDateTime = controlParameters.get(ConsentExtensionConstants.VALID_FROM_DATE_TIME); +// JSONObject date = isValidDateTimeObject(validateFromDateTime); +// if (!(Boolean.parseBoolean(date.getAsString(ConsentExtensionConstants.IS_VALID)))) { +// return date; +// } +// +// String validTo = controlParameters.getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME); +// String validFrom = controlParameters.getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME); +// +// OffsetDateTime validToDateTime = OffsetDateTime.parse(validTo); +// OffsetDateTime validFromDateTime = OffsetDateTime.parse(validFrom); +// OffsetDateTime currentDateTime = OffsetDateTime.now(validToDateTime.getOffset()); +// +// //If the ValidToDAte is older than current date OR ValidToDAte is older than ValidFromDAte, return error +// if (!validFromDateTime.isBefore(currentDateTime) || !currentDateTime.isBefore(validToDateTime)) { +// log.error(String.format("Invalid date-time range, " + +// "validToDateTime: %s, validFromDateTime: %s, currentDateTime: %s", +// validToDateTime, validFromDateTime, currentDateTime)); +// return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, +// ErrorConstants.INVALID_REQ_PAYLOAD, ErrorConstants.PATH_VALID_FROM_DATE); +// } +// } else { +// return ConsentManageUtil.getValidationResponse(ErrorConstants.FIELD_INVALID, +// ErrorConstants.INVALID_VALID_TO_DATE, ErrorConstants.PATH_VALID_FROM_DATE); +// } +// +// validationResponse.put(ConsentExtensionConstants.IS_VALID, true); +// return validationResponse; +// } + + +// public static JSONObject validateParameterDateTime(JSONObject controlParameters) { +// JSONObject validationResponse = new JSONObject(); +// +// if (controlParameters.containsKey(ConsentExtensionConstants.VALID_TO_DATE_TIME)) { +// Object validateToDateTime = controlParameters.get(ConsentExtensionConstants.VALID_TO_DATE_TIME); +// +//// JSONObject dateValidation = isValidDateTimeObject(validateToDateTime); +//// +//// if (!(Boolean.parseBoolean(dateValidation.getAsString(ConsentExtensionConstants.IS_VALID)))) { +//// return dateValidation; +//// } +// +// OffsetDateTime validToDateTime = OffsetDateTime.parse(controlParameters. +// getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME)); +// OffsetDateTime validFromDateTime = OffsetDateTime.parse(controlParameters. +// getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); +// OffsetDateTime currentDateTime = OffsetDateTime.now(validToDateTime.getOffset()); +// +// +// if (!validFromDateTime.isBefore(currentDateTime) || !currentDateTime.isBefore(validToDateTime)) { +// log.error(String.format("Invalid date-time range, " + +// "validToDateTime: %s, validFromDateTime: %s, currentDateTime: %s", +// validToDateTime, validFromDateTime, currentDateTime)); +// return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, +// ErrorConstants.INVALID_REQ_PAYLOAD, ErrorConstants.PATH_VALID_TO_DATE); +// } +// } else { +// return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, +// ErrorConstants.INVALID_REQ_PAYLOAD, ErrorConstants.PATH_VALID_TO_DATE); +// } +// +// if (controlParameters.containsKey(ConsentExtensionConstants.VALID_FROM_DATE_TIME)) { +// Object validateFromDateTime = controlParameters.get(ConsentExtensionConstants.VALID_FROM_DATE_TIME); +// JSONObject dateValidation = isValidDateTimeObject(validateFromDateTime); +// +// if (!(Boolean.parseBoolean(dateValidation.getAsString(ConsentExtensionConstants.IS_VALID)))) { +// return dateValidation; +// } +// +// OffsetDateTime validToDateTime = OffsetDateTime.parse(controlParameters. +// getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME)); +// OffsetDateTime validFromDateTime = OffsetDateTime.parse(controlParameters. +// getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); +// OffsetDateTime currentDateTime = OffsetDateTime.now(validToDateTime.getOffset()); +// +// if (!validFromDateTime.isBefore(currentDateTime) || !currentDateTime.isBefore(validToDateTime)) { +// log.debug("Invalid date-time range."); +// return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, +// ErrorConstants.INVALID_PARAMETER, ErrorConstants.PATH_VALID_FROM_DATE); +// } +// } else { +// return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, +// ErrorConstants.INVALID_REQ_PAYLOAD, ErrorConstants.PATH_VALID_TO_DATE); +// } +// +// validationResponse.put(ConsentExtensionConstants.IS_VALID, true); +// return validationResponse; +// } + + +// public static JSONObject validateParameterDateTime(JSONObject controlParameters) { +// JSONObject validationResponse = new JSONObject(); +// +// if (controlParameters.containsKey(ConsentExtensionConstants.VALID_TO_DATE_TIME)) { +// +// // Retrieve the validDateTime from the control parameters +// Object validateToDateTime = controlParameters.get(ConsentExtensionConstants.VALID_TO_DATE_TIME); +// +//// if (!isValidDateTimeObject(validateToDateTime)) { +//// return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR_VALID_TO_DATE, +//// ErrorConstants.INVALID_DATE_TIME_FORMAT, +//// ErrorConstants.PATH_VALID_TO_DATE); +//// } +// +// JSONObject date = isValidDateTimeObject(validateToDateTime); +// if (!(Boolean.parseBoolean(date. +// getAsString(ConsentExtensionConstants.IS_VALID)))) { +// // log.error(currency.get(ConsentExtensionConstants.ERRORS)); +// return date; +// } +// +// OffsetDateTime validToDateTime = OffsetDateTime.parse(controlParameters. +// getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME)); +// OffsetDateTime validFromDateTime = OffsetDateTime.parse(controlParameters. +// getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); +// OffsetDateTime currentDateTime = OffsetDateTime.now(validToDateTime.getOffset()); +// +// //If the ValidToDAte is older than current date OR currentDate is older than ValidFromDAte, return error +// if (!validFromDateTime.isBefore(currentDateTime) || !currentDateTime.isBefore(validToDateTime)) { +// log.error(String.format("Invalid date-time range, validToDateTime : %s , validFromDateTime : %s " + +// "and currentDateTime : %s ", validToDateTime, validFromDateTime, currentDateTime)); +// return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, +// ErrorConstants.INVALID_VALID_TO_DATE, ErrorConstants.PATH_VALID_TO_DATE); +// } +// } +// +// if (controlParameters.containsKey(ConsentExtensionConstants.VALID_FROM_DATE_TIME)) { +// +// // Retrieve the validDateTime from the control parameters +// Object validateFromDateTime = controlParameters.get(ConsentExtensionConstants.VALID_FROM_DATE_TIME); +// +//// if (!isValidDateTimeObject(validateFromDateTime)) { +//// return ConsentManageUtil.getValidationResponse(ErrorConstants.PAYLOAD_FORMAT_ERROR_VALID_FROM_DATE, +//// ErrorConstants.INVALID_DATE_TIME_FORMAT, +//// ErrorConstants.PATH_VALID_TO_DATE); +//// } +// JSONObject date = isValidDateTimeObject(validateFromDateTime); +// if (!(Boolean.parseBoolean(date. +// getAsString(ConsentExtensionConstants.IS_VALID)))) { +// // log.error(currency.get(ConsentExtensionConstants.ERRORS)); +// return date; +// } +// +// OffsetDateTime validToDateTime = OffsetDateTime.parse(controlParameters. +// getAsString(ConsentExtensionConstants.VALID_TO_DATE_TIME)); +// OffsetDateTime validFromDateTime = OffsetDateTime.parse(controlParameters. +// getAsString(ConsentExtensionConstants.VALID_FROM_DATE_TIME)); +// OffsetDateTime currentDateTime = OffsetDateTime.now(validToDateTime.getOffset()); +// +// //If the ValidToDAte is older than current date OR currentDate is older than ValidFromDAte, return error +// if (!validFromDateTime.isBefore(currentDateTime) || !currentDateTime.isBefore(validToDateTime)) { +// log.debug("Invalid date-time range."); +// return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, +// ErrorConstants.INVALID_VALID_TO_DATE, ErrorConstants.PATH_VALID_FROM_DATE); +// } +// } +// validationResponse.put(ConsentExtensionConstants.IS_VALID, true); +// return validationResponse; +// } + /** * Validator class to validate the payload of a variable recurring payment initiation. * This method performs validation on the initiation payload for a variable recurring payment. @@ -543,9 +677,8 @@ public static JSONObject validateVRPInitiationPayload(JSONObject initiation) { * @return true if the specified key is present in the parentObj and the associated value is a * non-empty string. */ - public static boolean validateJsonObjectKey(JSONObject parentObj, String key) { + public static JSONObject validateJsonObjectKey(JSONObject parentObj, String key) { JSONObject validationResponse = new JSONObject(); - validationResponse.put(ConsentExtensionConstants.IS_VALID, true); if (parentObj != null) { // Check if the specified key is present in the parentObj @@ -554,16 +687,34 @@ public static boolean validateJsonObjectKey(JSONObject parentObj, String key) { // Check if the value associated with the key is a non-empty string if (value instanceof String && !((String) value).isEmpty()) { - return true; // Valid: The key is present, and the value is a non-empty String + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; // Valid: The key is present, and the value is a non-empty String } else { - return false; // Invalid: The value associated with the key is not a non-empty String + String errorMessage = "The value associated is not a string or the value is empty'" + key + "'"; + return ConsentManageUtil.getValidationResponse( + ErrorConstants.INVALID_PARAMETER, + errorMessage, + ErrorConstants.PATH_REQUEST_BODY + ); + // Invalid: The value associated with the key is not a non-empty String } } else { - return false; // Invalid: The specified key is not present in parentObj + String errorMessage = "Mandatory parameter '" + key + "' is not present in payload"; + return ConsentManageUtil.getValidationResponse( + ErrorConstants.INVALID_PARAMETER, + errorMessage, + ErrorConstants.PATH_REQUEST_BODY + ); + // Invalid: The specified key is not present in parentObj } } - return false; // Invalid: parentObj is null + String errorMessage = "parameter passed in is null"; + return ConsentManageUtil.getValidationResponse( + ErrorConstants.INVALID_PARAMETER, + errorMessage, + ErrorConstants.PATH_REQUEST_BODY + ); } /** @@ -575,10 +726,8 @@ public static boolean validateJsonObjectKey(JSONObject parentObj, String key) { * @return true if the specified key is present in the parentObj and the associated value is a * non-empty string. */ - public static boolean validateAmountCurrencyPeriodicLimits(JSONArray parentArray, String key) { - JSONObject validationResponse = new JSONObject(); - validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + public static JSONObject validateAmountCurrencyPeriodicLimits(JSONArray parentArray, String key) { if (parentArray != null && key != null) { // Check if the specified key is present in the parentArray for (Object obj : parentArray) { @@ -590,17 +739,31 @@ public static boolean validateAmountCurrencyPeriodicLimits(JSONArray parentArray // Check if the value associated with the key is a non-empty string if (value instanceof String && !((String) value).isEmpty()) { - return true; // Valid: The key is present, and the value is a non-empty String + JSONObject validationResponse = new JSONObject(); + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; // Valid: The key is present, and the value is a non-empty String } else { - return false; // Invalid: The value associated with the key is not a non-empty String + String errorMessage = "Mandatory parameter '" + key + "' is not present in periodic" + + " limits or the value is not a string"; + return ConsentManageUtil.getValidationResponse( + ErrorConstants.RESOURCE_INVALID_FORMAT, + errorMessage, + ErrorConstants.PATH_REQUEST_BODY + ); + // Invalid: The value associated with the key is not a non-empty String } } } } } - return false; // Invalid: key is not present in parentArray or parentArray is null - } + String errorMessage = "Mandatory parameter '" + key + "' of periodic limits is not present in payload"; + return ConsentManageUtil.getValidationResponse( + ErrorConstants.RESOURCE_INVALID_FORMAT, + errorMessage, + ErrorConstants.PATH_REQUEST_BODY + ); + } /** * Validates the consent initiation payload in the VRP request. @@ -625,14 +788,14 @@ public static JSONObject validateConsentInitiation(JSONObject request) { if (!isValidJSONObject(initiation)) { return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD_INITIATION, - errorMessage, ErrorConstants.PATH_INITIATION); + errorMessage, ErrorConstants.PATH_INITIATION); } JSONObject initiationValidationResult = VRPConsentRequestValidator .validateVRPInitiationPayload((JSONObject) initiation); if (!(Boolean.parseBoolean(initiationValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(initiationValidationResult.get(ConsentExtensionConstants.ERRORS)); + // log.error(initiationValidationResult.get(ConsentExtensionConstants.ERRORS)); return initiationValidationResult; } } else { @@ -679,7 +842,7 @@ public static JSONObject validateConsentControlParameters(JSONObject request) { if (!(Boolean.parseBoolean(controlParameterValidationResult. getAsString(ConsentExtensionConstants.IS_VALID)))) { - log.error(controlParameterValidationResult.get(ConsentExtensionConstants.ERRORS)); + //log.error(controlParameterValidationResult.get(ConsentExtensionConstants.ERRORS)); return controlParameterValidationResult; } } else { @@ -718,65 +881,133 @@ public static JSONObject validateConsentRisk(JSONObject request) { } /** - * Validates the periodic alignments in the VRP request payload. + * Validates the periodic alignment in the VRP request payload. * - * @param periodicAlignment The JSONObject representing the VRP request. + * @param limit The JSONObject representing the VRP request. * @return A JSONObject containing the validation response. */ - - public static boolean validatePeriodicAlignment(Object periodicAlignment) { - + public static JSONObject validatePeriodAlignment(JSONObject limit) { JSONObject validationResponse = new JSONObject(); - validationResponse.put(ConsentExtensionConstants.IS_VALID, true); - JSONObject periodAlignmentObject = (JSONObject) periodicAlignment; + if (limit.containsKey(ConsentExtensionConstants.PERIOD_ALIGNMENT)) { + Object periodAlignmentObj = limit.get(ConsentExtensionConstants.PERIOD_ALIGNMENT); - if (periodAlignmentObject.containsKey(ConsentExtensionConstants.PERIODIC_LIMITS)) { - Object alignment = periodAlignmentObject.get(ConsentExtensionConstants.PERIOD_ALIGNMENT); + if (periodAlignmentObj instanceof String && !((String) periodAlignmentObj).isEmpty()) { + String periodAlignment = (String) periodAlignmentObj; - if (alignment != null && alignment instanceof String && !((String) alignment).isEmpty()) { - String periodType = (String) alignment; + if (ConsentExtensionConstants.CONSENT.equals(periodAlignment) || + ConsentExtensionConstants.CALENDAR.equals(periodAlignment)) { - List validAlignmentTypes = Arrays.asList( - ConsentExtensionConstants.CONSENT, - ConsentExtensionConstants.CALENDER + validationResponse.put("isValid", true); + validationResponse.put("periodAlignment", periodAlignment); + } else { + return ConsentManageUtil.getValidationResponse( + ErrorConstants.PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS, + ErrorConstants.INVALID_PERIOD_ALIGNMENT, + ErrorConstants.PATH_PERIOD_TYPE + ); + } + } else { + return ConsentManageUtil.getValidationResponse( + ErrorConstants.PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS, + ErrorConstants.PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS_ALIGNMENT, + ErrorConstants.PATH_PERIOD_TYPE ); - return validAlignmentTypes.contains(periodType); } + } else { + return ConsentManageUtil.getValidationResponse( + ErrorConstants.PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS, + ErrorConstants.MISSING_PERIOD_ALIGNMENT, + ErrorConstants.PATH_PERIOD_TYPE + ); } - return false; + return validationResponse; } -/** + + /** * Validates the periodic type in the VRP request payload. * - * @param periodicType The JSONObject representing the VRP request. + * @param limit The JSONObject representing the VRP request. * @return A JSONObject containing the validation response. */ - public static boolean validatePeriodicType(Object periodicType) { - + public static JSONObject validatePeriodType(JSONObject limit) { JSONObject validationResponse = new JSONObject(); - validationResponse.put(ConsentExtensionConstants.IS_VALID, true); - JSONObject limitObject = (JSONObject) periodicType; + if (limit.containsKey(ConsentExtensionConstants.PERIOD_TYPE)) { + Object periodTypeObj = limit.get(ConsentExtensionConstants.PERIOD_TYPE); - if (limitObject.containsKey(ConsentExtensionConstants.PERIOD_TYPE)) { - Object periodTypeObject = limitObject.get(ConsentExtensionConstants.PERIOD_TYPE); + if (periodTypeObj instanceof String && !((String) periodTypeObj).isEmpty()) { + String periodType = (String) periodTypeObj; - if (periodTypeObject != null && periodTypeObject instanceof String && - !((String) periodTypeObject).isEmpty()) { - String periodType = (String) periodTypeObject; + if (ConsentExtensionConstants.DAY.equals(periodType) || + ConsentExtensionConstants.WEEK.equals(periodType) || + ConsentExtensionConstants.FORTNIGHT.equals(periodType) || + ConsentExtensionConstants.MONTH.equals(periodType) || + ConsentExtensionConstants.HALF_YEAR.equals(periodType) || + ConsentExtensionConstants.YEAR.equals(periodType)) { - List validPeriodTypes = Arrays.asList( - ConsentExtensionConstants.DAY, - ConsentExtensionConstants.WEEK, - ConsentExtensionConstants.FORTNIGHT, - ConsentExtensionConstants.MONTH, - ConsentExtensionConstants.HALF_YEAR, - ConsentExtensionConstants.YEAR + validationResponse.put("isValid", true); + validationResponse.put("periodAlignment", periodType); + } else { + return ConsentManageUtil.getValidationResponse( + ErrorConstants.PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS, + ErrorConstants.INVALID_PERIOD_TYPE, + ErrorConstants.PATH_PERIOD_TYPE + ); + } + } else { + return ConsentManageUtil.getValidationResponse( + ErrorConstants.PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS, + ErrorConstants.PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS_PERIOD_TYPE, + ErrorConstants.PATH_PERIOD_TYPE ); - return validPeriodTypes.contains(periodType); } + } else { + return ConsentManageUtil.getValidationResponse( + ErrorConstants.PAYLOAD_FORMAT_ERROR_PERIODIC_LIMITS, + ErrorConstants.MISSING_PERIOD_TYPE, + ErrorConstants.PATH_PERIOD_TYPE + ); + } + return validationResponse; + } + + /** + * Checks if the given Object is a JSONObject and the JSONObject is non-empty . + * + * @param value The Object to be validated. + * @return true if the object is a non-null and non-empty JSONObject. + */ + public static boolean isValidJSONObject(Object value) { + return value instanceof JSONObject && !((JSONObject) value).isEmpty(); + } + + /** + * Checks if the given object is a valid date-time string and it is non empty. + * + * @param value The object to be checked for a valid date-time format. + * @return True if the object is a non-empty string in ISO date-time format, false otherwise. + */ + private static final DateTimeFormatter dateTimeFormat = DateTimeFormatter.ISO_DATE_TIME; + + public static JSONObject isValidDateTimeObject(Object value) { + JSONObject validationResponse = new JSONObject(); + + if (value instanceof String && !((String) value).isEmpty()) { + try { + String dateTimeString = (String) value; + dateTimeFormat.parse(dateTimeString); + } catch (DateTimeParseException e) { + return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, + ErrorConstants.INVALID_DATE_TIME_FORMAT, ErrorConstants.PATH_VALID_TO_DATE); + } + } else { + return ConsentManageUtil.getValidationResponse(ErrorConstants.INVALID_REQ_PAYLOAD, + ErrorConstants.MISSING_DATE_TIME_FORMAT, ErrorConstants.PATH_VALID_TO_DATE); } - return false; + + validationResponse.put(ConsentExtensionConstants.IS_VALID, true); + return validationResponse; } + }