From d440d7a380a33ba10aa5bb0e6d6dfbb394481729 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Thu, 8 Aug 2013 10:36:17 +0200 Subject: [PATCH] JCLOUDS-224: Allow pagination when retrieving the list of users belonging to an Enterprise Changes suggested in the Pull Request --- .../abiquo/domain/enterprise/Enterprise.java | 25 +++++- .../domain/enterprise/options/UserOptions.java | 70 ++++++++++++++++ .../org/jclouds/abiquo/features/EnterpriseApi.java | 17 ++++ .../abiquo/domain/enterprise/UserLiveApiTest.java | 14 +++- .../abiquo/features/EnterpriseApiExpectTest.java | 94 ++++++++++++++++++++++ .../test/resources/payloads/usr-list-page-2.xml | 23 ++++++ abiquo/src/test/resources/payloads/usr-list.xml | 54 +++++++++++++ 7 files changed, 293 insertions(+), 4 deletions(-) create mode 100644 abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/options/UserOptions.java create mode 100644 abiquo/src/test/java/org/jclouds/abiquo/features/EnterpriseApiExpectTest.java create mode 100644 abiquo/src/test/resources/payloads/usr-list-page-2.xml create mode 100644 abiquo/src/test/resources/payloads/usr-list.xml diff --git a/abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/Enterprise.java b/abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/Enterprise.java index c1fb3c5..c3e1c12 100644 --- a/abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/Enterprise.java +++ b/abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/Enterprise.java @@ -25,6 +25,7 @@ import org.jclouds.abiquo.domain.cloud.VirtualDatacenter; import org.jclouds.abiquo.domain.cloud.VirtualMachine; import org.jclouds.abiquo.domain.cloud.VirtualMachineTemplate; +import org.jclouds.abiquo.domain.enterprise.options.UserOptions; import org.jclouds.abiquo.domain.exception.AbiquoException; import org.jclouds.abiquo.domain.infrastructure.Datacenter; import org.jclouds.abiquo.domain.infrastructure.Machine; @@ -206,11 +207,31 @@ public EnterpriseProperties getEnterpriseProperties() { * "http://community.abiquo.com/display/ABI20/UserResource#UserResource-Retrievealistofusers" * > * http://community.abiquo.com/display/ABI20/UserResource#UserResource- - * Retrievealistofusers + * Retrievealistofusers * @return List of users of this enterprise. */ public List listUsers() { - UsersDto dto = context.getApi().getEnterpriseApi().listUsers(this.unwrap()); + // Delegate the retrieval of users to the options-enabled version of the + // method, with pagination disabled + return listUsers(UserOptions.builder().disablePagination().build()); + } + + /** + * Retrieve the list of users of this enterprise, allowing pagination + * + * @param options + * User options + * @see API: + * http://community.abiquo.com/display/ABI20/UserResource#UserResource- + * Retrievealistofusers + * @return List of users of this enterprise, according to the specified + * pagination options + */ + public List listUsers(final UserOptions options) { + UsersDto dto = context.getApi().getEnterpriseApi().listUsers( + this.unwrap(), options); return wrap(context, User.class, dto.getCollection()); } diff --git a/abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/options/UserOptions.java b/abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/options/UserOptions.java new file mode 100644 index 0000000..3848bca --- /dev/null +++ b/abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/options/UserOptions.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.abiquo.domain.enterprise.options; + +import org.jclouds.abiquo.domain.options.FilterOptions.BaseFilterOptionsBuilder; +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Available options to query users. + * + * @author Carlos Garcia + */ +public class UserOptions extends BaseHttpRequestOptions implements Cloneable { + public static Builder builder() { + return new Builder(); + } + + @Override + protected Object clone() throws CloneNotSupportedException { + UserOptions options = new UserOptions(); + options.queryParameters.putAll(queryParameters); + return options; + } + + public static class Builder extends BaseFilterOptionsBuilder { + + private final UserOptions options = new UserOptions(); + + public Builder page(int page) { + this.options.queryParameters.put("page", String.valueOf(page)); + return this; + } + + @Override + public Builder limit(int limit) { + this.options.queryParameters.put("numResults", String.valueOf(limit)); + return this; + } + + public UserOptions build() { + return addFilterOptions(this.options); + } + + // FIXME: This method is overriden to change its behavior until the + // issue ABICLOUDPREMIUM-5927 in Abiquo isissue is solved ( + // (http://jira.abiquo.com/browse/ABICLOUDPREMIUM-5927) + // Once its fixed, this method should be deleted + @Override + public Builder disablePagination() { + // Unlike in its parent method, disabling pagination means to set + // the limit attribute to the biggest value possible + this.limit = Integer.MAX_VALUE; + return (Builder) this; + } + } +} diff --git a/abiquo/src/main/java/org/jclouds/abiquo/features/EnterpriseApi.java b/abiquo/src/main/java/org/jclouds/abiquo/features/EnterpriseApi.java index 622d524..668e288 100644 --- a/abiquo/src/main/java/org/jclouds/abiquo/features/EnterpriseApi.java +++ b/abiquo/src/main/java/org/jclouds/abiquo/features/EnterpriseApi.java @@ -34,6 +34,7 @@ import org.jclouds.abiquo.binders.BindToPath; import org.jclouds.abiquo.binders.BindToXMLPayloadAndPath; import org.jclouds.abiquo.domain.enterprise.options.EnterpriseOptions; +import org.jclouds.abiquo.domain.enterprise.options.UserOptions; import org.jclouds.abiquo.functions.infrastructure.ParseDatacenterId; import org.jclouds.abiquo.http.filters.AbiquoAuthentication; import org.jclouds.abiquo.http.filters.AppendApiVersionToMediaType; @@ -328,6 +329,22 @@ DatacenterLimitsDto updateLimits( @Consumes(UsersDto.BASE_MEDIA_TYPE) @JAXBResponseParser UsersDto listUsers(@EndpointLink("users") @BinderParam(BindToPath.class) EnterpriseDto enterprise); + + /** + * List filtered users by enterprise. + * + * @param enterprise + * The given enterprise. + * @param options + * Filtering options. + * @return The list of Users. + */ + @Named("user:list") + @GET + @Consumes(UsersDto.BASE_MEDIA_TYPE) + @JAXBResponseParser + UsersDto listUsers(@EndpointLink("users") @BinderParam(BindToPath.class) EnterpriseDto enterprise, + UserOptions options); /** * Create a new user in the given enterprise. diff --git a/abiquo/src/test/java/org/jclouds/abiquo/domain/enterprise/UserLiveApiTest.java b/abiquo/src/test/java/org/jclouds/abiquo/domain/enterprise/UserLiveApiTest.java index 4250431..b27cb45 100644 --- a/abiquo/src/test/java/org/jclouds/abiquo/domain/enterprise/UserLiveApiTest.java +++ b/abiquo/src/test/java/org/jclouds/abiquo/domain/enterprise/UserLiveApiTest.java @@ -26,13 +26,13 @@ import javax.ws.rs.core.Response.Status; +import org.jclouds.abiquo.domain.enterprise.options.UserOptions; import org.jclouds.abiquo.domain.exception.AbiquoException; import org.jclouds.abiquo.internal.BaseAbiquoApiLiveApiTest; import org.testng.annotations.Test; import com.abiquo.server.core.enterprise.UserDto; import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; /** * Live integration tests for the {@link User} domain class. @@ -82,7 +82,7 @@ public void testChangeRoleAndUpdate() { public void testListUser() { Iterable users = env.enterprise.listUsers(); - assertEquals(Iterables.size(users), 2); + assertEquals(size(users), 2); users = filter(env.enterprise.listUsers(), nick(env.user.getNick())); assertEquals(size(users), 1); @@ -90,6 +90,16 @@ public void testListUser() { users = filter(env.enterprise.listUsers(), nick(env.user.getName() + "FAIL")); assertEquals(size(users), 0); } + + public void testListUserWithOptions() { + Iterable users = env.enterprise.listUsers(UserOptions.builder() + .limit(1).startWith(0).build()); + assertEquals(size(users), 1); + + users = env.enterprise.listUsers(UserOptions.builder() + .limit(1).page(2).build()); + assertEquals(size(users), 1); + } public void testGetCurrentUser() { User user = env.context.getAdministrationService().getCurrentUser(); diff --git a/abiquo/src/test/java/org/jclouds/abiquo/features/EnterpriseApiExpectTest.java b/abiquo/src/test/java/org/jclouds/abiquo/features/EnterpriseApiExpectTest.java new file mode 100644 index 0000000..cd05564 --- /dev/null +++ b/abiquo/src/test/java/org/jclouds/abiquo/features/EnterpriseApiExpectTest.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.abiquo.features; + +import static org.testng.Assert.assertEquals; + +import java.net.URI; + +import org.jclouds.abiquo.AbiquoApi; +import org.jclouds.abiquo.domain.enterprise.options.UserOptions; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpRequest.Builder; +import org.jclouds.http.HttpResponse; +import org.testng.annotations.Test; + +import com.abiquo.model.rest.RESTLink; +import com.abiquo.server.core.enterprise.EnterpriseDto; +import com.abiquo.server.core.enterprise.UsersDto; + +/** + * Expect tests for the {@link EnterpriseApi} class. + * + * @author Carlos Garcia + */ +@Test(groups = "unit", testName = "EnterpriseApiExpectTest") +public class EnterpriseApiExpectTest extends BaseAbiquoApiExpectTest { + + private EnterpriseApi buildMockEnterpriseApi(String payloadFile, Builder requestBuilder) { + return requestSendsResponse(requestBuilder.build(), + HttpResponse.builder().statusCode(200).payload( + payloadFromResourceWithContentType(payloadFile, + normalize(UsersDto.MEDIA_TYPE))) // + .build()); + } + + public Builder getRequestBuilder() { + return HttpRequest.builder() // + .method("GET") + .endpoint(URI.create("http://localhost/api/admin/enterprises/1/users")) + .addHeader("Cookie", tokenAuth) + .addHeader("Accept", normalize(UsersDto.MEDIA_TYPE)); + } + + public void testListUsersWithoutPagination() { + EnterpriseApi api = buildMockEnterpriseApi("/payloads/usr-list.xml", + getRequestBuilder()); + + EnterpriseDto enterprise = new EnterpriseDto(); + enterprise.addLink(new RESTLink("users", + "http://localhost/api/admin/enterprises/1/users")); + + UsersDto users = api.listUsers(enterprise); + assertEquals(users.getCollection().size(), 3); + assertEquals(users.getCollection().get(0).getNick(), "potter"); + assertEquals(users.getCollection().get(1).getNick(), "granger"); + assertEquals(users.getCollection().get(2).getNick(), "ron"); + } + + public void testListUsersWithPagination() { + Builder builder = getRequestBuilder(); + builder.addQueryParam("numResults", "2"); + builder.addQueryParam("page", "2"); + EnterpriseApi api = buildMockEnterpriseApi("/payloads/usr-list-page-2.xml", builder); + + EnterpriseDto enterprise = new EnterpriseDto(); + enterprise.addLink(new RESTLink("users", + "http://localhost/api/admin/enterprises/1/users")); + + UsersDto users = api.listUsers(enterprise, + UserOptions.builder().limit(2).page(2).build()); + assertEquals(users.getCollection().size(), 1); + assertEquals(users.getCollection().get(0).getNick(), "ron"); + } + + @Override + protected EnterpriseApi clientFrom(AbiquoApi api) { + return api.getEnterpriseApi(); + } + +} diff --git a/abiquo/src/test/resources/payloads/usr-list-page-2.xml b/abiquo/src/test/resources/payloads/usr-list-page-2.xml new file mode 100644 index 0000000..9f3012e --- /dev/null +++ b/abiquo/src/test/resources/payloads/usr-list-page-2.xml @@ -0,0 +1,23 @@ + + + + + + 3 + + + + + + 3 + ron + 0371e4dce3c8804f1543c3f0f309cc10 + Ron + Weasley + + ron.weasley@hogwarts.edu + en_EN + ABIQUO + true + + \ No newline at end of file diff --git a/abiquo/src/test/resources/payloads/usr-list.xml b/abiquo/src/test/resources/payloads/usr-list.xml new file mode 100644 index 0000000..0468815 --- /dev/null +++ b/abiquo/src/test/resources/payloads/usr-list.xml @@ -0,0 +1,54 @@ + + + + + 3 + + + + + + 1 + potter + 0371e4dce3c8804f1543c3f0f309cc10 + Harry + Potter + + harry.potter@hogwarts.edu + en_EN + ABIQUO + true + + + + + + + 2 + granger + 0371e4dce3c8804f1543c3f0f309cc10 + Hermione + Granger + + hermione.granger@hogwarts.edu + en_EN + ABIQUO + true + + + + + + + 3 + ron + 0371e4dce3c8804f1543c3f0f309cc10 + Ron + Weasley + + ron.weasley@hogwarts.edu + en_EN + ABIQUO + true + + \ No newline at end of file -- 1.8.1.6