gmunozfe commented on code in PR #4061:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/4061#discussion_r2348464410


##########
quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationIT.java:
##########
@@ -57,10 +66,76 @@ void tokenPropagations() {
         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();
+        //Thread.sleep(20000);

Review Comment:
   remove this commented line for clarity



##########
quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationIT.java:
##########
@@ -57,10 +66,76 @@ void tokenPropagations() {
         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();
+        //Thread.sleep(20000);
+        waitForProcessCompletion(processInstanceId, Duration.ofSeconds(25));
+        validateExternalServiceInvocations();
     }
 
     protected static String buildProcessInput(String query) {
         return "{\"workflowdata\": {\"query\": \"" + query + "\"} }";
     }
+
+    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_propagation/" + 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);
+    }
+
+    private void validateExternalServiceInvocations() {
+        WireMockServer wm = TokenPropagationExternalServicesMock.getInstance();
+
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service1/executeQuery1", "Bearer " + 
AUTHORIZATION_TOKEN, 2);
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service2/executeQuery2", "Bearer " + 
AUTHORIZATION_TOKEN, 2);
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service3/executeQuery3", "Bearer " + 
SERVICE3_AUTHORIZATION_TOKEN, 2);
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service4/executeQuery4", "Bearer " + 
SERVICE4_AUTHORIZATION_TOKEN, 2);
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service5/executeQuery5", "Bearer " + 
KeycloakServiceMock.KEYCLOAK_ACCESS_TOKEN, 2);

Review Comment:
   As only token expectation is changing for each call, probably it would be 
easier to maintain if we group it in a list to iterate over it:
   
   ```suggestion
   List<TokenExpectation> expectations = List.of(
       new TokenExpectation("/service1", "Bearer " + AUTHORIZATION_TOKEN),
       new TokenExpectation("/service2", "Bearer " + AUTHORIZATION_TOKEN),
       new TokenExpectation("/service3", "Bearer " + 
SERVICE3_AUTHORIZATION_TOKEN),
       new TokenExpectation("/service4", "Bearer " + 
SERVICE4_AUTHORIZATION_TOKEN),
       new TokenExpectation("/service5", "Bearer " + 
KeycloakServiceMock.KEYCLOAK_ACCESS_TOKEN)
   );
   
   expectations.forEach(e -> assertCalledExactlyWithAuth(wm, e.url(), 
e.token(), 2));
   ```



##########
quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationIT.java:
##########
@@ -57,10 +66,76 @@ void tokenPropagations() {
         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();
+        //Thread.sleep(20000);
+        waitForProcessCompletion(processInstanceId, Duration.ofSeconds(25));
+        validateExternalServiceInvocations();
     }
 
     protected static String buildProcessInput(String query) {
         return "{\"workflowdata\": {\"query\": \"" + query + "\"} }";
     }
+
+    private void waitForProcessCompletion(String processInstanceId, Duration 
timeout) {
+        long startTime = System.currentTimeMillis();
+        long timeoutMs = timeout.toMillis();
+
+        while (System.currentTimeMillis() - startTime < timeoutMs) {

Review Comment:
   Instead of this loop, have you considered to use Awaitility.await() method 
to avoid handling retries, etc?



##########
quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/TokenPropagationIT.java:
##########
@@ -57,10 +66,76 @@ void tokenPropagations() {
         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();
+        //Thread.sleep(20000);
+        waitForProcessCompletion(processInstanceId, Duration.ofSeconds(25));
+        validateExternalServiceInvocations();
     }
 
     protected static String buildProcessInput(String query) {
         return "{\"workflowdata\": {\"query\": \"" + query + "\"} }";
     }
+
+    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_propagation/" + 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);
+    }
+
+    private void validateExternalServiceInvocations() {
+        WireMockServer wm = TokenPropagationExternalServicesMock.getInstance();
+
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service1/executeQuery1", "Bearer " + 
AUTHORIZATION_TOKEN, 2);
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service2/executeQuery2", "Bearer " + 
AUTHORIZATION_TOKEN, 2);
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service3/executeQuery3", "Bearer " + 
SERVICE3_AUTHORIZATION_TOKEN, 2);
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service4/executeQuery4", "Bearer " + 
SERVICE4_AUTHORIZATION_TOKEN, 2);
+        assertCalledExactlyWithAuth(wm, 
"/token-propagation-external-service5/executeQuery5", "Bearer " + 
KeycloakServiceMock.KEYCLOAK_ACCESS_TOKEN, 2);
+    }
+
+    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("Authorization header should match for %s", url)
+                    .isEqualTo(expectedAuthHeader);

Review Comment:
   For tracking the obtained result in case of failure, probably we can logged 
it too:
   
   ```suggestion
               Assertions.assertThat(req.getHeader(HttpHeaders.AUTHORIZATION))
                         .as("Expected Authorization header should match for %s 
but got '%s'", 
                          url, req.getHeader(HttpHeaders.AUTHORIZATION))
                       .isEqualTo(expectedAuthHeader);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to