cleaned return type for server templates

This commit is contained in:
Adrian Cole 2012-01-02 10:43:16 -08:00
parent fad1034427
commit b78824f7e0
6 changed files with 140 additions and 67 deletions

View File

@ -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 <a href= "https://customer.glesys.com/api.php?a=doc#server_templates" />
*/
public class ServerTemplate {
public class ServerTemplate implements Comparable<ServerTemplate>{
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);
}
}

View File

@ -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<Map<String, Set<ServerTemplate>>> getTemplates();
ListenableFuture<Set<ServerTemplate>> getTemplates();
/**
* @see ServerClient#stopServer

View File

@ -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<HttpResponse, Set<ServerTemplate>> {
private final ParseFirstJsonValueNamed<Map<String, Set<ServerTemplate>>> parser;
@Inject
public ParseServerTemplatesFromHttpResponse(GsonWrapper gsonWrapper) {
this.parser = new ParseFirstJsonValueNamed<Map<String, Set<ServerTemplate>>>(checkNotNull(gsonWrapper,
"gsonWrapper"), new TypeLiteral<Map<String, Set<ServerTemplate>>>() {
}, "templates");
}
public Set<ServerTemplate> apply(HttpResponse response) {
checkNotNull(response, "response");
Map<String, Set<ServerTemplate>> toParse = parser.apply(response);
checkNotNull(toParse, "parsed result from %s", response);
return ImmutableSet.copyOf(Iterables.concat(toParse.values()));
}
}

View File

@ -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<T> extends RestClientTest<T> {
protected Class asyncClientClass;
protected Class<T> asyncClientClass;
protected String remoteServicePrefix;
@Override
@ -74,7 +77,13 @@ public abstract class BaseGleSYSAsyncClientTest<T> extends RestClientTest<T> {
* @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<String> argStrings = new ArrayList<String>();
List<Object> argValues = new ArrayList<Object>();
@ -107,7 +116,7 @@ public abstract class BaseGleSYSAsyncClientTest<T> extends RestClientTest<T> {
if (expectResponse) {
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertResponseParserClassEquals(method, httpRequest, responseParser);
}
if (argStrings.isEmpty()) {

View File

@ -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<ServerAsync
}
public void testGetTemplates() throws Exception {
testMethod("getTemplates", "templates", "GET", true, MapHttp4xxCodesToExceptions.class);
testMethod("getTemplates", "templates", "GET", true, ParseServerTemplatesFromHttpResponse.class,
MapHttp4xxCodesToExceptions.class);
}
public void testGetServer() throws Exception {

View File

@ -18,26 +18,29 @@
*/
package org.jclouds.glesys.parse;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import java.util.*;
import org.jclouds.glesys.config.GleSYSParserModule;
import org.jclouds.glesys.domain.ServerTemplate;
import org.jclouds.glesys.functions.ParseServerTemplatesFromHttpResponse;
import org.jclouds.json.BaseSetParserTest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.rest.annotations.ResponseParser;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.jclouds.glesys.config.GleSYSParserModule;
import org.jclouds.glesys.domain.*;
import org.jclouds.json.BaseItemParserTest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.rest.annotations.SelectJson;
import org.testng.annotations.Test;
/**
* @author Adam Lowe
*/
@Test(groups = "unit", testName = "ParseServerTemplatesTest")
public class ParseServerTemplatesTest extends BaseItemParserTest<Map<String, Set<ServerTemplate>>> {
public class ParseServerTemplatesTest extends BaseSetParserTest<ServerTemplate> {
@Override
public String resource() {
@ -45,40 +48,29 @@ public class ParseServerTemplatesTest extends BaseItemParserTest<Map<String, Set
}
@Override
@SelectJson("templates")
@ResponseParser(ParseServerTemplatesFromHttpResponse.class)
@Consumes(MediaType.APPLICATION_JSON)
public Map<String, Set<ServerTemplate>> expected() {
Map<String, Set<ServerTemplate>> result = new LinkedHashMap<String, Set<ServerTemplate>>();
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<ServerTemplate> expected() {
Builder<ServerTemplate> builder = ImmutableSet.<ServerTemplate> builder();
result.put("OpenVZ", new HashSet<ServerTemplate>());
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<ServerTemplate>());
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() {