Skip to content

Commit

Permalink
Refactor RunApi (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacodg authored Jun 12, 2024
1 parent 629e648 commit 8efe009
Showing 1 changed file with 7 additions and 134 deletions.
141 changes: 7 additions & 134 deletions src/main/java/nl/nn/testtool/web/api/RunApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package nl.nn.testtool.web.api;

import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -28,7 +27,6 @@
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
Expand All @@ -39,8 +37,6 @@
import nl.nn.testtool.TestTool;
import nl.nn.testtool.run.ReportRunner;
import nl.nn.testtool.run.RunResult;
import nl.nn.testtool.storage.CrudStorage;
import nl.nn.testtool.storage.Storage;
import nl.nn.testtool.storage.StorageException;
import nl.nn.testtool.transform.ReportXmlTransformer;
import nl.nn.testtool.web.ApiServlet;
Expand All @@ -51,34 +47,21 @@ public class RunApi extends ApiBase {
private @Setter @Inject @Autowired TestTool testTool;
private @Setter @Inject @Autowired ReportXmlTransformer reportXmlTransformer;

/**
* Rerun the given report, and save the output the target storage.
*
* @param storageId the id of the report in the testStorage to run
* @param testStorageName Name of the test storage.
* @param debugStorageName Name of the debug storage.
* @return The response of running the report.
*/
@POST
@Path("/run/{testStorageName}/{debugStorageName}/{storageId}")
@Path("/run/{storageName}/{storageId}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response runReport(@PathParam("storageId") int storageId, @PathParam("testStorageName") String testStorageName, @PathParam("debugStorageName") String debugStorageName) {
Storage testStorage = testTool.getStorage(testStorageName);
ReportRunner runner = getRunner(testTool.getStorage(debugStorageName));
public Response runReport(@PathParam("storageName") String storageName, @PathParam("storageId") int storageId) {
Map<String, Object> result = new HashMap<>();
// Reran reports will allow us to keep track of old reran reports. This will later be used in replace and result.
HashMap<Integer, Report> reranReports = getSessionAttr("reranReports", false);
if (reranReports == null) {
reranReports = new HashMap<>();
setSessionAttr("reranReports", reranReports);
}
String errorMessage = null;
try {
Report report = testStorage.getReport(storageId);
Report report = testTool.getStorage(storageName).getReport(storageId);
if (report != null) {
report.setTestTool(testTool);
reranReports.put(storageId, report);
ReportRunner runner = new ReportRunner();
runner.setTestTool(testTool);
runner.setDebugStorage(testTool.getDebugStorage());
runner.setSecurityContext(this);
errorMessage = runner.run(Collections.singletonList(report), true, true);
if (errorMessage == null) {
RunResult runResult = runner.getResults().get(storageId);
Expand Down Expand Up @@ -116,114 +99,4 @@ private Map<String, Object> extractRunResult(Report report, Report runResultRepo
return res;
}

@PUT
@Path("/replace/{debugStorage}/{storageId}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response runReport(@PathParam("debugStorage") String storageName, @PathParam("storageId") int storageId) {
try {
// Get run result report.
// TODO: Rename debugStorageStorageParam to debugStorageName and use testTool.getStorage(debugStorageName)
// (frontend needs to be changed to use storage name instead of bean name)
Storage debugStorage = testTool.getStorage(storageName);
ReportRunner reportRunner = getRunner(debugStorage);
Report runResultReport = reportRunner.getRunResultReport(reportRunner.getResults().get(storageId).correlationId);

// Get original report.
HashMap<Integer, Report> reranReports = getSessionAttr("reranReports", true);
Report report = reranReports.get(storageId);

// Apply transformations, etc
log.debug("Replacing report [" + report.getStorage().getName() + ":" + report.getStorageId() + "] " +
"with [" + debugStorage.getName() + ":" + runResultReport.getStorageId() + "]");
runResultReport.setTestTool(report.getTestTool());
runResultReport.setName(report.getName());
runResultReport.setDescription(report.getDescription());
if (report.getCheckpoints().get(0).containsVariables()) {
runResultReport.getCheckpoints().get(0).setMessage(report.getCheckpoints().get(0).getMessage());
}
runResultReport.setPath(report.getPath());
runResultReport.setTransformation(report.getTransformation());
runResultReport.setReportXmlTransformer(report.getReportXmlTransformer());
runResultReport.setVariableCsvWithoutException(report.getVariableCsv());
runResultReport.setStorageId(report.getStorageId());

((CrudStorage) report.getStorage()).update(runResultReport);
reportRunner.getResults().remove(storageId);
reranReports.remove(storageId);
return Response.ok(runResultReport).build();
} catch (StorageException e) {
e.printStackTrace();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Exception while replacing report with storage id [" + storageId + "] - detailed error message - " + e + Arrays.toString(e.getStackTrace())).build();
}
}
/**
* Resets all the report runners.
* @return The response after resetting the reports.
*/
@POST
@Path("/reset")
public Response resetAll() {
Object sessionAttr = getSessionAttr("reportRunner", false);
if (!(sessionAttr instanceof Map)) {
setSessionAttr("reportRunner", new HashMap<Object, Object>());
return Response.ok().build();
}

Map<Object, Object> map = (Map) sessionAttr;
for (Object key : map.keySet()) {
Object val = map.get(key);
if (val instanceof ReportRunner) {
ReportRunner runner = (ReportRunner) val;
runner.reset();
} else {
map.remove(key);
}
}
return Response.ok().build();
}

/**
* Resets the re-runner with the given debug storage.
* @param storageName Name of the debug storage.
* @return The response after resetting.
*/
@POST
@Path("/reset/{debugStorage}")
public Response resetRunner(@PathParam("debugStorage") String storageName) {
Storage storage = testTool.getStorage(storageName);
ReportRunner runner = getRunner(storage);
runner.reset();
return Response.ok().build();
}

/**
* Return from the map, or generate report re-runner with the given debug storage.
* With the API, each session can create multiple re-runners for different debug storages.
*
* @param debugStorage Debug storage.
* @return ReportRunner that uses the given debug storage.
*/
private ReportRunner getRunner(Storage debugStorage) {
Map<Object, Object> runners;

Object sessionAttr = getSessionAttr("reportRunner", false);
if (sessionAttr instanceof Map) {
runners = (Map) sessionAttr;
} else {
runners = new HashMap<>();
setSessionAttr("reportRunner", runners);
}

ReportRunner runner = (ReportRunner) runners.get(debugStorage);
if (runner == null) {
runner = new ReportRunner();
runner.setTestTool(testTool);
runner.setDebugStorage(debugStorage);
runner.setSecurityContext(this);
runners.put(debugStorage, runner);
}

return runner;
}
}

0 comments on commit 8efe009

Please sign in to comment.