Skip to content

Commit

Permalink
fix(sdk): added status and role for user list (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelianhan007 authored Dec 3, 2024
1 parent 189f7a1 commit 4370de7
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ public HttpResponse<Paging<User>> search(
@RequestParam(value = "size", required = false, defaultValue = PagingHelper.DEFAULT_SIZE_STR)
int size,
@RequestParam(value = "filterInternalUser", required = false, defaultValue = "false")
Boolean filterInternalUser) {
Boolean filterInternalUser,
@RequestParam(value = "state", required = false) String state,
@RequestParam(value = "role", required = false) String role) {
return HttpResponse.ok(
userService.search(
q, UnifiedAssetService.getSearchPageRequest(page, size), filterInternalUser));
q,
UnifiedAssetService.getSearchPageRequest(page, size),
filterInternalUser,
state,
role));
}

@Operation(summary = "add a new user")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +20,10 @@ public interface UserRepository
value =
"select e from #{#entityName} e "
+ " where ( (:q) is null or LOWER(e.name) like %:q% or LOWER(e.email) like %:q% )"
+ " and ( (:filterRoles) is null or e.role not in :filterRoles )")
+ " and ( (:filterRoles) is null or e.role not in :filterRoles )"
+ " and ((:state) is null or e.state = :state)"
+ " and ((:role) is null or e.role = :role)")
@Transactional(readOnly = true)
Page<UserEntity> search(String q, Pageable pageable, List<String> filterRoles);
Page<UserEntity> search(
String q, Pageable pageable, List<String> filterRoles, UserStateEnum state, String role);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,7 +78,8 @@ public void initialize() {

public void initSystemUpgradeUser() {
Paging<User> userPaging =
this.search(UserContext.SYSTEM_UPGRADE.toLowerCase(), PageRequest.of(0, 1), false);
this.search(
UserContext.SYSTEM_UPGRADE.toLowerCase(), PageRequest.of(0, 1), false, null, null);
if (userPaging.getTotal() == 0) {
CreateUserRequest request = new CreateUserRequest();
request.setEmail(UserContext.SYSTEM_UPGRADE);
Expand Down Expand Up @@ -122,18 +124,26 @@ public User create(CreateUserRequest request, String createdBy) {
UserEntity userEntity = UserMapper.INSTANCE.toEntity(request);
userEntity.setPassword(passwordEncoder.encode(request.getPassword()));
userEntity.setState(UserStateEnum.ENABLED);
userEntity.setRole(request.getRole());
userEntity.setCreatedAt(DateTime.nowInUTC());
userEntity.setCreatedBy(createdBy);
userEntity = userRepository.save(userEntity);
log.info("User {} created", userEntity.getId());
return UserMapper.INSTANCE.toUser(userEntity);
}

public Paging<User> search(String q, PageRequest pageRequest, boolean filterInternalUser) {
public Paging<User> 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<UserEntity> userEntityPage =
userRepository.search(
q, pageRequest, filterInternalUser ? List.of(UserRoleEnum.INTERNAL_USER.name()) : null);
q,
pageRequest,
filterInternalUser ? List.of(UserRoleEnum.INTERNAL_USER.name()) : null,
userStateEnum,
role);
log.info("Users found:{}", userEntityPage.getTotalElements());
return PagingHelper.toPaging(userEntityPage, UserMapper.INSTANCE::toUser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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})
Expand Down Expand Up @@ -429,6 +431,25 @@ void givenUserCreated_whenSearch_thenReturnOk() {
assertThat(bodyStr, hasJsonPath("$.data.data", hasSize(greaterThanOrEqualTo(1))));
assertThat(bodyStr, hasJsonPath("$.data.data[0].email", equalTo("[email protected]")));
});

// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public HttpResponse<Paging<UnifiedAssetDto>> search(
envId,
buyerId,
status,
null,
orderBy,
PageRequest.of(page, size, direction, FIELD_CREATE_AT_ORIGINAL)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.consoleconnect.kraken.operator.controller.mapper.BuyerAssetDtoMapper;
import com.consoleconnect.kraken.operator.controller.model.Environment;
import com.consoleconnect.kraken.operator.controller.model.MgmtProperty;
import com.consoleconnect.kraken.operator.core.dto.Tuple2;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.entity.UnifiedAssetEntity;
import com.consoleconnect.kraken.operator.core.enums.AssetStatusEnum;
Expand All @@ -25,17 +26,15 @@
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -94,6 +93,44 @@ public BuyerAssetDto create(String productId, CreateBuyerRequest buyerOnboard, S
buyerCreated, buyerOnboard.getBuyerId(), buyerOnboard.getTokenExpiredInSeconds());
}

