Skip to content

Commit

Permalink
Add pageToken to list fields option for GCS and resourcemanager
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Feb 25, 2016
1 parent b2c2d40 commit 1583e2c
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static ProjectListOption pageSize(int pageSize) {
*/
public static ProjectListOption fields(ProjectField... fields) {
StringBuilder builder = new StringBuilder();
builder.append("projects(").append(ProjectField.selector(fields)).append(")");
builder.append("projects(").append(ProjectField.selector(fields)).append("),nextPageToken");
return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, builder.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,18 @@ private static Map<String, Object> parseListOptions(String query) throws IOExcep
String[] argEntry = arg.split("=");
switch (argEntry[0]) {
case "fields":
// List fields are in the form "projects(field1, field2, ...)"
options.put(
"fields",
argEntry[1].substring("projects(".length(), argEntry[1].length() - 1).split(","));
// List fields are in the form "projects(field1, field2, ...),nextPageToken"
String option = argEntry[1];
int projectStart = option.indexOf("projects(");
int projectEnd = option.indexOf(')');
if (projectStart != -1 && projectEnd != -1
&& projectEnd > projectStart + "projects(".length()) {
String projectFields =
option.substring(projectStart + "projects(".length(), projectEnd);
options.put("projectFields", projectFields.split(","));
option = option.replace(option.substring(projectStart, projectEnd), "");
}
options.put("listFields", option.split(","));
break;
case "filter":
options.put("filter", argEntry[1].split(" "));
Expand Down Expand Up @@ -362,7 +370,7 @@ Response list(Map<String, Object> options) {
if (filters != null && !isValidFilter(filters)) {
return Error.INVALID_ARGUMENT.response("Could not parse the filter.");
}
String[] fields = (String[]) options.get("fields");
String[] projectFields = (String[]) options.get("projectFields");
int count = 0;
String pageToken = (String) options.get("pageToken");
Integer pageSize = (Integer) options.get("pageSize");
Expand All @@ -380,7 +388,7 @@ Response list(Map<String, Object> options) {
if (includeProject) {
count++;
try {
projectsSerialized.add(jsonFactory.toString(extractFields(p, fields)));
projectsSerialized.add(jsonFactory.toString(extractFields(p, projectFields)));
} catch (IOException e) {
return Error.INTERNAL_ERROR.response(
"Error when serializing project " + p.getProjectId());
Expand All @@ -391,7 +399,9 @@ Response list(Map<String, Object> options) {
responseBody.append("{\"projects\": [");
Joiner.on(",").appendTo(responseBody, projectsSerialized);
responseBody.append(']');
if (nextPageToken != null) {
String[] listFields = (String[]) options.get("listFields");
if (nextPageToken != null && (listFields == null
|| ImmutableSet.copyOf(listFields).contains("nextPageToken"))) {
responseBody.append(", \"nextPageToken\": \"");
responseBody.append(nextPageToken);
responseBody.append('"');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ public void testListPaging() {
@Test
public void testListFieldOptions() {
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "projects(projectId,name,labels)");
rpcOptions.put(ResourceManagerRpc.Option.FIELDS,
"projects(projectId,name,labels),nextPageToken");
rpc.create(PROJECT_WITH_PARENT);
Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model.Project>> projects =
rpc.list(rpcOptions);
Expand All @@ -349,6 +350,64 @@ public void testListFieldOptions() {
assertNull(returnedProject.getCreateTime());
}

@Test
public void testListPageTokenFieldOptions() {
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
rpcOptions.put(ResourceManagerRpc.Option.PAGE_SIZE, 1);
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "nextPageToken,projects(projectId,name)");
rpc.create(PARTIAL_PROJECT);
rpc.create(COMPLETE_PROJECT);
Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model.Project>> projects =
rpc.list(rpcOptions);
assertNotNull(projects.x());
Iterator<com.google.api.services.cloudresourcemanager.model.Project> iterator =
projects.y().iterator();
com.google.api.services.cloudresourcemanager.model.Project returnedProject = iterator.next();
assertEquals(COMPLETE_PROJECT.getProjectId(), returnedProject.getProjectId());
assertEquals(COMPLETE_PROJECT.getName(), returnedProject.getName());
assertNull(returnedProject.getLabels());
assertNull(returnedProject.getParent());
assertNull(returnedProject.getProjectNumber());
assertNull(returnedProject.getLifecycleState());
assertNull(returnedProject.getCreateTime());
assertFalse(iterator.hasNext());
rpcOptions.put(ResourceManagerRpc.Option.PAGE_TOKEN, projects.x());
projects = rpc.list(rpcOptions);
iterator = projects.y().iterator();
returnedProject = iterator.next();
assertEquals(PARTIAL_PROJECT.getProjectId(), returnedProject.getProjectId());
assertEquals(PARTIAL_PROJECT.getName(), returnedProject.getName());
assertNull(returnedProject.getLabels());
assertNull(returnedProject.getParent());
assertNull(returnedProject.getProjectNumber());
assertNull(returnedProject.getLifecycleState());
assertNull(returnedProject.getCreateTime());
assertNull(projects.x());
}

@Test
public void testListNoPageTokenFieldOptions() {
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
rpcOptions.put(ResourceManagerRpc.Option.PAGE_SIZE, 1);
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "projects(projectId,name)");
rpc.create(PARTIAL_PROJECT);
rpc.create(COMPLETE_PROJECT);
Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model.Project>> projects =
rpc.list(rpcOptions);
assertNull(projects.x());
Iterator<com.google.api.services.cloudresourcemanager.model.Project> iterator =
projects.y().iterator();
com.google.api.services.cloudresourcemanager.model.Project returnedProject = iterator.next();
assertEquals(COMPLETE_PROJECT.getProjectId(), returnedProject.getProjectId());
assertEquals(COMPLETE_PROJECT.getName(), returnedProject.getName());
assertNull(returnedProject.getLabels());
assertNull(returnedProject.getParent());
assertNull(returnedProject.getProjectNumber());
assertNull(returnedProject.getLifecycleState());
assertNull(returnedProject.getCreateTime());
assertFalse(iterator.hasNext());
}

@Test
public void testListFilterOptions() {
Map<ResourceManagerRpc.Option, Object> rpcFilterOptions = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,38 @@ public void testListFieldOptions() {
assertSame(RESOURCE_MANAGER, returnedProject.resourceManager());
}

@Test
public void testListPagingWithFieldOptions() {
RESOURCE_MANAGER.create(PARTIAL_PROJECT);
RESOURCE_MANAGER.create(COMPLETE_PROJECT);
Page<Project> projects = RESOURCE_MANAGER.list(LIST_FIELDS, ProjectListOption.pageSize(1));
assertNotNull(projects.nextPageCursor());
Iterator<Project> iterator = projects.values().iterator();
Project returnedProject = iterator.next();
assertEquals(COMPLETE_PROJECT.projectId(), returnedProject.projectId());
assertEquals(COMPLETE_PROJECT.name(), returnedProject.name());
assertEquals(COMPLETE_PROJECT.labels(), returnedProject.labels());
assertNull(returnedProject.parent());
assertNull(returnedProject.projectNumber());
assertNull(returnedProject.state());
assertNull(returnedProject.createTimeMillis());
assertSame(RESOURCE_MANAGER, returnedProject.resourceManager());
assertFalse(iterator.hasNext());
projects = projects.nextPage();
iterator = projects.values().iterator();
returnedProject = iterator.next();
assertEquals(PARTIAL_PROJECT.projectId(), returnedProject.projectId());
assertEquals(PARTIAL_PROJECT.name(), returnedProject.name());
assertEquals(PARTIAL_PROJECT.labels(), returnedProject.labels());
assertNull(returnedProject.parent());
assertNull(returnedProject.projectNumber());
assertNull(returnedProject.state());
assertNull(returnedProject.createTimeMillis());
assertSame(RESOURCE_MANAGER, returnedProject.resourceManager());
assertFalse(iterator.hasNext());
assertNull(projects.nextPageCursor());
}

@Test
public void testListFilterOptions() {
ProjectInfo matchingProject = ProjectInfo.builder("matching-project")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public static BucketListOption prefix(String prefix) {
*/
public static BucketListOption fields(BucketField... fields) {
StringBuilder builder = new StringBuilder();
builder.append("items(").append(BucketField.selector(fields)).append(")");
builder.append("items(").append(BucketField.selector(fields)).append("),nextPageToken");
return new BucketListOption(StorageRpc.Option.FIELDS, builder.toString());
}
}
Expand Down Expand Up @@ -708,7 +708,7 @@ public static BlobListOption recursive(boolean recursive) {
*/
public static BlobListOption fields(BlobField... fields) {
StringBuilder builder = new StringBuilder();
builder.append("items(").append(BlobField.selector(fields)).append(")");
builder.append("items(").append(BlobField.selector(fields)).append("),nextPageToken");
return new BlobListOption(StorageRpc.Option.FIELDS, builder.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ public void testListBucketsWithSelectedFields() {
assertTrue(selector.contains("name"));
assertTrue(selector.contains("acl"));
assertTrue(selector.contains("location"));
assertEquals(24, selector.length());
assertTrue(selector.contains("nextPageToken"));
assertEquals(38, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.values(), Bucket.class));
}
Expand All @@ -606,7 +607,8 @@ public void testListBucketsWithEmptyFields() {
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
assertTrue(selector.contains("name"));
assertEquals(11, selector.length());
assertTrue(selector.contains("nextPageToken"));
assertEquals(25, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.values(), Bucket.class));
}
Expand Down Expand Up @@ -678,7 +680,8 @@ public void testListBlobsWithSelectedFields() {
assertTrue(selector.contains("name"));
assertTrue(selector.contains("contentType"));
assertTrue(selector.contains("md5Hash"));
assertEquals(38, selector.length());
assertTrue(selector.contains("nextPageToken"));
assertEquals(52, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class));
}
Expand Down Expand Up @@ -706,7 +709,8 @@ public void testListBlobsWithEmptyFields() {
assertTrue(selector.contains("items"));
assertTrue(selector.contains("bucket"));
assertTrue(selector.contains("name"));
assertEquals(18, selector.length());
assertTrue(selector.contains("nextPageToken"));
assertEquals(32, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class));
}
Expand Down

0 comments on commit 1583e2c

Please sign in to comment.