From e7d787026791a45bd29f35d16a8f7d5c6bbcc6c0 Mon Sep 17 00:00:00 2001 From: Jason King Date: Fri, 11 Nov 2011 17:07:51 +0000 Subject: [PATCH] Converted Task parsing to JAXB --- .../enterprisecloud/domain/NamedResource.java | 4 + .../trmk/enterprisecloud/domain/Task.java | 49 +++++++++--- .../domain/internal/BaseNamedResource.java | 9 ++- .../domain/internal/BaseResource.java | 13 +++- .../features/TaskAsyncClient.java | 27 +++---- .../features/TaskAsyncClientTest.java | 14 ++-- .../enterprisecloud/xml/TaskHandlerTest.java | 77 +++++++++++++++---- 7 files changed, 141 insertions(+), 52 deletions(-) diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/NamedResource.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/NamedResource.java index 4fb56cc452..83b7aa8a25 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/NamedResource.java +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/NamedResource.java @@ -107,4 +107,8 @@ public class NamedResource extends BaseNamedResource { public NamedResource(URI href, String type, String name) { super(href, type, name); } + + protected NamedResource() { + //For JAXB + } } \ No newline at end of file diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Task.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Task.java index 3ddf490d26..760f5c312f 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Task.java +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Task.java @@ -29,32 +29,44 @@ import java.util.Map; import org.jclouds.javax.annotation.Nullable; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseResource; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlEnum; +import javax.xml.bind.annotation.XmlEnumValue; +import javax.xml.bind.annotation.XmlRootElement; + /** * * @author Adrian Cole * */ +@XmlRootElement(name = "Task") public class Task extends BaseResource { - public static enum Status { + @XmlEnum + public static enum Status { /** * the task is queued for execution. */ + @XmlEnumValue("Queued") QUEUED, /** * the task is running. */ + @XmlEnumValue("Running") RUNNING, /** * the task failed. */ + @XmlEnumValue("Failed") FAILED, /** * the task completed successfully. */ + @XmlEnumValue("Success") SUCCESS, /** * the task failed with an error. */ + @XmlEnumValue("Error") ERROR, /** * Status was not parsed by jclouds. @@ -212,14 +224,29 @@ public class Task extends BaseResource { } - protected final String operation; - protected final Status status; - protected final NamedResource impactedItem; - protected final Date startTime; - protected final Date completedTime; - protected final String notes; - protected final String errorMessage; - protected final NamedResource initiatedBy; + @XmlElement(name = "Operation", required = true) + protected String operation; + + @XmlElement(name = "Status", required = true) + protected Status status; + + @XmlElement(name = "ImpactedItem", required = true) + protected NamedResource impactedItem; + + @XmlElement(name = "StartTime", required = true) + protected Date startTime; + + @XmlElement(name = "CompletedTime", required = false) + protected Date completedTime; + + @XmlElement(name = "Notes", required = false) + protected String notes; + + @XmlElement(name = "ErrorMessage", required = false) + protected String errorMessage; + + @XmlElement(name = "InitiatedBy", required = true) + protected NamedResource initiatedBy; public Task(URI href, String type, String operation, Status status, NamedResource impactedItem, Date startTime, @Nullable Date completedTime, @Nullable String notes, @Nullable String errorMessage, NamedResource initiatedBy) { @@ -234,6 +261,10 @@ public class Task extends BaseResource { this.initiatedBy = checkNotNull(initiatedBy, "initiatedBy"); } + protected Task() { + //For JAXB + } + /** * * diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/internal/BaseNamedResource.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/internal/BaseNamedResource.java index bd84c0fbe5..5c3c5d14be 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/internal/BaseNamedResource.java +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/internal/BaseNamedResource.java @@ -18,6 +18,8 @@ */ package org.jclouds.trmk.enterprisecloud.domain.internal; +import javax.xml.bind.annotation.XmlAttribute; + import static com.google.common.base.Preconditions.checkNotNull; import java.net.URI; @@ -77,13 +79,18 @@ public class BaseNamedResource> extends BaseResou } } - protected final String name; + @XmlAttribute + protected String name; public BaseNamedResource(URI href, String type, String name) { super(href, type); this.name = checkNotNull(name, "name"); } + protected BaseNamedResource() { + //For JAXB + } + public String getName() { return name; } diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/internal/BaseResource.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/internal/BaseResource.java index 2e8e1b841b..284583728c 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/internal/BaseResource.java +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/internal/BaseResource.java @@ -18,6 +18,8 @@ */ package org.jclouds.trmk.enterprisecloud.domain.internal; +import javax.xml.bind.annotation.XmlAttribute; + import static com.google.common.base.Preconditions.checkNotNull; import java.net.URI; @@ -74,14 +76,21 @@ public class BaseResource> { } - protected final String type; - protected final URI href; + @XmlAttribute + protected String type; + + @XmlAttribute + protected URI href; public BaseResource(URI href, String type) { this.type = checkNotNull(type, "type"); this.href = checkNotNull(href, "href"); } + protected BaseResource() { + //For JAXB + } + /** * * @return type definition, type, expressed as an HTTP Content-Type diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/TaskAsyncClient.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/TaskAsyncClient.java index 7053820e67..7615f95c1d 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/TaskAsyncClient.java +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/TaskAsyncClient.java @@ -18,27 +18,20 @@ */ package org.jclouds.trmk.enterprisecloud.features; -import java.net.URI; -import java.util.Set; +import com.google.common.util.concurrent.ListenableFuture; +import org.jclouds.http.filters.BasicAuthentication; +import org.jclouds.rest.annotations.*; +import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; +import org.jclouds.trmk.enterprisecloud.domain.Task; +import org.jclouds.trmk.enterprisecloud.xml.TasksHandler; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; - -import org.jclouds.http.filters.BasicAuthentication; -import org.jclouds.rest.annotations.EndpointParam; -import org.jclouds.rest.annotations.ExceptionParser; -import org.jclouds.rest.annotations.Headers; -import org.jclouds.rest.annotations.RequestFilters; -import org.jclouds.rest.annotations.XMLResponseParser; -import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; -import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; -import org.jclouds.trmk.enterprisecloud.domain.Task; -import org.jclouds.trmk.enterprisecloud.xml.TaskHandler; -import org.jclouds.trmk.enterprisecloud.xml.TasksHandler; - -import com.google.common.util.concurrent.ListenableFuture; +import java.net.URI; +import java.util.Set; /** * Provides asynchronous access to Task via their REST API. @@ -69,7 +62,7 @@ public interface TaskAsyncClient { */ @GET @Consumes("application/vnd.tmrk.cloud.task") - @XMLResponseParser(TaskHandler.class) + @JAXBResponseParser @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture getTask(@EndpointParam URI taskId); diff --git a/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/features/TaskAsyncClientTest.java b/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/features/TaskAsyncClientTest.java index b3779ace15..ce31f8add0 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/features/TaskAsyncClientTest.java +++ b/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/features/TaskAsyncClientTest.java @@ -18,20 +18,19 @@ */ package org.jclouds.trmk.enterprisecloud.features; -import java.io.IOException; -import java.lang.reflect.Method; -import java.net.URI; - +import com.google.inject.TypeLiteral; import org.jclouds.http.HttpRequest; import org.jclouds.http.functions.ParseSax; +import org.jclouds.http.functions.ParseXMLWithJAXB; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.internal.RestAnnotationProcessor; -import org.jclouds.trmk.enterprisecloud.xml.TaskHandler; import org.jclouds.trmk.enterprisecloud.xml.TasksHandler; import org.testng.annotations.Test; -import com.google.inject.TypeLiteral; +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.URI; /** * Tests annotation parsing of {@code TaskAsyncClient} @@ -66,8 +65,7 @@ public class TaskAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClient assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.task\nx-trmk-version: 2011-07-01\n"); assertPayloadEquals(httpRequest, null, null, false); - assertResponseParserClassEquals(method, httpRequest, ParseSax.class); - assertSaxResponseParserClassEquals(method, TaskHandler.class); + assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class); assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); checkFilters(httpRequest); diff --git a/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/TaskHandlerTest.java b/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/TaskHandlerTest.java index f628bb4720..795a10d8e3 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/TaskHandlerTest.java +++ b/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/TaskHandlerTest.java @@ -18,21 +18,38 @@ */ package org.jclouds.trmk.enterprisecloud.xml; -import static org.testng.Assert.assertEquals; - -import java.io.InputStream; -import java.net.URI; - +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; +import com.google.inject.AbstractModule; +import com.google.inject.Module; +import com.google.inject.Provides; +import org.jclouds.crypto.Crypto; import org.jclouds.date.internal.SimpleDateFormatDateService; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; import org.jclouds.http.functions.ParseSax; -import org.jclouds.http.functions.ParseSax.Factory; -import org.jclouds.http.functions.config.SaxParserModule; +import org.jclouds.http.functions.ParseXMLWithJAXB; +import org.jclouds.logging.config.NullLoggingModule; +import org.jclouds.rest.AuthorizationException; +import org.jclouds.rest.BaseRestClientTest; +import org.jclouds.rest.RestContextSpec; +import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.trmk.enterprisecloud.domain.NamedResource; import org.jclouds.trmk.enterprisecloud.domain.Task; +import org.jclouds.trmk.enterprisecloud.features.TaskAsyncClient; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import com.google.inject.Guice; -import com.google.inject.Injector; +import javax.inject.Named; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.net.URI; +import java.util.Set; + +import static org.jclouds.io.Payloads.newInputStreamPayload; +import static org.jclouds.rest.RestContextFactory.contextSpec; +import static org.jclouds.rest.RestContextFactory.createContextBuilder; +import static org.testng.Assert.assertEquals; /** * Tests behavior of {@code TaskHandler} @@ -40,7 +57,7 @@ import com.google.inject.Injector; * @author Adrian Cole */ @Test(groups = "unit", testName = "TaskHandlerTest") -public class TaskHandlerTest { +public class TaskHandlerTest extends BaseRestClientTest { static SimpleDateFormatDateService dateService = new SimpleDateFormatDateService(); static Task expected = Task .builder() @@ -59,11 +76,41 @@ public class TaskHandlerTest { NamedResource.builder().href(URI.create("/livespec/admin/users/1")).name("User 1") .type("application/vnd.tmrk.cloud.admin.user").build()).build(); - public void test() { + @BeforeClass + void setupFactory() { + RestContextSpec contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo", + "credentialFoo", String.class, Integer.class, + ImmutableSet. of(new MockModule(), new NullLoggingModule(), new AbstractModule() { + + @Override + protected void configure() {} + + @SuppressWarnings("unused") + @Provides + @Named("exception") + Set exception() { + throw new AuthorizationException(); + } + + })); + + injector = createContextBuilder(contextSpec).buildInjector(); + parserFactory = injector.getInstance(ParseSax.Factory.class); + crypto = injector.getInstance(Crypto.class); + } + + @Test + public void testParseTaskWithJAXB() throws Exception { + + Method method = TaskAsyncClient.class.getMethod("getTask",URI.class); + HttpRequest request = factory(TaskAsyncClient.class).createRequest(method); + assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class); + + Function parser = (Function) RestAnnotationProcessor + .createResponseParser(parserFactory, injector, method, request); + InputStream is = getClass().getResourceAsStream("/task.xml"); - Injector injector = Guice.createInjector(new SaxParserModule()); - Factory factory = injector.getInstance(ParseSax.Factory.class); - Task result = factory.create(injector.getInstance(TaskHandler.class)).parse(is); - assertEquals(result.toString(), expected.toString()); + Task task = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is))); + assertEquals(task, expected); } }