Updated Branches:
  refs/heads/master 0d270da91 -> 0cec06649

JCLOUDS-224: Allow pagination when retrieving the list of users belonging to an 
Enterprise

Changes suggested in the Pull Request


Project: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/commit/0cec0664
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/tree/0cec0664
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/diff/0cec0664

Branch: refs/heads/master
Commit: 0cec066492d026cce7181c5f2bd676a7a3fda73f
Parents: 0d270da
Author: Carlos Garcia <[email protected]>
Authored: Thu Aug 8 10:36:17 2013 +0200
Committer: Ignasi Barrera <[email protected]>
Committed: Mon Aug 19 12:17:21 2013 +0200

----------------------------------------------------------------------
 .../abiquo/domain/enterprise/Enterprise.java    | 25 +++++-
 .../domain/enterprise/options/UserOptions.java  | 70 +++++++++++++++
 .../jclouds/abiquo/features/EnterpriseApi.java  | 17 ++++
 .../domain/enterprise/UserLiveApiTest.java      | 14 ++-
 .../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(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/0cec0664/abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/Enterprise.java
----------------------------------------------------------------------
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.VirtualAppliance;
 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 class Enterprise extends 
DomainWithLimitsWrapper<EnterpriseDto> {
     *      
"http://community.abiquo.com/display/ABI20/UserResource#UserResource-Retrievealistofusers";
     *      >
     *      http://community.abiquo.com/display/ABI20/UserResource#UserResource-
-    *      Retrievealistofusers </a>
+    *      Retrievealistofusers</a>
     * @return List of users of this enterprise.
     */
    public List<User> 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: <a href=
+    *      
"http://community.abiquo.com/display/ABI20/UserResource#UserResource-Retrievealistofusers";
+    *      >
+    *      http://community.abiquo.com/display/ABI20/UserResource#UserResource-
+    *      Retrievealistofusers</a>
+    * @return List of users of this enterprise, according to the specified 
+    * pagination options
+    */
+   public List<User> listUsers(final UserOptions options) {
+      UsersDto dto = context.getApi().getEnterpriseApi().listUsers(
+            this.unwrap(), options);
       return wrap(context, User.class, dto.getCollection());
    }
 

http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/0cec0664/abiquo/src/main/java/org/jclouds/abiquo/domain/enterprise/options/UserOptions.java
----------------------------------------------------------------------
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<Builder> {
+
+      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;
+      }
+   }
+}

