Skip to content

Commit

Permalink
VRP initiation flow implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kalpanakanagasabai committed Dec 21, 2023
1 parent c91c818 commit 3205f1b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ErrorConstants {
public static final String PAYLOAD_INVALID = "Consent validation failed due to invalid initiation payload";
public static final String NOT_JSON_OBJECT_ERROR = "Payload is not a JSON object";
public static final String PAYLOAD_FORMAT_ERROR = "Request Payload is not in correct JSON format";
public static final String PAYLOAD_FORMAT_ERROR_VALID_TO_DATE = "Invalid valid to date parameter in the payload" +
public static final String PAYLOAD_FORMAT_ERROR_VALID_TO_DATE = "Invalid valid_to_date parameter in the payload" +
"for valid to date";
public static final String PAYLOAD_FORMAT_ERROR_DEBTOR_ACC = "Request Payload is not in correct JSON " +
"format for debtor account";
Expand All @@ -65,7 +65,7 @@ public class ErrorConstants {
public static final String PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_AMOUNT = "Invalid maximum individual amount";

public static final String PAYLOAD_FORMAT_ERROR_MAXIMUM_INDIVIDUAL_CURRENCY = "Invalid maximum individual amount" +
" currency";
"currency";
public static final String PAYLOAD_FORMAT_ERROR_INITIATION = "Request Payload is not in correct JSON format" +
" for initiation key";
public static final String PAYLOAD_FORMAT_ERROR_RISK = "Request Payload is not in correct JSON format" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ public void handleConsentManagePost(ConsentManageData consentManageData) {

JSONObject validationResponse = VRPConsentRequestValidator.validateVRPPayload(request);

if (!((boolean) validationResponse.get(ConsentExtensionConstants.IS_VALID))) {
if (!(Boolean.parseBoolean(validationResponse.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(validationResponse.get(ConsentExtensionConstants.ERRORS));
throw new ConsentException((ResponseStatus) validationResponse
.get(ConsentExtensionConstants.HTTP_CODE),
String.valueOf(validationResponse.get(ConsentExtensionConstants.ERRORS)));
}


if (StringUtils.isEmpty(consentManageData.getHeaders()
.get(ConsentExtensionConstants.X_IDEMPOTENCY_KEY))) {
log.error(ErrorConstants.IDEMPOTENCY_KEY_NOT_FOUND);
Expand All @@ -87,8 +86,6 @@ public void handleConsentManagePost(ConsentManageData consentManageData) {
ErrorConstants.PAYMENT_INITIATION_HANDLE_ERROR);
}
}


/**
* This method is responsible for handling the GET request for retrieving consent initiation details.
* It validates the consent ID, checks if the consent exists,verifies if the consent belongs to the
Expand All @@ -109,10 +106,11 @@ public void handleConsentManageGet(ConsentManageData consentManageData) {
if (!consent.getClientID().equals(consentManageData.getClientId())) {
// Throwing same error as null scenario since client will not be able to identify if consent
// exists if consent does not belong to them

log.debug(String.format("ClientIds missmatch. " +
"consent client id: %s, consent manage data client id: %s",
consent.getClientID(), consentManageData.getClientId()));
if (log.isDebugEnabled()) {
log.debug(String.format("ClientIds missmatch. " +
"consent client id: %s, consent manage data client id: %s",
consent.getClientID(), consentManageData.getClientId()));
}
throw new ConsentException(ResponseStatus.BAD_REQUEST,
ErrorConstants.NO_CONSENT_FOR_CLIENT_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static JSONObject validateVRPPayload(Object request) {
//Check request body is valid and not empty
JSONObject dataValidationResult = ConsentManageUtil.validateInitiationDataBody(requestBody);

if (!(boolean) dataValidationResult.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(dataValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(dataValidationResult.get(ConsentExtensionConstants.ERRORS));
return dataValidationResult;
}
Expand All @@ -74,7 +74,7 @@ public static JSONObject validateVRPPayload(Object request) {
//Check consent initiation is valid and not empty
JSONObject initiationValidationResult = VRPConsentRequestValidator.validateConsentInitiation(requestBody);

if (!(boolean) initiationValidationResult.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(initiationValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(initiationValidationResult.get(ConsentExtensionConstants.ERRORS));
return initiationValidationResult;
}
Expand All @@ -83,15 +83,15 @@ public static JSONObject validateVRPPayload(Object request) {
JSONObject controlParameterValidationResult = VRPConsentRequestValidator.
validateConsentControlParameters(requestBody);

if (!(boolean) controlParameterValidationResult.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(controlParameterValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(controlParameterValidationResult.get(ConsentExtensionConstants.ERRORS));
return controlParameterValidationResult;
}


JSONObject riskValidationResult = VRPConsentRequestValidator.validateConsentRisk(requestBody);

if (!(boolean) riskValidationResult.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(riskValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(riskValidationResult.get(ConsentExtensionConstants.ERRORS));
return riskValidationResult;
}
Expand All @@ -106,17 +106,16 @@ public static JSONObject validateVRPPayload(Object request) {
* @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 non-null and non-empty JSONObject.
* Checks if the given object is a valid date-time string and it is non empty.
*
* @param value The Object to be validated.
* @return true if the Object is a non-null and non-empty string representing a valid date-time.
* @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) {

Expand Down Expand Up @@ -152,20 +151,20 @@ public static JSONObject validateControlParameters(JSONObject controlParameters)
JSONObject validationResponse = new JSONObject();

JSONObject maximumIndividualAmountResult = validateMaximumIndividualAmount(controlParameters);
if (!(boolean) maximumIndividualAmountResult.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(maximumIndividualAmountResult.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(maximumIndividualAmountResult.get(ConsentExtensionConstants.ERRORS));
return maximumIndividualAmountResult;
}

JSONObject validationResponses = validateParameterDateTime(controlParameters);
if (!(boolean) validationResponses.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(validationResponses.getAsString(ConsentExtensionConstants.IS_VALID)))){
log.error(validationResponses.get(ConsentExtensionConstants.ERRORS));
return validationResponses;
}

// Validate Periodic Limits
JSONObject periodicLimitsValidationResult = validatePeriodicLimits(controlParameters);
if (!(boolean) periodicLimitsValidationResult.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(validationResponses.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(ErrorConstants.PAYLOAD_INVALID);
return validationResponses;
}
Expand All @@ -185,7 +184,12 @@ public static boolean isValidJSONArray(Object value) {
return value instanceof JSONArray;
}


/**
* Validates the Maximum Individual Amount in the control parameters of a consent request.
*
* @param controlParameters The JSON object representing the control parameters of the consent request.
* @return A JSON object containing the validation response.
*/
public static JSONObject validateMaximumIndividualAmount(JSONObject controlParameters) {

JSONObject validationResponse = new JSONObject();
Expand Down Expand Up @@ -310,6 +314,14 @@ public static JSONObject validatePeriodicLimits(JSONObject controlParameters) {
return validationResponse;
}


/**
* Validates the date-time parameters in the control parameters of a consent request.
*
* @param controlParameters The JSON object representing the control parameters of the consent request.
* @return A JSON object containing the validation response. If the date-time parameters are valid,
* it sets the "IS_VALID" field to true; otherwise, it contains an error response.
*/
public static JSONObject validateParameterDateTime(JSONObject controlParameters) {
JSONObject validationResponse = new JSONObject();

Expand Down Expand Up @@ -378,9 +390,10 @@ public static JSONObject validateVRPInitiationPayload(JSONObject initiation) {
ErrorConstants.INVALID_PARAMETER_DEBTOR_ACC,
ErrorConstants.PATH_DEBTOR_ACCOUNT);
}

JSONObject validationResult = ConsentManageUtil.validateDebtorAccount((JSONObject) debtorAccount);

if (!(boolean) validationResult.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(validationResult.get(ConsentExtensionConstants.ERRORS));
return validationResult;
}
Expand All @@ -404,7 +417,7 @@ public static JSONObject validateVRPInitiationPayload(JSONObject initiation) {

JSONObject validationResult = ConsentManageUtil.validateCreditorAccount((JSONObject) creditorAccount);

if (!Boolean.parseBoolean(String.valueOf(validationResult))) {
if (!(Boolean.parseBoolean(validationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(validationResult.get(ConsentExtensionConstants.ERRORS));
return validationResult;
}
Expand Down Expand Up @@ -510,7 +523,7 @@ public static JSONObject validateConsentInitiation(JSONObject request) {
JSONObject initiationValidationResult = VRPConsentRequestValidator
.validateVRPInitiationPayload((JSONObject) data.get(ConsentExtensionConstants.INITIATION));

if (!(boolean) initiationValidationResult.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(initiationValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(initiationValidationResult.get(ConsentExtensionConstants.ERRORS));
return initiationValidationResult;
}
Expand Down Expand Up @@ -553,7 +566,7 @@ public static JSONObject validateConsentControlParameters(JSONObject request) {
VRPConsentRequestValidator.validateControlParameters((JSONObject)
data.get(ConsentExtensionConstants.CONTROL_PARAMETERS));

if (!(boolean) controlParameterValidationResult.get(ConsentExtensionConstants.IS_VALID)) {
if (!(Boolean.parseBoolean(controlParameterValidationResult.getAsString(ConsentExtensionConstants.IS_VALID)))) {
log.error(controlParameterValidationResult.get(ConsentExtensionConstants.ERRORS));
return controlParameterValidationResult;
}
Expand Down

0 comments on commit 3205f1b

Please sign in to comment.