davsclaus commented on code in PR #25091: URL: https://github.com/apache/camel/pull/25091#discussion_r3645277709
########## components/camel-keycloak/src/test/java/org/apache/camel/component/keycloak/KeycloakProducerFederatedIdentityTest.java: ########## @@ -0,0 +1,237 @@ +/* + * 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.apache.camel.component.keycloak; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import jakarta.ws.rs.core.Response; + +import org.apache.camel.BindToRegistry; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.keycloak.admin.client.Keycloak; +import org.keycloak.admin.client.resource.RealmResource; +import org.keycloak.admin.client.resource.UserResource; +import org.keycloak.admin.client.resource.UsersResource; +import org.keycloak.representations.idm.FederatedIdentityRepresentation; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Unit tests for the federated identity (identity provider link) operations. + */ +public class KeycloakProducerFederatedIdentityTest extends CamelTestSupport { + Review Comment: Per project test conventions, new test classes should be package-private (no `public` modifier). Same applies to all `@Test` methods in this file. The existing `KeycloakProducerTest` uses `public` (pre-existing style), but new files should follow the current convention. ```suggestion class KeycloakProducerFederatedIdentityTest extends CamelTestSupport { ``` ########## components/camel-keycloak/src/test/java/org/apache/camel/component/keycloak/KeycloakProducerFederatedIdentityTest.java: ########## @@ -0,0 +1,237 @@ +/* + * 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.apache.camel.component.keycloak; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import jakarta.ws.rs.core.Response; + +import org.apache.camel.BindToRegistry; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.keycloak.admin.client.Keycloak; +import org.keycloak.admin.client.resource.RealmResource; +import org.keycloak.admin.client.resource.UserResource; +import org.keycloak.admin.client.resource.UsersResource; +import org.keycloak.representations.idm.FederatedIdentityRepresentation; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Unit tests for the federated identity (identity provider link) operations. + */ +public class KeycloakProducerFederatedIdentityTest extends CamelTestSupport { + + @BindToRegistry("keycloakClient") + private Keycloak keycloakClient = Mockito.mock(Keycloak.class); + + private RealmResource realmResource = Mockito.mock(RealmResource.class); + private UsersResource usersResource = Mockito.mock(UsersResource.class); + private UserResource userResource = Mockito.mock(UserResource.class); + private Response response = Mockito.mock(Response.class); + + private void stubUserLookup() { + when(keycloakClient.realm(anyString())).thenReturn(realmResource); + when(realmResource.users()).thenReturn(usersResource); + when(usersResource.get(anyString())).thenReturn(userResource); + } + + private Map<String, Object> headers(String... keyValues) { + Map<String, Object> headers = new HashMap<>(); + headers.put(KeycloakConstants.REALM_NAME, "testRealm"); + headers.put(KeycloakConstants.USER_ID, "user-1"); + for (int i = 0; i < keyValues.length; i += 2) { + headers.put(keyValues[i], keyValues[i + 1]); + } + return headers; + } + + @Test + public void testAddFederatedIdentity() throws Exception { + stubUserLookup(); + when(response.getStatus()).thenReturn(204); + when(userResource.addFederatedIdentity(anyString(), any(FederatedIdentityRepresentation.class))) + .thenReturn(response); + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + template.sendBodyAndHeaders("direct:addFederatedIdentity", null, + headers(KeycloakConstants.IDENTITY_PROVIDER, "google", + KeycloakConstants.FEDERATED_USER_ID, "google-123", + KeycloakConstants.FEDERATED_USERNAME, "[email protected]")); + + MockEndpoint.assertIsSatisfied(context); + + ArgumentCaptor<FederatedIdentityRepresentation> captor + = ArgumentCaptor.forClass(FederatedIdentityRepresentation.class); + verify(userResource).addFederatedIdentity(eq("google"), captor.capture()); + + FederatedIdentityRepresentation sent = captor.getValue(); + assertEquals("google", sent.getIdentityProvider()); Review Comment: Minor: project guidelines prefer AssertJ assertions (`assertThat(...)`) over JUnit assertions for new test code. The existing `KeycloakProducerTest` uses JUnit style, but new files should prefer the updated convention. For example: ```java assertThat(sent.getIdentityProvider()).isEqualTo("google"); assertThat(sent.getUserId()).isEqualTo("google-123"); assertThat(sent.getUserName()).isEqualTo("[email protected]"); ``` ########## components/camel-keycloak/src/test/java/org/apache/camel/component/keycloak/KeycloakProducerFederatedIdentityTest.java: ########## @@ -0,0 +1,237 @@ +/* + * 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.apache.camel.component.keycloak; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import jakarta.ws.rs.core.Response; + +import org.apache.camel.BindToRegistry; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.keycloak.admin.client.Keycloak; +import org.keycloak.admin.client.resource.RealmResource; +import org.keycloak.admin.client.resource.UserResource; +import org.keycloak.admin.client.resource.UsersResource; +import org.keycloak.representations.idm.FederatedIdentityRepresentation; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Unit tests for the federated identity (identity provider link) operations. + */ +public class KeycloakProducerFederatedIdentityTest extends CamelTestSupport { + + @BindToRegistry("keycloakClient") + private Keycloak keycloakClient = Mockito.mock(Keycloak.class); + + private RealmResource realmResource = Mockito.mock(RealmResource.class); + private UsersResource usersResource = Mockito.mock(UsersResource.class); + private UserResource userResource = Mockito.mock(UserResource.class); + private Response response = Mockito.mock(Response.class); + + private void stubUserLookup() { + when(keycloakClient.realm(anyString())).thenReturn(realmResource); + when(realmResource.users()).thenReturn(usersResource); + when(usersResource.get(anyString())).thenReturn(userResource); + } + + private Map<String, Object> headers(String... keyValues) { + Map<String, Object> headers = new HashMap<>(); + headers.put(KeycloakConstants.REALM_NAME, "testRealm"); + headers.put(KeycloakConstants.USER_ID, "user-1"); + for (int i = 0; i < keyValues.length; i += 2) { + headers.put(keyValues[i], keyValues[i + 1]); + } + return headers; + } + + @Test + public void testAddFederatedIdentity() throws Exception { + stubUserLookup(); + when(response.getStatus()).thenReturn(204); + when(userResource.addFederatedIdentity(anyString(), any(FederatedIdentityRepresentation.class))) + .thenReturn(response); + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + template.sendBodyAndHeaders("direct:addFederatedIdentity", null, + headers(KeycloakConstants.IDENTITY_PROVIDER, "google", + KeycloakConstants.FEDERATED_USER_ID, "google-123", + KeycloakConstants.FEDERATED_USERNAME, "[email protected]")); + + MockEndpoint.assertIsSatisfied(context); + + ArgumentCaptor<FederatedIdentityRepresentation> captor + = ArgumentCaptor.forClass(FederatedIdentityRepresentation.class); + verify(userResource).addFederatedIdentity(eq("google"), captor.capture()); + + FederatedIdentityRepresentation sent = captor.getValue(); + assertEquals("google", sent.getIdentityProvider()); + assertEquals("google-123", sent.getUserId()); + assertEquals("[email protected]", sent.getUserName()); + } + + @Test + public void testAddFederatedIdentityFallsBackToUserIdAsUsername() throws Exception { + stubUserLookup(); + when(response.getStatus()).thenReturn(204); + when(userResource.addFederatedIdentity(anyString(), any(FederatedIdentityRepresentation.class))) + .thenReturn(response); + + template.sendBodyAndHeaders("direct:addFederatedIdentity", null, + headers(KeycloakConstants.IDENTITY_PROVIDER, "github", + KeycloakConstants.FEDERATED_USER_ID, "gh-42")); + + ArgumentCaptor<FederatedIdentityRepresentation> captor + = ArgumentCaptor.forClass(FederatedIdentityRepresentation.class); + verify(userResource).addFederatedIdentity(eq("github"), captor.capture()); + + // no username supplied, so the external user id is used rather than leaving the link with a blank username + assertEquals("gh-42", captor.getValue().getUserName()); + } + + @Test + public void testAddFederatedIdentityFailsOnErrorStatus() throws Exception { + stubUserLookup(); + when(response.getStatus()).thenReturn(409); + when(userResource.addFederatedIdentity(anyString(), any(FederatedIdentityRepresentation.class))) + .thenReturn(response); + + try { + template.sendBodyAndHeaders("direct:addFederatedIdentity", null, + headers(KeycloakConstants.IDENTITY_PROVIDER, "google", + KeycloakConstants.FEDERATED_USER_ID, "google-123")); + } catch (Exception e) { + assertTrue(e.getCause().getMessage().contains("409"), + "The failing status should be reported — was: " + e.getCause().getMessage()); + } + } Review Comment: This try-catch pattern silently passes if the exception is never thrown — a broken status check would go unnoticed. The same pattern exists in `KeycloakProducerTest`, but for new code `assertThrows` is more robust: ```java Exception e = assertThrows(Exception.class, () -> template.sendBodyAndHeaders("direct:addFederatedIdentity", null, headers(KeycloakConstants.IDENTITY_PROVIDER, "google", KeycloakConstants.FEDERATED_USER_ID, "google-123"))); assertThat(e.getCause().getMessage()).contains("409"); ``` Same concern applies to `testMissingIdentityProvider`, `testMissingFederatedUserId`, `testMissingUserId`, and `testMissingRealmName` below. -- 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]
