From 97257f957789049239a5c0ab3ae93c8363b8d8ef Mon Sep 17 00:00:00 2001 From: John Niang Date: Tue, 27 Aug 2024 17:23:18 +0800 Subject: [PATCH] Fix the problem that return empty result while listing users (#6532) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area core /milestone 2.19.0 #### What this PR does / why we need it: This PR makes the return value of method `DefaultRoleService#getRolesByUsernames` never be `Mono#empty`. See https://github.com/halo-dev/halo/issues/6528 for more. #### Which issue(s) this PR fixes: Fixes https://github.com/halo-dev/halo/issues/6528 #### Special notes for your reviewer: 1. Try to execute command `http -a admin:admin http://127.0.0.1:8090/apis/api.console.halo.run/v1alpha1/users?keyword=xyz`. 2. See the output #### Does this PR introduce a user-facing change? ```release-note 修复获取用户列表时可能返回空结果的问题 ``` --- .../extension/service/DefaultRoleService.java | 2 +- .../service/DefaultRoleServiceTest.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java b/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java index 11b042aae..b02954113 100644 --- a/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java +++ b/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java @@ -72,7 +72,7 @@ public class DefaultRoleService implements RoleService { @Override public Mono>> getRolesByUsernames(Collection usernames) { if (CollectionUtils.isEmpty(usernames)) { - return Mono.empty(); + return Mono.just(Map.of()); } var subjects = usernames.stream().map(DefaultRoleService::toUserSubject) .map(Object::toString) diff --git a/application/src/test/java/run/halo/app/core/extension/service/DefaultRoleServiceTest.java b/application/src/test/java/run/halo/app/core/extension/service/DefaultRoleServiceTest.java index b2ca0f179..2498b0e10 100644 --- a/application/src/test/java/run/halo/app/core/extension/service/DefaultRoleServiceTest.java +++ b/application/src/test/java/run/halo/app/core/extension/service/DefaultRoleServiceTest.java @@ -8,6 +8,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,6 +19,8 @@ import org.assertj.core.api.AssertionsForInterfaceTypes; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -45,6 +48,19 @@ class DefaultRoleServiceTest { @InjectMocks private DefaultRoleService roleService; + @ParameterizedTest + @MethodSource("usernamesProvider") + void shouldReturnEmptyMapIfNoUsernamesProvided(Collection usernames) { + roleService.getRolesByUsernames(usernames) + .as(StepVerifier::create) + .expectNext(Map.of()) + .verifyComplete(); + } + + static Stream> usernamesProvider() { + return Stream.of(null, List.of(), Set.of()); + } + @Nested class ListDependenciesTest { @Test