Search in sources :

Example 6 with TestExecReport

use of de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport in project IPK-BrAPI-Validator by plantbreeding.

the class TestItemRunner method saveVariable.

/**
 * Save a element from the query to a variable in VariableStorage.
 *
 * @param path         JsonNode path to the variable.
 * @param variableName Key to store the variable.
 * @return TestItemReport
 */
private TestExecReport saveVariable(String path, String variableName) {
    LOGGER.info("Store variable: " + variableName + " | " + path);
    String json = this.vr.extract().asString();
    ObjectMapper mapper = new ObjectMapper();
    TestExecReport ter = new TestExecReport("Save variable at: " + path + " | Key: " + variableName, false);
    ter.setType("error storing variable");
    try {
        JsonNode root = mapper.readTree(json);
        JsonNode value = root.at(path);
        if (value.asText().equals("") || value.isNull()) {
            throw new IllegalArgumentException("Value is empty string, null, or was not found.");
        }
        this.variables.setVariable(variableName, value);
        ter.setPassed(true);
        ter.addMessage("Stored value: " + value);
        return ter;
    } catch (IOException | IllegalArgumentException e) {
        ter.addMessage(e.getMessage());
        return ter;
    }
}
Also used : TestExecReport(de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 7 with TestExecReport

use of de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport in project IPK-BrAPI-Validator by plantbreeding.

the class TestItemRunner method schemaMatch.

/**
 * Check if response matches schema
 *
 * @param p Path to the schema to be tested
 * @return TestItemReport
 */
private TestExecReport schemaMatch(String p) {
    LOGGER.info("Testing Schema");
    TestExecReport tr = new TestExecReport("Json matches schema: " + p, false);
    tr.setType("schema mismatch");
    tr.setSchema(p);
    try {
        String jsonString = vr.extract().response().asString();
        SchemaValidator schemaValidator = new SchemaValidator();
        ProcessingReport r = schemaValidator.validate(p, jsonString);
        if (r.isSuccess()) {
            LOGGER.info("Schema Test Passed");
            tr.addMessage("Response structure matches schema.");
            tr.setPassed(true);
        } else {
            LOGGER.info("Schema Test Failed");
            tr.addMessage("Response structure doesn't match schema.");
            r.forEach(message -> tr.addError(message.asJson()));
        }
        return tr;
    } catch (ConnectionClosedException | JsonParseException e1) {
        LOGGER.info("Invalid response");
        LOGGER.info("== cause ==");
        LOGGER.info(e1.getMessage());
        tr.addMessage("Server response is not valid JSON.");
        return tr;
    } catch (AssertionError | IOException | ProcessingException e1) {
        LOGGER.info("Doesn't match schema");
        LOGGER.info("== cause ==");
        LOGGER.info(e1.getMessage());
        tr.addMessage(e1.getMessage());
        return tr;
    }
}
Also used : TestExecReport(de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) ConnectionClosedException(org.apache.http.ConnectionClosedException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Aggregations

TestExecReport (de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport)7 IOException (java.io.IOException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)1 TestItemReport (de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestItemReport)1 ConnectionClosedException (org.apache.http.ConnectionClosedException)1