-
Notifications
You must be signed in to change notification settings - Fork 2.6k
FINERACT-2165: Migrate Schedulerjobhelper internals to typed Feign cl… #6115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
elnafateh
wants to merge
1
commit into
apache:develop
Choose a base branch
from
elnafateh:FINERACT-2165/migrate-schedulerjobhelper
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,175 +18,132 @@ | |
| */ | ||
| package org.apache.fineract.integrationtests.common; | ||
|
|
||
| import static org.apache.fineract.infrastructure.jobs.api.SchedulerJobApiConstants.SHORT_NAME_PARAM; | ||
| import static org.apache.fineract.client.feign.util.FeignCalls.executeVoid; | ||
| import static org.apache.fineract.client.feign.util.FeignCalls.ok; | ||
| import static org.awaitility.Awaitility.await; | ||
| import static org.hamcrest.core.Is.is; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import io.restassured.builder.ResponseSpecBuilder; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.JsonPrimitive; | ||
| import com.google.gson.JsonSerializer; | ||
| import io.restassured.specification.RequestSpecification; | ||
| import io.restassured.specification.ResponseSpecification; | ||
| import java.time.Duration; | ||
| import java.util.HashMap; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.OffsetDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.fineract.client.models.CommandProcessingResult; | ||
| import org.apache.fineract.client.models.ExecuteJobRequest; | ||
| import org.apache.fineract.client.models.GetJobsResponse; | ||
| import org.apache.fineract.client.models.GetSchedulerResponse; | ||
| import org.apache.fineract.client.models.PutJobsJobIDRequest; | ||
| import org.apache.fineract.client.util.Calls; | ||
| import org.hamcrest.MatcherAssert; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Feign-based. Still instance-shaped (see constructor) purely for source compatibility with existing call sites; a | ||
| * follow-up PR will static-ify this class and update all callers in one mechanical pass. | ||
| */ | ||
| public class SchedulerJobHelper { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(SchedulerJobHelper.class); | ||
| private final RequestSpecification requestSpec; | ||
| private final ResponseSpecification response200Spec; | ||
| private static final Gson GSON = new GsonBuilder() | ||
| .registerTypeAdapter(OffsetDateTime.class, | ||
| (JsonSerializer<OffsetDateTime>) (src, type, ctx) -> src == null ? null | ||
| : new JsonPrimitive(DateTimeFormatter.ISO_INSTANT.format(src.toInstant()))) | ||
| .registerTypeAdapter(LocalDateTime.class, | ||
| (JsonSerializer<LocalDateTime>) (src, type, ctx) -> src == null ? null | ||
| : new JsonPrimitive(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").format(src))) | ||
| .registerTypeAdapter(LocalDate.class, | ||
| (JsonSerializer<LocalDate>) (src, type, ctx) -> src == null ? null : new JsonPrimitive(src.toString())) | ||
| .create(); | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| /** | ||
| * @deprecated the {@code requestSpec} parameter is unused now that this Helper is Feign-based. Retained only so | ||
| * existing {@code new SchedulerJobHelper(requestSpec)} call sites keep compiling; prefer relying on | ||
| * static usage once callers are migrated in a follow-up PR. | ||
| */ | ||
| @Deprecated | ||
| public SchedulerJobHelper(final RequestSpecification requestSpec) { | ||
| this.requestSpec = requestSpec; | ||
| this.response200Spec = new ResponseSpecBuilder().expectStatusCode(200).build(); | ||
| // no-op: retained for source compatibility with existing callers | ||
| } | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| private List<Map<String, Object>> getAllSchedulerJobs() { | ||
| final String GET_ALL_SCHEDULER_JOBS_URL = "/fineract-provider/api/v1/jobs?" + Utils.TENANT_IDENTIFIER; | ||
| private List<GetJobsResponse> getAllSchedulerJobs() { | ||
| LOG.info("------------------------ RETRIEVING ALL SCHEDULER JOBS -------------------------"); | ||
| List<Map<String, Object>> response = Utils.performServerGet(requestSpec, response200Spec, GET_ALL_SCHEDULER_JOBS_URL, ""); | ||
| List<GetJobsResponse> response = ok( | ||
| () -> FineractFeignClientHelper.getFineractFeignClient().schedulerJob().retrieveAllSchedulerJobs()); | ||
| assertNotNull(response); | ||
| return response; | ||
| } | ||
|
|
||
| private <T> List<T> getAllSchedulerJobDetails(Function<Map<String, Object>, T> mapper) { | ||
| private <T> List<T> getAllSchedulerJobDetails(Function<GetJobsResponse, T> mapper) { | ||
| return getAllSchedulerJobs().stream().map(mapper).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public List<Integer> getAllSchedulerJobIds() { | ||
| return getAllSchedulerJobDetails(map -> (Integer) map.get("jobId")); | ||
| return getAllSchedulerJobDetails(job -> job.getJobId().intValue()); | ||
| } | ||
|
|
||
| public List<String> getAllSchedulerJobNames() { | ||
| return getAllSchedulerJobDetails(map -> (String) map.get("displayName")); | ||
| return getAllSchedulerJobDetails(GetJobsResponse::getDisplayName); | ||
| } | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| public Map<String, Object> getSchedulerJobById(int jobId) { | ||
| final String GET_SCHEDULER_JOB_BY_ID_URL = "/fineract-provider/api/v1/jobs/" + jobId + "?" + Utils.TENANT_IDENTIFIER; | ||
| LOG.info("------------------------ RETRIEVING SCHEDULER JOB BY ID -------------------------"); | ||
| final Map<String, Object> response = Utils.performServerGet(requestSpec, response200Spec, GET_SCHEDULER_JOB_BY_ID_URL, ""); | ||
| LOG.info("{}", response.toString()); | ||
| GetJobsResponse response = ok( | ||
| () -> FineractFeignClientHelper.getFineractFeignClient().schedulerJob().retrieveOneSchedulerJob((long) jobId)); | ||
| assertNotNull(response); | ||
| return response; | ||
| LOG.info("{}", response); | ||
| return toMap(response); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return the real object, do not map with gson |
||
| } | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| public Boolean getSchedulerStatus() { | ||
| final String GET_SCHEDULER_STATUS_URL = "/fineract-provider/api/v1/scheduler?" + Utils.TENANT_IDENTIFIER; | ||
| LOG.info("------------------------ RETRIEVING SCHEDULER STATUS -------------------------"); | ||
| final Map<String, Object> response = Utils.performServerGet(requestSpec, response200Spec, GET_SCHEDULER_STATUS_URL, ""); | ||
| return (Boolean) response.get("active"); | ||
| GetSchedulerResponse response = ok(() -> FineractFeignClientHelper.getFineractFeignClient().scheduler().retrieveSchedulerStatus()); | ||
| return response.getActive(); | ||
| } | ||
|
|
||
| public void updateSchedulerStatus(final boolean on) { | ||
| String command = on ? "start" : "stop"; | ||
| Calls.ok(FineractClientHelper.getFineractClient().jobsScheduler.handleCommandsScheduler(command)); | ||
| executeVoid(() -> FineractFeignClientHelper.getFineractFeignClient().scheduler().handleCommandsScheduler(command)); | ||
| } | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| public Map<String, Object> updateSchedulerJob(int jobId, final boolean active) { | ||
| final String UPDATE_SCHEDULER_JOB_URL = "/fineract-provider/api/v1/jobs/" + jobId + "?" + Utils.TENANT_IDENTIFIER; | ||
| LOG.info("------------------------ UPDATING SCHEDULER JOB -------------------------"); | ||
| final Map<String, Object> response = Utils.performServerPut(requestSpec, response200Spec, UPDATE_SCHEDULER_JOB_URL, | ||
| updateSchedulerJobAsJSON(active), "changes"); | ||
| return response; | ||
| CommandProcessingResult response = ok(() -> FineractFeignClientHelper.getFineractFeignClient().schedulerJob() | ||
| .updateJobDetail((long) jobId, new PutJobsJobIDRequest().active(active))); | ||
| return response.getChanges(); | ||
| } | ||
|
|
||
| public void updateSchedulerJob(long jobId, PutJobsJobIDRequest request) { | ||
| Calls.ok(FineractClientHelper.getFineractClient().jobs.updateJobDetail(jobId, request)); | ||
| } | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| private static String updateSchedulerJobAsJSON(final boolean active) { | ||
| final Map<String, String> map = new HashMap<>(); | ||
| map.put("active", Boolean.toString(active)); | ||
| LOG.info("map : {}", map); | ||
| return new Gson().toJson(map); | ||
| ok(() -> FineractFeignClientHelper.getFineractFeignClient().schedulerJob().updateJobDetail(jobId, request)); | ||
| } | ||
|
|
||
| public void runSchedulerJob(int jobId) { | ||
| Calls.ok(FineractClientHelper.getFineractClient().jobs.executeJob((long) jobId, "executeJob")); | ||
| executeVoid(() -> FineractFeignClientHelper.getFineractFeignClient().schedulerJob().executeJob((long) jobId, "executeJob", | ||
| new ExecuteJobRequest())); | ||
| } | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| public void runSchedulerJob(int jobId, ResponseSpecification responseSpec) { | ||
| final String RUN_SCHEDULER_JOB_URL = "/fineract-provider/api/v1/jobs/" + jobId + "?command=executeJob&" + Utils.TENANT_IDENTIFIER; | ||
| LOG.info("------------------------ RUN SCHEDULER JOB -------------------------"); | ||
| Utils.performServerPost(requestSpec, responseSpec, RUN_SCHEDULER_JOB_URL, runSchedulerJobAsJSON(), null); | ||
| } | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| public void runSchedulerJobByShortName(String shortName) { | ||
| final ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(202).build(); | ||
| runSchedulerJobByShortName(shortName, responseSpec); | ||
| } | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| public void runSchedulerJobByShortName(String shortName, ResponseSpecification responseSpec) { | ||
| final String RUN_SCHEDULER_JOB_URL = "/fineract-provider/api/v1/jobs/" + SHORT_NAME_PARAM + "/" + shortName + "?command=executeJob&" | ||
| + Utils.TENANT_IDENTIFIER; | ||
| LOG.info("------------------------ RUN SCHEDULER JOB -------------------------"); | ||
| Utils.performServerPost(requestSpec, responseSpec, RUN_SCHEDULER_JOB_URL, runSchedulerJobAsJSON(), null); | ||
| } | ||
|
|
||
| // TODO: Rewrite to use fineract-client instead! | ||
| // Example: org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper.disburseLoan(java.lang.Long, | ||
| // org.apache.fineract.client.models.PostLoansLoanIdRequest) | ||
| @Deprecated(forRemoval = true) | ||
| private static String runSchedulerJobAsJSON() { | ||
| final Map<String, String> map = new HashMap<>(); | ||
| String runSchedulerJob = new Gson().toJson(map); | ||
| LOG.info(runSchedulerJob); | ||
| return runSchedulerJob; | ||
| executeVoid(() -> FineractFeignClientHelper.getFineractFeignClient().schedulerJob().executeJobByShortName(shortName, "executeJob", | ||
| new ExecuteJobRequest())); | ||
| } | ||
|
|
||
| public int getSchedulerJobIdByName(String jobName) { | ||
| List<Map<String, Object>> allSchedulerJobsData = getAllSchedulerJobs(); | ||
| for (Integer jobIndex = 0; jobIndex < allSchedulerJobsData.size(); jobIndex++) { | ||
| if (allSchedulerJobsData.get(jobIndex).get("displayName").equals(jobName)) { | ||
| return (Integer) allSchedulerJobsData.get(jobIndex).get("jobId"); | ||
| List<GetJobsResponse> allSchedulerJobsData = getAllSchedulerJobs(); | ||
| for (GetJobsResponse job : allSchedulerJobsData) { | ||
| if (jobName.equals(job.getDisplayName())) { | ||
| return job.getJobId().intValue(); | ||
| } | ||
| } | ||
| throw new IllegalArgumentException( | ||
|
|
@@ -195,7 +152,7 @@ public int getSchedulerJobIdByName(String jobName) { | |
|
|
||
| public Long getSchedulerJobIdByShortName(String shortName) { | ||
| LOG.info("------------------------ RETRIEVING SCHEDULER JOB ID BY SHORT NAME -------------------------"); | ||
| GetJobsResponse job = Calls.ok(FineractClientHelper.getFineractClient().jobs.retrieveByShortName(shortName)); | ||
| GetJobsResponse job = ok(() -> FineractFeignClientHelper.getFineractFeignClient().schedulerJob().retrieveByShortName(shortName)); | ||
| assertNotNull(job); | ||
| return job.getJobId(); | ||
| } | ||
|
|
@@ -265,7 +222,7 @@ private void awaitJob(long jobId, Long previousRunHistoryId) { | |
| // (if any) | ||
| String status = (String) finalRunHistory.get("status"); | ||
| if (!status.equals("success")) { | ||
| fail("Job status is not success: " + finalRunHistory.toString()); | ||
| fail("Job status is not success for jobId=" + jobId + ": " + finalRunHistory.toString()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -285,14 +242,25 @@ private Long getRunHistoryId(Map<String, Object> runHistory) { | |
|
|
||
| @SuppressWarnings("unchecked") | ||
| private Map<String, Object> getLatestJobRunHistory(long jobId) { | ||
| final String GET_LATEST_SCHEDULER_JOB_RUN_HISTORY_URL = "/fineract-provider/api/v1/jobs/" + jobId | ||
| + "/runhistory?offset=0&limit=1&orderBy=id&sortOrder=DESC&" + Utils.TENANT_IDENTIFIER; | ||
| LOG.info("------------------------ RETRIEVING LATEST SCHEDULER JOB RUN HISTORY -------------------------"); | ||
| Map<String, Object> response = Utils.performServerGet(requestSpec, response200Spec, GET_LATEST_SCHEDULER_JOB_RUN_HISTORY_URL, ""); | ||
| List<Map<String, Object>> pageItems = (List<Map<String, Object>>) response.get("pageItems"); | ||
| Object response = ok( | ||
| () -> FineractFeignClientHelper.getFineractFeignClient().schedulerJob().retrieveHistory(jobId, 0, 1, "id", "DESC")); | ||
| Map<String, Object> responseMap = toMap(response); | ||
| List<Map<String, Object>> pageItems = (List<Map<String, Object>>) responseMap.get("pageItems"); | ||
| if (pageItems == null || pageItems.isEmpty()) { | ||
| return null; | ||
| } | ||
| return pageItems.get(0); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a typed Feign response into a plain {@code Map<String, Object>} via a JSON round-trip, preserving the | ||
| * exact field-name contract that callers previously relied on when this Helper deserialized raw RestAssured JSON | ||
| * responses into generic maps. Uses a Gson instance with explicit {@code java.time} serializers (rather than a bare | ||
| * {@code new Gson()}) so date/time fields round-trip as plain ISO-8601 strings, matching what the original raw | ||
| * server JSON produced, instead of Gson's default reflective (and much uglier) field-by-field breakdown. | ||
| */ | ||
| private static Map<String, Object> toMap(Object typedResponse) { | ||
| return GSON.fromJson(GSON.toJson(typedResponse), Map.class); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We dont need GSON... we are moving away from this. use typed objects