ishanjogi89 commented on code in PR #4021: URL: https://github.com/apache/incubator-kie-kogito-runtimes/pull/4021#discussion_r2476725731
########## quarkus/addons/jwt-parser/integration-tests/src/test/java/org/kie/kogito/addons/jwt/it/JwtParserIT.java: ########## @@ -0,0 +1,180 @@ +/* + * 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.addons.jwt.it; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.kie.kogito.addons.jwt.JwtTokenParser; + +import io.quarkus.test.junit.QuarkusIntegrationTest; +import io.restassured.path.json.JsonPath; + +import jakarta.inject.Inject; +import jakarta.ws.rs.core.HttpHeaders; + +import static io.restassured.RestAssured.given; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.kie.kogito.test.utils.ProcessInstancesRESTTestUtils.assertProcessInstanceHasFinished; +import static org.kie.kogito.test.utils.ProcessInstancesRESTTestUtils.assertProcessInstanceNotExists; +import static org.kie.kogito.test.utils.ProcessInstancesRESTTestUtils.newProcessInstance; + +@QuarkusIntegrationTest +class JwtParserIT { + + @Inject + JwtTokenParser jwtTokenParser; + + // Valid JWT token for testing (contains: {"sub":"1234567890","preferred_username":"johndoe","email":"[email protected]","iat":1516239022}) + private static final String VALID_JWT_TOKEN = + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwicHJlZmVycmVkX3VzZXJuYW1lIjoiam9obmRvZSIsImVtYWlsIjoiam9obmRvZUBleGFtcGxlLmNvbSIsImlhdCI6MTUxNjIzOTAyMn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; + + @Test + void testJwtTokenParserInjection() { + assertThat(jwtTokenParser).isNotNull(); + } + + @Test + void testParseTokenWithNullToken() { + assertThrows(IllegalArgumentException.class, () -> jwtTokenParser.parseToken(null)); + } + + @Test + void testParseTokenWithEmptyToken() { + assertThrows(IllegalArgumentException.class, () -> jwtTokenParser.parseToken("")); + } + + @Test + void testParseValidJwtToken() { + var result = jwtTokenParser.parseToken(VALID_JWT_TOKEN); + assertThat(result).isNotNull(); + assertThat(result.get("sub").asText()).isEqualTo("1234567890"); + assertThat(result.get("preferred_username").asText()).isEqualTo("johndoe"); + assertThat(result.get("email").asText()).isEqualTo("[email protected]"); + } + + @Test + void testParseTokenWithBearerPrefix() { + String tokenWithBearer = "Bearer " + VALID_JWT_TOKEN; + var result = jwtTokenParser.parseToken(tokenWithBearer); + assertThat(result).isNotNull(); + assertThat(result.get("sub").asText()).isEqualTo("1234567890"); + } + + @Test + void testExtractUser() { + var result = jwtTokenParser.extractUser(VALID_JWT_TOKEN); + assertThat(result).isNotNull(); + assertThat(result.get("sub").asText()).isEqualTo("1234567890"); + assertThat(result.get("preferred_username").asText()).isEqualTo("johndoe"); + assertThat(result.get("email").asText()).isEqualTo("[email protected]"); + } + + @Test + void testExtractSpecificClaim() { + var result = jwtTokenParser.extractClaim(VALID_JWT_TOKEN, "preferred_username"); + assertThat(result).isNotNull(); + assertThat(result.asText()).isEqualTo("johndoe"); + } + + @Test + void testExtractUserWithInvalidToken() { + assertThrows(RuntimeException.class, () -> jwtTokenParser.extractUser("invalid.token.here")); + } + + @Test + void testExtractClaimWithInvalidToken() { + assertThrows(RuntimeException.class, () -> jwtTokenParser.extractClaim("invalid.token.here", "sub")); + } + + /** + * End-to-end test that verifies the JWT parser works within a complete SonataFlow workflow. + * This test demonstrates the feature working as requested in Issue #1899. + */ + @Test + void testJwtParserWorkflowEndToEnd() { + // Prepare workflow input + String processInput = "{}"; + + // Set up headers with JWT token + Map<String, String> headers = new HashMap<>(); + headers.put("X-Authorization-acme_financial_auth", VALID_JWT_TOKEN); + + // Start the workflow process + JsonPath jsonPath = newProcessInstance("/jwt_example", processInput, headers); + String processInstanceId = jsonPath.getString("id"); + assertThat(processInstanceId).isNotBlank(); + + // Wait for process completion and verify results + assertProcessInstanceHasFinished("/jwt_example/{id}", processInstanceId, 10); + + // Verify the final workflow output contains the expected user information + JsonPath completedProcess = given() Review Comment: @wmedvede @gabriel-farache All the review comment chanegs have been incorporated.Kindly take a look and let me know.Thanks! -- 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]
