Skip to content

Commit

Permalink
Added ListUsersRequest (#14943)
Browse files Browse the repository at this point in the history
* Added ListUsersRequest

CHANGELOG_BEGIN
CHANGELOG_END

* Add code change

CHANGELOG_BEGIN
Allow to list all users by providing page token and page size in the Java bindings
CHANGELOG_END

* added test cases to cover the new method
  • Loading branch information
chunlokling-da authored Sep 7, 2022
1 parent 62c64c6 commit a516be7
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public interface UserManagementClient {

Single<ListUsersResponse> listUsers(String accessToken);

Single<ListUsersResponse> listUsers(@NonNull ListUsersRequest request);

Single<ListUsersResponse> listUsers(@NonNull ListUsersRequest request, String accessToken);

Single<GrantUserRightsResponse> grantUserRights(@NonNull GrantUserRightsRequest request);

Single<GrantUserRightsResponse> grantUserRights(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,36 @@ public Single<DeleteUserResponse> deleteUser(
return deleteUser(request, Optional.of(accessToken));
}

private Single<ListUsersResponse> listUsers(@NonNull Optional<String> maybeToken) {
private Single<ListUsersResponse> listUsers(
@NonNull Optional<ListUsersRequest> maybeRequest, @NonNull Optional<String> maybeToken) {
UserManagementServiceOuterClass.ListUsersRequest request =
maybeRequest
.map(ListUsersRequest::toProto)
.orElse(UserManagementServiceOuterClass.ListUsersRequest.getDefaultInstance());
return Single.fromFuture(
StubHelper.authenticating(this.serviceFutureStub, maybeToken)
.listUsers(UserManagementServiceOuterClass.ListUsersRequest.getDefaultInstance()))
StubHelper.authenticating(this.serviceFutureStub, maybeToken).listUsers(request))
.map(ListUsersResponse::fromProto);
}

@Override
public Single<ListUsersResponse> listUsers() {
return listUsers(Optional.empty());
return listUsers(Optional.empty(), Optional.empty());
}

@Override
public Single<ListUsersResponse> listUsers(String accessToken) {
return listUsers(Optional.of(accessToken));
return listUsers(Optional.empty(), Optional.of(accessToken));
}

@Override
public Single<ListUsersResponse> listUsers(@NonNull ListUsersRequest request) {
return listUsers(Optional.of(request), Optional.empty());
}

@Override
public Single<ListUsersResponse> listUsers(
@NonNull ListUsersRequest request, String accessToken) {
return listUsers(Optional.of(request), Optional.of(accessToken));
}

private Single<GrantUserRightsResponse> grantUserRights(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.daml.ledger.rxjava.grpc.helpers.LedgerServices
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import java.util.Optional

class UserManagementClientImplTest extends AnyFlatSpec with Matchers with AuthMatchers {

private val ledgerServices = new LedgerServices("user-management-service-ledger")
Expand All @@ -22,6 +24,8 @@ class UserManagementClientImplTest extends AnyFlatSpec with Matchers with AuthMa
client.getUser(new GetUserRequest("get")).blockingGet()
client.deleteUser(new DeleteUserRequest("delete")).blockingGet()
client.listUsers().blockingGet()
client.listUsers(new ListUsersRequest(Optional.empty(), 10)).blockingGet()
client.listUsers(new ListUsersRequest(Optional.of("page-token"), 20)).blockingGet()
client
.grantUserRights(
new GrantUserRightsRequest(
Expand All @@ -39,7 +43,7 @@ class UserManagementClientImplTest extends AnyFlatSpec with Matchers with AuthMa
client.listUserRights(new ListUserRightsRequest("listRights")).blockingGet()

val requests = service.requests()
requests should have length 7
requests should have length 9
requests should contain theSameElementsAs Array(
proto.CreateUserRequest(
Some(proto.User("createId", "createParty")),
Expand All @@ -48,6 +52,8 @@ class UserManagementClientImplTest extends AnyFlatSpec with Matchers with AuthMa
proto.GetUserRequest("get"),
proto.DeleteUserRequest("delete"),
proto.ListUsersRequest(),
proto.ListUsersRequest(pageSize = 10),
proto.ListUsersRequest("page-token", 20),
proto.GrantUserRightsRequest(
"grant",
Vector(
Expand Down Expand Up @@ -97,6 +103,11 @@ class UserManagementClientImplTest extends AnyFlatSpec with Matchers with AuthMa
client.listUsers().blockingGet()
}
}
expectUnauthenticated {
toAuthenticatedServer { client =>
client.listUsers(new ListUsersRequest(Optional.empty(), 100)).blockingGet()
}
}
}
withClue("grantUserRights") {
expectUnauthenticated {
Expand Down Expand Up @@ -157,6 +168,11 @@ class UserManagementClientImplTest extends AnyFlatSpec with Matchers with AuthMa
client.listUsers(emptyToken).blockingGet()
}
}
expectUnauthenticated {
toAuthenticatedServer { client =>
client.listUsers(new ListUsersRequest(Optional.empty(), 100), emptyToken).blockingGet()
}
}
}
withClue("grantUserRights") {
expectUnauthenticated {
Expand Down Expand Up @@ -212,6 +228,9 @@ class UserManagementClientImplTest extends AnyFlatSpec with Matchers with AuthMa
toAuthenticatedServer { client =>
client.listUsers(adminToken).blockingGet()
}
toAuthenticatedServer { client =>
client.listUsers(new ListUsersRequest(Optional.empty(), 100), adminToken).blockingGet()
}
}
withClue("grantUserRights") {
toAuthenticatedServer { client =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.javaapi.data;

import com.daml.ledger.api.v1.admin.UserManagementServiceOuterClass;
import java.util.Objects;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.NonNull;

public final class ListUsersRequest {

private final Optional<String> pageToken;

private final Integer pageSize;

public ListUsersRequest(@NonNull Optional<String> pageToken, @NonNull Integer pageSize) {
this.pageToken = pageToken;
this.pageSize = pageSize;
}

public Optional<String> getPageToken() {
return pageToken;
}

public Integer getPageSize() {
return pageSize;
}

@Override
public String toString() {
return "ListUsersRequest{" + "pageToken=" + pageToken + ", pageSize=" + pageSize + '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ListUsersRequest that = (ListUsersRequest) o;
return Objects.equals(pageToken, that.pageToken) && Objects.equals(pageSize, that.pageSize);
}

@Override
public int hashCode() {
return Objects.hash(pageToken, pageSize);
}

public UserManagementServiceOuterClass.ListUsersRequest toProto() {
UserManagementServiceOuterClass.ListUsersRequest.Builder builder =
UserManagementServiceOuterClass.ListUsersRequest.newBuilder();
pageToken.ifPresent(builder::setPageToken);
builder.setPageSize(pageSize);
return builder.build();
}
}

0 comments on commit a516be7

Please sign in to comment.