Search in sources :

Example 1 with EvaluationException

use of io.gravitee.gateway.services.healthcheck.eval.EvaluationException in project gravitee-gateway by gravitee-io.

the class AssertionEvaluation method validate.

@Override
public boolean validate() throws EvaluationException {
    try {
        final ExpressionParser parser = new SpelExpressionParser();
        final Expression expr = parser.parseExpression(assertion);
        final StandardEvaluationContext context = new StandardEvaluationContext();
        context.registerFunction("jsonPath", BeanUtils.resolveSignature("evaluate", JsonPathFunction.class));
        context.setVariables(variables);
        return expr.getValue(context, boolean.class);
    } catch (SpelEvaluationException spelex) {
        throw new EvaluationException("Assertion can not be verified : " + assertion, spelex);
    }
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationException(io.gravitee.gateway.services.healthcheck.eval.EvaluationException) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) JsonPathFunction(io.gravitee.gateway.el.function.JsonPathFunction)

Example 2 with EvaluationException

use of io.gravitee.gateway.services.healthcheck.eval.EvaluationException in project gravitee-gateway by gravitee-io.

the class HttpEndpointRuleHandler method validateAssertions.

private EndpointStatus.StepBuilder validateAssertions(final io.gravitee.definition.model.services.healthcheck.Step step, final EvaluableHttpResponse response) {
    EndpointStatus.StepBuilder stepBuilder = EndpointStatus.forStep(step.getName());
    // Run assertions
    if (step.getResponse().getAssertions() != null) {
        Iterator<String> assertionIterator = step.getResponse().getAssertions().iterator();
        boolean success = true;
        while (success && assertionIterator.hasNext()) {
            try {
                // TODO: assertion must be compiled only one time to preserve CPU
                AssertionEvaluation evaluation = new AssertionEvaluation(assertionIterator.next());
                evaluation.setVariable("response", response);
                // Run validation
                success = evaluation.validate();
                if (success) {
                    stepBuilder.success();
                } else {
                    stepBuilder.fail("Assertion not validated: " + evaluation.getAssertion());
                }
            } catch (EvaluationException eex) {
                success = false;
                stepBuilder.fail(eex.getMessage());
            }
        }
    }
    return stepBuilder;
}
Also used : EndpointStatus(io.gravitee.reporter.api.health.EndpointStatus) EvaluationException(io.gravitee.gateway.services.healthcheck.eval.EvaluationException) AssertionEvaluation(io.gravitee.gateway.services.healthcheck.eval.assertion.AssertionEvaluation)

Aggregations

EvaluationException (io.gravitee.gateway.services.healthcheck.eval.EvaluationException)2 JsonPathFunction (io.gravitee.gateway.el.function.JsonPathFunction)1 AssertionEvaluation (io.gravitee.gateway.services.healthcheck.eval.assertion.AssertionEvaluation)1 EndpointStatus (io.gravitee.reporter.api.health.EndpointStatus)1 Expression (org.springframework.expression.Expression)1 ExpressionParser (org.springframework.expression.ExpressionParser)1 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1