Search in sources :

Example 1 with TestReport

use of de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.TestReport in project IPK-BrAPI-Validator by plantbreeding.

the class TestReportResource method resourcesReport.

/**
 * Get reduced report in a specific format
 *
 * @return JSON with endpoint data
 */
@GET
@Path("/shortreport/{reportId}/{format}")
@Produces({ MediaType.APPLICATION_JSON, "text/csv" })
public Response resourcesReport(@PathParam("reportId") String reportId, @PathParam("format") String format) {
    if (Config.get("advancedMode") == null) {
        return Response.status(Status.NOT_IMPLEMENTED).build();
    }
    LOGGER.debug("New GET /shortreport/" + format + ". Id: " + reportId);
    if (!format.equalsIgnoreCase("csv") && !format.equalsIgnoreCase("json")) {
        String e1 = JsonMessageManager.jsonMessage(400, "Invalid format.", 4400);
        return Response.status(Status.BAD_REQUEST).entity(e1).build();
    }
    try {
        TestReport tr = TestReportService.getReport(reportId);
        String output;
        String type;
        if (format.equalsIgnoreCase("csv")) {
            type = "text/csv";
            output = generateCSVReport(tr);
            return Response.ok().entity(output).type(type).header("Content-Disposition", "attachment; filename=\"report.csv\"").build();
        } else {
            type = "application/json";
            output = generateJSONReport(tr);
            return Response.ok().entity(output).type(type).build();
        }
    } catch (SQLException | IOException e) {
        e.printStackTrace();
        String e1 = JsonMessageManager.jsonMessage(500, "Internal server error", 5401);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e1).build();
    }
}
Also used : MiniTestReport(de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.MiniTestReport) TestReport(de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.TestReport) SQLException(java.sql.SQLException) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with TestReport

use of de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.TestReport in project IPK-BrAPI-Validator by plantbreeding.

the class SingleTestResource method callTest.

/**
 * Run a call test
 *
 * @param url  Url of the BrAPI server. Example: https://test.brapi.org/brapi/v1/
 * @return Response with json report
 */
@GET
@Path("/call")
@Produces(MediaType.APPLICATION_JSON)
public Response callTest(@QueryParam("url") String url, @QueryParam("brapiversion") @DefaultValue("") String version) {
    LOGGER.debug("New GET /call call.");
    try {
        if (url.equals("")) {
            String jsonError = JsonMessageManager.jsonMessage(400, "Missing or invalid url parameter", 4202);
            return Response.status(Status.BAD_REQUEST).encoding(jsonError).build();
        }
        if (!version.equals("v1.0") && !version.equals("v1.1")) {
            String jsonError = JsonMessageManager.jsonMessage(400, "Missing or invalid version parameter", 4202);
            return Response.status(Status.BAD_REQUEST).encoding(jsonError).build();
        }
        String collectionResource;
        ObjectMapper mapper = new ObjectMapper();
        collectionResource = "/collections/CompleteBrapiTest." + version + ".json";
        InputStream inJson = TestCollection.class.getResourceAsStream(collectionResource);
        TestCollection tc = mapper.readValue(inJson, TestCollection.class);
        Resource res = new Resource(url);
        TestSuiteReport testSuiteReport = RunnerService.testEndpointWithCall(res, tc);
        TestReport report = new TestReport(res, mapper.writeValueAsString(testSuiteReport));
        return Response.ok().entity(report).build();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        String e1 = JsonMessageManager.jsonMessage(400, "invalid URL: port must be 80", 5201);
        return Response.status(Status.BAD_REQUEST).entity(e1).build();
    } catch (IOException e) {
        // Thrown by .getResourceAsStream(""). Most probably because of missing file or wrong config structure.
        e.printStackTrace();
        String e1 = JsonMessageManager.jsonMessage(500, "internal server error", 5201);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e1).build();
    }
}
Also used : TestReport(de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.TestReport) InputStream(java.io.InputStream) Resource(de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Resource) TestSuiteReport(de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestSuiteReport) IOException(java.io.IOException) TestCollection(de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.config.TestCollection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

TestReport (de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.TestReport)2 IOException (java.io.IOException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 MiniTestReport (de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.MiniTestReport)1 Resource (de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Resource)1 TestCollection (de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.config.TestCollection)1 TestSuiteReport (de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestSuiteReport)1 InputStream (java.io.InputStream)1 SQLException (java.sql.SQLException)1