From 147b82411dd14bb412ddfbfb21a9a0b25f87af0b Mon Sep 17 00:00:00 2001 From: Jason King Date: Mon, 14 Nov 2011 16:33:08 +0000 Subject: [PATCH] Issue 695: Added Links,Tasks,Actions for VirtualMachine, name is optional in BaseNamedResource, renamed Task test --- .../trmk/enterprisecloud/domain/Action.java | 17 ++- .../trmk/enterprisecloud/domain/Actions.java | 52 +++++++++ .../trmk/enterprisecloud/domain/Link.java | 20 +++- .../trmk/enterprisecloud/domain/Links.java | 52 +++++++++ .../trmk/enterprisecloud/domain/Task.java | 5 + .../trmk/enterprisecloud/domain/Tasks.java | 9 +- .../domain/VirtualMachine.java | 61 ++++++---- .../domain/internal/BaseNamedResource.java | 5 +- .../features/VirtualMachineAsyncClient.java | 59 ++++++++++ .../features/VirtualMachineClient.java | 47 ++++++++ .../VirtualMachineAsyncClientTest.java | 59 ++++++++++ ...dlerTest.java => TaskJAXBParsingTest.java} | 2 +- .../xml/VirtualMachineJAXBParsingTest.java | 108 ++++++++++++++++++ 13 files changed, 466 insertions(+), 30 deletions(-) create mode 100644 sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Actions.java create mode 100644 sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Links.java create mode 100644 sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineAsyncClient.java create mode 100644 sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineClient.java create mode 100644 sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineAsyncClientTest.java rename sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/{TaskHandlerTest.java => TaskJAXBParsingTest.java} (99%) create mode 100644 sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/VirtualMachineJAXBParsingTest.java diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Action.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Action.java index 817ea55e1c..df959d8206 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Action.java +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Action.java @@ -29,18 +29,26 @@ import org.jclouds.javax.annotation.Nullable; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseNamedResource; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseResource; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlEnum; +import javax.xml.bind.annotation.XmlEnumValue; +import javax.xml.bind.annotation.XmlRootElement; + /** * * @author Adrian Cole * */ +@XmlRootElement(name = "Action") public class Action extends BaseNamedResource { - public static enum ActionDisabled { + @XmlEnum + public static enum ActionDisabled { /** * The actionDisabled attribute will have a value of noAccess when a user * does not have permission to perform the action. For example, for a user * with read-only access, all actions have actionDisabled="noAccess" set. */ + @XmlEnumValue("noAccess") NO_ACCESS, /** * The attribute will have a value of disabled when the action is contrary @@ -51,6 +59,7 @@ public class Action extends BaseNamedResource { * conditions apply, actions have actionDisabled="noAccess" set. If * neither condition applies, the attribute will not appear. */ + @XmlEnumValue("disabled") DISABLED, /** * ActionDisabled was not parsed by jclouds. @@ -162,13 +171,17 @@ public class Action extends BaseNamedResource { } - protected final ActionDisabled actionDisabled; + @XmlAttribute + protected ActionDisabled actionDisabled; public Action(URI href, String type, String name, @Nullable ActionDisabled actionDisabled) { super(href, type, name); this.actionDisabled = actionDisabled; } + protected Action() { + //For JAXB + } /** * The attribute actionDisabled appears only when the example has an action * disabled for business rules. diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Actions.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Actions.java new file mode 100644 index 0000000000..2641ebc48a --- /dev/null +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Actions.java @@ -0,0 +1,52 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.trmk.enterprisecloud.domain; + +import com.google.common.collect.Sets; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Wraps individual Action elements. + * Needed because parsing is done with JAXB and it does not handle Generic collections + * @author Jason King + */ +@XmlRootElement(name = "Actions") +public class Actions { + + private LinkedHashSet actions = Sets.newLinkedHashSet(); + + @XmlElement(name = "Action") + void setAction(Action action) { + this.actions.add(action); + } + + public Set getActions() { + return Collections.unmodifiableSet(actions); + } + + public String toString() { + return "["+ actions.toString()+"]"; + } + +} diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Link.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Link.java index 1c74cba6f7..32ea105ec4 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Link.java +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Link.java @@ -26,41 +26,52 @@ import java.util.Map; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseNamedResource; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseResource; +import javax.xml.bind.annotation.*; + /** * * @author Adrian Cole * */ +@XmlRootElement(name = "Link") public class Link extends BaseNamedResource { - public static enum Relationship { + @XmlEnum + public static enum Relationship { /** * The entity in the link owns the entity in the response */ + @XmlEnumValue("up") UP, /** * The entity in the response owns the entity in the link */ + @XmlEnumValue("down") DOWN, /** * The entity in the link is an alternate view of the entity in the * response */ + @XmlEnumValue("alternate") ALTERNATE, /** * The link is a path to the first page in the pages of responses */ + @XmlEnumValue("first") FIRST, /** * The link is a path to the previous page in the pages of responses */ + @XmlEnumValue("previous") PREVIOUS, /** * The link is a path to the next page in the pages of responses */ + @XmlEnumValue("next") NEXT, /** * The link is a path to the last page in the pages of responses */ + @XmlEnumValue("last") LAST, /** * Relationship was not parsed by jclouds. @@ -171,13 +182,18 @@ public class Link extends BaseNamedResource { } } - protected final Relationship rel; + @XmlAttribute + protected Relationship rel; public Link(URI href, String type, String name, Relationship rel) { super(href, type, name); this.rel = checkNotNull(rel, "rel"); } + protected Link() { + //For JAXB + } + /** * * @return diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Links.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Links.java new file mode 100644 index 0000000000..f1d71a2dfe --- /dev/null +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Links.java @@ -0,0 +1,52 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.trmk.enterprisecloud.domain; + +import com.google.common.collect.Sets; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Wraps individual Link elements. + * Needed because parsing is done with JAXB and it does not handle Generic collections + * @author Jason King + */ +@XmlRootElement(name = "Links") +public class Links { + + private LinkedHashSet links = Sets.newLinkedHashSet(); + + @XmlElement(name = "Link") + public void setLink(Link link) { + this.links.add(link); + } + + public Set getLinks() { + return Collections.unmodifiableSet(links); + } + + public String toString() { + return "["+ links.toString()+"]"; + } + +} 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 760f5c312f..ea266543bb 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 @@ -63,6 +63,11 @@ public class Task extends BaseResource { */ @XmlEnumValue("Success") SUCCESS, + /** + * the task completed successfully. + */ + @XmlEnumValue("Complete") + COMPLETE, /** * the task failed with an error. */ diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Tasks.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Tasks.java index 6fba554fa7..9ccdd0bed5 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Tasks.java +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/Tasks.java @@ -18,6 +18,8 @@ */ package org.jclouds.trmk.enterprisecloud.domain; +import com.google.common.collect.Sets; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.util.Collections; @@ -32,10 +34,10 @@ import java.util.Set; @XmlRootElement(name = "Tasks") public class Tasks { - private LinkedHashSet tasks = new LinkedHashSet(); + private LinkedHashSet tasks = Sets.newLinkedHashSet(); @XmlElement(name = "Task") - void setTask(Task task) { + public void setTask(Task task) { tasks.add(task); } @@ -43,4 +45,7 @@ public class Tasks { return Collections.unmodifiableSet(tasks); } + public String toString() { + return "["+tasks.toString()+"]"; + } } diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/VirtualMachine.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/VirtualMachine.java index 836e857e45..e38a98e3a1 100644 --- a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/VirtualMachine.java +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/domain/VirtualMachine.java @@ -21,9 +21,12 @@ package org.jclouds.trmk.enterprisecloud.domain; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseNamedResource; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseResource; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; import java.net.URI; -import java.util.List; +import java.util.Collections; import java.util.Map; +import java.util.Set; import static com.google.common.base.Preconditions.checkNotNull; @@ -32,6 +35,7 @@ import static com.google.common.base.Preconditions.checkNotNull; * @author Jason King * */ +@XmlRootElement(name = "VirtualMachine") public class VirtualMachine extends BaseNamedResource { @SuppressWarnings("unchecked") @@ -48,32 +52,35 @@ public class VirtualMachine extends BaseNamedResource { } public static class Builder extends BaseNamedResource.Builder { - private List links; - private List actions; - private List tasks; + private Links links; + private Actions actions; + private Tasks tasks; private String description; /** * @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getLinks */ - public Builder links(List links) { - this.links = links; + public Builder links(Set links) { + this.links = new Links(); + for(Link link:links) this.links.setLink(link); return this; } /** * @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getActions */ - public Builder actions(List actions) { - this.actions = actions; + public Builder actions(Set actions) { + this.actions = new Actions(); + for(Action action:actions) this.actions.setAction(action); return this; } /** * @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getTasks */ - public Builder tasks(List tasks) { - this.tasks = tasks; + public Builder tasks(Set tasks) { + this.tasks = new Tasks(); + for(Task task: tasks) this.tasks.setTask(task); return this; } @@ -150,12 +157,19 @@ public class VirtualMachine extends BaseNamedResource { } - private final List links; - private final List tasks; - private final List actions; - private final String description; + @XmlElement(name = "Links", required = true) + private Links links; - public VirtualMachine(URI href, String type, String name, List tasks, List actions, List links, String description) { + @XmlElement(name = "Tasks", required = true) + private Tasks tasks; + + @XmlElement(name = "Actions", required = true) + private Actions actions; + + @XmlElement(name = "Description", required = true) + private String description; + + public VirtualMachine(URI href, String type, String name, Tasks tasks, Actions actions, Links links, String description) { super(href, type, name); this.description = checkNotNull(description, "description"); this.links = checkNotNull(links, "links"); @@ -163,8 +177,13 @@ public class VirtualMachine extends BaseNamedResource { this.actions = checkNotNull(actions, "actions"); } - public List getLinks() { - return links; + protected VirtualMachine() { + //For JAXB + } + + + public Set getLinks() { + return Collections.unmodifiableSet(links.getLinks()); } /** @@ -173,12 +192,12 @@ public class VirtualMachine extends BaseNamedResource { * Use the href to retrieve the complete list of tasks. * @return most recent tasks */ - public List getTasks() { - return tasks; + public Set getTasks() { + return Collections.unmodifiableSet(tasks.getTasks()); } - public List getActions() { - return actions; + public Set getActions() { + return Collections.unmodifiableSet(actions.getActions()); } public String getDescription() { 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 5c3c5d14be..273666b5ca 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 @@ -103,7 +103,8 @@ public class BaseNamedResource> extends BaseResou BaseNamedResource that = (BaseNamedResource) o; - if (!name.equals(that.name)) return false; + if (name != null ? !name.equals(that.name) : that.name != null) + return false; return true; } @@ -111,7 +112,7 @@ public class BaseNamedResource> extends BaseResou @Override public int hashCode() { int result = super.hashCode(); - result = 31 * result + name.hashCode(); + result = 31 * result + (name != null ? name.hashCode() : 0); return result; } diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineAsyncClient.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineAsyncClient.java new file mode 100644 index 0000000000..4a3b720440 --- /dev/null +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineAsyncClient.java @@ -0,0 +1,59 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.trmk.enterprisecloud.features; + +import com.google.common.util.concurrent.ListenableFuture; +import org.jclouds.http.filters.BasicAuthentication; +import org.jclouds.rest.annotations.ExceptionParser; +import org.jclouds.rest.annotations.Headers; +import org.jclouds.rest.annotations.JAXBResponseParser; +import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; +import org.jclouds.trmk.enterprisecloud.domain.VirtualMachine; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +/** + * Provides asynchronous access to VirtualMachine via their REST API. + *

+ * + * @see org.jclouds.trmk.enterprisecloud.features.TaskClient + * @see + * @author Adrian Cole + */ +@RequestFilters(BasicAuthentication.class) +@Headers(keys = "x-trmk-version", values = "{jclouds.api-version}") +public interface VirtualMachineAsyncClient { + + /** + * @see org.jclouds.trmk.enterprisecloud.features.VirtualMachineClient#getVirtualMachine + */ + @GET + @Path("/virtualMachines/{id}") + @Consumes("application/vnd.tmrk.cloud.virtualMachine") + @JAXBResponseParser + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture getVirtualMachine(@PathParam("id") long id); + +} diff --git a/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineClient.java b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineClient.java new file mode 100644 index 0000000000..fd846db66f --- /dev/null +++ b/sandbox-providers/trmk-enterprisecloud/src/main/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineClient.java @@ -0,0 +1,47 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.trmk.enterprisecloud.features; + +import org.jclouds.concurrent.Timeout; +import org.jclouds.trmk.enterprisecloud.domain.VirtualMachine; + +import java.util.concurrent.TimeUnit; + +/** + * Provides synchronous access to VirtualMachine. + *

+ * + * @see org.jclouds.trmk.enterprisecloud.features.VirtualMachineAsyncClient + * @see + * @author Jason King + */ +@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS) +public interface VirtualMachineClient { + + /** + * The Get Virtual Machines by ID call returns information regarding a + * specified virtual machine defined in an environment. + * + * @return the virtual Machine or null if not found + */ + VirtualMachine getVirtualMachine(long id); + +} diff --git a/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineAsyncClientTest.java b/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineAsyncClientTest.java new file mode 100644 index 0000000000..5f41cf7b53 --- /dev/null +++ b/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/features/VirtualMachineAsyncClientTest.java @@ -0,0 +1,59 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.trmk.enterprisecloud.features; + +import com.google.inject.TypeLiteral; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.functions.ParseXMLWithJAXB; +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; + +/** + * Tests annotation parsing of {@code TaskAsyncClient} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "VirtualMachineAsyncClientTest") +public class VirtualMachineAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClientTest { + + public void testGetVirtualMachine() throws SecurityException, NoSuchMethodException, IOException { + Method method = VirtualMachineAsyncClient.class.getMethod("getVirtualMachine", long.class); + HttpRequest httpRequest = processor.createRequest(method, 1); + + assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/virtualMachines/1 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.virtualMachine\nx-trmk-version: 2011-07-01\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + @Override + protected TypeLiteral> createTypeLiteral() { + return new TypeLiteral>() { + }; + } +} 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/TaskJAXBParsingTest.java similarity index 99% rename from sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/TaskHandlerTest.java rename to sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/TaskJAXBParsingTest.java index 4065e5ac8c..5ee0610af3 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/TaskJAXBParsingTest.java @@ -60,7 +60,7 @@ import static org.testng.Assert.assertTrue; * @author Adrian Cole */ @Test(groups = "unit", testName = "TaskHandlerTest") -public class TaskHandlerTest extends BaseRestClientTest { +public class TaskJAXBParsingTest extends BaseRestClientTest { private SimpleDateFormatDateService dateService; private Task expected1; private Task expected2; diff --git a/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/VirtualMachineJAXBParsingTest.java b/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/VirtualMachineJAXBParsingTest.java new file mode 100644 index 0000000000..28ffca70e8 --- /dev/null +++ b/sandbox-providers/trmk-enterprisecloud/src/test/java/org/jclouds/trmk/enterprisecloud/xml/VirtualMachineJAXBParsingTest.java @@ -0,0 +1,108 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.trmk.enterprisecloud.xml; + +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.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.VirtualMachine; +import org.jclouds.trmk.enterprisecloud.features.VirtualMachineAsyncClient; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import javax.inject.Named; +import java.io.InputStream; +import java.lang.reflect.Method; +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.AssertJUnit.assertEquals; + +/** + * Tests behavior of JAXB parsing for VirtualMachines + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "VirtualMachineJAXBParsingTest") +public class VirtualMachineJAXBParsingTest extends BaseRestClientTest { + private SimpleDateFormatDateService dateService; + + @BeforeMethod + public void setUp() { + dateService = new SimpleDateFormatDateService(); + } + + @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 testParseVirtualMachineWithJAXB() throws Exception { + + Method method = VirtualMachineAsyncClient.class.getMethod("getVirtualMachine", long.class); + HttpRequest request = factory(VirtualMachineAsyncClient.class).createRequest(method,1); + assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class); + + Function parser = (Function) RestAnnotationProcessor + .createResponseParser(parserFactory, injector, method, request); + + InputStream is = getClass().getResourceAsStream("/virtualMachine.xml"); + VirtualMachine virtualMachine = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is))); + + assertEquals("My first terremark server",virtualMachine.getDescription()); + assertEquals(6,virtualMachine.getLinks().size()); + assertEquals(11,virtualMachine.getActions().size()); + assertEquals(1,virtualMachine.getTasks().size()); + } +}