From 45450e1b8f1c48353fb65615f6bfec70dedf3099 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 19 Jul 2010 13:07:25 -0700 Subject: [PATCH] added unit test to rimu --- .../miro/RimuHostingAsyncClient.java | 14 ++-- .../rimuhosting/miro/RimuHostingClient.java | 52 ++++++++------ .../miro/RimuHostingAsyncClientTest.java | 67 +++++++++++++++++++ .../miro/RimuHostingClientLiveTest.java | 13 ++-- 4 files changed, 110 insertions(+), 36 deletions(-) create mode 100644 rimuhosting/src/test/java/org/jclouds/rimuhosting/miro/RimuHostingAsyncClientTest.java diff --git a/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/RimuHostingAsyncClient.java b/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/RimuHostingAsyncClient.java index 818feb2e30..772e84fd90 100644 --- a/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/RimuHostingAsyncClient.java +++ b/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/RimuHostingAsyncClient.java @@ -19,7 +19,7 @@ package org.jclouds.rimuhosting.miro; import java.util.List; -import java.util.SortedSet; +import java.util.Set; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -76,7 +76,7 @@ public interface RimuHostingAsyncClient { @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ExceptionParser(ParseRimuHostingException.class) - ListenableFuture> getImageList(); + ListenableFuture> getImageList(); /** * @see RimuHostingClient#getServerList @@ -88,7 +88,7 @@ public interface RimuHostingAsyncClient { @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ExceptionParser(ParseRimuHostingException.class) - ListenableFuture> getServerList(); + ListenableFuture> getServerList(); /** * @see RimuHostingClient#getPricingPlanList @@ -99,7 +99,7 @@ public interface RimuHostingAsyncClient { @Consumes(MediaType.APPLICATION_JSON) @ExceptionParser(ParseRimuHostingException.class) @ResponseParser(ParsePricingPlansFromJsonResponse.class) - ListenableFuture> getPricingPlanList(); + ListenableFuture> getPricingPlanList(); /** * @see RimuHostingClient#createServer @@ -111,10 +111,8 @@ public interface RimuHostingAsyncClient { @ExceptionParser(ParseRimuHostingException.class) @Unwrap @MapBinder(CreateServerOptions.class) - ListenableFuture createServer( - @MapPayloadParam("name") String name, - @MapPayloadParam("imageId") String imageId, - @MapPayloadParam("planId") String planId, + ListenableFuture createServer(@MapPayloadParam("name") String name, + @MapPayloadParam("imageId") String imageId, @MapPayloadParam("planId") String planId, CreateServerOptions... options); /** diff --git a/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/RimuHostingClient.java b/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/RimuHostingClient.java index 6c8c67ac5a..f049347c8f 100644 --- a/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/RimuHostingClient.java +++ b/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/RimuHostingClient.java @@ -18,19 +18,24 @@ */ package org.jclouds.rimuhosting.miro; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + import org.jclouds.concurrent.Timeout; import org.jclouds.rimuhosting.miro.binder.CreateServerOptions; -import org.jclouds.rimuhosting.miro.domain.*; - -import java.util.List; -import java.util.SortedSet; -import java.util.concurrent.TimeUnit; +import org.jclouds.rimuhosting.miro.domain.Image; +import org.jclouds.rimuhosting.miro.domain.MetaData; +import org.jclouds.rimuhosting.miro.domain.NewServerResponse; +import org.jclouds.rimuhosting.miro.domain.PricingPlan; +import org.jclouds.rimuhosting.miro.domain.Server; +import org.jclouds.rimuhosting.miro.domain.ServerInfo; /** * Provides synchronous access to RimuHosting. *

- * - * @author Ivan Meredith + * + * @author Ivan Meredith * @see RimuHostingAsyncClient * @see */ @@ -38,38 +43,42 @@ import java.util.concurrent.TimeUnit; public interface RimuHostingClient { /** - * This operation returns a list of images that can be used for server creation. - *c + * This operation returns a list of images that can be used for server + * creation. c + * * @see Image */ - SortedSet getImageList(); + Set getImageList(); /** - * Returns a list of servers that belong to this identity. + * Returns a list of servers that belong to this identity. * * @return An empty set if there are no servers. * @see Server */ - SortedSet getServerList(); + Set getServerList(); /** * Returns a list of pricing plans that can be used for server creation. + * * @see PricingPlan */ - SortedSet getPricingPlanList(); + Set getPricingPlanList(); /** * This operation creates a node based on its name, imageId and planId. * - * A password can be specified with the option {@link CreateServerOptions#withPassword(String) | withPassword()} + * A password can be specified with the option + * {@link CreateServerOptions#withPassword(String) | withPassword()} * - * Key-Value @{link {@link MetaData | metadata} can be included with the option {@link CreateServerOptions#withMetaData(List) | withMetaData()} + * Key-Value @{link {@link MetaData | metadata} can be included with the + * option {@link CreateServerOptions#withMetaData(List) | withMetaData()} * * @see CreateServerOptions * - * TODO: add more CreateServerOptions + * TODO: add more CreateServerOptions */ - NewServerResponse createServer(String name, String imageId, String planId, CreateServerOptions ... options); + NewServerResponse createServer(String name, String imageId, String planId, CreateServerOptions... options); /** * Gets a server based on its id. @@ -78,18 +87,19 @@ public interface RimuHostingClient { * @see Server */ Server getServer(Long id); - + /** * Restarts a server. * * @return State of the server. */ ServerInfo restartServer(Long id); - + /** * Destroys a server. This an async operation. - * - * @return A list of messages that have something to do with the shutdown. Can ignore safely. + * + * @return A list of messages that have something to do with the shutdown. + * Can ignore safely. */ List destroyServer(Long id); } diff --git a/rimuhosting/src/test/java/org/jclouds/rimuhosting/miro/RimuHostingAsyncClientTest.java b/rimuhosting/src/test/java/org/jclouds/rimuhosting/miro/RimuHostingAsyncClientTest.java new file mode 100644 index 0000000000..9cdb43be53 --- /dev/null +++ b/rimuhosting/src/test/java/org/jclouds/rimuhosting/miro/RimuHostingAsyncClientTest.java @@ -0,0 +1,67 @@ +package org.jclouds.rimuhosting.miro; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.Properties; + +import org.jclouds.http.HttpRequest; +import org.jclouds.http.functions.UnwrapOnlyJsonValue; +import org.jclouds.rest.RestClientTest; +import org.jclouds.rest.RestContextFactory; +import org.jclouds.rest.RestContextFactory.ContextSpec; +import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.jclouds.rest.internal.RestAnnotationProcessor; +import org.jclouds.rimuhosting.miro.binder.CreateServerOptions; +import org.jclouds.rimuhosting.miro.filters.RimuHostingAuthentication; +import org.jclouds.rimuhosting.miro.functions.ParseRimuHostingException; +import org.testng.annotations.Test; + +import com.google.inject.TypeLiteral; + +/** + * Tests annotation parsing of {@code RimuHostingAsyncClient} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "rimuhosting.RimuHostingAsyncClientTest") +public class RimuHostingAsyncClientTest extends RestClientTest { + + public void testCreateServer() throws SecurityException, NoSuchMethodException, IOException { + Method method = RimuHostingAsyncClient.class.getMethod("createServer", String.class, String.class, String.class, + CreateServerOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, "test.ivan.api.com", + "lenny", "MIRO1B"); + + assertRequestLineEquals(httpRequest, "POST https://rimuhosting.com/r/orders/new-vps HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals( + httpRequest, + "{\"request\":{\"instantiation_options\":{\"distro\":\"lenny\",\"domain_name\":\"test.ivan.api.com\"},\"pricing_plan_code\":\"MIRO1B\",\"meta_data\":[]}}", + "application/json", false); + assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyJsonValue.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ParseRimuHostingException.class); + + checkFilters(httpRequest); + + } + + @Override + protected void checkFilters(HttpRequest request) { + assertEquals(request.getFilters().size(), 1); + assertEquals(request.getFilters().get(0).getClass(), RimuHostingAuthentication.class); + } + + @Override + protected TypeLiteral> createTypeLiteral() { + return new TypeLiteral>() { + }; + } + + @Override + public ContextSpec createContextSpec() { + return new RestContextFactory().createContextSpec("rimuhosting", "apikey", "null", new Properties()); + } +} diff --git a/rimuhosting/src/test/java/org/jclouds/rimuhosting/miro/RimuHostingClientLiveTest.java b/rimuhosting/src/test/java/org/jclouds/rimuhosting/miro/RimuHostingClientLiveTest.java index 06ed933217..30714cd496 100644 --- a/rimuhosting/src/test/java/org/jclouds/rimuhosting/miro/RimuHostingClientLiveTest.java +++ b/rimuhosting/src/test/java/org/jclouds/rimuhosting/miro/RimuHostingClientLiveTest.java @@ -23,7 +23,7 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; -import java.util.SortedSet; +import java.util.Set; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.rest.RestContext; @@ -54,15 +54,15 @@ public class RimuHostingClientLiveTest { @BeforeGroups(groups = { "live" }) public void setupClient() { String credential = checkNotNull(System.getProperty("jclouds.test.credential"), "jclouds.test.credential"); - this.context = new RestContextFactory().createContext("rimuhosting", credential, credential, - ImmutableSet. of(new Log4JLoggingModule())); + this.context = new RestContextFactory().createContext("rimuhosting", credential, credential, ImmutableSet + . of(new Log4JLoggingModule())); this.connection = context.getApi(); } @Test public void testPricingPlans() { - SortedSet plans = connection.getPricingPlanList(); + Set plans = connection.getPricingPlanList(); for (PricingPlan plan : plans) { if (plan.getId().equalsIgnoreCase("miro1")) { assertTrue(true); @@ -74,7 +74,7 @@ public class RimuHostingClientLiveTest { @Test public void testImages() { - SortedSet images = connection.getImageList(); + Set images = connection.getImageList(); for (Image image : images) { if (image.getId().equalsIgnoreCase("lenny")) { assertTrue(true); @@ -87,8 +87,7 @@ public class RimuHostingClientLiveTest { @Test public void testLifeCycle() { // Get the first image, we dont really care what it is in this test. - NewServerResponse serverResponse = connection.createServer("test.ivan.api.com", "lenny", - "MIRO1B"); + NewServerResponse serverResponse = connection.createServer("test.ivan.api.com", "lenny", "MIRO1B"); Server server = serverResponse.getServer(); // Now we have the server, lets restart it assertNotNull(server.getId());