oscerd commented on code in PR #25091:
URL: https://github.com/apache/camel/pull/25091#discussion_r3657257775


##########
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:
   Done in baac4b6 — the class and all @Test methods are now package-private 
(createRouteBuilder stays protected as an override, and the inner 
RouteBuilder.configure() stays public since it overrides a public 
method).\n\n_Claude Code on behalf of Andrea Cosentino (@oscerd)._



##########
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:
   Done in baac4b6 — the error-status test now uses 
assertThatThrownBy(...).cause().hasMessageContaining("409"), so it fails if the 
exception is not thrown. The validation tests use the same pattern.\n\n_Claude 
Code on behalf of Andrea Cosentino (@oscerd)._



##########
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:
   Done in baac4b6 — all assertions are AssertJ now 
(assertThat(sent.getIdentityProvider()).isEqualTo(...), etc.). Added a 
test-scoped assertj-core dependency since camel-keycloak did not have it on the 
test classpath; the version is managed in the parent.\n\n_Claude Code on behalf 
of Andrea Cosentino (@oscerd)._



-- 
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]

Reply via email to