From 5b511be137c2bfb0c7cfa5c6bf8e781f770aebfc Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Wed, 11 Sep 2024 08:29:27 +0200 Subject: [PATCH] Use cmmn script task expression value if no string value is missing and do not throw exception if field is missing Not throwing the exception aligns this with the way it works for BPMN, where no exception is thrown by the model. There will eventually be an exception thrown when the script is executed --- ...ScriptServiceTaskCmmnXmlConverterTest.java | 30 +++++++++++++++ .../script-task-expression-field.cmmn | 38 +++++++++++++++++++ .../cmmn/model/ScriptServiceTask.java | 19 ++++++---- 3 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 modules/flowable-cmmn-converter/src/test/resources/org/flowable/test/cmmn/converter/script-task-expression-field.cmmn diff --git a/modules/flowable-cmmn-converter/src/test/java/org/flowable/test/cmmn/converter/ScriptServiceTaskCmmnXmlConverterTest.java b/modules/flowable-cmmn-converter/src/test/java/org/flowable/test/cmmn/converter/ScriptServiceTaskCmmnXmlConverterTest.java index a9da14dca6a..e999c9fef73 100644 --- a/modules/flowable-cmmn-converter/src/test/java/org/flowable/test/cmmn/converter/ScriptServiceTaskCmmnXmlConverterTest.java +++ b/modules/flowable-cmmn-converter/src/test/java/org/flowable/test/cmmn/converter/ScriptServiceTaskCmmnXmlConverterTest.java @@ -84,4 +84,34 @@ public void validateModel(CmmnModel cmmnModel) { }); } + + @CmmnXmlConverterTest("org/flowable/test/cmmn/converter/script-task-expression-field.cmmn") + public void scriptTaskWithExpressionField(CmmnModel cmmnModel) { + assertThat(cmmnModel).isNotNull(); + + PlanItem planItemTaskA = cmmnModel.findPlanItem("planItemTaskA"); + assertThat(planItemTaskA.getEntryCriteria()).isEmpty(); + + PlanItemDefinition planItemDefinition = planItemTaskA.getPlanItemDefinition(); + assertThat(planItemDefinition) + .isInstanceOfSatisfying(ScriptServiceTask.class, scriptTask -> { + assertThat(scriptTask.getType()).isEqualTo(ScriptServiceTask.SCRIPT_TASK); + assertThat(scriptTask.getScriptFormat()).isEqualTo("javascript"); + assertThat(scriptTask.getScript()).isEqualTo("var a = '${testA}';"); + assertThat(scriptTask.getResultVariableName()).isEqualTo("scriptResult"); + assertThat(scriptTask.isAutoStoreVariables()).isFalse(); + assertThat(scriptTask.isBlocking()).isTrue(); + assertThat(scriptTask.isAsync()).isFalse(); + }); + + PlanItem planItemTaskB = cmmnModel.findPlanItem("planItemTaskB"); + planItemDefinition = planItemTaskB.getPlanItemDefinition(); + assertThat(planItemDefinition) + .isInstanceOfSatisfying(ScriptServiceTask.class, scriptServiceTask -> { + assertThat(scriptServiceTask.getScriptFormat()).isEqualTo("groovy"); + assertThat(scriptServiceTask.getScript()).isEqualTo("var b = '${testB}';"); + assertThat(scriptServiceTask.isAutoStoreVariables()).isTrue(); + }); + } + } diff --git a/modules/flowable-cmmn-converter/src/test/resources/org/flowable/test/cmmn/converter/script-task-expression-field.cmmn b/modules/flowable-cmmn-converter/src/test/resources/org/flowable/test/cmmn/converter/script-task-expression-field.cmmn new file mode 100644 index 00000000000..f5609cc3580 --- /dev/null +++ b/modules/flowable-cmmn-converter/src/test/resources/org/flowable/test/cmmn/converter/script-task-expression-field.cmmn @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/modules/flowable-cmmn-model/src/main/java/org/flowable/cmmn/model/ScriptServiceTask.java b/modules/flowable-cmmn-model/src/main/java/org/flowable/cmmn/model/ScriptServiceTask.java index 36da3d73ae3..7d06dbcdcdf 100644 --- a/modules/flowable-cmmn-model/src/main/java/org/flowable/cmmn/model/ScriptServiceTask.java +++ b/modules/flowable-cmmn-model/src/main/java/org/flowable/cmmn/model/ScriptServiceTask.java @@ -12,9 +12,7 @@ */ package org.flowable.cmmn.model; -import java.util.Optional; - -import org.flowable.common.engine.api.FlowableException; +import org.apache.commons.lang3.StringUtils; /** * @author Dennis @@ -38,11 +36,16 @@ public void setScriptFormat(String scriptFormat) { } public String getScript() { - Optional script = fieldExtensions.stream() - .filter(e -> "script".equalsIgnoreCase(e.getFieldName())) - .findFirst() - .map(FieldExtension::getStringValue); - return script.orElseThrow(() -> new FlowableException("Missing script")); + for (FieldExtension fieldExtension : fieldExtensions) { + if ("script".equalsIgnoreCase(fieldExtension.getFieldName())) { + String script = fieldExtension.getStringValue(); + if (StringUtils.isNotEmpty(script)) { + return script; + } + return fieldExtension.getExpression(); + } + } + return null; } public boolean isAutoStoreVariables() {