Converted Task parsing to JAXB

This commit is contained in:
Jason King 2011-11-11 17:07:51 +00:00
parent e6c0e5fe37
commit e7d7870267
7 changed files with 141 additions and 52 deletions

View File

@ -107,4 +107,8 @@ public class NamedResource extends BaseNamedResource<NamedResource> {
public NamedResource(URI href, String type, String name) { public NamedResource(URI href, String type, String name) {
super(href, type, name); super(href, type, name);
} }
protected NamedResource() {
//For JAXB
}
} }

View File

@ -29,32 +29,44 @@ import java.util.Map;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import org.jclouds.trmk.enterprisecloud.domain.internal.BaseResource; 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 * @author Adrian Cole
* *
*/ */
@XmlRootElement(name = "Task")
public class Task extends BaseResource<Task> { public class Task extends BaseResource<Task> {
public static enum Status { @XmlEnum
public static enum Status {
/** /**
* the task is queued for execution. * the task is queued for execution.
*/ */
@XmlEnumValue("Queued")
QUEUED, QUEUED,
/** /**
* the task is running. * the task is running.
*/ */
@XmlEnumValue("Running")
RUNNING, RUNNING,
/** /**
* the task failed. * the task failed.
*/ */
@XmlEnumValue("Failed")
FAILED, FAILED,
/** /**
* the task completed successfully. * the task completed successfully.
*/ */
@XmlEnumValue("Success")
SUCCESS, SUCCESS,
/** /**
* the task failed with an error. * the task failed with an error.
*/ */
@XmlEnumValue("Error")
ERROR, ERROR,
/** /**
* Status was not parsed by jclouds. * Status was not parsed by jclouds.
@ -212,14 +224,29 @@ public class Task extends BaseResource<Task> {
} }
protected final String operation; @XmlElement(name = "Operation", required = true)
protected final Status status; protected String operation;
protected final NamedResource impactedItem;
protected final Date startTime; @XmlElement(name = "Status", required = true)
protected final Date completedTime; protected Status status;
protected final String notes;
protected final String errorMessage; @XmlElement(name = "ImpactedItem", required = true)
protected final NamedResource initiatedBy; 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, 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) { @Nullable Date completedTime, @Nullable String notes, @Nullable String errorMessage, NamedResource initiatedBy) {
@ -234,6 +261,10 @@ public class Task extends BaseResource<Task> {
this.initiatedBy = checkNotNull(initiatedBy, "initiatedBy"); this.initiatedBy = checkNotNull(initiatedBy, "initiatedBy");
} }
protected Task() {
//For JAXB
}
/** /**
* *
* *

View File

@ -18,6 +18,8 @@
*/ */
package org.jclouds.trmk.enterprisecloud.domain.internal; package org.jclouds.trmk.enterprisecloud.domain.internal;
import javax.xml.bind.annotation.XmlAttribute;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI; import java.net.URI;
@ -77,13 +79,18 @@ public class BaseNamedResource<T extends BaseNamedResource<T>> extends BaseResou
} }
} }
protected final String name; @XmlAttribute
protected String name;
public BaseNamedResource(URI href, String type, String name) { public BaseNamedResource(URI href, String type, String name) {
super(href, type); super(href, type);
this.name = checkNotNull(name, "name"); this.name = checkNotNull(name, "name");
} }
protected BaseNamedResource() {
//For JAXB
}
public String getName() { public String getName() {
return name; return name;
} }

View File

@ -18,6 +18,8 @@
*/ */
package org.jclouds.trmk.enterprisecloud.domain.internal; package org.jclouds.trmk.enterprisecloud.domain.internal;
import javax.xml.bind.annotation.XmlAttribute;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI; import java.net.URI;
@ -74,14 +76,21 @@ public class BaseResource<T extends BaseResource<T>> {
} }
protected final String type; @XmlAttribute
protected final URI href; protected String type;
@XmlAttribute
protected URI href;
public BaseResource(URI href, String type) { public BaseResource(URI href, String type) {
this.type = checkNotNull(type, "type"); this.type = checkNotNull(type, "type");
this.href = checkNotNull(href, "href"); this.href = checkNotNull(href, "href");
} }
protected BaseResource() {
//For JAXB
}
/** /**
* *
* @return type definition, type, expressed as an HTTP Content-Type * @return type definition, type, expressed as an HTTP Content-Type

View File

@ -18,27 +18,20 @@
*/ */
package org.jclouds.trmk.enterprisecloud.features; package org.jclouds.trmk.enterprisecloud.features;
import java.net.URI; import com.google.common.util.concurrent.ListenableFuture;
import java.util.Set; 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.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import java.net.URI;
import org.jclouds.http.filters.BasicAuthentication; import java.util.Set;
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;
/** /**
* Provides asynchronous access to Task via their REST API. * Provides asynchronous access to Task via their REST API.
@ -69,7 +62,7 @@ public interface TaskAsyncClient {
*/ */
@GET @GET
@Consumes("application/vnd.tmrk.cloud.task") @Consumes("application/vnd.tmrk.cloud.task")
@XMLResponseParser(TaskHandler.class) @JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class) @ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Task> getTask(@EndpointParam URI taskId); ListenableFuture<Task> getTask(@EndpointParam URI taskId);

