From 075e4f540d040327ef2fab8a9012bf80c51e137c Mon Sep 17 00:00:00 2001 From: xuelianhan007 Date: Mon, 2 Dec 2024 18:05:19 +0800 Subject: [PATCH] fix state in user list --- .../operator/auth/repo/UserRepository.java | 3 ++- .../operator/auth/service/UserService.java | 5 ++++- .../controller/UserMgmtControllerTest.java | 21 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/repo/UserRepository.java b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/repo/UserRepository.java index 0b4c62c7..ec82d758 100644 --- a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/repo/UserRepository.java +++ b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/repo/UserRepository.java @@ -1,6 +1,7 @@ package com.consoleconnect.kraken.operator.auth.repo; import com.consoleconnect.kraken.operator.auth.entity.UserEntity; +import com.consoleconnect.kraken.operator.auth.enums.UserStateEnum; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -24,5 +25,5 @@ public interface UserRepository + " and ((:role) is null or e.role = :role)") @Transactional(readOnly = true) Page search( - String q, Pageable pageable, List filterRoles, String state, String role); + String q, Pageable pageable, List filterRoles, UserStateEnum state, String role); } diff --git a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/service/UserService.java b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/service/UserService.java index c75781d1..e04545ea 100644 --- a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/service/UserService.java +++ b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/service/UserService.java @@ -19,6 +19,7 @@ import java.util.*; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -134,12 +135,14 @@ public User create(CreateUserRequest request, String createdBy) { public Paging search( String q, PageRequest pageRequest, boolean filterInternalUser, String state, String role) { log.info("Searching users, q:{}, pageRequest:{}", q, pageRequest); + UserStateEnum userStateEnum = + (StringUtils.isBlank(state) ? null : UserStateEnum.valueOf(state)); Page userEntityPage = userRepository.search( q, pageRequest, filterInternalUser ? List.of(UserRoleEnum.INTERNAL_USER.name()) : null, - state, + userStateEnum, role); log.info("Users found:{}", userEntityPage.getTotalElements()); return PagingHelper.toPaging(userEntityPage, UserMapper.INSTANCE::toUser); diff --git a/kraken-java-sdk/kraken-java-sdk-auth/src/test/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtControllerTest.java b/kraken-java-sdk/kraken-java-sdk-auth/src/test/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtControllerTest.java index dff99503..a25b871f 100644 --- a/kraken-java-sdk/kraken-java-sdk-auth/src/test/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtControllerTest.java +++ b/kraken-java-sdk/kraken-java-sdk-auth/src/test/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtControllerTest.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import java.util.*; import java.util.function.Consumer; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.SpyBean; @@ -31,6 +32,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.reactive.server.WebTestClient; +@Slf4j @ActiveProfiles("test-login-enabled") @MockIntegrationTest @ContextConfiguration(classes = {TestApplication.class}) @@ -429,6 +431,25 @@ void givenUserCreated_whenSearch_thenReturnOk() { assertThat(bodyStr, hasJsonPath("$.data.data", hasSize(greaterThanOrEqualTo(1)))); assertThat(bodyStr, hasJsonPath("$.data.data[0].email", equalTo("abc@test.com"))); }); + + // search by role and state + testClientHelper.requestAndVerify( + HttpMethod.GET, + (uriBuilder -> + uriBuilder + .path("/users") + .queryParam("role", "USER") + .queryParam("state", "ENABLED") + .build()), + headers, + null, + HttpStatus.OK.value(), + bodyStr -> { + log.info("bodyStr:{}", bodyStr); + Assertions.assertNotNull(bodyStr); + assertThat(bodyStr, hasJsonPath("$.data.data", notNullValue())); + assertThat(bodyStr, hasJsonPath("$.data.data", hasSize(greaterThanOrEqualTo(3)))); + }); } @Test