This is an automated email from the ASF dual-hosted git repository.

fjtiradosarti pushed a commit to branch main
in repository 
https://gitbox.apache.org/repos/asf/incubator-kie-kogito-runtimes.git


The following commit(s) were added to refs/heads/main by this push:
     new 9f52b4879b Test subflow after sleep (#4061)
9f52b4879b is described below

commit 9f52b4879be6a9b2a5b84b8137465e1a92d810ee
Author: gabriel-farache <[email protected]>
AuthorDate: Mon Sep 15 17:00:36 2025 +0200

    Test subflow after sleep (#4061)
    
    * IT for token propagation with subflow and sleep
    
    Signed-off-by: gabriel-farache <[email protected]>
    
    * Apply suggestions from code review
    
    Co-authored-by: Gonzalo Muñoz <[email protected]>
    
    * Use Awaitility.await() instead of while loop
    Signed-off-by: gabriel-farache <[email protected]>
    
    * Fix issue introduced by suggested commit
    
    Signed-off-by: gabriel-farache <[email protected]>
    
    ---------
    
    Signed-off-by: gabriel-farache <[email protected]>
    Co-authored-by: Gonzalo Muñoz <[email protected]>
---
 .../token-propagation-subflow.sw.json}             | 59 ++++++++++++----------
 .../src/main/resources/token-propagation.sw.json   | 23 +++++++++
 .../quarkus/workflows/ProcessAwaitUtils.java       | 50 ++++++++++++++++++
 .../kogito/quarkus/workflows/TokenExchangeIT.java  | 42 +--------------
 .../kogito/quarkus/workflows/TokenExpectation.java | 23 +++++++++
 .../TokenPropagationExternalServicesMock.java      |  6 ++-
 .../quarkus/workflows/TokenPropagationIT.java      | 40 ++++++++++++++-
 7 files changed, 171 insertions(+), 72 deletions(-)

diff --git 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/token-propagation.sw.json
 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/subflows/token-propagation-subflow.sw.json
similarity index 67%
copy from 
quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/token-propagation.sw.json
copy to 
quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/subflows/token-propagation-subflow.sw.json
index 190eb1fcca..178220bfc4 100644
--- 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/token-propagation.sw.json
+++ 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/subflows/token-propagation-subflow.sw.json
@@ -1,9 +1,9 @@
 {
-  "id": "token_propagation",
+  "id": "tokenPropagationSubflow",
   "version": "1.0",
-  "name": "Token propagation SW",
+  "name": "Token propagation subflow SW",
   "description": "Executes different external services at the time it combines 
the different token propagation options",
-  "start": "Execution1",
+  "start": "SubflowExecution1",
   "errors": [
     {
       "name": "execution_error",
@@ -14,35 +14,38 @@
     {
       "name": "executeQuery1",
       "type": "rest",
-      "operation": 
"specs/token-propagation-external-service1.yaml#executeQuery1"
+      "operation": 
"../specs/token-propagation-external-service1.yaml#executeQuery1"
     },
     {
       "name": "executeQuery2",
       "type": "rest",
-      "operation": 
"specs/token-propagation-external-service2.yaml#executeQuery2"
+      "operation": 
"../specs/token-propagation-external-service2.yaml#executeQuery2"
     },
     {
       "name": "executeQuery3",
       "type": "rest",
-      "operation": 
"specs/token-propagation-external-service3.yaml#executeQuery3"
+      "operation": 
"../specs/token-propagation-external-service3.yaml#executeQuery3"
     },
     {
       "name": "executeQuery4",
       "type": "rest",
-      "operation": 
"specs/token-propagation-external-service4.yaml#executeQuery4"
+      "operation": 
"../specs/token-propagation-external-service4.yaml#executeQuery4"
     },
     {
       "name": "executeQuery5",
       "type": "rest",
-      "operation": 
"specs/token-propagation-external-service5.yaml#executeQuery5"
+      "operation": 
"../specs/token-propagation-external-service5.yaml#executeQuery5"
     }
   ],
   "states": [
     {
-      "name": "Execution1",
+      "name": "SubflowExecution1",
       "type": "operation",
       "actions": [
         {
+          "sleep": {
+            "before": "PT5S"
+          },
           "name": "executeQuery1Action",
           "functionRef": {
             "refName": "executeQuery1",
@@ -53,16 +56,16 @@
           }
         }
       ],
-      "transition": "Execution2",
+      "transition": "SubflowExecution2",
       "onErrors": [
         {
           "errorRef": "execution_error",
-          "transition": "EndWithError"
+          "transition": "SubflowEndWithError"
         }
       ]
     },
     {
-      "name": "Execution2",
+      "name": "SubflowExecution2",
       "type": "operation",
       "actions": [
         {
@@ -76,16 +79,16 @@
           }
         }
       ],
-      "transition": "Execution3",
+      "transition": "SubflowExecution3",
       "onErrors": [
         {
           "errorRef": "execution_error",
-          "transition": "EndWithError"
+          "transition": "SubflowEndWithError"
         }
       ]
     },
     {
-      "name": "Execution3",
+      "name": "SubflowExecution3",
       "type": "operation",
       "actions": [
         {
@@ -99,16 +102,16 @@
           }
         }
       ],
-      "transition": "Execution4",
+      "transition": "SubflowExecution4",
       "onErrors": [
         {
           "errorRef": "execution_error",
-          "transition": "EndWithError"
+          "transition": "SubflowEndWithError"
         }
       ]
     },
     {
-      "name": "Execution4",
+      "name": "SubflowExecution4",
       "type": "operation",
       "actions": [
         {
@@ -122,16 +125,16 @@
           }
         }
       ],
-      "transition": "Execution5",
+      "transition": "SubflowExecution5",
       "onErrors": [
         {
           "errorRef": "execution_error",
-          "transition": "EndWithError"
+          "transition": "SubflowEndWithError"
         }
       ]
     },
     {
-      "name": "Execution5",
+      "name": "SubflowExecution5",
       "type": "operation",
       "actions": [
         {
@@ -145,27 +148,27 @@
           }
         }
       ],
-      "transition": "End",
+      "transition": "SubflowEnd",
       "onErrors": [
         {
           "errorRef": "execution_error",
-          "transition": "EndWithError"
+          "transition": "SubflowEndWithError"
         }
       ]
     },
     {
-      "name": "EndWithError",
+      "name": "SubflowEndWithError",
       "type": "inject",
       "data": {
-        "executionStatus": "Service execution failed"
+        "executionStatus": "Subflow Service execution failed"
       },
-      "transition": "End"
+      "transition": "SubflowEnd"
     },
     {
-      "name": "End",
+      "name": "SubflowEnd",
       "type": "inject",
       "data": {
-        "executionStatus": "Service execution successful"
+        "executionStatus": "Subflow Service execution successful"
       },
       "end": true
     }
