From b78824f7e0bb72ea2982fee37b3296c5ea513c24 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 2 Jan 2012 10:43:16 -0800 Subject: [PATCH] cleaned return type for server templates --- .../jclouds/glesys/domain/ServerTemplate.java | 8 ++- .../glesys/features/ServerAsyncClient.java | 38 ++++++++--- .../ParseServerTemplatesFromHttpResponse.java | 41 ++++++++++++ .../features/BaseGleSYSAsyncClientTest.java | 35 ++++++---- .../features/ServerAsyncClientTest.java | 21 +++--- .../parse/ParseServerTemplatesTest.java | 64 ++++++++----------- 6 files changed, 140 insertions(+), 67 deletions(-) create mode 100644 sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/ParseServerTemplatesFromHttpResponse.java diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerTemplate.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerTemplate.java index 3f923ff5f4..5ae1105265 100644 --- a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerTemplate.java +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerTemplate.java @@ -1,6 +1,7 @@ package org.jclouds.glesys.domain; import com.google.common.base.Objects; +import com.google.common.collect.Ordering; import com.google.gson.annotations.SerializedName; /** @@ -9,7 +10,7 @@ import com.google.gson.annotations.SerializedName; * @author Adam Lowe * @see */ -public class ServerTemplate { +public class ServerTemplate implements Comparable{ public static Builder builder() { return new Builder(); @@ -131,4 +132,9 @@ public class ServerTemplate { return String.format("[name=%s, min_disk_size=%d, min_mem_size=%d, os=%s, platform=%s]", name, minDiskSize, minMemSize, os, platform); } + + @Override + public int compareTo(ServerTemplate arg0) { + return Ordering.usingToString().compare(this, arg0); + } } diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerAsyncClient.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerAsyncClient.java index da0ae14357..02c7fe503c 100644 --- a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerAsyncClient.java +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerAsyncClient.java @@ -18,19 +18,41 @@ */ package org.jclouds.glesys.features; -import com.google.common.util.concurrent.ListenableFuture; -import org.jclouds.glesys.domain.*; -import org.jclouds.glesys.options.*; +import java.util.Map; +import java.util.Set; +import java.util.SortedMap; + +import javax.ws.rs.Consumes; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; + +import org.jclouds.glesys.domain.Server; +import org.jclouds.glesys.domain.ServerAllowedArguments; +import org.jclouds.glesys.domain.ServerConsole; +import org.jclouds.glesys.domain.ServerCreated; +import org.jclouds.glesys.domain.ServerDetails; +import org.jclouds.glesys.domain.ServerLimit; +import org.jclouds.glesys.domain.ServerStatus; +import org.jclouds.glesys.domain.ServerTemplate; +import org.jclouds.glesys.functions.ParseServerTemplatesFromHttpResponse; +import org.jclouds.glesys.options.ServerCloneOptions; +import org.jclouds.glesys.options.ServerCreateOptions; +import org.jclouds.glesys.options.ServerDestroyOptions; +import org.jclouds.glesys.options.ServerEditOptions; +import org.jclouds.glesys.options.ServerStatusOptions; +import org.jclouds.glesys.options.ServerStopOptions; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.ResponseParser; import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; -import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; -import java.util.*; +import com.google.common.util.concurrent.ListenableFuture; /** * Provides asynchronous access to Server via their REST API. @@ -110,9 +132,9 @@ public interface ServerAsyncClient { */ @GET @Path("/server/templates/format/json") - @SelectJson("templates") + @ResponseParser(ParseServerTemplatesFromHttpResponse.class) @Consumes(MediaType.APPLICATION_JSON) - ListenableFuture>> getTemplates(); + ListenableFuture> getTemplates(); /** * @see ServerClient#stopServer diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/ParseServerTemplatesFromHttpResponse.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/ParseServerTemplatesFromHttpResponse.java new file mode 100644 index 0000000000..7f67de05f5 --- /dev/null +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/ParseServerTemplatesFromHttpResponse.java @@ -0,0 +1,41 @@ +package org.jclouds.glesys.functions; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; +import java.util.Set; + +import javax.inject.Singleton; + +import org.jclouds.glesys.domain.ServerTemplate; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.ParseFirstJsonValueNamed; +import org.jclouds.json.internal.GsonWrapper; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.inject.Inject; +import com.google.inject.TypeLiteral; + +/** + * @author Adrian Cole + */ +@Singleton +public class ParseServerTemplatesFromHttpResponse implements Function> { + private final ParseFirstJsonValueNamed>> parser; + + @Inject + public ParseServerTemplatesFromHttpResponse(GsonWrapper gsonWrapper) { + this.parser = new ParseFirstJsonValueNamed>>(checkNotNull(gsonWrapper, + "gsonWrapper"), new TypeLiteral>>() { + }, "templates"); + } + + public Set apply(HttpResponse response) { + checkNotNull(response, "response"); + Map> toParse = parser.apply(response); + checkNotNull(toParse, "parsed result from %s", response); + return ImmutableSet.copyOf(Iterables.concat(toParse.values())); + } +} diff --git a/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/BaseGleSYSAsyncClientTest.java b/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/BaseGleSYSAsyncClientTest.java index 69fe8b45ed..fb0aa432f6 100644 --- a/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/BaseGleSYSAsyncClientTest.java +++ b/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/BaseGleSYSAsyncClientTest.java @@ -18,9 +18,16 @@ */ package org.jclouds.glesys.features; -import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableSortedSet; -import com.google.common.collect.Maps; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; + import org.jclouds.glesys.GleSYSAsyncClient; import org.jclouds.glesys.GleSYSClient; import org.jclouds.http.HttpRequest; @@ -32,20 +39,16 @@ import org.jclouds.rest.RestContextFactory; import org.jclouds.rest.RestContextSpec; import org.jclouds.util.Strings2; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import static org.testng.Assert.*; +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableSortedSet; +import com.google.common.collect.Maps; /** * @author Adrian Cole * @author Adam Lowe */ public abstract class BaseGleSYSAsyncClientTest extends RestClientTest { - protected Class asyncClientClass; + protected Class asyncClientClass; protected String remoteServicePrefix; @Override @@ -74,7 +77,13 @@ public abstract class BaseGleSYSAsyncClientTest extends RestClientTest { * @param exceptionParser the class of exception handler expected * @param args either Map.Entry or BaseHttpRequestOptions that make up the arguments to the method */ - protected void testMethod(String localMethod, String remoteCall, String httpMethod, boolean expectResponse, Class exceptionParser, Object... args) throws Exception { + protected void testMethod(String localMethod, String remoteCall, String httpMethod, boolean expectResponse, + Class exceptionParser, Object... args) throws Exception { + testMethod(localMethod, remoteCall, httpMethod, expectResponse, ParseFirstJsonValueNamed.class, exceptionParser, + args); + } + + protected void testMethod(String localMethod, String remoteCall, String httpMethod, boolean expectResponse, Class responseParser, Class exceptionParser, Object... args) throws Exception { List argStrings = new ArrayList(); List argValues = new ArrayList(); @@ -107,7 +116,7 @@ public abstract class BaseGleSYSAsyncClientTest extends RestClientTest { if (expectResponse) { assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); - assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class); + assertResponseParserClassEquals(method, httpRequest, responseParser); } if (argStrings.isEmpty()) { diff --git a/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/ServerAsyncClientTest.java b/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/ServerAsyncClientTest.java index 4a81ea9a00..283c622c6e 100644 --- a/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/ServerAsyncClientTest.java +++ b/sandbox-providers/glesys/src/test/java/org/jclouds/glesys/features/ServerAsyncClientTest.java @@ -18,20 +18,22 @@ */ package org.jclouds.glesys.features; -import com.google.common.collect.Maps; -import com.google.inject.TypeLiteral; -import org.jclouds.glesys.options.*; -import org.jclouds.http.HttpRequest; -import org.jclouds.http.functions.ParseFirstJsonValueNamed; +import java.util.Map; + +import org.jclouds.glesys.functions.ParseServerTemplatesFromHttpResponse; +import org.jclouds.glesys.options.ServerCloneOptions; +import org.jclouds.glesys.options.ServerCreateOptions; +import org.jclouds.glesys.options.ServerDestroyOptions; +import org.jclouds.glesys.options.ServerEditOptions; +import org.jclouds.glesys.options.ServerStatusOptions; +import org.jclouds.glesys.options.ServerStopOptions; import org.jclouds.rest.functions.MapHttp4xxCodesToExceptions; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.internal.RestAnnotationProcessor; import org.testng.annotations.Test; -import java.io.IOException; -import java.lang.reflect.Method; -import java.util.Map; +import com.google.inject.TypeLiteral; /** * Tests annotation parsing of {@code ServerAsyncClient} @@ -58,7 +60,8 @@ public class ServerAsyncClientTest extends BaseGleSYSAsyncClientTest>> { +public class ParseServerTemplatesTest extends BaseSetParserTest { @Override public String resource() { @@ -45,40 +48,29 @@ public class ParseServerTemplatesTest extends BaseItemParserTest> expected() { - Map> result = new LinkedHashMap>(); - - String[] vzNames = new String[]{ - "Centos 5", "Centos 5 64-bit", "Centos 6 32-bit", "Centos 6 64-bit", - "Debian 5.0 32-bit", "Debian 5.0 64-bit", "Debian 6.0 32-bit", "Debian 6.0 64-bit", - "Fedora Core 11", "Fedora Core 11 64-bit", "Gentoo", "Gentoo 64-bit", - "Scientific Linux 6", "Scientific Linux 6 64-bit", "Slackware 12", - "Ubuntu 10.04 LTS 32-bit", "Ubuntu 10.04 LTS 64-bit", "Ubuntu 11.04 64-bit" - }; - String[] xenLinuxNames = new String[] { - "CentOS 5.5 x64", "CentOS 5.5 x86", "Centos 6 x64", "Centos 6 x86", "Debian-6 x64", "Debian 5.0.1 x64", - "FreeBSD 8.2", "Gentoo 10.1 x64", "Ubuntu 8.04 x64", "Ubuntu 10.04 LTS 64-bit", "Ubuntu 10.10 x64", "Ubuntu 11.04 x64", - }; - String[] xenWindowsNames = new String[] { - "Windows Server 2008 R2 x64 std", "Windows Server 2008 R2 x64 web", "Windows Server 2008 x64 web", "Windows Server 2008 x86 web" - }; + public Set expected() { + Builder builder = ImmutableSet. builder(); - result.put("OpenVZ", new HashSet()); - for (String name : vzNames) { - result.get("OpenVZ").add(new ServerTemplate(name, 5, 128, "linux", "OpenVZ")); + for (String name : new String[] { "Centos 5", "Centos 5 64-bit", "Centos 6 32-bit", "Centos 6 64-bit", + "Debian 5.0 32-bit", "Debian 5.0 64-bit", "Debian 6.0 32-bit", "Debian 6.0 64-bit", "Fedora Core 11", + "Fedora Core 11 64-bit", "Gentoo", "Gentoo 64-bit", "Scientific Linux 6", "Scientific Linux 6 64-bit", + "Slackware 12", "Ubuntu 10.04 LTS 32-bit", "Ubuntu 10.04 LTS 64-bit", "Ubuntu 11.04 64-bit" }) { + builder.add(new ServerTemplate(name, 5, 128, "linux", "OpenVZ")); } - result.put("Xen", new HashSet()); - for (String name : xenLinuxNames) { - result.get("Xen").add(new ServerTemplate(name, 5, 512, name.startsWith("FreeBSD") ? "freebsd" : "linux", "Xen")); + for (String name : new String[] { "CentOS 5.5 x64", "CentOS 5.5 x86", "Centos 6 x64", "Centos 6 x86", + "Debian-6 x64", "Debian 5.0.1 x64", "FreeBSD 8.2", "Gentoo 10.1 x64", "Ubuntu 8.04 x64", + "Ubuntu 10.04 LTS 64-bit", "Ubuntu 10.10 x64", "Ubuntu 11.04 x64" }) { + builder.add(new ServerTemplate(name, 5, 512, name.startsWith("FreeBSD") ? "freebsd" : "linux", "Xen")); } - for (String name : xenWindowsNames) { - result.get("Xen").add(new ServerTemplate(name, 20, 1024, "windows", "Xen")); + for (String name : new String[] { "Windows Server 2008 R2 x64 std", "Windows Server 2008 R2 x64 web", + "Windows Server 2008 x64 web", "Windows Server 2008 x86 web" }) { + builder.add(new ServerTemplate(name, 20, 1024, "windows", "Xen")); } - - return result; + + return builder.build(); } protected Injector injector() {