first step to clean up in SchedulerJobsTest (preparing for FINERACT-922)

make two methods in SchedulerJobHelper private

and start to un-static-ify SchedulerJobHelper

This does NOT, yet, fix FINERACT-922.
This commit is contained in:
Michael Vorburger
2020-05-03 19:49:27 +02:00
committed by Michael Vorburger ⛑️
parent 90dec19201
commit 85bf8dc3e9
2 changed files with 64 additions and 56 deletions
@@ -18,16 +18,18 @@
*/ */
package org.apache.fineract.integrationtests; package org.apache.fineract.integrationtests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import io.restassured.builder.RequestSpecBuilder; import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType; import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification; import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification; import io.restassured.specification.ResponseSpecification;
import java.util.ArrayList; import java.util.List;
import java.util.HashMap; import java.util.Map;
import org.apache.fineract.integrationtests.common.SchedulerJobHelper; import org.apache.fineract.integrationtests.common.SchedulerJobHelper;
import org.apache.fineract.integrationtests.common.Utils; import org.apache.fineract.integrationtests.common.Utils;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@@ -37,7 +39,6 @@ public class SchedulerJobsTest {
private ResponseSpecification responseSpec; private ResponseSpecification responseSpec;
private RequestSpecification requestSpec; private RequestSpecification requestSpec;
private ResponseSpecification responseSpecForSchedulerJob;
private SchedulerJobHelper schedulerJobHelper; private SchedulerJobHelper schedulerJobHelper;
@Before @Before
@@ -46,41 +47,40 @@ public class SchedulerJobsTest {
this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build();
this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey());
this.requestSpec.header("Fineract-Platform-TenantId", "default"); this.requestSpec.header("Fineract-Platform-TenantId", "default");
this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); this.schedulerJobHelper = new SchedulerJobHelper(this.requestSpec);
this.responseSpecForSchedulerJob = new ResponseSpecBuilder().expectStatusCode(202).build(); }
@Test
public void testGetAndUpdateSchedulerStatus() throws InterruptedException {
// Retrieving Status of Scheduler
Boolean schedulerStatus = this.schedulerJobHelper.getSchedulerStatus();
if (schedulerStatus == true) {
this.schedulerJobHelper.updateSchedulerStatus("stop");
schedulerStatus = this.schedulerJobHelper.getSchedulerStatus();
// Verifying Status of the Scheduler after stopping
assertEquals("Verifying Scheduler Job Status", false, schedulerStatus);
} else {
this.schedulerJobHelper.updateSchedulerStatus("start");
schedulerStatus = this.schedulerJobHelper.getSchedulerStatus();
// Verifying Status of the Scheduler after starting
assertEquals("Verifying Scheduler Job Status", true, schedulerStatus);
}
} }
@Test @Test
@Ignore // TODO FINERACT-852 @Ignore // TODO FINERACT-852
public void testSchedulerJobs() throws InterruptedException { public void testSchedulerJobs() throws InterruptedException {
this.schedulerJobHelper = new SchedulerJobHelper(this.requestSpec, this.responseSpec);
// Retrieving Status of Scheduler
HashMap schedulerStatus = this.schedulerJobHelper.getSchedulerStatus(this.requestSpec, this.responseSpec);
Boolean status = (Boolean) schedulerStatus.get("active");
if (status == true) {
this.schedulerJobHelper.updateSchedulerStatus(this.requestSpec, this.responseSpecForSchedulerJob, "stop");
schedulerStatus = this.schedulerJobHelper.getSchedulerStatus(this.requestSpec, this.responseSpec);
// Verifying Status of the Scheduler after updation
Assert.assertEquals("Verifying Scheduler Job Status", false, schedulerStatus.get("active"));
} else {
this.schedulerJobHelper.updateSchedulerStatus(this.requestSpec, this.responseSpecForSchedulerJob, "start");
schedulerStatus = this.schedulerJobHelper.getSchedulerStatus(this.requestSpec, this.responseSpec);
// Verifying Status of the Scheduler after updation
Assert.assertEquals("Verifying Scheduler Job Status", true, schedulerStatus.get("active"));
}
// Retrieving All Scheduler Jobs // Retrieving All Scheduler Jobs
ArrayList<HashMap> allSchedulerJobsData = this.schedulerJobHelper.getAllSchedulerJobs(this.requestSpec, this.responseSpec); List<Map> allSchedulerJobsData = this.schedulerJobHelper.getAllSchedulerJobs(this.requestSpec, this.responseSpec);
Assert.assertNotNull(allSchedulerJobsData); assertNotNull(allSchedulerJobsData);
for (Integer jobIndex = 0; jobIndex < allSchedulerJobsData.size(); jobIndex++) { for (Integer jobIndex = 0; jobIndex < allSchedulerJobsData.size(); jobIndex++) {
Integer jobId = (Integer) allSchedulerJobsData.get(jobIndex).get("jobId"); Integer jobId = (Integer) allSchedulerJobsData.get(jobIndex).get("jobId");
// Retrieving Scheduler Job by ID // Retrieving Scheduler Job by ID
HashMap schedulerJob = this.schedulerJobHelper.getSchedulerJobById(this.requestSpec, this.responseSpec, jobId.toString()); Map schedulerJob = this.schedulerJobHelper.getSchedulerJobById(this.requestSpec, this.responseSpec, jobId.toString());
Assert.assertNotNull(schedulerJob); assertNotNull(schedulerJob);
Boolean active = (Boolean) schedulerJob.get("active"); Boolean active = (Boolean) schedulerJob.get("active");
@@ -91,31 +91,31 @@ public class SchedulerJobsTest {
} }
// Updating Scheduler Job // Updating Scheduler Job
HashMap changes = this.schedulerJobHelper.updateSchedulerJob(this.requestSpec, this.responseSpec, jobId.toString(), Map changes = this.schedulerJobHelper.updateSchedulerJob(this.requestSpec, this.responseSpec, jobId.toString(),
active.toString()); active.toString());
// Verifying Scheduler Job updates // Verifying Scheduler Job updates
Assert.assertEquals("Verifying Scheduler Job Updates", active, changes.get("active")); assertEquals("Verifying Scheduler Job Updates", active, changes.get("active"));
// Executing Scheduler Job // Executing Scheduler Job
this.schedulerJobHelper.runSchedulerJob(this.requestSpec, jobId.toString()); this.schedulerJobHelper.runSchedulerJob(this.requestSpec, jobId.toString());
// Retrieving Scheduler Job by ID // Retrieving Scheduler Job by ID
schedulerJob = this.schedulerJobHelper.getSchedulerJobById(this.requestSpec, this.responseSpec, jobId.toString()); schedulerJob = this.schedulerJobHelper.getSchedulerJobById(this.requestSpec, this.responseSpec, jobId.toString());
Assert.assertNotNull(schedulerJob); assertNotNull(schedulerJob);
// Waiting for Job to complete // Waiting for Job to complete
while ((Boolean) schedulerJob.get("currentlyRunning") == true) { while ((Boolean) schedulerJob.get("currentlyRunning") == true) {
Thread.sleep(15000); Thread.sleep(15000);
schedulerJob = this.schedulerJobHelper.getSchedulerJobById(this.requestSpec, this.responseSpec, jobId.toString()); schedulerJob = this.schedulerJobHelper.getSchedulerJobById(this.requestSpec, this.responseSpec, jobId.toString());
Assert.assertNotNull(schedulerJob); assertNotNull(schedulerJob);
System.out.println("Job " +jobId.toString() +" is Still Running"); System.out.println("Job " +jobId.toString() +" is Still Running");
} }
ArrayList<HashMap> jobHistoryData = this.schedulerJobHelper.getSchedulerJobHistory(this.requestSpec, this.responseSpec, List<Map> jobHistoryData = this.schedulerJobHelper.getSchedulerJobHistory(this.requestSpec, this.responseSpec,
jobId.toString()); jobId.toString());
// Verifying the Status of the Recently executed Scheduler Job // Verifying the Status of the Recently executed Scheduler Job
Assert.assertFalse("Job History is empty :( Was it too slow? Failures in background job?", jobHistoryData.isEmpty()); assertFalse("Job History is empty :( Was it too slow? Failures in background job?", jobHistoryData.isEmpty());
Assert.assertEquals("Verifying Last Scheduler Job Status", "success", assertEquals("Verifying Last Scheduler Job Status", "success",
jobHistoryData.get(jobHistoryData.size() - 1).get("status")); jobHistoryData.get(jobHistoryData.size() - 1).get("status"));
} }
} }
@@ -25,20 +25,29 @@ import io.restassured.specification.ResponseSpecification;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
public class SchedulerJobHelper { public class SchedulerJobHelper {
private final RequestSpecification requestSpec; private final RequestSpecification requestSpec;
private final ResponseSpecification responseSpec; private final ResponseSpecification response200Spec;
private final ResponseSpecification response202Spec;
public SchedulerJobHelper(final RequestSpecification requestSpec) {
this.requestSpec = requestSpec;
this.response200Spec = new ResponseSpecBuilder().expectStatusCode(200).build();
this.response202Spec = new ResponseSpecBuilder().expectStatusCode(202).build();
}
public SchedulerJobHelper(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { public SchedulerJobHelper(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) {
this.requestSpec = requestSpec; this.requestSpec = requestSpec;
this.responseSpec = responseSpec; this.response200Spec = responseSpec;
this.response202Spec = responseSpec;
} }
public static ArrayList getAllSchedulerJobs(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { public static List getAllSchedulerJobs(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) {
final String GET_ALL_SCHEDULER_JOBS_URL = "/fineract-provider/api/v1/jobs?" + Utils.TENANT_IDENTIFIER; final String GET_ALL_SCHEDULER_JOBS_URL = "/fineract-provider/api/v1/jobs?" + Utils.TENANT_IDENTIFIER;
System.out.println("------------------------ RETRIEVING ALL SCHEDULER JOBS -------------------------"); System.out.println("------------------------ RETRIEVING ALL SCHEDULER JOBS -------------------------");
final ArrayList response = Utils.performServerGet(requestSpec, responseSpec, GET_ALL_SCHEDULER_JOBS_URL, ""); final ArrayList response = Utils.performServerGet(requestSpec, responseSpec, GET_ALL_SCHEDULER_JOBS_URL, "");
@@ -53,41 +62,40 @@ public class SchedulerJobHelper {
return response; return response;
} }
public static HashMap getSchedulerStatus(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { public Boolean getSchedulerStatus() {
final String GET_SCHEDULER_STATUS_URL = "/fineract-provider/api/v1/scheduler?" + Utils.TENANT_IDENTIFIER; final String GET_SCHEDULER_STATUS_URL = "/fineract-provider/api/v1/scheduler?" + Utils.TENANT_IDENTIFIER;
System.out.println("------------------------ RETRIEVING SCHEDULER STATUS -------------------------"); System.out.println("------------------------ RETRIEVING SCHEDULER STATUS -------------------------");
final HashMap response = Utils.performServerGet(requestSpec, responseSpec, GET_SCHEDULER_STATUS_URL, ""); final Map<String, Object> response = Utils.performServerGet(requestSpec, response200Spec, GET_SCHEDULER_STATUS_URL, "");
return response; return (Boolean) response.get("active");
} }
public static void updateSchedulerStatus(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, public void updateSchedulerStatus(final String command) {
final String command) {
final String UPDATE_SCHEDULER_STATUS_URL = "/fineract-provider/api/v1/scheduler?command=" + command + "&" + Utils.TENANT_IDENTIFIER; final String UPDATE_SCHEDULER_STATUS_URL = "/fineract-provider/api/v1/scheduler?command=" + command + "&" + Utils.TENANT_IDENTIFIER;
System.out.println("------------------------ UPDATING SCHEDULER STATUS -------------------------"); System.out.println("------------------------ UPDATING SCHEDULER STATUS -------------------------");
Utils.performServerPost(requestSpec, responseSpec, UPDATE_SCHEDULER_STATUS_URL, runSchedulerJobAsJSON(), null); Utils.performServerPost(requestSpec, response202Spec, UPDATE_SCHEDULER_STATUS_URL, runSchedulerJobAsJSON(), null);
} }
public static HashMap updateSchedulerJob(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, public static Map updateSchedulerJob(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String jobId, final String active) { final String jobId, final String active) {
final String UPDATE_SCHEDULER_JOB_URL = "/fineract-provider/api/v1/jobs/" + jobId + "?" + Utils.TENANT_IDENTIFIER; final String UPDATE_SCHEDULER_JOB_URL = "/fineract-provider/api/v1/jobs/" + jobId + "?" + Utils.TENANT_IDENTIFIER;
System.out.println("------------------------ UPDATING SCHEDULER JOB -------------------------"); System.out.println("------------------------ UPDATING SCHEDULER JOB -------------------------");
final HashMap response = Utils.performServerPut(requestSpec, responseSpec, UPDATE_SCHEDULER_JOB_URL, final Map response = Utils.performServerPut(requestSpec, responseSpec, UPDATE_SCHEDULER_JOB_URL,
updateSchedulerJobAsJSON(active), "changes"); updateSchedulerJobAsJSON(active), "changes");
return response; return response;
} }
public static String updateSchedulerJobAsJSON(final String active) { private static String updateSchedulerJobAsJSON(final String active) {
final HashMap<String, String> map = new HashMap<>(); final Map<String, String> map = new HashMap<>();
map.put("active", active); map.put("active", active);
System.out.println("map : " + map); System.out.println("map : " + map);
return new Gson().toJson(map); return new Gson().toJson(map);
} }
public static ArrayList getSchedulerJobHistory(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, public static List getSchedulerJobHistory(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String jobId) { final String jobId) {
final String GET_SCHEDULER_STATUS_URL = "/fineract-provider/api/v1/jobs/" + jobId + "/runhistory?" + Utils.TENANT_IDENTIFIER; final String GET_SCHEDULER_STATUS_URL = "/fineract-provider/api/v1/jobs/" + jobId + "/runhistory?" + Utils.TENANT_IDENTIFIER;
System.out.println("------------------------ RETRIEVING SCHEDULER JOB HISTORY -------------------------"); System.out.println("------------------------ RETRIEVING SCHEDULER JOB HISTORY -------------------------");
final HashMap response = Utils.performServerGet(requestSpec, responseSpec, GET_SCHEDULER_STATUS_URL, ""); final Map response = Utils.performServerGet(requestSpec, responseSpec, GET_SCHEDULER_STATUS_URL, "");
return (ArrayList) response.get("pageItems"); return (ArrayList) response.get("pageItems");
} }
@@ -98,15 +106,15 @@ public class SchedulerJobHelper {
Utils.performServerPost(requestSpec, responseSpec, RUN_SCHEDULER_JOB_URL, runSchedulerJobAsJSON(), null); Utils.performServerPost(requestSpec, responseSpec, RUN_SCHEDULER_JOB_URL, runSchedulerJobAsJSON(), null);
} }
public static String runSchedulerJobAsJSON() { private static String runSchedulerJobAsJSON() {
final HashMap<String, String> map = new HashMap<>(); final Map<String, String> map = new HashMap<>();
String runSchedulerJob = new Gson().toJson(map); String runSchedulerJob = new Gson().toJson(map);
System.out.println(runSchedulerJob); System.out.println(runSchedulerJob);
return runSchedulerJob; return runSchedulerJob;
} }
public void executeJob(String jobName) throws InterruptedException { public void executeJob(String jobName) throws InterruptedException {
ArrayList<HashMap> allSchedulerJobsData = getAllSchedulerJobs(this.requestSpec, this.responseSpec); List<Map> allSchedulerJobsData = getAllSchedulerJobs(this.requestSpec, this.response200Spec);
Assert.assertNotNull(allSchedulerJobsData); Assert.assertNotNull(allSchedulerJobsData);
for (Integer jobIndex = 0; jobIndex < allSchedulerJobsData.size(); jobIndex++) { for (Integer jobIndex = 0; jobIndex < allSchedulerJobsData.size(); jobIndex++) {
@@ -117,18 +125,18 @@ public class SchedulerJobHelper {
runSchedulerJob(this.requestSpec, jobId.toString()); runSchedulerJob(this.requestSpec, jobId.toString());
// Retrieving Scheduler Job by ID // Retrieving Scheduler Job by ID
HashMap schedulerJob = getSchedulerJobById(this.requestSpec, this.responseSpec, jobId.toString()); Map schedulerJob = getSchedulerJobById(this.requestSpec, this.response200Spec, jobId.toString());
Assert.assertNotNull(schedulerJob); Assert.assertNotNull(schedulerJob);
// Waiting for Job to complete // Waiting for Job to complete
while ((Boolean) schedulerJob.get("currentlyRunning") == true) { while ((Boolean) schedulerJob.get("currentlyRunning") == true) {
Thread.sleep(15000); Thread.sleep(15000);
schedulerJob = getSchedulerJobById(this.requestSpec, this.responseSpec, jobId.toString()); schedulerJob = getSchedulerJobById(this.requestSpec, this.response200Spec, jobId.toString());
Assert.assertNotNull(schedulerJob); Assert.assertNotNull(schedulerJob);
System.out.println("Job is Still Running"); System.out.println("Job is Still Running");
} }
List<HashMap> jobHistoryData = getSchedulerJobHistory(this.requestSpec, this.responseSpec, jobId.toString()); List<Map> jobHistoryData = getSchedulerJobHistory(this.requestSpec, this.response200Spec, jobId.toString());
Assert.assertFalse("Job History is empty :( Was it too slow? Failures in background job?", jobHistoryData.isEmpty()); Assert.assertFalse("Job History is empty :( Was it too slow? Failures in background job?", jobHistoryData.isEmpty());