View File

@ -18,20 +18,19 @@
*/ */
package org.jclouds.trmk.enterprisecloud.features; package org.jclouds.trmk.enterprisecloud.features;
import java.io.IOException; import com.google.inject.TypeLiteral;
import java.lang.reflect.Method;
import java.net.URI;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.ParseXMLWithJAXB;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.trmk.enterprisecloud.xml.TaskHandler;
import org.jclouds.trmk.enterprisecloud.xml.TasksHandler; import org.jclouds.trmk.enterprisecloud.xml.TasksHandler;
import org.testng.annotations.Test; 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} * 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"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.task\nx-trmk-version: 2011-07-01\n");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseSax.class); assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
assertSaxResponseParserClassEquals(method, TaskHandler.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest); checkFilters(httpRequest);

View File

@ -18,21 +18,38 @@
*/ */
package org.jclouds.trmk.enterprisecloud.xml; package org.jclouds.trmk.enterprisecloud.xml;
import static org.testng.Assert.assertEquals; import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import java.io.InputStream; import com.google.inject.AbstractModule;
import java.net.URI; import com.google.inject.Module;
import com.google.inject.Provides;
import org.jclouds.crypto.Crypto;
import org.jclouds.date.internal.SimpleDateFormatDateService; 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;
import org.jclouds.http.functions.ParseSax.Factory; import org.jclouds.http.functions.ParseXMLWithJAXB;
import org.jclouds.http.functions.config.SaxParserModule; 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.NamedResource;
import org.jclouds.trmk.enterprisecloud.domain.Task; 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 org.testng.annotations.Test;
import com.google.inject.Guice; import javax.inject.Named;
import com.google.inject.Injector; 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} * Tests behavior of {@code TaskHandler}
@ -40,7 +57,7 @@ import com.google.inject.Injector;
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test(groups = "unit", testName = "TaskHandlerTest") @Test(groups = "unit", testName = "TaskHandlerTest")
public class TaskHandlerTest { public class TaskHandlerTest extends BaseRestClientTest {
static SimpleDateFormatDateService dateService = new SimpleDateFormatDateService(); static SimpleDateFormatDateService dateService = new SimpleDateFormatDateService();
static Task expected = Task static Task expected = Task
.builder() .builder()
@ -59,11 +76,41 @@ public class TaskHandlerTest {
NamedResource.builder().href(URI.create("/livespec/admin/users/1")).name("User 1") NamedResource.builder().href(URI.create("/livespec/admin/users/1")).name("User 1")
.type("application/vnd.tmrk.cloud.admin.user").build()).build(); .type("application/vnd.tmrk.cloud.admin.user").build()).build();
public void test() { @BeforeClass
void setupFactory() {
RestContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo",
"credentialFoo", String.class, Integer.class,
ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), new AbstractModule() {
@Override
protected void configure() {}
@SuppressWarnings("unused")
@Provides
@Named("exception")
Set<String> 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<HttpResponse, Task> parser = (Function<HttpResponse, Task>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request);
InputStream is = getClass().getResourceAsStream("/task.xml"); InputStream is = getClass().getResourceAsStream("/task.xml");
Injector injector = Guice.createInjector(new SaxParserModule()); Task task = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is)));
Factory factory = injector.getInstance(ParseSax.Factory.class); assertEquals(task, expected);
Task result = factory.create(injector.getInstance(TaskHandler.class)).parse(is);
assertEquals(result.toString(), expected.toString());
} }
} }