Skip to content

Commit

Permalink
[Fix apache#3733] Including process validation check
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Oct 30, 2024
1 parent 50926f3 commit 44e8f2f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ public static RuleFlowProcessValidator getInstance() {
return INSTANCE;
}

public ProcessValidationError[] validateProcess(final RuleFlowProcess process) {
final List<ProcessValidationError> errors = new ArrayList<>();

public List<ProcessValidationError> validateProcess(final RuleFlowProcess process, List<ProcessValidationError> errors) {
if (process.getName() == null) {
errors.add(new ProcessValidationErrorImpl(process,
"Process has no name."));
Expand Down Expand Up @@ -151,6 +149,12 @@ public ProcessValidationError[] validateProcess(final RuleFlowProcess process) {
errors,
process);

return errors;

}

public ProcessValidationError[] validateProcess(final RuleFlowProcess process) {
final List<ProcessValidationError> errors = validateProcess(process, new ArrayList<>());
return errors.toArray(new ProcessValidationError[errors.size()]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.jbpm.process.core.datatype.impl.type.ObjectDataType;
import org.jbpm.process.core.validation.ProcessValidationError;
import org.jbpm.process.core.validation.impl.ProcessValidationErrorImpl;
import org.jbpm.ruleflow.core.Metadata;
import org.jbpm.ruleflow.core.RuleFlowProcessFactory;
import org.jbpm.ruleflow.core.validation.RuleFlowProcessValidator;
import org.jbpm.workflow.core.WorkflowModelValidator;
import org.kie.kogito.codegen.api.GeneratedInfo;
import org.kie.kogito.codegen.api.context.KogitoBuildContext;
Expand Down Expand Up @@ -147,10 +151,6 @@ private GeneratedInfo<KogitoWorkflowProcess> parseProcess() {
handlers.forEach(StateHandler::handleTransitions);
handlers.forEach(StateHandler::handleConnections);

if (!parserContext.validationErrors().isEmpty()) {
throw new ValidationException(workflow.getId(), parserContext.validationErrors().stream().collect(Collectors.joining()));
}

if (parserContext.isCompensation()) {
factory.metaData(Metadata.COMPENSATION, true);
factory.metaData(Metadata.COMPENSATE_WHEN_ABORTED, true);
Expand All @@ -176,8 +176,13 @@ private GeneratedInfo<KogitoWorkflowProcess> parseProcess() {
if (!annotations.isEmpty()) {
factory.metaData(Metadata.ANNOTATIONS, annotations);
}

return new GeneratedInfo<>(factory.validate().getProcess(), parserContext.generatedFiles());
factory.link();
List<ProcessValidationError> errors = RuleFlowProcessValidator.getInstance().validateProcess(factory.getProcess(), new ArrayList<>());
parserContext.validationErrors().forEach(m -> errors.add(new ProcessValidationErrorImpl(factory.getProcess(), m)));
if (!errors.isEmpty()) {
throw new ValidationException(factory.getProcess().getId(), errors);
}
return new GeneratedInfo<>(factory.getProcess(), parserContext.generatedFiles());
}

private Optional<WorkflowModelValidator> modelValidator(ParserContext parserContext, Optional<DataInputSchema> schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import ch.qos.logback.core.read.ListAppender;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.kie.kogito.serverless.workflow.utils.ServerlessWorkflowUtils.getWorkflow;
import static org.kie.kogito.serverless.workflow.utils.ServerlessWorkflowUtils.writeWorkflow;

Expand Down Expand Up @@ -114,7 +114,9 @@ void testValidationError() throws IOException {
try (Reader reader = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("wrong.sw.json"));
StaticWorkflowApplication application = StaticWorkflowApplication.create()) {
Workflow workflow = getWorkflow(reader, WorkflowFormat.JSON);
assertThatThrownBy(() -> application.process(workflow)).isInstanceOf(ValidationException.class).hasMessageContaining("error").hasMessageContaining("function");
ValidationException validationException = catchThrowableOfType(() -> application.process(workflow), ValidationException.class);
assertThat(validationException.getErrors()).hasSizeGreaterThanOrEqualTo(3);
assertThat(validationException).hasMessageContaining("error").hasMessageContaining("function").hasMessageContaining("connect");
}
}

Expand Down

0 comments on commit 44e8f2f

Please sign in to comment.