mirror of https://github.com/apache/jclouds.git
Converted Task parsing to JAXB
This commit is contained in:
parent
e6c0e5fe37
commit
e7d7870267
|
@ -107,4 +107,8 @@ public class NamedResource extends BaseNamedResource<NamedResource> {
|
|||
public NamedResource(URI href, String type, String name) {
|
||||
super(href, type, name);
|
||||
}
|
||||
|
||||
protected NamedResource() {
|
||||
//For JAXB
|
||||
}
|
||||
}
|
|
@ -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<Task> {
|
||||
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<Task> {
|
|||
|
||||
}
|
||||
|
||||
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<Task> {
|
|||
this.initiatedBy = checkNotNull(initiatedBy, "initiatedBy");
|
||||
}
|
||||
|
||||
protected Task() {
|
||||
//For JAXB
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
|
|
|
@ -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<T extends BaseNamedResource<T>> 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;
|
||||
}
|
||||
|
|
|
@ -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<T extends BaseResource<T>> {
|
|||
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -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<Task> getTask(@EndpointParam URI taskId);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<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");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue