From 861d4adfaf3f0144a91f2846053b17ea82b62392 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Tue, 19 Mar 2024 12:24:20 +0100 Subject: [PATCH] [Fix #3346] Fixing mistakes --- .../DefaultProcessEventListenerConfig.java | 2 +- .../executor/StaticWorkflowApplication.java | 40 +++++++++++-------- .../StaticFluentWorkflowApplicationTest.java | 11 ++++- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/impl/DefaultProcessEventListenerConfig.java b/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/impl/DefaultProcessEventListenerConfig.java index 9d4ba427425..143033d8c53 100644 --- a/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/impl/DefaultProcessEventListenerConfig.java +++ b/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/impl/DefaultProcessEventListenerConfig.java @@ -28,7 +28,7 @@ public DefaultProcessEventListenerConfig(ProcessEventListener... listeners) { } } - public DefaultProcessEventListenerConfig(Iterable listeners) { + public DefaultProcessEventListenerConfig(Iterable listeners) { for (ProcessEventListener listener : listeners) { register(listener); } diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-executor-core/src/main/java/org/kie/kogito/serverless/workflow/executor/StaticWorkflowApplication.java b/kogito-serverless-workflow/kogito-serverless-workflow-executor-core/src/main/java/org/kie/kogito/serverless/workflow/executor/StaticWorkflowApplication.java index e9d92c9fc6b..4c0af4af738 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-executor-core/src/main/java/org/kie/kogito/serverless/workflow/executor/StaticWorkflowApplication.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-executor-core/src/main/java/org/kie/kogito/serverless/workflow/executor/StaticWorkflowApplication.java @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Map; +import java.util.Map.Entry; import java.util.Optional; import java.util.Properties; import java.util.ServiceLoader; @@ -32,12 +33,12 @@ import java.util.concurrent.SynchronousQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; import org.jbpm.workflow.core.impl.WorkflowProcessImpl; import org.jbpm.workflow.core.node.SubProcessNode; import org.jbpm.workflow.instance.WorkflowProcessInstance; import org.kie.api.event.process.ProcessCompletedEvent; -import org.kie.api.event.process.ProcessEventListener; import org.kie.kogito.Addons; import org.kie.kogito.KogitoEngine; import org.kie.kogito.Model; @@ -47,6 +48,7 @@ import org.kie.kogito.config.StaticConfigBean; import org.kie.kogito.event.impl.EventFactoryUtils; import org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener; +import org.kie.kogito.internal.process.event.KogitoProcessEventListener; import org.kie.kogito.internal.process.runtime.KogitoWorkItemHandler; import org.kie.kogito.process.Process; import org.kie.kogito.process.ProcessInstance; @@ -121,27 +123,41 @@ public void afterProcessCompleted(ProcessCompletedEvent event) { } } - private static class WorkflowApplicationBuilder { + public static class WorkflowApplicationBuilder { private Map properties; - private Collection listeners = new ArrayList<>(); + private Collection listeners = new ArrayList<>(); + + private WorkflowApplicationBuilder() { + } public WorkflowApplicationBuilder withProperties(Map properties) { this.properties = properties; return this; } - public WorkflowApplicationBuilder withEventListener(ProcessEventListener listener, ProcessEventListener... extraListeners) { + public WorkflowApplicationBuilder withEventListener(KogitoProcessEventListener listener, KogitoProcessEventListener... extraListeners) { listeners.add(listener); - for (ProcessEventListener extraListener : extraListeners) { + for (KogitoProcessEventListener extraListener : extraListeners) { listeners.add(extraListener); } return this; } public StaticWorkflowApplication build() { + if (properties == null) { + Properties properties = new Properties(); + try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties")) { + if (is != null) { + properties.load(is); + } + } catch (IOException io) { + logger.warn("Error loading application.properties from classpath", io); + } + this.properties = properties.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), Entry::getValue)); + } Map> queues = new ConcurrentHashMap<>(); - withEventListener(new StaticCompletionEventListener(queues)); + listeners.add(new StaticCompletionEventListener(queues)); StaticWorkflowApplication application = new StaticWorkflowApplication(properties, queues, listeners); application.applicationRegisters.forEach(register -> register.register(application)); return application; @@ -153,22 +169,14 @@ public static WorkflowApplicationBuilder builder() { } public static StaticWorkflowApplication create() { - Properties properties = new Properties(); - try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties")) { - if (is != null) { - properties.load(is); - } - } catch (IOException io) { - logger.warn("Error loading application.properties from classpath", io); - } - return create((Map) properties); + return builder().build(); } public static StaticWorkflowApplication create(Map properties) { return builder().withProperties(properties).build(); } - private StaticWorkflowApplication(Map properties, Map> queues, Collection listeners) { + private StaticWorkflowApplication(Map properties, Map> queues, Collection listeners) { super(new StaticConfig(new Addons(Collections.emptySet()), new StaticProcessConfig(new CachedWorkItemHandlerConfig(), new DefaultProcessEventListenerConfig(listeners), new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory())), new StaticConfigBean())); diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-executor-core/src/test/java/org/kie/kogito/serverless/workflow/executor/StaticFluentWorkflowApplicationTest.java b/kogito-serverless-workflow/kogito-serverless-workflow-executor-core/src/test/java/org/kie/kogito/serverless/workflow/executor/StaticFluentWorkflowApplicationTest.java index d96afcd2c9d..81de083a09a 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-executor-core/src/test/java/org/kie/kogito/serverless/workflow/executor/StaticFluentWorkflowApplicationTest.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-executor-core/src/test/java/org/kie/kogito/serverless/workflow/executor/StaticFluentWorkflowApplicationTest.java @@ -23,8 +23,11 @@ import java.util.Collections; import java.util.Map; import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; import org.junit.jupiter.api.Test; +import org.kie.api.event.process.ProcessCompletedEvent; +import org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener; import org.kie.kogito.process.Process; import org.kie.kogito.serverless.workflow.actions.SysoutAction; import org.kie.kogito.serverless.workflow.actions.WorkflowLogLevel; @@ -67,9 +70,15 @@ public class StaticFluentWorkflowApplicationTest { @Test void helloWorld() { final String GREETING_STRING = "Hello World!!!"; - try (StaticWorkflowApplication application = StaticWorkflowApplication.create()) { + AtomicBoolean completed = new AtomicBoolean(false); + try (StaticWorkflowApplication application = StaticWorkflowApplication.builder().withEventListener(new DefaultKogitoProcessEventListener() { + public void afterProcessCompleted(ProcessCompletedEvent event) { + completed.set(true); + } + }).build()) { Workflow workflow = workflow("HelloWorld").start(inject(new TextNode(GREETING_STRING))).end().build(); assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata()).contains(new TextNode(GREETING_STRING)); + assertThat(completed.get()).isTrue(); } }