Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/box/sdk/BoxUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.eclipsesource.json.JsonArray;
Expand Down Expand Up @@ -112,6 +113,41 @@ public static BoxUser getCurrentUser(BoxAPIConnection api) {
return new BoxUser(api, jsonObject.get("id").asString());
}

/**
* Returns an iterable containing all the enterprise users.
* @param api the API connection to be used when retrieving the users.
* @return an iterable containing all the enterprise users.
*/
public static Iterable<BoxUser.Info> getAllEnterpriseUsers(final BoxAPIConnection api) {
return getAllEnterpriseUsers(api, null);
}

/**
* Returns an iterable containing all the enterprise users that matches the filter and specifies which child fields
* to retrieve from the API.
* @param api the API connection to be used when retrieving the users.
* @param filterTerm used to filter the results to only users starting with this string in either the name or the
* login. Can be null to not filter the results.
* @param fields the fields to retrieve. Leave this out for the standard fields.
* @return an iterable containing all the enterprise users that matches the filter.
*/
public static Iterable<BoxUser.Info> getAllEnterpriseUsers(final BoxAPIConnection api, final String filterTerm,
final String... fields) {
return new Iterable<BoxUser.Info>() {
public Iterator<BoxUser.Info> iterator() {
QueryStringBuilder builder = new QueryStringBuilder();
if (filterTerm != null) {
builder.appendParam("filter_term", filterTerm);
}
if (fields.length > 0) {
builder.appendParam("fields", fields);
}
URL url = USERS_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString());
return new BoxUserIterator(api, url);
}
};
}

/**
* Gets information about this user.
* @param fields the optional fields to retrieve.
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/box/sdk/BoxUserIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.box.sdk;

import java.net.URL;
import java.util.Iterator;

import com.eclipsesource.json.JsonObject;

class BoxUserIterator implements Iterator<BoxUser.Info> {
private static final long LIMIT = 1000;

private final BoxAPIConnection api;
private final JSONIterator jsonIterator;

BoxUserIterator(BoxAPIConnection api, URL url) {
this.api = api;
this.jsonIterator = new JSONIterator(api, url, LIMIT);
}

public boolean hasNext() {
return this.jsonIterator.hasNext();
}

public BoxUser.Info next() {
JsonObject nextJSONObject = this.jsonIterator.next();
String id = nextJSONObject.get("id").asString();

BoxUser user = new BoxUser(this.api, id);
return user.new Info(nextJSONObject);
}

public void remove() {
throw new UnsupportedOperationException();
}
}
39 changes: 39 additions & 0 deletions src/test/java/com/box/sdk/BoxUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,60 @@
import java.text.ParseException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.common.collect.Lists;

public class BoxUserTest {
@Rule
public final WireMockRule wireMockRule = new WireMockRule(8080);

@Test
@Category(UnitTest.class)
public void getAllEnterpriseUsersRequestsCorrectFilterAndFields() {
final String filterTerm = "login";
final String name = "enterprise user";
BoxAPIConnection api = new BoxAPIConnection("");
api.setBaseURL("http://localhost:8080/");

stubFor(get(urlPathEqualTo("/users"))
.withQueryParam("offset", WireMock.equalTo("0"))
.withQueryParam("limit", WireMock.equalTo("1000"))
.withQueryParam("filter_term", WireMock.equalTo(filterTerm))
.withQueryParam("fields", containing("name"))
.withQueryParam("fields", containing("role"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\"total_count\": 1, \"offset\": 0, \"entries\":"
+ "[{\"type\": \"user\", \"id\": \"0\", \"name\": \"" + name + "\", \"role\": \"user\"}]}")));

Iterable<BoxUser.Info> users = BoxUser.getAllEnterpriseUsers(api, filterTerm, "name", "role");
List<BoxUser.Info> usersList = Lists.newArrayList(users);
assertThat(usersList.size(), is(1));
assertThat(usersList.get(0).getName(), is(equalTo(name)));
}

@Test
@Category(UnitTest.class)
Expand Down