Commit ae30eb5b authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub
Browse files

fix(validation) improve condition expression validation (#2287)

Closes STUDIO-3533
parent 9ccf632a
......@@ -46,6 +46,7 @@ import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.service.OperationCanceledError;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;
......@@ -81,17 +82,18 @@ public class ComparisonExpressionValidator implements IExpressionValidator {
return ValidationStatus.error(e.getMessage());
}
return validateXtextResource(injector, resource, getContextResourceSet());
}
public IStatus validateXtextResource(final Injector injector, Resource resource, ResourceSet resourceSet) throws OperationCanceledError {
final IResourceValidator xtextResourceChecker = injector.getInstance(IResourceValidator.class);
final MultiStatus status = new MultiStatus(ExpressionEditorPlugin.PLUGIN_ID, 0, "", null);
final ConditionModelJavaValidator validator = injector.getInstance(ConditionModelJavaValidator.class);
final ResourceSet resourceSet = getContextResourceSet();
validator.setCurrentResourceSet(resourceSet);
final List<Issue> issues = xtextResourceChecker.validate(resource, CheckMode.FAST_ONLY, null);
if(issues.isEmpty()){
updateDependencies(resource);
}
for(final Issue issue : issues){
int severity = IStatus.ERROR;
final Severity issueSeverity = issue.getSeverity();
......@@ -102,7 +104,7 @@ public class ComparisonExpressionValidator implements IExpressionValidator {
}
return status;
}
}
/**
* Public for test purpose
......@@ -121,8 +123,7 @@ public class ComparisonExpressionValidator implements IExpressionValidator {
* @return
*/
public ResourceSet getContextResourceSet() {
final ResourceSet resourceSet = context.eResource().getResourceSet();
return resourceSet;
return context.eResource().getResourceSet();
}
private void updateDependencies(final Resource resource) {
......
......@@ -36,9 +36,12 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import com.google.inject.Injector;
/**
* @author Romain Bioteau
......@@ -55,13 +58,14 @@ public class ConditionExpressionConstraintTest {
@Spy
private ConditionExpressionConstraint constraintUnderTest;
/**
* @throws java.lang.Exception
*/
@Mock
private Injector injector;
@Before
public void setUp() throws Exception {
when(context.createSuccessStatus()).thenReturn(Status.OK_STATUS);
when(context.createFailureStatus(anyObject())).thenReturn(new Status(IStatus.ERROR, "unknown", ""));
doReturn(injector).when(constraintUnderTest).getInjector();
}
@Test
......@@ -87,7 +91,7 @@ public class ConditionExpressionConstraintTest {
Operation_Compare op = ConditionModelFactory.eINSTANCE.createOperation_Compare();
Operation_Greater operation_Greater = ConditionModelFactory.eINSTANCE.createOperation_Greater();
op.setOp(operation_Greater);
doReturn(op).when(constraintUnderTest).getCompareOperation(expression);
doReturn(op).when(constraintUnderTest).getCompareOperation(injector, expression);
assertThat(constraintUnderTest.performBatchValidation(context).isOK()).isTrue();
}
......@@ -102,10 +106,10 @@ public class ConditionExpressionConstraintTest {
f.setCondition(expression);
when(context.getTarget()).thenReturn(f);
Operation_Compare op = ConditionModelFactory.eINSTANCE.createOperation_Compare();
doReturn(op).when(constraintUnderTest).getCompareOperation(expression);
doReturn(op).when(constraintUnderTest).getCompareOperation(injector, expression);
assertThat(constraintUnderTest.performBatchValidation(context).isOK()).isFalse();
doReturn(null).when(constraintUnderTest).getCompareOperation(expression);
doReturn(null).when(constraintUnderTest).getCompareOperation(injector, expression);
assertThat(constraintUnderTest.performBatchValidation(context).isOK()).isFalse();
}
......
......@@ -21,6 +21,7 @@ import org.bonitasoft.studio.common.model.ModelSearch;
import org.bonitasoft.studio.condition.conditionModel.Operation_Compare;
import org.bonitasoft.studio.condition.scoping.ConditionModelGlobalScopeProvider;
import org.bonitasoft.studio.condition.ui.expression.ComparisonExpressionLoadException;
import org.bonitasoft.studio.condition.ui.expression.ComparisonExpressionValidator;
import org.bonitasoft.studio.condition.ui.expression.ProjectXtextResourceProvider;
import org.bonitasoft.studio.condition.ui.expression.XtextComparisonExpressionLoader;
import org.bonitasoft.studio.condition.ui.internal.ConditionModelActivator;
......@@ -39,6 +40,8 @@ import com.google.inject.Injector;
*/
public class ConditionExpressionConstraint extends AbstractLiveValidationMarkerConstraint {
private ComparisonExpressionValidator comparisonExpressionValidator = new ComparisonExpressionValidator();
@Override
protected String getConstraintId() {
return "org.bonitasoft.studio.validation.constraints.condition_expression";
......@@ -51,20 +54,29 @@ public class ConditionExpressionConstraint extends AbstractLiveValidationMarkerC
final Expression conditionExpression = ((SequenceFlow) target).getCondition();
if (conditionExpression != null
&& ExpressionConstants.CONDITION_TYPE.equals(conditionExpression.getType())
&& conditionExpression.getContent() != null
&& !conditionExpression.getContent().isEmpty()) {
final Operation_Compare opCompare = getCompareOperation(conditionExpression);
&& conditionExpression.hasContent()) {
Injector injector = getInjector();
final Operation_Compare opCompare = getCompareOperation(injector, conditionExpression);
if (opCompare == null || opCompare.getOp() == null) {
return ctx.createFailureStatus(Messages.bind(Messages.invalidConditionExpression, conditionExpression.getName()));
}
if(target.eResource() != null) {
IStatus status = comparisonExpressionValidator.validateXtextResource(injector, opCompare.eResource(), target.eResource().getResourceSet());
if(status.getSeverity() == IStatus.ERROR) {
return ctx.createFailureStatus(Messages.bind(Messages.invalidConditionExpression, conditionExpression.getName()));
}
}
}
}
return ctx.createSuccessStatus();
}
protected Operation_Compare getCompareOperation(final Expression conditionExpression) {
Injector injector = ConditionModelActivator.getInstance()
Injector getInjector() {
return ConditionModelActivator.getInstance()
.getInjector(ConditionModelActivator.ORG_BONITASOFT_STUDIO_CONDITION_CONDITIONMODEL);
}
protected Operation_Compare getCompareOperation(Injector injector, final Expression conditionExpression) {
final XtextComparisonExpressionLoader comparisonExpressionConverter = new XtextComparisonExpressionLoader(
injector.getInstance(ConditionModelGlobalScopeProvider.class),
new ModelSearch(Collections::emptyList),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment