Issue 695: Added Links,Tasks,Actions for VirtualMachine, name is optional in BaseNamedResource, renamed Task test

This commit is contained in:
Jason King 2011-11-14 16:33:08 +00:00
parent ea349810ef
commit 147b82411d
13 changed files with 466 additions and 30 deletions

View File

@ -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<Action> {
@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<Action> {
* 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<Action> {
}
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.

View File

@ -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<Action> actions = Sets.newLinkedHashSet();
@XmlElement(name = "Action")
void setAction(Action action) {
this.actions.add(action);
}
public Set<Action> getActions() {
return Collections.unmodifiableSet(actions);
}
public String toString() {
return "["+ actions.toString()+"]";
}
}

View File

@ -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<Link> {
@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<Link> {
}
}
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

View File

@ -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<Link> links = Sets.newLinkedHashSet();
@XmlElement(name = "Link")
public void setLink(Link link) {
this.links.add(link);
}
public Set<Link> getLinks() {
return Collections.unmodifiableSet(links);
}
public String toString() {
return "["+ links.toString()+"]";
}
}

View File

@ -63,6 +63,11 @@ public class Task extends BaseResource<Task> {
*/
@XmlEnumValue("Success")
SUCCESS,
/**
* the task completed successfully.
*/
@XmlEnumValue("Complete")
COMPLETE,
/**
* the task failed with an error.
*/

View File

@ -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<Task> tasks = new LinkedHashSet<Task>();
private LinkedHashSet<Task> 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()+"]";
}
}

View File

@ -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<VirtualMachine> {
@SuppressWarnings("unchecked")
@ -48,32 +52,35 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
}
public static class Builder extends BaseNamedResource.Builder<VirtualMachine> {
private List<Link> links;
private List<Action> actions;
private List<Task> 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<Link> links) {
this.links = links;
public Builder links(Set<Link> 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<Action> actions) {
this.actions = actions;
public Builder actions(Set<Action> 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<Task> tasks) {
this.tasks = tasks;
public Builder tasks(Set<Task> tasks) {
this.tasks = new Tasks();
for(Task task: tasks) this.tasks.setTask(task);
return this;
}
@ -150,12 +157,19 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
}
private final List<Link> links;
private final List<Task> tasks;
private final List<Action> actions;
private final String description;
@XmlElement(name = "Links", required = true)
private Links links;
public VirtualMachine(URI href, String type, String name, List<Task> tasks, List<Action> actions, List<Link> 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<VirtualMachine> {
this.actions = checkNotNull(actions, "actions");
}
public List<Link> getLinks() {
return links;
protected VirtualMachine() {
//For JAXB
}
public Set<Link> getLinks() {
return Collections.unmodifiableSet(links.getLinks());
}
/**
@ -173,12 +192,12 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
* Use the href to retrieve the complete list of tasks.
* @return most recent tasks
*/
public List<Task> getTasks() {
return tasks;
public Set<Task> getTasks() {
return Collections.unmodifiableSet(tasks.getTasks());
}
public List<Action> getActions() {
return actions;
public Set<Action> getActions() {
return Collections.unmodifiableSet(actions.getActions());
}
public String getDescription() {

View File

@ -103,7 +103,8 @@ public class BaseNamedResource<T extends BaseNamedResource<T>> 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<T extends BaseNamedResource<T>> 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;
}

View File

@ -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.
* <p/>
*
* @see org.jclouds.trmk.enterprisecloud.features.TaskClient
* @see <a href=
* "http://support.theenterprisecloud.com/kb/default.asp?id=984&Lang=1&SID="
* />
* @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<VirtualMachine> getVirtualMachine(@PathParam("id") long id);
}

View File

@ -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.
* <p/>
*
* @see org.jclouds.trmk.enterprisecloud.features.VirtualMachineAsyncClient
* @see <a href=
* "http://support.theenterprisecloud.com/kb/default.asp?id=984&Lang=1&SID="
* />
* @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);
}

View File

@ -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<VirtualMachineAsyncClient> {
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<RestAnnotationProcessor<VirtualMachineAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<VirtualMachineAsyncClient>>() {
};
}
}

View File

@ -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;

View File

@ -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<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 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<HttpResponse, VirtualMachine> parser = (Function<HttpResponse, VirtualMachine>) 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());
}
}