@Transactional(readOnly = true)
public Paging<UnifiedAssetDto> search(
String parentId,
String envId,
String buyerId,
String status,
String orderBy,
PageRequest pageRequest) {
if (parentId != null) {
parentId = unifiedAssetService.findOneByIdOrKey(parentId).getId().toString();
}
List<Tuple2> eqConditions = new ArrayList<>();
eqConditions.add(Tuple2.of("kind", PRODUCT_BUYER.getKind()));
if (StringUtils.isNotBlank(parentId)) {
eqConditions.add(Tuple2.of("parentId", parentId));
}

if (StringUtils.isNotBlank(status)
&& (AssetStatusEnum.ACTIVATED.getKind().equals(status)
|| AssetStatusEnum.DEACTIVATED.getKind().equals(status))) {
eqConditions.add(Tuple2.of("status", status));
}
List<Tuple2> labelConditions = new ArrayList<>();
if (StringUtils.isNotBlank(envId)) {
labelConditions.add(Tuple2.of(LABEL_ENV_ID, envId));
}
if (StringUtils.isNotBlank(buyerId)) {
labelConditions.add(Tuple2.of(LABEL_BUYER_ID, buyerId));
}
if (StringUtils.isNotBlank(orderBy)) {
pageRequest =
PageRequest.of(
pageRequest.getPageNumber(), pageRequest.getPageSize(), Sort.Direction.DESC, orderBy);
}
return unifiedAssetService.findBySpecification(
eqConditions, labelConditions, null, pageRequest, null);
}

@Transactional(readOnly = true)
public Paging<UnifiedAssetDto> search(
String parentId,
Expand Down Expand Up @@ -185,6 +222,7 @@ private UnifiedAsset createBuyer(String buyerId, String envId, String companyNam
unifiedAsset.getMetadata().setDescription(BUYER_DESC);
unifiedAsset.getMetadata().getLabels().put(LABEL_ENV_ID, envId);
unifiedAsset.getMetadata().getLabels().put(LABEL_BUYER_ID, buyerId);

unifiedAsset
.getMetadata()
.getLabels()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.consoleconnect.kraken.operator.core.dto.Tuple2;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum;
import com.consoleconnect.kraken.operator.core.enums.AssetStatusEnum;
import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService;
import com.consoleconnect.kraken.operator.core.toolkit.AssetsConstants;
import com.consoleconnect.kraken.operator.core.toolkit.LabelConstants;
Expand Down Expand Up @@ -74,7 +75,12 @@ void givenBuyer_whenCreate_thenOK() {
void givenBuyer_whenSearch_thenOK() {
webTestClient.requestAndVerify(
HttpMethod.GET,
uriBuilder -> uriBuilder.path(BUYER_BASE_URL).build(),
uriBuilder ->
uriBuilder
.path(BUYER_BASE_URL)
.queryParam("status", AssetStatusEnum.ACTIVATED.getKind())
.queryParam("buyerId", "testing-company")
.build(),
HttpStatus.OK.value(),
null,
bodyStr -> {
Expand All @@ -83,6 +89,25 @@ void givenBuyer_whenSearch_thenOK() {
});
}

@Test
@Order(3)
void givenBuyer_whenSearchInactive_thenReturnEmptyData() {
webTestClient.requestAndVerify(
HttpMethod.GET,
uriBuilder ->
uriBuilder
.path(BUYER_BASE_URL)
.queryParam("status", AssetStatusEnum.DEACTIVATED.getKind())
.build(),
HttpStatus.OK.value(),
null,
bodyStr -> {
log.info(bodyStr);
assertThat(bodyStr, Matchers.notNullValue());
assertThat(bodyStr, hasJsonPath("$.data.data", hasSize(0)));
});
}

@Test
@Order(4)
void givenDuplicatedBuyer_whenCreate_thenNot200() {
Expand Down

0 comments on commit 4370de7

Please sign in to comment.