Search in sources :

Example 1 with ModelValidatorContext

use of org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext in project scheduling by ow2-proactive.

the class DefaultModelJobValidatorServiceProvider method validateJob.

@Override
public TaskFlowJob validateJob(TaskFlowJob job) throws JobValidationException {
    ModelValidatorContext context = new ModelValidatorContext(job);
    for (JobVariable jobVariable : job.getVariables().values()) {
        checkVariableFormat(null, jobVariable, context);
        context.updateJobWithContext(job);
    }
    for (Task task : job.getTasks()) {
        context = new ModelValidatorContext(task);
        for (TaskVariable taskVariable : task.getVariables().values()) {
            checkVariableFormat(task, taskVariable, context);
            context.updateTaskWithContext(task);
        }
    }
    return job;
}
Also used : Task(org.ow2.proactive.scheduler.common.task.Task) TaskVariable(org.ow2.proactive.scheduler.common.task.TaskVariable) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable)

Example 2 with ModelValidatorContext

use of org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext in project scheduling by ow2-proactive.

the class SpelValidator method validate.

@Override
public String validate(String parameterValue, ModelValidatorContext context) throws ValidationException {
    try {
        context.getSpELContext().setVariable("value", parameterValue);
        Object untypedResult = spelExpression.getValue(context.getSpELContext());
        if (!(untypedResult instanceof Boolean)) {
            throw new ValidationException("SPEL expression did not return a boolean value.");
        }
        boolean evaluationResult = (Boolean) untypedResult;
        if (!evaluationResult) {
            throw new ValidationException("SPEL expression returned false, received " + parameterValue);
        }
    } catch (EvaluationException e) {
        throw new ValidationException("SPEL expression raised an error: " + e.getMessage(), e);
    }
    return parameterValue;
}
Also used : ValidationException(org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException) EvaluationException(org.springframework.expression.EvaluationException)

Example 3 with ModelValidatorContext

use of org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext in project scheduling by ow2-proactive.

the class SpelValidatorTest method testSpelOKUpdateTaskVariable.

@Test
public void testSpelOKUpdateTaskVariable() throws ValidationException, UserException {
    SpelValidator validator = new SpelValidator("#value == 'MyString'?(variables['var1'] = 'toto1') instanceof T(String) : false");
    String value = "MyString";
    ModelValidatorContext context = new ModelValidatorContext(createTask());
    Assert.assertEquals(value, validator.validate(value, context));
    Assert.assertEquals("toto1", context.getSpELVariables().getVariables().get("var1"));
}
Also used : ModelValidatorContext(org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext) Test(org.junit.Test)

Example 4 with ModelValidatorContext

use of org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext in project scheduling by ow2-proactive.

the class SpelValidatorTest method testSpelKO.

@Test(expected = ValidationException.class)
public void testSpelKO() throws ValidationException {
    SpelValidator validator = new SpelValidator("#value == 'MyString'");
    String value = "MyString123";
    validator.validate(value, new ModelValidatorContext(new StandardEvaluationContext()));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ModelValidatorContext(org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext) Test(org.junit.Test)

Example 5 with ModelValidatorContext

use of org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext in project scheduling by ow2-proactive.

the class SpelValidatorTest method testSpelOKUpdateJobVariableWhenEmptyOK.

@Test
public void testSpelOKUpdateJobVariableWhenEmptyOK() throws ValidationException, UserException {
    SpelValidator validator = new SpelValidator("#value == 'MyString' ? (variables['var4'] == null ? (variables['var4'] = 'toto1') instanceof T(String) : true) : false");
    String value = "MyString";
    ModelValidatorContext context = new ModelValidatorContext(createJob());
    Assert.assertEquals(value, validator.validate(value, context));
    Assert.assertEquals("toto1", context.getSpELVariables().getVariables().get("var4"));
}
Also used : ModelValidatorContext(org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 ModelValidatorContext (org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext)8 ValidationException (org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)2 UserException (org.ow2.proactive.scheduler.common.exception.UserException)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 JobVariable (org.ow2.proactive.scheduler.common.job.JobVariable)1 Task (org.ow2.proactive.scheduler.common.task.Task)1 TaskVariable (org.ow2.proactive.scheduler.common.task.TaskVariable)1 EvaluationException (org.springframework.expression.EvaluationException)1