Issue 695: Added Location+Service call+tests

This commit is contained in:
Jason King 2011-12-05 14:24:10 +00:00
parent da56c047a1
commit d8ef0a6d4f
8 changed files with 454 additions and 3 deletions

View File

@ -19,9 +19,7 @@
package org.jclouds.tmrk.enterprisecloud;
import org.jclouds.rest.annotations.Delegate;
import org.jclouds.tmrk.enterprisecloud.features.TaskAsyncClient;
import org.jclouds.tmrk.enterprisecloud.features.TemplateAsyncClient;
import org.jclouds.tmrk.enterprisecloud.features.VirtualMachineAsyncClient;
import org.jclouds.tmrk.enterprisecloud.features.*;
/**
* Provides asynchronous access to TerremarkEnterpriseCloud via their REST API.
@ -35,6 +33,12 @@ import org.jclouds.tmrk.enterprisecloud.features.VirtualMachineAsyncClient;
*/
public interface TerremarkEnterpriseCloudAsyncClient {
/**
* Provides asynchronous access to Location features.
*/
@Delegate
LocationAsyncClient getLocationClient();
/**
* Provides asynchronous access to Task features.
*/

View File

@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout;
import org.jclouds.rest.annotations.Delegate;
import org.jclouds.tmrk.enterprisecloud.features.LocationClient;
import org.jclouds.tmrk.enterprisecloud.features.TaskClient;
import org.jclouds.tmrk.enterprisecloud.features.TemplateClient;
import org.jclouds.tmrk.enterprisecloud.features.VirtualMachineClient;
@ -39,6 +40,12 @@ import org.jclouds.tmrk.enterprisecloud.features.VirtualMachineClient;
@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS)
public interface TerremarkEnterpriseCloudClient {
/**
* Provides synchronous access to Location features.
*/
@Delegate
LocationClient getLocationClient();
/**
* Provides synchronous access to Task features.
*/

View File

@ -47,6 +47,7 @@ public class TerremarkEnterpriseCloudRestClientModule extends
RestClientModule<TerremarkEnterpriseCloudClient, TerremarkEnterpriseCloudAsyncClient> {
public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()
.put(LocationClient.class, LocationAsyncClient.class)
.put(TaskClient.class, TaskAsyncClient.class)
.put(VirtualMachineClient.class, VirtualMachineAsyncClient.class)
.put(TemplateClient.class, TemplateAsyncClient.class)

View File

@ -0,0 +1,221 @@
/**
* 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.tmrk.enterprisecloud.domain;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.tmrk.enterprisecloud.domain.internal.BaseResource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.Resource;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.net.URI;
import java.util.Map;
import java.util.Set;
/**
* <xs:complexType name="Location">
* @author Jason King
*
*/
@XmlRootElement(name = "Location")
public class Location extends Resource<Location> {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
/**
* {@inheritDoc}
*/
@Override
public Builder toBuilder() {
return new Builder().fromTask(this);
}
public static class Builder extends Resource.Builder<Location> {
private String friendlyName;
private String locode;
private String iso3166;
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.Location#getFriendlyName
*/
public Builder friendlyName(String friendlyName) {
this.friendlyName = friendlyName;
return this;
}
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.Location#getLocode
*/
public Builder locode(String locode) {
this.locode = locode;
return this;
}
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.Location#getIso3166
*/
public Builder iso3166(String iso3166) {
this.iso3166 = iso3166;
return this;
}
@Override
public Location build() {
return new Location(href, type, name, links,
actions, friendlyName, locode, iso3166);
}
public Builder fromTask(Location in) {
return fromResource(in).friendlyName(in.getFriendlyName()).locode(in.getLocode()).iso3166(in.getIso3166());
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromBaseResource(BaseResource<Location> in) {
return Builder.class.cast(super.fromBaseResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(Resource<Location> in) {
return Builder.class.cast(super.fromResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder links(Set<Link> links) {
return Builder.class.cast(super.links(links));
}
/**
* {@inheritDoc}
*/
@Override
public Builder actions(Set<Action> actions) {
return Builder.class.cast(super.actions(actions));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes));
}
}
@XmlElement(name = "FriendlyName", required = true)
private String friendlyName;
@XmlElement(name = "Locode", required = false)
private String locode;
@XmlElement(name = "ISO3166", required = false)
private String iso3166;
private Location(URI href, String type, String name, Set<Link> links, Set<Action> actions, @Nullable String friendlyName, @Nullable String locode, @Nullable String iso3166) {
super(href, type, name, links, actions);
this.friendlyName = friendlyName;
this.locode = locode;
this.iso3166 = iso3166;
}
private Location() {
//For JAXB
}
public String getFriendlyName() {
return friendlyName;
}
public String getLocode() {
return locode;
}
public String getIso3166() {
return iso3166;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Location location = (Location) o;
if (friendlyName != null ? !friendlyName.equals(location.friendlyName) : location.friendlyName != null)
return false;
if (iso3166 != null ? !iso3166.equals(location.iso3166) : location.iso3166 != null)
return false;
if (locode != null ? !locode.equals(location.locode) : location.locode != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (friendlyName != null ? friendlyName.hashCode() : 0);
result = 31 * result + (locode != null ? locode.hashCode() : 0);
result = 31 * result + (iso3166 != null ? iso3166.hashCode() : 0);
return result;
}
@Override
public String string() {
return super.string()+", friendlyName="+friendlyName+", locode="+locode+", iso3166="+iso3166;
}
}

View File

@ -0,0 +1,54 @@
/**
* 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.tmrk.enterprisecloud.features;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.http.filters.BasicAuthentication;
import org.jclouds.rest.annotations.*;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.tmrk.enterprisecloud.domain.Location;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import java.net.URI;
/**
* Provides asynchronous access to Locations via their REST API.
* <p/>
*
* @see org.jclouds.tmrk.enterprisecloud.features.LocationClient
* @see <a href=
* "http://support.theenterprisecloud.com/kb/default.asp?id=984&Lang=1&SID="
* />
* @author Jason King
*/
@RequestFilters(BasicAuthentication.class)
@Headers(keys = "x-tmrk-version", values = "{jclouds.api-version}")
public interface LocationAsyncClient {
/**
* @see org.jclouds.tmrk.enterprisecloud.features.LocationClient#getLocationById
*/
@GET
@Consumes("application/vnd.tmrk.cloud.location")
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Location> getLocationById(@EndpointParam URI uri);
}

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.tmrk.enterprisecloud.features;
import org.jclouds.concurrent.Timeout;
import org.jclouds.tmrk.enterprisecloud.domain.Location;
import java.net.URI;
import java.util.concurrent.TimeUnit;
/**
* Provides synchronous access to Location.
* <p/>
*
* @see org.jclouds.tmrk.enterprisecloud.features.LocationAsyncClient
* @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 LocationClient {
/**
* The Get Locations by ID call returns information regarding a single data center location.
* @param uri the uri of the location
* @return the location
*/
Location getLocationById(URI uri);
}

View File

@ -0,0 +1,62 @@
/**
* 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.tmrk.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;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Tests annotation parsing of {@code LocationAsyncClient}
*
* @author Jason King
*/
@Test(groups = "unit", testName = "LocationAsyncClientTest")
public class LocationAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClientTest<LocationAsyncClient> {
public void testGetLocationById() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException {
Method method = LocationAsyncClient.class.getMethod("getLocationById", URI.class);
HttpRequest httpRequest = processor.createRequest(method, new URI("/cloudapi/ecloud/locations/1"));
assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/locations/1 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest,
"Accept: application/vnd.tmrk.cloud.location\nx-tmrk-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<LocationAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<LocationAsyncClient>>() {
};
}
}

View File

@ -0,0 +1,55 @@
/**
* 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.tmrk.enterprisecloud.features;
import org.jclouds.tmrk.enterprisecloud.domain.Location;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import java.net.URI;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
/**
* Tests behavior of {@code LocationClient}
*
* @author Jason King
*/
@Test(groups = "live", testName = "LocationClientLiveTest")
public class LocationClientLiveTest extends BaseTerremarkEnterpriseCloudClientLiveTest {
@BeforeGroups(groups = { "live" })
public void setupClient() {
super.setupClient();
client = context.getApi().getLocationClient();
}
private LocationClient client;
public void testGetLocationById() throws Exception {
Location location = client.getLocationById(URI.create("/cloudapi/ecloud/locations/1"));
Location expected = Location.builder().href(URI.create("/cloudapi/ecloud/locations/1")).name("Terremark - Richardson").type("application/vnd.tmrk.cloud.location")
.friendlyName("Terremark - Richardson").locode("DAC").iso3166("US-TX").build();
assertEquals(location,expected);
}
public void testMissingLocation() {
assertNull(client.getLocationById(URI.create("/cloudapi/ecloud/locations/-1")));
}
}