FINERACT-1744 - Idempotency support

- [x] Idempotency write move to filters
- [x] Idempotency integration test with success and failure
- [x] Added response status code to database
- [x] SynchronousCommandProcessingService not generate the command, just marked to the filter
- [x] IdempotencyStoreFilter store the command request if available.
- [x] Removed JpaExceptionHandler.java
This commit is contained in:
Janos Haber
2022-11-28 12:04:03 +01:00
committed by Arnold Gálovics
parent 25c0b6a08d
commit 5210bf7412
46 changed files with 1428 additions and 166 deletions
@@ -0,0 +1,158 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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 org.apache.fineract.integrationtests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.fineract.cob.data.BusinessStep;
import org.apache.fineract.cob.data.JobBusinessStepConfigData;
import org.apache.fineract.infrastructure.core.exception.AbstractIdempotentCommandException;
import org.apache.fineract.integrationtests.common.IdempotencyHelper;
import org.apache.fineract.integrationtests.common.Utils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class IdempotencyTest {
private ResponseSpecification responseSpec;
private RequestSpecification requestSpec;
public static final String LOAN_JOB_NAME = "LOAN_CLOSE_OF_BUSINESS";
public static final String LOAN_CATEGORY_NAME = "loan";
public static final String APPLY_CHARGE_TO_OVERDUE_LOANS = "APPLY_CHARGE_TO_OVERDUE_LOANS";
public static final String NOT_BELONGING_BUSINESS_STEP_NAME = "APPLY_CHARGE_TO_OVERDUE_LOANS_2";
public static final String LOAN_DELINQUENCY_CLASSIFICATION = "LOAN_DELINQUENCY_CLASSIFICATION";
@BeforeEach
public void setup() {
Utils.initializeRESTAssured();
this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build();
this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey());
this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build();
}
@Test
public void shouldUpdateStepOrder() {
JobBusinessStepConfigData originalStepConfig = IdempotencyHelper.getConfiguredBusinessStepsByJobName(requestSpec, responseSpec,
LOAN_JOB_NAME);
String idempotencyKeyHeader = UUID.randomUUID().toString();
List<BusinessStep> requestBody = new ArrayList<>();
requestBody.add(getBusinessSteps(1L, APPLY_CHARGE_TO_OVERDUE_LOANS));
Response response = IdempotencyHelper.updateBusinessStepOrder(requestSpec, responseSpec, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(requestBody), idempotencyKeyHeader);
Response responseSecond = IdempotencyHelper.updateBusinessStepOrder(requestSpec, responseSpec, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(requestBody), idempotencyKeyHeader);
Assertions.assertEquals(response.getBody().asString(), responseSecond.getBody().asString());
Assertions.assertNull(response.header(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
Assertions.assertNotNull(responseSecond.header(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
idempotencyKeyHeader = UUID.randomUUID().toString();
JobBusinessStepConfigData newStepConfig = IdempotencyHelper.getConfiguredBusinessStepsByJobName(requestSpec, responseSpec,
LOAN_JOB_NAME);
BusinessStep applyChargeStep = newStepConfig.getBusinessSteps().stream()
.filter(businessStep -> APPLY_CHARGE_TO_OVERDUE_LOANS.equals(businessStep.getStepName())).findFirst().get();
assertEquals(1, newStepConfig.getBusinessSteps().size());
assertEquals(1L, applyChargeStep.getOrder());
requestBody.add(getBusinessSteps(2L, LOAN_DELINQUENCY_CLASSIFICATION));
Response update = IdempotencyHelper.updateBusinessStepOrder(requestSpec, responseSpec, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(requestBody), idempotencyKeyHeader);
Response updateSecond = IdempotencyHelper.updateBusinessStepOrder(requestSpec, responseSpec, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(requestBody), idempotencyKeyHeader);
Assertions.assertNull(update.header(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
Assertions.assertNotNull(updateSecond.header(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
Assertions.assertEquals(update.getBody().asString(), updateSecond.getBody().asString());
newStepConfig = IdempotencyHelper.getConfiguredBusinessStepsByJobName(requestSpec, responseSpec, LOAN_JOB_NAME);
applyChargeStep = newStepConfig.getBusinessSteps().stream()
.filter(businessStep -> APPLY_CHARGE_TO_OVERDUE_LOANS.equals(businessStep.getStepName())).findFirst().get();
BusinessStep loanDelinquencyStep = newStepConfig.getBusinessSteps().stream()
.filter(businessStep -> LOAN_DELINQUENCY_CLASSIFICATION.equals(businessStep.getStepName())).findFirst().get();
assertEquals(2, newStepConfig.getBusinessSteps().size());
assertEquals(1L, applyChargeStep.getOrder());
assertEquals(2L, loanDelinquencyStep.getOrder());
requestBody.remove(1);
idempotencyKeyHeader = UUID.randomUUID().toString();
update = IdempotencyHelper.updateBusinessStepOrder(requestSpec, responseSpec, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(requestBody), idempotencyKeyHeader);
updateSecond = IdempotencyHelper.updateBusinessStepOrder(requestSpec, responseSpec, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(requestBody), idempotencyKeyHeader);
Assertions.assertNull(update.header(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
Assertions.assertNotNull(updateSecond.header(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
Assertions.assertEquals(update.getBody().asString(), updateSecond.getBody().asString());
newStepConfig = IdempotencyHelper.getConfiguredBusinessStepsByJobName(requestSpec, responseSpec, LOAN_JOB_NAME);
applyChargeStep = newStepConfig.getBusinessSteps().stream()
.filter(businessStep -> APPLY_CHARGE_TO_OVERDUE_LOANS.equals(businessStep.getStepName())).findFirst().get();
assertEquals(1, newStepConfig.getBusinessSteps().size());
assertEquals(1L, applyChargeStep.getOrder());
idempotencyKeyHeader = UUID.randomUUID().toString();
update = IdempotencyHelper.updateBusinessStepOrder(requestSpec, responseSpec, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(originalStepConfig.getBusinessSteps()), idempotencyKeyHeader);
updateSecond = IdempotencyHelper.updateBusinessStepOrder(requestSpec, responseSpec, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(originalStepConfig.getBusinessSteps()), idempotencyKeyHeader);
Assertions.assertNull(update.header(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
Assertions.assertNotNull(updateSecond.header(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
Assertions.assertEquals(update.getBody().asString(), updateSecond.getBody().asString());
}
@Test
public void shoudTheSecondRequestWithSameIdempotencyKeyWillFailureToo() {
ResponseSpecification responseSpecForError = new ResponseSpecBuilder().expectStatusCode(400).build();
List<BusinessStep> requestBody = new ArrayList<>();
String idempotencyKey = UUID.randomUUID().toString();
// IdempotencyHelper.configuredApiParameterErrorFromJsonString(response.getBody().asString())
Response response1 = IdempotencyHelper.updateBusinessStepOrderWithError(requestSpec, responseSpecForError, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(requestBody), idempotencyKey);
Assertions.assertNull(response1.getHeader(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
String originalBody = response1.getBody().asString();
Response response2 = IdempotencyHelper.updateBusinessStepOrderWithError(requestSpec, responseSpecForError, LOAN_JOB_NAME,
IdempotencyHelper.toJsonString(requestBody), idempotencyKey);
Assertions.assertNotNull(response2.getHeader(AbstractIdempotentCommandException.IDEMPOTENT_CACHE_HEADER));
Assertions.assertEquals(originalBody, response2.getBody().asString());
}
private BusinessStep getBusinessSteps(Long order, String stepName) {
BusinessStep businessStep = new BusinessStep();
businessStep.setStepName(stepName);
businessStep.setOrder(order);
return businessStep;
}
}
@@ -0,0 +1,95 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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 org.apache.fineract.integrationtests.common;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.cob.data.BusinessStep;
import org.apache.fineract.cob.data.JobBusinessStepConfigData;
import org.apache.fineract.cob.data.JobBusinessStepDetail;
@Slf4j
public final class IdempotencyHelper {
private static final String BUSINESS_STEPS_API_URL_START = "/fineract-provider/api/v1/jobs/";
private static final String BUSINESS_STEPS_API_URL_END = "/steps?" + Utils.TENANT_IDENTIFIER;
private static final String GET_AVAILABLE_BUSINESS_STEPS_API_URL_END = "/available-steps?" + Utils.TENANT_IDENTIFIER;
private IdempotencyHelper() {
}
public static String toJsonString(final List<BusinessStep> batchRequests) {
return new Gson().toJson(new BusinessStepWrapper(batchRequests));
}
public static JobBusinessStepConfigData configuredBusinessStepFromJsonString(final String json) {
return new Gson().fromJson(json, new TypeToken<JobBusinessStepConfigData>() {}.getType());
}
private static JobBusinessStepDetail availableBusinessStepFromJsonString(final String json) {
return new Gson().fromJson(json, new TypeToken<JobBusinessStepDetail>() {}.getType());
}
public static JobBusinessStepConfigData getConfiguredBusinessStepsByJobName(final RequestSpecification requestSpec,
final ResponseSpecification responseSpec, String jobName) {
final String response = Utils.performServerGet(requestSpec, responseSpec,
BUSINESS_STEPS_API_URL_START + jobName + BUSINESS_STEPS_API_URL_END);
log.info("BusinessStepConfigurationHelper Response: {}", response);
return configuredBusinessStepFromJsonString(response);
}
public static JobBusinessStepDetail getAvailableBusinessStepsByJobName(final RequestSpecification requestSpec,
final ResponseSpecification responseSpec, String jobName) {
final String response = Utils.performServerGet(requestSpec, responseSpec,
BUSINESS_STEPS_API_URL_START + jobName + GET_AVAILABLE_BUSINESS_STEPS_API_URL_END);
log.info("BusinessStepConfigurationHelper Response: {}", response);
return availableBusinessStepFromJsonString(response);
}
public static Response updateBusinessStepOrder(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
String jobName, String jsonBodyToSend, String idempotencyKey) {
Response response = Utils.performServerPutRaw(requestSpec, responseSpec,
BUSINESS_STEPS_API_URL_START + jobName + BUSINESS_STEPS_API_URL_END,
request -> request.header("Idempotency-Key", idempotencyKey).body(jsonBodyToSend));
log.info("BusinessStepConfigurationHelper Response: {}", response.getBody().asString());
return response;
}
public static Response updateBusinessStepOrderWithError(final RequestSpecification requestSpec,
final ResponseSpecification responseSpec, String jobName, String jsonBodyToSend, String idempotencyKey) {
String url = BUSINESS_STEPS_API_URL_START + jobName + BUSINESS_STEPS_API_URL_END;
return Utils.performServerPutRaw(requestSpec, responseSpec, url,
request -> request.header("Idempotency-Key", idempotencyKey).body(jsonBodyToSend));
}
private static final class BusinessStepWrapper {
private List<BusinessStep> businessSteps;
private BusinessStepWrapper(List<BusinessStep> businessSteps) {
this.businessSteps = businessSteps;
}
}
}
@@ -53,6 +53,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.TimeZone;
import java.util.function.Function;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.HttpHostConnectException;
import org.slf4j.Logger;
@@ -164,6 +165,11 @@ public final class Utils {
return performServerGet(requestSpec, responseSpec, url, null);
}
public static Response performServerGetRaw(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String getURL, Function<RequestSpecification, RequestSpecification> requestMapper) {
return requestMapper.apply(given().spec(requestSpec)).expect().spec(responseSpec).log().ifError().when().get(getURL).andReturn();
}
public static <T> T performServerGet(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String getURL, final String jsonAttributeToGetBack) {
final String json = given().spec(requestSpec).expect().spec(responseSpec).log().ifError().when().get(getURL).andReturn().asString();
@@ -212,6 +218,11 @@ public final class Utils {
return (T) JsonPath.from(json).get(jsonAttributeToGetBack);
}
public static Response performServerPutRaw(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String putURL, Function<RequestSpecification, RequestSpecification> bodyMapper) {
return bodyMapper.apply(given().spec(requestSpec)).expect().spec(responseSpec).log().ifError().when().put(putURL).andReturn();
}
public static String performServerPut(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String putURL, final String jsonBodyToSend) {
return performServerPut(requestSpec, responseSpec, putURL, jsonBodyToSend, null);