Skip to content

Commit

Permalink
Fixed getUser
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Aug 8, 2023
1 parent 8b972d4 commit b36d569
Show file tree
Hide file tree
Showing 7 changed files with 444 additions and 34 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/vonage/client/users/BaseUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class BaseUser implements Jsonable {
BaseUser() {
}

void setId(String id) {
this.id = id;
}

/**
* Unique user ID.
*
Expand Down
68 changes: 64 additions & 4 deletions src/main/java/com/vonage/client/users/ListUsersRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.vonage.client.users;

import com.fasterxml.jackson.annotation.JsonValue;
import com.vonage.client.QueryParamsRequest;
import com.vonage.client.common.HalLinks;
import java.net.URI;
Expand All @@ -25,12 +26,12 @@
* Query parameters for {@link UsersClient#listUsers(ListUsersRequest)}.
*/
public final class ListUsersRequest implements QueryParamsRequest {
private final int pageSize;
private final Integer pageSize;
private final SortOrder order;
private final String name, cursor;

private ListUsersRequest(Builder builder) {
if ((pageSize = builder.pageSize) < 1 || pageSize > 100) {
if ((pageSize = builder.pageSize) != null && (pageSize < 1 || pageSize > 100)) {
throw new IllegalArgumentException("Page size must be between 1 and 100.");
}
order = builder.order;
Expand All @@ -47,7 +48,9 @@ static String parseCursor(URI cursor) {
@Override
public Map<String, String> makeParams() {
LinkedHashMap<String, String> params = new LinkedHashMap<>(4);
params.put("page_size", String.valueOf(pageSize));
if (pageSize != null) {
params.put("page_size", pageSize.toString());
}
if (order != null) {
params.put("order", order.toString());
}
Expand All @@ -60,6 +63,42 @@ public Map<String, String> makeParams() {
return params;
}

/**
*
*
* @return
*/
public Integer getPageSize() {
return pageSize;
}

/**
*
*
* @return
*/
public SortOrder getOrder() {
return order;
}

/**
*
*
* @return
*/
public String getName() {
return name;
}

/**
*
*
* @return
*/
public String getCursor() {
return cursor;
}

/**
* Entry point for constructing an instance of this class.
*
Expand All @@ -70,7 +109,7 @@ public static Builder builder() {
}

public static class Builder {
private int pageSize = 10;
private Integer pageSize;
private SortOrder order;
private String name;
private URI cursor;
Expand Down Expand Up @@ -141,4 +180,25 @@ public ListUsersRequest build() {
return new ListUsersRequest(this);
}
}

/**
* Represents the sort order for events.
*/
public enum SortOrder {
/**
* Ascending
*/
ASC,

/**
* Descending
*/
DESC;

@JsonValue
@Override
public String toString() {
return name().toLowerCase();
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/vonage/client/users/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ public Channels getChannels() {
return channels;
}


public static User fromJson(String json) {
User user = new User();
user.updateFromJson(json);
return user;
}

/**
* Entry point for creating an instance of this class.
*
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/vonage/client/users/UsersClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,22 @@ public User createUser(User user) throws UsersResponseException {
/**
* Update an existing user.
*
* @param user The application properties for the user to be updated with.
* @param userId Unique ID of the user to update.
* @param user The properties for the user to be updated with.
*
* @return The user which has been updated.
*
* @throws UsersResponseException If there was an error processing the request.
*/
public User updateUser(User user) throws UsersResponseException {
return updateUser.execute(validateUser(user));
public User updateUser(String userId, User user) throws UsersResponseException {
validateUser(user).setId(validateUserId(userId));
return updateUser.execute(user);
}

/**
* Retrieve a user.
*
* @param userId The unique ID of the user to retrieve.
* @param userId Unique ID of the user to retrieve.
*
* @return The corresponding user.
*
Expand All @@ -119,7 +121,7 @@ public User getUser(String userId) throws UsersResponseException {
/**
* Delete a user.
*
* @param userId Unique ID of the user to delete as a string.
* @param userId Unique ID of the user to delete.
*
* @throws UsersResponseException If there was an error processing the request.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ public void testGetApplication() throws Exception {
public void testDeleteApplication() throws Exception {
String request = SAMPLE_APPLICATION_ID.toString();
stubResponseAndRun(204, () -> client.deleteApplication(request));
assertThrows(NullPointerException.class, () -> client.getApplication(null));
assertThrows(IllegalArgumentException.class, () -> client.getApplication("abc123"));
assertThrows(NullPointerException.class, () -> client.deleteApplication(null));
assertThrows(IllegalArgumentException.class, () -> client.deleteApplication("abc123"));
assert400ResponseException(() -> client.deleteApplication(request));
}

@Test
public void testListApplicationWithOneResult() throws Exception {
public void testListApplicationsWithOneResult() throws Exception {
stubResponse(200, "{\n" +
" \"page_size\": 10,\n" +
" \"page\": 5,\n" +
Expand Down Expand Up @@ -251,7 +251,7 @@ public void testListApplicationWithOneResult() throws Exception {
}

@Test
public void testListApplicationWithMultipleResults() throws Exception {
public void testListApplicationsWithMultipleResults() throws Exception {
String json = "{\n" +
" \"page_size\": 10,\n" +
" \"page\": 1,\n" +
Expand Down Expand Up @@ -318,7 +318,7 @@ public void testListApplicationWithMultipleResults() throws Exception {
}

@Test
public void testListApplicationWithNoResults() throws Exception {
public void testListApplicationsWithNoResults() throws Exception {
String json = "{\"page\":1,\"_embedded\":{\"applications\":[]}}";
ApplicationList hal = stubResponseAndGet(json, () ->
client.listApplications(ListApplicationRequest.builder().page(1).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,6 @@
*/
package com.vonage.client.users;

import com.fasterxml.jackson.annotation.JsonValue;
public class UserTest {

/**
* Represents the sort order for events.
*/
public enum SortOrder {
/**
* Ascending
*/
ASC,

/**
* Descending
*/
DESC;

@JsonValue
@Override
public String toString() {
return name().toLowerCase();
}
}
Loading

0 comments on commit b36d569

Please sign in to comment.