From d2985b5c4bbc175b28a7993c8aa370981dc7c2d6 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Tue, 21 Jul 2009 12:40:23 +0000 Subject: [PATCH] Issue 77: shared ip group support git-svn-id: http://jclouds.googlecode.com/svn/trunk@1823 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../cloudservers/CloudServersConnection.java | 80 +++++- .../cloudservers/domain/SharedIpGroup.java | 83 ++++-- .../ParseSharedIpGroupFromGsonResponse.java | 61 ++++ ...arseSharedIpGroupListFromGsonResponse.java | 63 +++++ .../ReturnSharedIpGroupNotFoundOn404.java | 48 ++++ .../options/CreateSharedIpGroupOptions.java | 68 +++++ .../CloudServersConnectionLiveTest.java | 153 ++++++++--- .../CloudServersConnectionTest.java | 260 ++++++++++++++---- ...arseSharedIpGroupFromGsonResponseTest.java | 62 +++++ ...SharedIpGroupListFromGsonResponseTest.java | 77 ++++++ .../CreateSharedIpGroupOptionsTest.java | 83 ++++++ .../test_get_sharedipgroup_details.json | 7 + .../resources/test_list_sharedipgroups.json | 12 + .../test_list_sharedipgroups_detail.json | 14 + 14 files changed, 949 insertions(+), 122 deletions(-) create mode 100644 rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupFromGsonResponse.java create mode 100644 rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupListFromGsonResponse.java create mode 100644 rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ReturnSharedIpGroupNotFoundOn404.java create mode 100644 rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/options/CreateSharedIpGroupOptions.java create mode 100644 rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupFromGsonResponseTest.java create mode 100644 rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupListFromGsonResponseTest.java create mode 100644 rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/options/CreateSharedIpGroupOptionsTest.java create mode 100644 rackspace/cloudservers/core/src/test/resources/test_get_sharedipgroup_details.json create mode 100644 rackspace/cloudservers/core/src/test/resources/test_list_sharedipgroups.json create mode 100644 rackspace/cloudservers/core/src/test/resources/test_list_sharedipgroups_detail.json diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/CloudServersConnection.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/CloudServersConnection.java index f381f8e00b..f309456207 100755 --- a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/CloudServersConnection.java +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/CloudServersConnection.java @@ -40,16 +40,21 @@ import org.jclouds.rackspace.cloudservers.binders.ChangeServerNameBinder; import org.jclouds.rackspace.cloudservers.domain.Flavor; import org.jclouds.rackspace.cloudservers.domain.Image; import org.jclouds.rackspace.cloudservers.domain.Server; +import org.jclouds.rackspace.cloudservers.domain.SharedIpGroup; import org.jclouds.rackspace.cloudservers.functions.ParseFlavorFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseFlavorListFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseImageFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseImageListFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseServerFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseServerListFromGsonResponse; +import org.jclouds.rackspace.cloudservers.functions.ParseSharedIpGroupFromGsonResponse; +import org.jclouds.rackspace.cloudservers.functions.ParseSharedIpGroupListFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ReturnFlavorNotFoundOn404; import org.jclouds.rackspace.cloudservers.functions.ReturnImageNotFoundOn404; import org.jclouds.rackspace.cloudservers.functions.ReturnServerNotFoundOn404; +import org.jclouds.rackspace.cloudservers.functions.ReturnSharedIpGroupNotFoundOn404; import org.jclouds.rackspace.cloudservers.options.CreateServerOptions; +import org.jclouds.rackspace.cloudservers.options.CreateSharedIpGroupOptions; import org.jclouds.rackspace.cloudservers.options.ListOptions; import org.jclouds.rackspace.filters.AuthenticateRequest; import org.jclouds.rest.EntityParam; @@ -210,6 +215,22 @@ public interface CloudServersConnection { // (400) List listFlavors(ListOptions... options); + /** + * + * This operation returns details of the specified flavor. + * + * @return {@link Flavor#NOT_FOUND} if the flavor is not found + * @see Flavor + */ + @GET + @ResponseParser(ParseFlavorFromGsonResponse.class) + @Query(key = "format", value = "json") + @ExceptionParser(ReturnFlavorNotFoundOn404.class) + @Path("/flavors/{id}") + // TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest + // (400) + Flavor getFlavor(@PathParam("id") int id); + /** * * List available images (IDs and names only) @@ -243,18 +264,63 @@ public interface CloudServersConnection { /** * - * This operation returns details of the specified flavor. + * List shared IP groups (IDs and names only) * - * @return {@link Flavor#NOT_FOUND} if the flavor is not found - * @see Flavor + * in order to retrieve all details, pass the option {@link ListOptions#withDetails() + * withDetails()} */ @GET - @ResponseParser(ParseFlavorFromGsonResponse.class) + @ResponseParser(ParseSharedIpGroupListFromGsonResponse.class) @Query(key = "format", value = "json") - @ExceptionParser(ReturnFlavorNotFoundOn404.class) - @Path("/flavors/{id}") + @Path("/shared_ip_groups") // TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest // (400) - Flavor getFlavor(@PathParam("id") int id); + List listSharedIpGroups(ListOptions... options); + /** + * + * This operation returns details of the specified shared IP group. + * + * @return {@link SharedIpGroup#NOT_FOUND} if the shared IP group is not found + * @see SharedIpGroup + */ + @GET + @ResponseParser(ParseSharedIpGroupFromGsonResponse.class) + @Query(key = "format", value = "json") + @ExceptionParser(ReturnSharedIpGroupNotFoundOn404.class) + @Path("/shared_ip_groups/{id}") + // TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest + // (400) + SharedIpGroup getSharedIpGroup(@PathParam("id") int id); + + /** + * This operation creates a new shared IP group. Please note, all responses to requests for + * shared_ip_groups return an array of servers. However, on a create request, the shared IP group + * can be created empty or can be initially populated with a single server. Use + * {@link CreateSharedIpGroupOptions} to specify an server. + */ + @POST + @ResponseParser(ParseSharedIpGroupFromGsonResponse.class) + @Query(key = "format", value = "json") + @Path("/shared_ip_groups") + @PostBinder(CreateSharedIpGroupOptions.class) + // TODO: cloudSharedIpGroupsFault (400, 500), serviceUnavailable (503), unauthorized (401), + // badRequest (400), badMediaType(415), overLimit (413) + SharedIpGroup createSharedIpGroup(@PostParam("name") String name, + CreateSharedIpGroupOptions... options); + + /** + * This operation deletes the specified shared IP group. This operation will ONLY succeed if 1) + * there are no active servers in the group (i.e. they have all been terminated) or 2) no servers + * in the group are actively sharing IPs. + * + * @return false if the shared ip group is not found + * @see SharedIpGroup + */ + @DELETE + @ExceptionParser(ReturnFalseOn404.class) + @Path("/shared_ip_groups/{id}") + // TODO:cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest + // (400) + boolean deleteSharedIpGroup(@PathParam("id") int id); } diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/domain/SharedIpGroup.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/domain/SharedIpGroup.java index 9874015568..371f38e774 100644 --- a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/domain/SharedIpGroup.java +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/domain/SharedIpGroup.java @@ -27,34 +27,36 @@ import java.util.List; import com.google.inject.internal.Lists; +/** + * A shared IP group is a collection of servers that can share IPs with other members of the group. + * Any server in a group can share one or more public IPs with any other server in the group. With + * the exception of the first server in a shared IP group, servers must be launched into shared IP + * groups. A server may only be a member of one shared IP group. + * + * @author Adrian Cole + */ public class SharedIpGroup { - private int server; - private List servers = Lists.newArrayList(); - private Integer id; + public static final SharedIpGroup NOT_FOUND = new SharedIpGroup(-1, "NOT_FOUND"); + + private int id; private String name; - public void setServer(int server) { - this.server = server; + private List servers = Lists.newArrayList(); + + public SharedIpGroup() { } - public int getServer() { - return server; + public SharedIpGroup(int id, String name) { + this.id = id; + this.name = name; } - public void setServers(List servers) { - this.servers = servers; - } - - public List getServers() { - return servers; - } - - public void setId(Integer id) { + public void setId(int id) { this.id = id; } - public Integer getId() { + public int getId() { return id; } @@ -66,4 +68,51 @@ public class SharedIpGroup { return name; } + public void setServers(List servers) { + this.servers = servers; + } + + public List getServers() { + return servers; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + id; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((servers == null) ? 0 : servers.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SharedIpGroup other = (SharedIpGroup) obj; + if (id != other.id) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (servers == null) { + if (other.servers != null) + return false; + } else if (!servers.equals(other.servers)) + return false; + return true; + } + + @Override + public String toString() { + return "SharedIpGroup [id=" + id + ", name=" + name + ", servers=" + servers + "]"; + } + } diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupFromGsonResponse.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupFromGsonResponse.java new file mode 100644 index 0000000000..98b026656d --- /dev/null +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupFromGsonResponse.java @@ -0,0 +1,61 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * 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.rackspace.cloudservers.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; + +import org.jclouds.http.functions.ParseJson; +import org.jclouds.rackspace.cloudservers.domain.SharedIpGroup; + +import com.google.gson.Gson; +import com.google.inject.Inject; + +/** + * This parses {@link SharedIpGroup} from a gson string. + * + * @author Adrian Cole + */ +public class ParseSharedIpGroupFromGsonResponse extends ParseJson { + + @Inject + public ParseSharedIpGroupFromGsonResponse(Gson gson) { + super(gson); + } + + private static class SharedIpGroupListResponse { + SharedIpGroup sharedIpGroup; + } + + public SharedIpGroup apply(InputStream stream) { + + try { + return gson.fromJson(new InputStreamReader(stream, "UTF-8"), + SharedIpGroupListResponse.class).sharedIpGroup; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupListFromGsonResponse.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupListFromGsonResponse.java new file mode 100644 index 0000000000..89996e0d96 --- /dev/null +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupListFromGsonResponse.java @@ -0,0 +1,63 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * 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.rackspace.cloudservers.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.List; + +import org.jclouds.http.functions.ParseJson; +import org.jclouds.rackspace.cloudservers.domain.SharedIpGroup; + +import com.google.gson.Gson; +import com.google.inject.Inject; +import com.google.inject.internal.Lists; + +/** + * This parses {@link SharedIpGroup} from a gson string. + * + * @author Adrian Cole + */ +public class ParseSharedIpGroupListFromGsonResponse extends ParseJson> { + + @Inject + public ParseSharedIpGroupListFromGsonResponse(Gson gson) { + super(gson); + } + + private static class SharedIpGroupListResponse { + List sharedIpGroups = Lists.newArrayList(); + } + + public List apply(InputStream stream) { + + try { + return gson.fromJson(new InputStreamReader(stream, "UTF-8"), + SharedIpGroupListResponse.class).sharedIpGroups; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ReturnSharedIpGroupNotFoundOn404.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ReturnSharedIpGroupNotFoundOn404.java new file mode 100644 index 0000000000..3df8f455e1 --- /dev/null +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ReturnSharedIpGroupNotFoundOn404.java @@ -0,0 +1,48 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * 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.rackspace.cloudservers.functions; + +import org.jclouds.http.HttpResponseException; +import org.jclouds.rackspace.cloudservers.domain.SharedIpGroup; + +import com.google.common.base.Function; + +/** + * + * + * @author Adrian Cole + */ +public class ReturnSharedIpGroupNotFoundOn404 implements Function { + + public SharedIpGroup apply(Exception from) { + if (from instanceof HttpResponseException) { + HttpResponseException responseException = (HttpResponseException) from; + if (responseException.getResponse().getStatusCode() == 404) { + return SharedIpGroup.NOT_FOUND; + } + } + return null; + } + +} diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/options/CreateSharedIpGroupOptions.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/options/CreateSharedIpGroupOptions.java new file mode 100644 index 0000000000..fbef397861 --- /dev/null +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/options/CreateSharedIpGroupOptions.java @@ -0,0 +1,68 @@ +package org.jclouds.rackspace.cloudservers.options; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import org.jclouds.http.HttpRequest; +import org.jclouds.http.binders.JsonBinder; + +import com.google.common.collect.ImmutableMap; +import com.google.inject.internal.Nullable; + +/** + * + * + * @author Adrian Cole + * + */ +public class CreateSharedIpGroupOptions extends JsonBinder { + Integer serverId; + + @SuppressWarnings("unused") + private class SharedIpGroupRequest { + final String name; + Integer server; + + private SharedIpGroupRequest(String name, @Nullable Integer serverId) { + this.name = name; + this.server = serverId; + } + + } + + @Override + public void addEntityToRequest(Map postParams, HttpRequest request) { + SharedIpGroupRequest createRequest = new SharedIpGroupRequest(checkNotNull(postParams + .get("name")), serverId); + super.addEntityToRequest(ImmutableMap.of("sharedIpGroup", createRequest), request); + } + + @Override + public void addEntityToRequest(Object toBind, HttpRequest request) { + throw new IllegalStateException("CreateSharedIpGroup is a POST operation"); + } + + /** + * + * @param id + * of the server to include with this request. + */ + public CreateSharedIpGroupOptions withServer(int id) { + checkArgument(id > 0, "server id must be a positive number"); + this.serverId = id; + return this; + } + + public static class Builder { + + /** + * @see CreateSharedIpGroupOptions#withServer(int) + */ + public static CreateSharedIpGroupOptions withServer(int id) { + CreateSharedIpGroupOptions options = new CreateSharedIpGroupOptions(); + return options.withServer(id); + } + } +} diff --git a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionLiveTest.java b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionLiveTest.java index 11755faa58..80de103e5f 100755 --- a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionLiveTest.java +++ b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionLiveTest.java @@ -24,6 +24,7 @@ package org.jclouds.rackspace.cloudservers; import static org.jclouds.rackspace.cloudservers.options.CreateServerOptions.Builder.withFile; +import static org.jclouds.rackspace.cloudservers.options.CreateSharedIpGroupOptions.Builder.withServer; import static org.jclouds.rackspace.cloudservers.options.ListOptions.Builder.withDetails; import static org.jclouds.rackspace.reference.RackspaceConstants.PROPERTY_RACKSPACE_KEY; import static org.jclouds.rackspace.reference.RackspaceConstants.PROPERTY_RACKSPACE_USER; @@ -44,12 +45,14 @@ import org.jclouds.rackspace.cloudservers.domain.Flavor; import org.jclouds.rackspace.cloudservers.domain.Image; import org.jclouds.rackspace.cloudservers.domain.Server; import org.jclouds.rackspace.cloudservers.domain.ServerStatus; +import org.jclouds.rackspace.cloudservers.domain.SharedIpGroup; import org.jclouds.ssh.SshConnection; import org.jclouds.ssh.jsch.config.JschSshConnectionModule; import org.jclouds.util.Utils; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.inject.Injector; @@ -94,33 +97,6 @@ public class CloudServersConnectionLiveTest { assertTrue(initialContainerCount >= 0); } - @Test - public void testListFlavors() throws Exception { - List response = connection.listFlavors(); - assert null != response; - long flavorCount = response.size(); - assertTrue(flavorCount >= 1); - for (Flavor flavor : response) { - assertTrue(flavor.getId() >= 0); - assert null != flavor.getName() : flavor; - } - - } - - @Test - public void testListFlavorsDetail() throws Exception { - List response = connection.listFlavors(withDetails()); - assert null != response; - long flavorCount = response.size(); - assertTrue(flavorCount >= 0); - for (Flavor flavor : response) { - assertTrue(flavor.getId() >= 1); - assert null != flavor.getName() : flavor; - assert null != flavor.getDisk() : flavor; - assert null != flavor.getRam() : flavor; - } - } - @Test public void testListImages() throws Exception { List response = connection.listImages(); @@ -170,6 +146,49 @@ public class CloudServersConnectionLiveTest { assertEquals(Image.NOT_FOUND, newDetails); } + public void testGetServerDetailsNotFound() throws Exception { + Server newDetails = connection.getServer(12312987); + assertEquals(Server.NOT_FOUND, newDetails); + } + + public void testGetServersDetail() throws Exception { + List response = connection.listServers(withDetails()); + assert null != response; + long serverCount = response.size(); + assertTrue(serverCount >= 0); + for (Server server : response) { + Server newDetails = connection.getServer(server.getId()); + assertEquals(server, newDetails); + } + } + + @Test + public void testListFlavors() throws Exception { + List response = connection.listFlavors(); + assert null != response; + long flavorCount = response.size(); + assertTrue(flavorCount >= 1); + for (Flavor flavor : response) { + assertTrue(flavor.getId() >= 0); + assert null != flavor.getName() : flavor; + } + + } + + @Test + public void testListFlavorsDetail() throws Exception { + List response = connection.listFlavors(withDetails()); + assert null != response; + long flavorCount = response.size(); + assertTrue(flavorCount >= 0); + for (Flavor flavor : response) { + assertTrue(flavor.getId() >= 1); + assert null != flavor.getName() : flavor; + assert null != flavor.getDisk() : flavor; + assert null != flavor.getRam() : flavor; + } + } + @Test public void testGetFlavorsDetail() throws Exception { List response = connection.listFlavors(withDetails()); @@ -187,22 +206,80 @@ public class CloudServersConnectionLiveTest { assertEquals(Flavor.NOT_FOUND, newDetails); } - public void testGetServerDetailsNotFound() throws Exception { - Server newDetails = connection.getServer(12312987); - assertEquals(Server.NOT_FOUND, newDetails); + @Test + public void testListSharedIpGroups() throws Exception { + List response = connection.listSharedIpGroups(); + assert null != response; + long sharedIpGroupCount = response.size(); + assertTrue(sharedIpGroupCount >= 0); + for (SharedIpGroup sharedIpGroup : response) { + assertTrue(sharedIpGroup.getId() >= 0); + assert null != sharedIpGroup.getName() : sharedIpGroup; + } + } - public void testGetServersDetail() throws Exception { - List response = connection.listServers(withDetails()); + @Test + public void testListSharedIpGroupsDetail() throws Exception { + List response = connection.listSharedIpGroups(withDetails()); assert null != response; - long serverCount = response.size(); - assertTrue(serverCount >= 0); - for (Server server : response) { - Server newDetails = connection.getServer(server.getId()); - assertEquals(server, newDetails); + long sharedIpGroupCount = response.size(); + assertTrue(sharedIpGroupCount >= 0); + for (SharedIpGroup sharedIpGroup : response) { + assertTrue(sharedIpGroup.getId() >= 1); + assert null != sharedIpGroup.getName() : sharedIpGroup; + assert null != sharedIpGroup.getServers() : sharedIpGroup; } } + @Test + public void testGetSharedIpGroupsDetail() throws Exception { + List response = connection.listSharedIpGroups(withDetails()); + assert null != response; + long sharedIpGroupCount = response.size(); + assertTrue(sharedIpGroupCount >= 0); + for (SharedIpGroup sharedIpGroup : response) { + SharedIpGroup newDetails = connection.getSharedIpGroup(sharedIpGroup.getId()); + assertEquals(sharedIpGroup, newDetails); + } + } + + public void testGetSharedIpGroupDetailsNotFound() throws Exception { + SharedIpGroup newDetails = connection.getSharedIpGroup(12312987); + assertEquals(SharedIpGroup.NOT_FOUND, newDetails); + } + + @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateServer") + public void testCreateSharedIpGroup() throws Exception { + SharedIpGroup sharedIpGroup = null; + while (sharedIpGroup == null) { + String sharedIpGroupName = serverPrefix + "createSharedIpGroup" + + new SecureRandom().nextInt(); + try { + sharedIpGroup = connection.createSharedIpGroup(sharedIpGroupName, withServer(serverId)); + } catch (UndeclaredThrowableException e) { + HttpResponseException htpe = (HttpResponseException) e.getCause().getCause(); + if (htpe.getResponse().getStatusCode() == 400) + continue; + throw e; + } + } + assertNotNull(sharedIpGroup.getName()); + sharedIpGroupId = sharedIpGroup.getId(); + assertEquals(sharedIpGroup.getServers(), ImmutableList.of(serverId)); + } + + @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testCreateSharedIpGroup" }) + void testDeleteSharedIpGroup() { + if (sharedIpGroupId > 0) { + connection.deleteSharedIpGroup(sharedIpGroupId); + SharedIpGroup server = connection.getSharedIpGroup(sharedIpGroupId); + assertEquals(server, SharedIpGroup.NOT_FOUND); + } + } + + private int sharedIpGroupId; + private String serverPrefix = System.getProperty("user.name") + ".cs"; private int serverId; private String adminPass; @@ -300,7 +377,7 @@ public class CloudServersConnectionLiveTest { // TODO test createServer.withSharedIp // must be last! - @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = {"testChangePassword","testRenameServer"}) + @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testChangePassword", "testRenameServer" }) void deleteServer() { if (serverId > 0) { connection.deleteServer(serverId); diff --git a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java index cbeb88f95b..dc93a089b2 100755 --- a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java +++ b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java @@ -26,9 +26,11 @@ package org.jclouds.rackspace.cloudservers; import static org.jclouds.rackspace.cloudservers.options.CreateServerOptions.Builder.withFile; import static org.jclouds.rackspace.cloudservers.options.CreateServerOptions.Builder.withMetadata; import static org.jclouds.rackspace.cloudservers.options.CreateServerOptions.Builder.withSharedIpGroup; +import static org.jclouds.rackspace.cloudservers.options.CreateSharedIpGroupOptions.Builder.withServer; import static org.jclouds.rackspace.cloudservers.options.ListOptions.Builder.changesSince; import static org.jclouds.rackspace.cloudservers.options.ListOptions.Builder.withDetails; -import static org.testng.Assert.*; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; import java.lang.reflect.Method; import java.net.InetAddress; @@ -53,10 +55,14 @@ import org.jclouds.rackspace.cloudservers.functions.ParseImageFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseImageListFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseServerFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseServerListFromGsonResponse; +import org.jclouds.rackspace.cloudservers.functions.ParseSharedIpGroupFromGsonResponse; +import org.jclouds.rackspace.cloudservers.functions.ParseSharedIpGroupListFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ReturnFlavorNotFoundOn404; import org.jclouds.rackspace.cloudservers.functions.ReturnImageNotFoundOn404; import org.jclouds.rackspace.cloudservers.functions.ReturnServerNotFoundOn404; +import org.jclouds.rackspace.cloudservers.functions.ReturnSharedIpGroupNotFoundOn404; import org.jclouds.rackspace.cloudservers.options.CreateServerOptions; +import org.jclouds.rackspace.cloudservers.options.CreateSharedIpGroupOptions; import org.jclouds.rackspace.cloudservers.options.ListOptions; import org.jclouds.rest.JaxrsAnnotationProcessor; import org.jclouds.rest.config.JaxrsModule; @@ -94,7 +100,18 @@ public class CloudServersConnectionTest { assertEquals("{\"server\":{\"name\":\"ralphie\",\"imageId\":2,\"flavorId\":1}}", httpMethod .getEntity()); validateCreateServer(method, httpMethod); + } + public void testCreateServerWithIpGroup() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("createServer", String.class, + int.class, int.class, createServerOptionsVarargsClass); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] { "ralphie", + 2, 1, withSharedIpGroup(2) }); + assertEquals( + "{\"server\":{\"name\":\"ralphie\",\"imageId\":2,\"flavorId\":1,\"sharedIpGroupId\":2}}", + httpMethod.getEntity()); + validateCreateServer(method, httpMethod); } public void testCreateServerWithFile() throws SecurityException, NoSuchMethodException { @@ -121,18 +138,6 @@ public class CloudServersConnectionTest { validateCreateServer(method, httpMethod); } - public void testCreateServerWithIpGroup() throws SecurityException, NoSuchMethodException { - Method method = CloudServersConnection.class.getMethod("createServer", String.class, - int.class, int.class, createServerOptionsVarargsClass); - URI endpoint = URI.create("http://localhost"); - HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] { "ralphie", - 2, 1, withSharedIpGroup(2) }); - assertEquals( - "{\"server\":{\"name\":\"ralphie\",\"imageId\":2,\"flavorId\":1,\"sharedIpGroupId\":2}}", - httpMethod.getEntity()); - validateCreateServer(method, httpMethod); - } - public void testCreateServerWithIpGroupAndSharedIp() throws SecurityException, NoSuchMethodException, UnknownHostException { Method method = CloudServersConnection.class.getMethod("createServer", String.class, @@ -217,6 +222,21 @@ public class CloudServersConnectionTest { assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); } + public void testGetServer() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("getServer", int.class); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] { 2 }); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/servers/2"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json"); + assertEquals(httpMethod.getMethod(), HttpMethod.GET); + assertEquals(httpMethod.getHeaders().size(), 0); + assertEquals(processor.createResponseParser(method).getClass(), + ParseServerFromGsonResponse.class); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(), + ReturnServerNotFoundOn404.class); + } + public void testListFlavors() throws SecurityException, NoSuchMethodException { Method method = CloudServersConnection.class .getMethod("listFlavors", listOptionsVarargsClass); @@ -265,6 +285,38 @@ public class CloudServersConnectionTest { assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); } + public void testListFlavorsDetailOptions() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class + .getMethod("listFlavors", listOptionsVarargsClass); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, + new Object[] { withDetails().changesSince(now).maxResults(1).startAt(2) }); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/flavors/detail"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json" + "&limit=1&changes-since=" + + now.getMillis() / 1000 + "&offset=2"); + assertEquals(httpMethod.getMethod(), HttpMethod.GET); + assertEquals(httpMethod.getHeaders().size(), 0); + assertEquals(processor.createResponseParser(method).getClass(), + ParseFlavorListFromGsonResponse.class); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); + } + + public void testGetFlavor() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("getFlavor", int.class); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] { 2 }); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/flavors/2"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json"); + assertEquals(httpMethod.getMethod(), HttpMethod.GET); + assertEquals(httpMethod.getHeaders().size(), 0); + assertEquals(processor.createResponseParser(method).getClass(), + ParseFlavorFromGsonResponse.class); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(), + ReturnFlavorNotFoundOn404.class); + } + public void testListImages() throws SecurityException, NoSuchMethodException { Method method = CloudServersConnection.class.getMethod("listImages", listOptionsVarargsClass); URI endpoint = URI.create("http://localhost"); @@ -294,23 +346,6 @@ public class CloudServersConnectionTest { assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); } - public void testListFlavorsDetailOptions() throws SecurityException, NoSuchMethodException { - Method method = CloudServersConnection.class - .getMethod("listFlavors", listOptionsVarargsClass); - URI endpoint = URI.create("http://localhost"); - HttpRequest httpMethod = processor.createRequest(endpoint, method, - new Object[] { withDetails().changesSince(now).maxResults(1).startAt(2) }); - assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); - assertEquals(httpMethod.getEndpoint().getPath(), "/flavors/detail"); - assertEquals(httpMethod.getEndpoint().getQuery(), "format=json" + "&limit=1&changes-since=" - + now.getMillis() / 1000 + "&offset=2"); - assertEquals(httpMethod.getMethod(), HttpMethod.GET); - assertEquals(httpMethod.getHeaders().size(), 0); - assertEquals(processor.createResponseParser(method).getClass(), - ParseFlavorListFromGsonResponse.class); - assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); - } - public void testListImagesOptions() throws SecurityException, NoSuchMethodException { Method method = CloudServersConnection.class.getMethod("listImages", listOptionsVarargsClass); URI endpoint = URI.create("http://localhost"); @@ -358,36 +393,6 @@ public class CloudServersConnectionTest { ReturnImageNotFoundOn404.class); } - public void testGetFlavor() throws SecurityException, NoSuchMethodException { - Method method = CloudServersConnection.class.getMethod("getFlavor", int.class); - URI endpoint = URI.create("http://localhost"); - HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] { 2 }); - assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); - assertEquals(httpMethod.getEndpoint().getPath(), "/flavors/2"); - assertEquals(httpMethod.getEndpoint().getQuery(), "format=json"); - assertEquals(httpMethod.getMethod(), HttpMethod.GET); - assertEquals(httpMethod.getHeaders().size(), 0); - assertEquals(processor.createResponseParser(method).getClass(), - ParseFlavorFromGsonResponse.class); - assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(), - ReturnFlavorNotFoundOn404.class); - } - - public void testGetServer() throws SecurityException, NoSuchMethodException { - Method method = CloudServersConnection.class.getMethod("getServer", int.class); - URI endpoint = URI.create("http://localhost"); - HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] { 2 }); - assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); - assertEquals(httpMethod.getEndpoint().getPath(), "/servers/2"); - assertEquals(httpMethod.getEndpoint().getQuery(), "format=json"); - assertEquals(httpMethod.getMethod(), HttpMethod.GET); - assertEquals(httpMethod.getHeaders().size(), 0); - assertEquals(processor.createResponseParser(method).getClass(), - ParseServerFromGsonResponse.class); - assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(), - ReturnServerNotFoundOn404.class); - } - public void testDeleteServer() throws SecurityException, NoSuchMethodException { Method method = CloudServersConnection.class.getMethod("deleteServer", int.class); URI endpoint = URI.create("http://localhost"); @@ -439,6 +444,141 @@ public class CloudServersConnectionTest { assertEquals(processor.createResponseParser(method).getClass(), ReturnTrueIf2xx.class); } + public void testListSharedIpGroups() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("listSharedIpGroups", + listOptionsVarargsClass); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] {}); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/shared_ip_groups"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json"); + assertEquals(httpMethod.getMethod(), HttpMethod.GET); + assertEquals(httpMethod.getHeaders().size(), 0); + assertEquals(processor.createResponseParser(method).getClass(), + ParseSharedIpGroupListFromGsonResponse.class); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); + } + + public void testListSharedIpGroupsOptions() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("listSharedIpGroups", + listOptionsVarargsClass); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, + new Object[] { changesSince(now).maxResults(1).startAt(2) }); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/shared_ip_groups"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json" + "&limit=1&changes-since=" + + now.getMillis() / 1000 + "&offset=2"); + assertEquals(httpMethod.getMethod(), HttpMethod.GET); + assertEquals(httpMethod.getHeaders().size(), 0); + assertEquals(processor.createResponseParser(method).getClass(), + ParseSharedIpGroupListFromGsonResponse.class); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); + } + + public void testListSharedIpGroupsDetail() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("listSharedIpGroups", + listOptionsVarargsClass); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, + new Object[] { withDetails() }); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/shared_ip_groups/detail"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json"); + assertEquals(httpMethod.getMethod(), HttpMethod.GET); + assertEquals(httpMethod.getHeaders().size(), 0); + assertEquals(processor.createResponseParser(method).getClass(), + ParseSharedIpGroupListFromGsonResponse.class); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); + } + + public void testListSharedIpGroupsDetailOptions() throws SecurityException, + NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("listSharedIpGroups", + listOptionsVarargsClass); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, + new Object[] { withDetails().changesSince(now).maxResults(1).startAt(2) }); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/shared_ip_groups/detail"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json" + "&limit=1&changes-since=" + + now.getMillis() / 1000 + "&offset=2"); + assertEquals(httpMethod.getMethod(), HttpMethod.GET); + assertEquals(httpMethod.getHeaders().size(), 0); + assertEquals(processor.createResponseParser(method).getClass(), + ParseSharedIpGroupListFromGsonResponse.class); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); + } + + public void testGetSharedIpGroup() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("getSharedIpGroup", int.class); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] { 2 }); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/shared_ip_groups/2"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json"); + assertEquals(httpMethod.getMethod(), HttpMethod.GET); + assertEquals(httpMethod.getHeaders().size(), 0); + assertEquals(processor.createResponseParser(method).getClass(), + ParseSharedIpGroupFromGsonResponse.class); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(), + ReturnSharedIpGroupNotFoundOn404.class); + } + + private static final Class createSharedIpGroupOptionsVarargsClass = new CreateSharedIpGroupOptions[] {} + .getClass(); + + public void testCreateSharedIpGroup() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("createSharedIpGroup", String.class, + createSharedIpGroupOptionsVarargsClass); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor + .createRequest(endpoint, method, new Object[] { "ralphie" }); + assertEquals("{\"sharedIpGroup\":{\"name\":\"ralphie\"}}", httpMethod.getEntity()); + validateCreateSharedIpGroup(method, httpMethod); + } + + public void testCreateSharedIpGroupWithIpGroup() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("createSharedIpGroup", String.class, + createSharedIpGroupOptionsVarargsClass); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] { "ralphie", + withServer(2) }); + assertEquals("{\"sharedIpGroup\":{\"name\":\"ralphie\",\"server\":2}}", httpMethod + .getEntity()); + validateCreateSharedIpGroup(method, httpMethod); + } + + private void validateCreateSharedIpGroup(Method method, HttpRequest httpMethod) { + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/shared_ip_groups"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json"); + assertEquals(httpMethod.getMethod(), HttpMethod.POST); + assertEquals(httpMethod.getHeaders().size(), 2); + assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_LENGTH), Collections + .singletonList(httpMethod.getEntity().toString().getBytes().length + "")); + assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections + .singletonList(MediaType.APPLICATION_JSON)); + assertEquals(processor.createResponseParser(method).getClass(), + ParseSharedIpGroupFromGsonResponse.class); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); + assertNotNull(processor.getPostEntityBinderOrNull(method, new Object[] { "", + new CreateSharedIpGroupOptions[] { withServer(2) } })); + } + + public void testDeleteSharedIpGroup() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("deleteSharedIpGroup", int.class); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = processor.createRequest(endpoint, method, new Object[] { 2 }); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/shared_ip_groups/2"); + assertEquals(httpMethod.getMethod(), HttpMethod.DELETE); + assertEquals(httpMethod.getHeaders().size(), 0); + assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(), + ReturnFalseOn404.class); + assertEquals(processor.createResponseParser(method).getClass(), ReturnTrueIf2xx.class); + } + JaxrsAnnotationProcessor processor; @BeforeClass diff --git a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupFromGsonResponseTest.java b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupFromGsonResponseTest.java new file mode 100644 index 0000000000..75af3efa13 --- /dev/null +++ b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupFromGsonResponseTest.java @@ -0,0 +1,62 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * 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.rackspace.cloudservers.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.net.UnknownHostException; + +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.rackspace.cloudservers.domain.SharedIpGroup; +import org.jclouds.util.DateService; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.gson.Gson; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseSharedIpGroupFromGsonResponse} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "cloudservers.ParseSharedIpGroupFromGsonResponseTest") +public class ParseSharedIpGroupFromGsonResponseTest { + + Injector i = Guice.createInjector(new ParserModule()); + DateService dateService = new DateService(); + + public void testApplyInputStreamDetails() throws UnknownHostException { + InputStream is = getClass().getResourceAsStream("/test_get_sharedipgroup_details.json"); + + ParseSharedIpGroupFromGsonResponse parser = new ParseSharedIpGroupFromGsonResponse(i + .getInstance(Gson.class)); + SharedIpGroup response = parser.apply(is); + assertEquals(response.getId(), 1234); + assertEquals(response.getName(), "Shared IP Group 1"); + assertEquals(response.getServers(), ImmutableList.of(422)); + } +} \ No newline at end of file diff --git a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupListFromGsonResponseTest.java b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupListFromGsonResponseTest.java new file mode 100644 index 0000000000..04cd1bcded --- /dev/null +++ b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseSharedIpGroupListFromGsonResponseTest.java @@ -0,0 +1,77 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * 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.rackspace.cloudservers.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.net.UnknownHostException; +import java.util.List; + +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.rackspace.cloudservers.domain.SharedIpGroup; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.gson.Gson; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseSharedIpGroupListFromGsonResponseTest} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "cloudSharedIpGroups.ParseSharedIpGroupListFromGsonResponseTest") +public class ParseSharedIpGroupListFromGsonResponseTest { + + Injector i = Guice.createInjector(new ParserModule()); + + public void testApplyInputStream() { + InputStream is = getClass().getResourceAsStream("/test_list_sharedipgroups.json"); + + List expects = ImmutableList.of(new SharedIpGroup(1234, "Shared IP Group 1"), + new SharedIpGroup(5678, "Shared IP Group 2")); + ParseSharedIpGroupListFromGsonResponse parser = new ParseSharedIpGroupListFromGsonResponse(i + .getInstance(Gson.class)); + assertEquals(parser.apply(is), expects); + } + + public void testApplyInputStreamDetails() throws UnknownHostException { + InputStream is = getClass().getResourceAsStream("/test_list_sharedipgroups_detail.json"); + + ParseSharedIpGroupListFromGsonResponse parser = new ParseSharedIpGroupListFromGsonResponse(i + .getInstance(Gson.class)); + List response = parser.apply(is); + assertEquals(response.get(0).getId(), 1234); + assertEquals(response.get(0).getName(), "Shared IP Group 1"); + assertEquals(response.get(0).getServers(), ImmutableList.of(422, 3445)); + + assertEquals(response.get(1).getId(), 5678); + assertEquals(response.get(1).getName(), "Shared IP Group 2"); + assertEquals(response.get(1).getServers(), ImmutableList.of(23203, 2456, 9891)); + + } + +} diff --git a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/options/CreateSharedIpGroupOptionsTest.java b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/options/CreateSharedIpGroupOptionsTest.java new file mode 100644 index 0000000000..b67b8d1c7a --- /dev/null +++ b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/options/CreateSharedIpGroupOptionsTest.java @@ -0,0 +1,83 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * 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.rackspace.cloudservers.options; + +import static org.jclouds.rackspace.cloudservers.options.CreateSharedIpGroupOptions.Builder.withServer; +import static org.testng.Assert.assertEquals; + +import java.net.URI; + +import org.jclouds.http.HttpMethod; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.functions.config.ParserModule; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseFlavorFromGsonResponse} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "cloudsharedIpGroups.CreateSharedIpGroupOptionsTest") +public class CreateSharedIpGroupOptionsTest { + + Injector injector = Guice.createInjector(new ParserModule()); + + @Test + public void testAddEntityToRequestMapOfStringStringHttpRequest() { + CreateSharedIpGroupOptions options = new CreateSharedIpGroupOptions(); + HttpRequest request = buildRequest(options); + assertEquals("{\"sharedIpGroup\":{\"name\":\"foo\"}}", request.getEntity()); + } + + private HttpRequest buildRequest(CreateSharedIpGroupOptions options) { + injector.injectMembers(options); + HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("/")); + options.addEntityToRequest(ImmutableMap.of("name", "foo"), request); + return request; + } + + @Test + public void testWithServer() { + CreateSharedIpGroupOptions options = new CreateSharedIpGroupOptions(); + options.withServer(3); + HttpRequest request = buildRequest(options); + assertSharedIpGroup(request); + } + + @Test + public void testWithServerStatic() { + CreateSharedIpGroupOptions options = withServer(3); + HttpRequest request = buildRequest(options); + assertSharedIpGroup(request); + } + + private void assertSharedIpGroup(HttpRequest request) { + assertEquals("{\"sharedIpGroup\":{\"name\":\"foo\",\"server\":3}}", request.getEntity()); + } + +} diff --git a/rackspace/cloudservers/core/src/test/resources/test_get_sharedipgroup_details.json b/rackspace/cloudservers/core/src/test/resources/test_get_sharedipgroup_details.json new file mode 100644 index 0000000000..d10f33d656 --- /dev/null +++ b/rackspace/cloudservers/core/src/test/resources/test_get_sharedipgroup_details.json @@ -0,0 +1,7 @@ +{ + "sharedIpGroup" : { + "id" : 1234, + "name" : "Shared IP Group 1", + "servers" : [422] + } +} \ No newline at end of file diff --git a/rackspace/cloudservers/core/src/test/resources/test_list_sharedipgroups.json b/rackspace/cloudservers/core/src/test/resources/test_list_sharedipgroups.json new file mode 100644 index 0000000000..73717c4a76 --- /dev/null +++ b/rackspace/cloudservers/core/src/test/resources/test_list_sharedipgroups.json @@ -0,0 +1,12 @@ +{ + "sharedIpGroups" : [ + { + "id" : 1234, + "name" : "Shared IP Group 1" + }, + { + "id" : 5678, + "name" : "Shared IP Group 2" + } + ] +} \ No newline at end of file diff --git a/rackspace/cloudservers/core/src/test/resources/test_list_sharedipgroups_detail.json b/rackspace/cloudservers/core/src/test/resources/test_list_sharedipgroups_detail.json new file mode 100644 index 0000000000..de6e37336b --- /dev/null +++ b/rackspace/cloudservers/core/src/test/resources/test_list_sharedipgroups_detail.json @@ -0,0 +1,14 @@ +{ + "sharedIpGroups" : [ + { + "id" : 1234, + "name" : "Shared IP Group 1", + "servers" : [422, 3445] + }, + { + "id" : 5678, + "name" : "Shared IP Group 2", + "servers" : [23203, 2456, 9891] + } + ] +} \ No newline at end of file