From 95ef27fb169fa7ab7295d491f4a6ad9c8ea2fc3d Mon Sep 17 00:00:00 2001 From: Gheorghe Soimu Date: Tue, 2 Jul 2024 15:52:49 +0300 Subject: [PATCH] fixed some issues with user matching --- .../webprotegeusermanagement/commands/UserRepository.java | 2 +- .../commands/UsersQueryCommandHandler.java | 2 +- .../commands/dto/KeycloakUserRepository.java | 4 ++-- .../commands/dto/UsersQueryRequest.java | 2 +- .../KeycloakUserRepositoryTest.java | 7 +++---- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/UserRepository.java b/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/UserRepository.java index ebe2c71..8382f0e 100644 --- a/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/UserRepository.java +++ b/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/UserRepository.java @@ -6,5 +6,5 @@ public interface UserRepository { - public List findUserIdsFromName(String name); + List findUserIdsFromName(String name, boolean exactMatch); } diff --git a/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/UsersQueryCommandHandler.java b/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/UsersQueryCommandHandler.java index 80d2d58..b044758 100644 --- a/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/UsersQueryCommandHandler.java +++ b/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/UsersQueryCommandHandler.java @@ -38,7 +38,7 @@ public Class getRequestClass() { @Override public Mono handleRequest(UsersQueryRequest request, ExecutionContext executionContext) { LOGGER.info("Handle request"); - List users = userRepository.findUserIdsFromName(request.userName()); + List users = userRepository.findUserIdsFromName(request.userName(), request.exactMatch()); return Mono.just(new UsersQueryResponse(users)); } } diff --git a/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/dto/KeycloakUserRepository.java b/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/dto/KeycloakUserRepository.java index 30d91c4..1777a08 100644 --- a/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/dto/KeycloakUserRepository.java +++ b/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/dto/KeycloakUserRepository.java @@ -28,10 +28,10 @@ public KeycloakUserRepository( @Value("${webprotege.keycloak.realmName}") String @Override - public List findUserIdsFromName(String name) { + public List findUserIdsFromName(String name, boolean exactMatch) { try { return keycloak.realm(realmName) - .users().search(name, false).stream() + .users().search(name, exactMatch).stream() .map(userRepresentation -> new UserId(userRepresentation.getUsername())) .collect(Collectors.toList()); }catch (Exception e) { diff --git a/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/dto/UsersQueryRequest.java b/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/dto/UsersQueryRequest.java index d1cabae..98135c9 100644 --- a/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/dto/UsersQueryRequest.java +++ b/src/main/java/edu/stanford/protege/webprotegeusermanagement/commands/dto/UsersQueryRequest.java @@ -4,7 +4,7 @@ import edu.stanford.protege.webprotege.common.Request; @JsonTypeName(UsersQueryRequest.CHANNEL) -public record UsersQueryRequest(@JsonProperty("completionText") String userName) implements Request { +public record UsersQueryRequest(@JsonProperty("completionText") String userName, @JsonProperty("exactMatch") boolean exactMatch) implements Request { public final static String CHANNEL = "webprotege.usersquery.QueryUsers"; diff --git a/src/test/java/edu/stanford/protege/webprotegeusermanagement/KeycloakUserRepositoryTest.java b/src/test/java/edu/stanford/protege/webprotegeusermanagement/KeycloakUserRepositoryTest.java index 068c656..2dac7d3 100644 --- a/src/test/java/edu/stanford/protege/webprotegeusermanagement/KeycloakUserRepositoryTest.java +++ b/src/test/java/edu/stanford/protege/webprotegeusermanagement/KeycloakUserRepositoryTest.java @@ -3,7 +3,6 @@ import edu.stanford.protege.webprotege.common.UserId; import edu.stanford.protege.webprotegeusermanagement.commands.dto.KeycloakUserRepository; -import org.apache.http.ConnectionClosedException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -51,7 +50,7 @@ public void GIVEN_anExistingUser_WHEN_queryForUsers_THEN_userIsCorrectlyMapped() user.setUsername("johndoe"); when(userResource.search(eq("john"), eq(false))).thenReturn(Arrays.asList(user)); - List response = repository.findUserIdsFromName("john"); + List response = repository.findUserIdsFromName("john", false); assertNotNull(response); assertEquals(1, response.size()); @@ -62,7 +61,7 @@ public void GIVEN_anExistingUser_WHEN_queryForUsers_THEN_userIsCorrectlyMapped() public void GIVEN_missingUser_WHEN_queryForUsers_THEN_responseIsEmpty(){ when(userResource.search(eq("alice"), eq(false))).thenReturn(new ArrayList<>()); - List response = repository.findUserIdsFromName("alice"); + List response = repository.findUserIdsFromName("alice", false); assertNotNull(response); assertEquals(0, response.size()); @@ -72,7 +71,7 @@ public void GIVEN_missingUser_WHEN_queryForUsers_THEN_responseIsEmpty(){ public void GIVEN_exception_WHEN_fetchForUsers_THEN_responseIsEmpty(){ when(userResource.search(eq("bob"), eq(false))).thenThrow(new RuntimeException()); - List response = repository.findUserIdsFromName("bob"); + List response = repository.findUserIdsFromName("bob", false); assertNotNull(response); assertEquals(0, response.size());