diff --git 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/token-propagation.sw.json
 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/token-propagation.sw.json
index 190eb1fcca..eda65c8575 100644
--- 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/token-propagation.sw.json
+++ 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/token-propagation.sw.json
@@ -136,6 +136,9 @@
       "actions": [
         {
           "name": "executeQuery5Action",
+          "sleep": {
+            "before": "PT5S"
+          },
           "functionRef": {
             "refName": "executeQuery5",
             "arguments": {
@@ -145,6 +148,26 @@
           }
         }
       ],
+      "transition": "ExecuteSubflow",
+      "onErrors": [
+        {
+          "errorRef": "execution_error",
+          "transition": "EndWithError"
+        }
+      ]
+    },
+    {
+      "name": "ExecuteSubflow",
+      "type": "operation",
+      "actions": [
+        {
+          "name": "executeSubflow",
+          "subFlowRef": "tokenPropagationSubflow",
+          "actionDataFilter": {
+            "useResults": false
+          }
+        }
+      ],
       "transition": "End",
       "onErrors": [
         {
diff --git 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ProcessAwaitUtils.java
 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ProcessAwaitUtils.java
new file mode 100644
index 0000000000..4ed561f2f0
--- /dev/null
+++ 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ProcessAwaitUtils.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.quarkus.workflows;
+
+import java.time.Duration;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.restassured.http.ContentType;
+
+import static io.restassured.RestAssured.given;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.awaitility.Awaitility.await;
+
+public final class ProcessAwaitUtils {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(ProcessAwaitUtils.class);
+
+    private ProcessAwaitUtils() {
+    }
+
+    public static void waitForProcessCompletion(String basePath, String 
processInstanceId, Duration timeout) {
+        LOGGER.info("Waiting up to {} for process instance {} at path {} to 
complete", timeout, processInstanceId, basePath);
+        await("process completion").atMost(timeout)
+                .with().pollInterval(1, SECONDS)
+                .untilAsserted(() -> given()
+                        .contentType(ContentType.JSON)
+                        .accept(ContentType.JSON)
+                        .get("/" + basePath + "/" + processInstanceId)
+                        .then()
+                        .statusCode(404));
+    }
+}
diff --git 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenExchangeIT.java
 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenExchangeIT.java
index 981381b67c..92010a0d67 100644
--- 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenExchangeIT.java
+++ 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenExchangeIT.java
@@ -41,7 +41,6 @@ import io.restassured.path.json.JsonPath;
 
 import jakarta.ws.rs.core.HttpHeaders;
 
-import static io.restassured.RestAssured.given;
 import static 
org.kie.kogito.addons.quarkus.token.exchange.OpenApiCustomCredentialProvider.LOG_PREFIX_COMPLETED_TOKEN_EXCHANGE;
 import static 
org.kie.kogito.addons.quarkus.token.exchange.OpenApiCustomCredentialProvider.LOG_PREFIX_FAILED_TOKEN_EXCHANGE;
 import static 
org.kie.kogito.addons.quarkus.token.exchange.OpenApiCustomCredentialProvider.LOG_PREFIX_STARTING_TOKEN_EXCHANGE;
@@ -84,7 +83,7 @@ class TokenExchangeIT {
         // Wait for the process to complete - it should take approximately 11+ 
seconds
         // due to the 1s delay + 10s delay in the workflow
         long startTime = System.currentTimeMillis();
-        waitForProcessCompletion(processInstanceId, Duration.ofSeconds(25));
+        ProcessAwaitUtils.waitForProcessCompletion("token_exchange", 
processInstanceId, Duration.ofSeconds(25));
         long endTime = System.currentTimeMillis();
 
         LOGGER.info("Process completed in {} seconds", (endTime - startTime) / 
1000.0);
@@ -161,45 +160,6 @@ class TokenExchangeIT {
         LOGGER.info("  - Token refresh: {} times", 
refreshTokenExchangeLogLines.size());
     }
 
-    private void waitForProcessCompletion(String processInstanceId, Duration 
timeout) {
-        long startTime = System.currentTimeMillis();
-        long timeoutMs = timeout.toMillis();
-
-        while (System.currentTimeMillis() - startTime < timeoutMs) {
-            try {
-                // Check if process still exists - 404 means it completed and 
was cleaned up
-                int statusCode = given()
-                        .contentType("application/json")
-                        .accept("application/json")
-                        .when()
-                        .get("/token_exchange/" + processInstanceId)
-                        .then()
-                        .extract()
-                        .statusCode();
-
-                if (statusCode == 404) {
-                    LOGGER.info("Process instance {} completed successfully 
(404 - cleaned up)", processInstanceId);
-                    return;
-                }
-
-                Thread.sleep(1000); // Wait 1 second before checking again
-            } catch (InterruptedException e) {
-                Thread.currentThread().interrupt();
-                throw new RuntimeException("Interrupted while waiting for 
process completion", e);
-            } catch (Exception e) {
-                LOGGER.debug("Error checking process state (will retry): {}", 
e.getMessage());
-                try {
-                    Thread.sleep(1000);
-                } catch (InterruptedException ie) {
-                    Thread.currentThread().interrupt();
-                    throw new RuntimeException("Interrupted while waiting for 
process completion", ie);
-                }
-            }
-        }
-
-        throw new RuntimeException("Process instance " + processInstanceId + " 
did not complete within " + timeout);
-    }
-
     protected static String buildProcessInput(String query) {
         return "{\"workflowdata\": {\"query\": \"" + query + "\"} }";
     }
diff --git 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenExpectation.java
 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenExpectation.java
new file mode 100644
index 0000000000..915c628125
--- /dev/null
+++ 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenExpectation.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.kie.kogito.quarkus.workflows;
+
+public record TokenExpectation(String url, String token, int expectedCalls) {
+}
diff --git 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationExternalServicesMock.java
 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationExternalServicesMock.java
index acc2c88be5..20dfef02eb 100644
--- 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationExternalServicesMock.java
+++ 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationExternalServicesMock.java
@@ -49,7 +49,11 @@ public class TokenPropagationExternalServicesMock implements 
QuarkusTestResource
 
     public static final String TOKEN_PROPAGATION_EXTERNAL_SERVICE_MOCK_URL = 
"propagation-external-service-mock.url";
 
-    private WireMockServer wireMockServer;
+    private static WireMockServer wireMockServer;
+
+    public static WireMockServer getInstance() {
+        return wireMockServer;
+    }
 
     @Override
     public Map<String, String> start() {
diff --git 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationIT.java
 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationIT.java
index 174ce0c6eb..0b96dea0c0 100644
--- 
a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationIT.java
+++ 
b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationIT.java
@@ -18,12 +18,18 @@
  */
 package org.kie.kogito.quarkus.workflows;
 
+import java.time.Duration;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.assertj.core.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.client.WireMock;
+import com.github.tomakehurst.wiremock.verification.LoggedRequest;
+
 import io.quarkus.test.common.QuarkusTestResource;
 import io.quarkus.test.junit.QuarkusIntegrationTest;
 import io.restassured.path.json.JsonPath;
@@ -44,7 +50,7 @@ import static 
org.kie.kogito.test.utils.ProcessInstancesRESTTestUtils.newProcess
 class TokenPropagationIT {
 
     @Test
-    void tokenPropagations() {
+    void tokenPropagations() throws InterruptedException {
         // start a new process instance by sending the post query and collect 
the process instance id.
         String processInput = buildProcessInput(SUCCESSFUL_QUERY);
         Map<String, String> headers = new HashMap<>();
@@ -57,10 +63,40 @@ class TokenPropagationIT {
         headers.put(SERVICE4_HEADER_TO_PROPAGATE, 
SERVICE4_AUTHORIZATION_TOKEN);
 
         JsonPath jsonPath = newProcessInstance("/token_propagation", 
processInput, headers);
-        Assertions.assertThat(jsonPath.getString("id")).isNotBlank();
+        String processInstanceId = jsonPath.getString("id");
+        Assertions.assertThat(processInstanceId).isNotBlank();
+        ProcessAwaitUtils.waitForProcessCompletion("token_propagation", 
processInstanceId, Duration.ofSeconds(25));
+        validateExternalServiceInvocations();
     }
 
     protected static String buildProcessInput(String query) {
         return "{\"workflowdata\": {\"query\": \"" + query + "\"} }";
     }
+
+    private void validateExternalServiceInvocations() {
+        WireMockServer wm = TokenPropagationExternalServicesMock.getInstance();
+
+        List<TokenExpectation> expectations = List.of(
+                new 
TokenExpectation("/token-propagation-external-service1/executeQuery1", "Bearer 
" + AUTHORIZATION_TOKEN, 2),
+                new 
TokenExpectation("/token-propagation-external-service2/executeQuery2", "Bearer 
" + AUTHORIZATION_TOKEN, 2),
+                new 
TokenExpectation("/token-propagation-external-service3/executeQuery3", "Bearer 
" + SERVICE3_AUTHORIZATION_TOKEN, 2),
+                new 
TokenExpectation("/token-propagation-external-service4/executeQuery4", "Bearer 
" + SERVICE4_AUTHORIZATION_TOKEN, 2),
+                new 
TokenExpectation("/token-propagation-external-service5/executeQuery5", "Bearer 
" + KeycloakServiceMock.KEYCLOAK_ACCESS_TOKEN, 2));
+
+        expectations.forEach(e -> assertCalledExactlyWithAuth(wm, e.url(), 
e.token(), e.expectedCalls()));
+    }
+
+    private void assertCalledExactlyWithAuth(WireMockServer wm, String url, 
String expectedAuthHeader, int expectedCalls) {
+        var pattern = WireMock.postRequestedFor(WireMock.urlEqualTo(url));
+        var requests = wm.findAll(pattern);
+        Assertions.assertThat(requests)
+                .as("Expected %s to be called exactly %d times", url, 
expectedCalls)
+                .hasSize(expectedCalls);
+        for (LoggedRequest req : requests) {
+            Assertions.assertThat(req.getHeader(HttpHeaders.AUTHORIZATION))
+                    .as("Expected Authorization header should match for %s but 
got '%s'",
+                            url, req.getHeader(HttpHeaders.AUTHORIZATION))
+                    .isEqualTo(expectedAuthHeader);
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to