http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/0cec0664/abiquo/src/main/java/org/jclouds/abiquo/features/EnterpriseApi.java
----------------------------------------------------------------------
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.AppendToPath;
 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 @@ public interface EnterpriseApi extends Closeable {
    @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.

http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/0cec0664/abiquo/src/test/java/org/jclouds/abiquo/domain/enterprise/UserLiveApiTest.java
----------------------------------------------------------------------
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 static org.testng.Assert.fail;
 
 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 class UserLiveApiTest extends BaseAbiquoApiLiveApiTest 
{
 
    public void testListUser() {
       Iterable<User> 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 class UserLiveApiTest extends 
BaseAbiquoApiLiveApiTest {
       users = filter(env.enterprise.listUsers(), nick(env.user.getName() + 
"FAIL"));
       assertEquals(size(users), 0);
    }
+   
+   public void testListUserWithOptions() {
+      Iterable<User> 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();

http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/0cec0664/abiquo/src/test/java/org/jclouds/abiquo/features/EnterpriseApiExpectTest.java
----------------------------------------------------------------------
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<EnterpriseApi> {
+
+   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();
+   }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/0cec0664/abiquo/src/test/resources/payloads/usr-list-page-2.xml
----------------------------------------------------------------------
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 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<users>
+       <link rel="first" 
href="http://10.7.3.10:80/api/admin/enterprises/1/users?limit=1"/>
+       <link rel="previous" 
href="http://10.7.3.10:80/api/admin/enterprises/1/users?startwith=0&amp;limit=1"/>
+       <link rel="last" 
href="http://10.7.3.10:80/api/admin/enterprises/1/users?startwith=2&amp;limit=1"/>
+       <totalSize>3</totalSize>
+    <user>
+               <link rel="edit" type="application/vnd.abiquo.user+xml" 
href="http://localhost/api/admin/enterprises/1/users/3"/>
+               <link rel="enterprise" 
type="application/vnd.abiquo.enterprise+xml" 
href="http://localhost/api/admin/enterprises/1"/>
+               <link rel="role" type="application/vnd.abiquo.role+xml" 
href="http://localhost/api/admin/roles/7"/>
+               <link rel="virtualmachines" 
type="application/vnd.abiquo.virtualmachines+xml" 
href="http://localhost/api/admin/enterprises/1/users/3/action/virtualmachines"/>
+               <id>3</id>
+               <nick>ron</nick>
+               <password>0371e4dce3c8804f1543c3f0f309cc10</password>
+               <name>Ron</name>
+               <surname>Weasley</surname>
+               <description/>
+               <email>[email protected]</email>
+               <locale>en_EN</locale>
+               <authType>ABIQUO</authType>
+               <active>true</active>
+    </user>
+</users>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/0cec0664/abiquo/src/test/resources/payloads/usr-list.xml
----------------------------------------------------------------------
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 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<users>
+       <link rel="first" 
href="http://localhost/api/admin/enterprises/1/users?limit=2"/>
+       <link rel="last" 
href="http://localhost/api/admin/enterprises/1/users?startwith=0&amp;limit=2"/>
+       <totalSize>3</totalSize>
+    <user>
+               <link rel="edit" type="application/vnd.abiquo.user+xml" 
href="http://localhost/api/admin/enterprises/1/users/1"/>
+               <link rel="enterprise" 
type="application/vnd.abiquo.enterprise+xml" 
href="http://localhost/api/admin/enterprises/1"/>
+               <link rel="role" type="application/vnd.abiquo.role+xml" 
href="http://localhost/api/admin/roles/7"/>
+               <link rel="virtualmachines" 
type="application/vnd.abiquo.virtualmachines+xml" 
href="http://localhost/api/admin/enterprises/1/users/1/action/virtualmachines"/>
+               <id>1</id>
+               <nick>potter</nick>
+               <password>0371e4dce3c8804f1543c3f0f309cc10</password>
+               <name>Harry</name>
+               <surname>Potter</surname>
+               <description/>
+               <email>[email protected]</email>
+               <locale>en_EN</locale>
+               <authType>ABIQUO</authType>
+               <active>true</active>
+    </user>
+    <user>
+               <link rel="edit" type="application/vnd.abiquo.user+xml" 
href="http://localhost/api/admin/enterprises/1/users/2"/>
+               <link rel="enterprise" 
type="application/vnd.abiquo.enterprise+xml" 
href="http://localhost/api/admin/enterprises/1"/>
+               <link rel="role" type="application/vnd.abiquo.role+xml" 
href="http://localhost/api/admin/roles/7"/>
+               <link rel="virtualmachines" 
type="application/vnd.abiquo.virtualmachines+xml" 
href="http://localhost/api/admin/enterprises/1/users/2/action/virtualmachines"/>
+               <id>2</id>
+               <nick>granger</nick>
+               <password>0371e4dce3c8804f1543c3f0f309cc10</password>
+               <name>Hermione</name>
+               <surname>Granger</surname>
+               <description/>
+               <email>[email protected]</email>
+               <locale>en_EN</locale>
+               <authType>ABIQUO</authType>
+               <active>true</active>
+    </user>
+    <user>
+               <link rel="edit" type="application/vnd.abiquo.user+xml" 
href="http://localhost/api/admin/enterprises/1/users/3"/>
+               <link rel="enterprise" 
type="application/vnd.abiquo.enterprise+xml" 
href="http://localhost/api/admin/enterprises/1"/>
+               <link rel="role" type="application/vnd.abiquo.role+xml" 
href="http://localhost/api/admin/roles/7"/>
+               <link rel="virtualmachines" 
type="application/vnd.abiquo.virtualmachines+xml" 
href="http://localhost/api/admin/enterprises/1/users/3/action/virtualmachines"/>
+               <id>3</id>
+               <nick>ron</nick>
+               <password>0371e4dce3c8804f1543c3f0f309cc10</password>
+               <name>Ron</name>
+               <surname>Weasley</surname>
+               <description/>
+               <email>[email protected]</email>
+               <locale>en_EN</locale>
+               <authType>ABIQUO</authType>
+               <active>true</active>
+    </user>
+</users>
\ No newline at end of file

Reply via email to