From 6522adca13a90df012d7936608c14b6e73e0157e Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Wed, 30 Aug 2023 17:53:39 +0200 Subject: [PATCH] [KOGITO-9673] Adding SWF annotations to process metadata This way annotations can be retrieved by process management interface --- .../BaseProcessInstanceManagementResource.java | 10 +++++++++- .../BaseProcessInstanceManagementResourceTest.java | 5 +++-- .../src/main/java/org/jbpm/ruleflow/core/Metadata.java | 1 + .../workflow/parser/ServerlessWorkflowParser.java | 8 ++++++-- .../src/main/resources/parallel.sw.json | 1 + .../kie/kogito/quarkus/workflows/ManagementFlowIT.java | 10 +++++++++- 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/addons/common/process-management/src/main/java/org/kie/kogito/process/management/BaseProcessInstanceManagementResource.java b/addons/common/process-management/src/main/java/org/kie/kogito/process/management/BaseProcessInstanceManagementResource.java index c71fada0f91..210cd758954 100644 --- a/addons/common/process-management/src/main/java/org/kie/kogito/process/management/BaseProcessInstanceManagementResource.java +++ b/addons/common/process-management/src/main/java/org/kie/kogito/process/management/BaseProcessInstanceManagementResource.java @@ -74,7 +74,15 @@ public T doGetProcessInfo(String processId) { data.put("version", process.version()); if (process instanceof Supplier) { org.kie.api.definition.process.Process processDefinition = ((Supplier) process).get(); - data.put("description", processDefinition.getMetaData().get(Metadata.DESCRIPTION)); + Map metadata = processDefinition.getMetaData(); + String description = (String) metadata.get(Metadata.DESCRIPTION); + if (description != null) { + data.put("description", description); + } + List annotations = (List) metadata.get(Metadata.ANNOTATIONS); + if (annotations != null) { + data.put("annotations", annotations); + } if (processDefinition instanceof WorkflowProcess) { WorkflowProcess workflowProcess = (WorkflowProcess) processDefinition; workflowProcess.getInputValidator().flatMap(v -> v.schema(JsonNode.class)).ifPresent(s -> data.put("inputSchema", s)); diff --git a/addons/common/process-management/src/test/java/org/kie/kogito/process/management/BaseProcessInstanceManagementResourceTest.java b/addons/common/process-management/src/test/java/org/kie/kogito/process/management/BaseProcessInstanceManagementResourceTest.java index e9f5b6e2da6..192f0b6f794 100644 --- a/addons/common/process-management/src/test/java/org/kie/kogito/process/management/BaseProcessInstanceManagementResourceTest.java +++ b/addons/common/process-management/src/test/java/org/kie/kogito/process/management/BaseProcessInstanceManagementResourceTest.java @@ -113,7 +113,7 @@ void setUp() { lenient().when(process.version()).thenReturn("1_0"); lenient().when(process.type()).thenReturn("BPMN"); - lenient().when(workflowProcess.getMetaData()).thenReturn(Map.of(Metadata.DESCRIPTION, "cool")); + lenient().when(workflowProcess.getMetaData()).thenReturn(Map.of(Metadata.DESCRIPTION, "cool", Metadata.ANNOTATIONS, Arrays.asList("good"))); lenient().when(workflowValidator.schema(JsonNode.class)).thenReturn(Optional.of(NullNode.instance)); lenient().when(workflowProcess.getInputValidator()).thenReturn(Optional.of(workflowValidator)); lenient().when(workflowProcess.getOutputValidator()).thenReturn(Optional.empty()); @@ -210,7 +210,8 @@ void testDoGetProcessInfo() { Object response = tested.doGetProcessInfo(PROCESS_ID); assertThat(response).isInstanceOf(Map.class); Map data = (Map) response; - assertThat(data).containsKey("type").containsKey("id").containsKey("version").containsKey("description").containsEntry("inputSchema", NullNode.instance).containsKey("name") + assertThat(data).containsKey("type").containsKey("id").containsKey("version").containsKey("description").containsKey("annotations").containsEntry("inputSchema", NullNode.instance) + .containsKey("name") .doesNotContainKey("outputSchema"); } diff --git a/jbpm/jbpm-flow/src/main/java/org/jbpm/ruleflow/core/Metadata.java b/jbpm/jbpm-flow/src/main/java/org/jbpm/ruleflow/core/Metadata.java index 7bb21033c97..05edd9c751b 100644 --- a/jbpm/jbpm-flow/src/main/java/org/jbpm/ruleflow/core/Metadata.java +++ b/jbpm/jbpm-flow/src/main/java/org/jbpm/ruleflow/core/Metadata.java @@ -19,6 +19,7 @@ public class Metadata { public static final String ASSOCIATION = "association"; public static final String ACTION = "Action"; + public static final String ANNOTATIONS = "annotations"; public static final String TRIGGER_REF = "TriggerRef"; public static final String REF = "Ref"; public static final String MESSAGE_TYPE = "MessageType"; diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/parser/ServerlessWorkflowParser.java b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/parser/ServerlessWorkflowParser.java index 0883c48caed..4a6c592c290 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/parser/ServerlessWorkflowParser.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/parser/ServerlessWorkflowParser.java @@ -22,6 +22,7 @@ import java.net.URL; import java.nio.file.Path; import java.util.Collection; +import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -128,7 +129,6 @@ private GeneratedInfo parseProcess() { .type(KogitoWorkflowProcess.SW_TYPE); ParserContext parserContext = new ParserContext(idGenerator, factory, context, WorkflowOperationIdFactoryProvider.getFactory(context.getApplicationProperty(WorkflowOperationIdFactoryProvider.PROPERTY_NAME))); - modelValidator(parserContext, Optional.ofNullable(workflow.getDataInputSchema())).ifPresent(factory::inputValidator); modelValidator(parserContext, ServerlessWorkflowUtils.getExtension(workflow, OutputSchema.class).map(OutputSchema::getOutputSchema)).ifPresent(factory::outputValidator); loadConstants(factory, parserContext); @@ -144,7 +144,6 @@ private GeneratedInfo parseProcess() { factory.metaData(Metadata.COMPENSATION, true); factory.addCompensationContext(workflow.getId()); } - TimeoutsDefinition timeouts = workflow.getTimeouts(); if (timeouts != null) { WorkflowExecTimeout workflowTimeout = timeouts.getWorkflowExecTimeout(); @@ -161,6 +160,11 @@ private GeneratedInfo parseProcess() { if (!ConversionUtils.isEmpty(description)) { factory.metaData(Metadata.DESCRIPTION, description); } + List annotations = workflow.getAnnotations(); + if (!annotations.isEmpty()) { + factory.metaData(Metadata.ANNOTATIONS, annotations); + } + return new GeneratedInfo<>(factory.validate().getProcess(), parserContext.generatedFiles()); } diff --git a/quarkus/addons/process-management/integration-tests/src/main/resources/parallel.sw.json b/quarkus/addons/process-management/integration-tests/src/main/resources/parallel.sw.json index 41a9d1c058a..e6c77718dea 100644 --- a/quarkus/addons/process-management/integration-tests/src/main/resources/parallel.sw.json +++ b/quarkus/addons/process-management/integration-tests/src/main/resources/parallel.sw.json @@ -3,6 +3,7 @@ "version": "1.0", "name": "Welcome to the Parallel dimension", "description": "Testing parallelism", +"annotations": ["Football", "Betis"], "start": "Parallel", "functions": [ { diff --git a/quarkus/addons/process-management/integration-tests/src/test/java/org/kie/kogito/quarkus/workflows/ManagementFlowIT.java b/quarkus/addons/process-management/integration-tests/src/test/java/org/kie/kogito/quarkus/workflows/ManagementFlowIT.java index 046e53b564b..c499cc3edb5 100644 --- a/quarkus/addons/process-management/integration-tests/src/test/java/org/kie/kogito/quarkus/workflows/ManagementFlowIT.java +++ b/quarkus/addons/process-management/integration-tests/src/test/java/org/kie/kogito/quarkus/workflows/ManagementFlowIT.java @@ -15,6 +15,7 @@ */ package org.kie.kogito.quarkus.workflows; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -27,16 +28,23 @@ import static io.restassured.RestAssured.given; import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; @QuarkusIntegrationTest class ManagementFlowIT { @Test - void testManagementAPI() { + void testManagementAPINodes() { assertThat(given().contentType(ContentType.JSON).accept(ContentType.JSON).get("management/processes/parallel/nodes") .then().statusCode(200).extract().as(new TypeRef>>() { }).stream().map(m -> (Map) m.get("metadata")).filter(m -> m.containsKey(SWFConstants.STATE_NAME) && m.containsKey(SWFConstants.ACTION_NAME) && m.containsKey(SWFConstants.BRANCH_NAME)) .count()).isGreaterThanOrEqualTo(3); } + + @Test + void testManagementAPIProcess() { + given().contentType(ContentType.JSON).accept(ContentType.JSON).get("management/processes/parallel") + .then().statusCode(200).body("annotations", is(Arrays.asList("Football", "Betis"))); + } }