Merge branch 'master' of github.com:jclouds/jclouds

This commit is contained in:
andreaturli 2011-10-15 01:00:42 +01:00
commit 362d79d879
12 changed files with 468 additions and 227 deletions

View File

@ -133,7 +133,7 @@ public class AMIClientLiveTest {
} }
public void testDescribeImagesCC() { public void testDescribeImagesCC() {
Set<? extends Image> twoResults = client.describeImagesInRegion(Region.US_EAST_1, filters( Set<? extends Image> ccResults = client.describeImagesInRegion(Region.US_EAST_1, filters(
ImmutableMultimap.<String, String> builder()// ImmutableMultimap.<String, String> builder()//
.put("virtualization-type", "hvm")// .put("virtualization-type", "hvm")//
.put("architecture", "x86_64")// .put("architecture", "x86_64")//
@ -143,8 +143,8 @@ public class AMIClientLiveTest {
.put("image-type", "machine")// .put("image-type", "machine")//
.put("root-device-type", "ebs")// .put("root-device-type", "ebs")//
.build()).ownedBy("137112412989", "099720109477")); .build()).ownedBy("137112412989", "099720109477"));
assertNotNull(twoResults); assertNotNull(ccResults);
assertEquals(twoResults.size(), 35); assert (ccResults.size() >= 34) : ccResults;
} }
@Test(enabled = false) @Test(enabled = false)

View File

@ -25,70 +25,74 @@ import com.google.common.primitives.Longs;
*/ */
public class BillingToken implements Comparable<BillingToken> { public class BillingToken implements Comparable<BillingToken> {
private long id; private long id;
private String name; private String name;
private double price; private double price;
/** /**
* A no-args constructor is required for deserialization * A no-args constructor is required for deserialization
*/ */
public BillingToken() { public BillingToken() {
} }
public BillingToken(long id, String name, double price) { public BillingToken(long id, String name, double price) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.price = price; this.price = price;
} }
public long getId() { public long getId() {
return id; return id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public double getPrice() { public double getPrice() {
return price; return price;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object obj) {
if (this == o) return true; if (this == obj)
if (o == null || getClass() != o.getClass()) return false; return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BillingToken other = (BillingToken) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
return true;
}
BillingToken that = (BillingToken) o; @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
if (id != that.id) return false; @Override
if (Double.compare(that.price, price) != 0) return false; public int compareTo(BillingToken o) {
if (!name.equals(that.name)) return false; return Longs.compare(id, o.getId());
}
return true; @Override
} public String toString() {
return "BillingToken{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + '}';
@Override }
public int hashCode() {
int result;
long temp;
result = (int) (id ^ (id >>> 32));
result = 31 * result + name.hashCode();
temp = price != +0.0d ? Double.doubleToLongBits(price) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public int compareTo(BillingToken o) {
return Longs.compare(id, o.getId());
}
@Override
public String toString() {
return "BillingToken{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
} }

View File

@ -36,29 +36,25 @@ import org.jclouds.http.options.BaseHttpRequestOptions;
public class GetImageListOptions extends BaseHttpRequestOptions { public class GetImageListOptions extends BaseHttpRequestOptions {
public GetImageListOptions setType(ServerImageType imageType) { public GetImageListOptions setType(ServerImageType imageType) {
checkState(!queryParameters.containsKey(IMAGE_TYPE_KEY), checkState(!queryParameters.containsKey(IMAGE_TYPE_KEY), "Can't have duplicate image type restrictions");
"Can't have duplicate image type restrictions");
queryParameters.put(IMAGE_TYPE_KEY, imageType.toString()); queryParameters.put(IMAGE_TYPE_KEY, imageType.toString());
return this; return this;
} }
public GetImageListOptions setState(ServerImageState imageState) { public GetImageListOptions setState(ServerImageState imageState) {
checkState(!queryParameters.containsKey(IMAGE_STATE_KEY), checkState(!queryParameters.containsKey(IMAGE_STATE_KEY), "Can't have duplicate image state restrictions");
"Can't have duplicate image state restrictions");
queryParameters.put(IMAGE_STATE_KEY, imageState.toString()); queryParameters.put(IMAGE_STATE_KEY, imageState.toString());
return this; return this;
} }
public GetImageListOptions onlyPublic() { public GetImageListOptions onlyPublic() {
checkState(!queryParameters.containsKey(IS_PUBLIC_KEY), checkState(!queryParameters.containsKey(IS_PUBLIC_KEY), "Can't have duplicate image visibility restrictions");
"Can't have duplicate image visibility restrictions");
queryParameters.put(IS_PUBLIC_KEY, "true"); queryParameters.put(IS_PUBLIC_KEY, "true");
return this; return this;
} }
public GetImageListOptions onlyPrivate() { public GetImageListOptions onlyPrivate() {
checkState(!queryParameters.containsKey(IS_PUBLIC_KEY), checkState(!queryParameters.containsKey(IS_PUBLIC_KEY), "Can't have duplicate image visibility restrictions");
"Can't have duplicate image visibility restrictions");
queryParameters.put(IS_PUBLIC_KEY, "false"); queryParameters.put(IS_PUBLIC_KEY, "false");
return this; return this;
} }
@ -70,26 +66,28 @@ public class GetImageListOptions extends BaseHttpRequestOptions {
} }
public GetImageListOptions maxItemsNumber(Integer maxNumber) { public GetImageListOptions maxItemsNumber(Integer maxNumber) {
checkState(!queryParameters.containsKey(MAX_NUMBER_KEY), checkState(!queryParameters.containsKey(MAX_NUMBER_KEY), "Can't have duplicate parameter of max returned items");
"Can't have duplicate parameter of max returned items");
queryParameters.put(MAX_NUMBER_KEY, maxNumber.toString()); queryParameters.put(MAX_NUMBER_KEY, maxNumber.toString());
return this; return this;
} }
public static class Builder { public static class Builder {
public GetImageListOptions inDatacenter(String datacenterId) { public static GetImageListOptions maxItems(int maxNumber) {
GetImageListOptions getImageListOptions = new GetImageListOptions(); return new GetImageListOptions().maxItemsNumber(maxNumber);
return getImageListOptions.inDatacenter(checkNotNull(datacenterId));
} }
public GetImageListOptions publicWebServers() { public static GetImageListOptions inDatacenter(String datacenterId) {
return new GetImageListOptions().inDatacenter(checkNotNull(datacenterId, "datacenterId"));
}
public static GetImageListOptions publicWebServers() {
return new GetImageListOptions().setState(ServerImageState.AVAILABLE).setType( return new GetImageListOptions().setState(ServerImageState.AVAILABLE).setType(
ServerImageType.WEB_APPLICATION_SERVER).onlyPublic(); ServerImageType.WEB_APPLICATION_SERVER).onlyPublic();
} }
public GetImageListOptions publicDatabaseServers() { public static GetImageListOptions publicDatabaseServers() {
return new GetImageListOptions().setState(ServerImageState.AVAILABLE).setType( return new GetImageListOptions().setState(ServerImageState.AVAILABLE).setType(ServerImageType.DATABASE_SERVER)
ServerImageType.DATABASE_SERVER).onlyPublic(); .onlyPublic();
} }
} }

View File

@ -18,87 +18,102 @@
*/ */
package org.jclouds.gogrid.options; package org.jclouds.gogrid.options;
import org.jclouds.gogrid.domain.JobState; import static com.google.common.base.Preconditions.checkState;
import org.jclouds.gogrid.domain.ObjectType; import static org.jclouds.gogrid.reference.GoGridQueryParams.END_DATE_KEY;
import org.jclouds.http.options.BaseHttpRequestOptions; import static org.jclouds.gogrid.reference.GoGridQueryParams.JOB_OBJECT_TYPE_KEY;
import static org.jclouds.gogrid.reference.GoGridQueryParams.JOB_STATE_KEY;
import static org.jclouds.gogrid.reference.GoGridQueryParams.MAX_NUMBER_KEY;
import static org.jclouds.gogrid.reference.GoGridQueryParams.OBJECT_KEY;
import static org.jclouds.gogrid.reference.GoGridQueryParams.OWNER_KEY;
import static org.jclouds.gogrid.reference.GoGridQueryParams.START_DATE_KEY;
import java.util.Date; import java.util.Date;
import static com.google.common.base.Preconditions.checkState; import org.jclouds.gogrid.domain.JobState;
import static org.jclouds.gogrid.reference.GoGridQueryParams.*; import org.jclouds.gogrid.domain.ObjectType;
import org.jclouds.http.options.BaseHttpRequestOptions;
/** /**
* @author Oleksiy Yarmula * @author Oleksiy Yarmula
*/ */
public class GetJobListOptions extends BaseHttpRequestOptions { public class GetJobListOptions extends BaseHttpRequestOptions {
public static final GetJobListOptions NONE = new GetJobListOptions(); public static final GetJobListOptions NONE = new GetJobListOptions();
public GetJobListOptions maxItemsNumber(Integer maxNumber) { public GetJobListOptions maxItemsNumber(Integer maxNumber) {
checkState(!queryParameters.containsKey(MAX_NUMBER_KEY), "Can't have duplicate parameter of max returned items"); checkState(!queryParameters.containsKey(MAX_NUMBER_KEY), "Can't have duplicate parameter of max returned items");
queryParameters.put(MAX_NUMBER_KEY, maxNumber.toString()); queryParameters.put(MAX_NUMBER_KEY, maxNumber.toString());
return this; return this;
} }
public GetJobListOptions withStartDate(Date startDate) { public GetJobListOptions withStartDate(Date startDate) {
checkState(!queryParameters.containsKey(START_DATE_KEY), "Can't have duplicate start date for filtering"); checkState(!queryParameters.containsKey(START_DATE_KEY), "Can't have duplicate start date for filtering");
queryParameters.put(START_DATE_KEY, String.valueOf(startDate.getTime())); queryParameters.put(START_DATE_KEY, String.valueOf(startDate.getTime()));
return this; return this;
} }
public GetJobListOptions withEndDate(Date endDate) { public GetJobListOptions withEndDate(Date endDate) {
checkState(!queryParameters.containsKey(END_DATE_KEY), "Can't have duplicate end date for filtering"); checkState(!queryParameters.containsKey(END_DATE_KEY), "Can't have duplicate end date for filtering");
queryParameters.put(END_DATE_KEY, String.valueOf(endDate.getTime())); queryParameters.put(END_DATE_KEY, String.valueOf(endDate.getTime()));
return this; return this;
} }
public GetJobListOptions withOwner(String owner) { public GetJobListOptions withOwner(String owner) {
checkState(!queryParameters.containsKey(OWNER_KEY), "Can't have duplicate owner name for filtering"); checkState(!queryParameters.containsKey(OWNER_KEY), "Can't have duplicate owner name for filtering");
queryParameters.put(OWNER_KEY, owner); queryParameters.put(OWNER_KEY, owner);
return this; return this;
} }
public GetJobListOptions onlyForState(JobState jobState) { public GetJobListOptions onlyForState(JobState jobState) {
checkState(!queryParameters.containsKey(JOB_STATE_KEY), "Can't have duplicate job state for filtering"); checkState(!queryParameters.containsKey(JOB_STATE_KEY), "Can't have duplicate job state for filtering");
queryParameters.put(JOB_STATE_KEY, jobState.toString()); queryParameters.put(JOB_STATE_KEY, jobState.toString());
return this; return this;
} }
public GetJobListOptions onlyForObjectType(ObjectType objectType) { public GetJobListOptions onlyForObjectType(ObjectType objectType) {
checkState(!queryParameters.containsKey(JOB_OBJECT_TYPE_KEY), "Can't have duplicate object type for filtering"); checkState(!queryParameters.containsKey(JOB_OBJECT_TYPE_KEY), "Can't have duplicate object type for filtering");
queryParameters.put(JOB_OBJECT_TYPE_KEY, objectType.toString()); queryParameters.put(JOB_OBJECT_TYPE_KEY, objectType.toString());
return this; return this;
} }
public GetJobListOptions onlyForObjectName(String objectName) { public GetJobListOptions onlyForObjectName(String objectName) {
checkState(!queryParameters.containsKey(OBJECT_KEY), "Can't have duplicate object name for filtering"); checkState(!queryParameters.containsKey(OBJECT_KEY), "Can't have duplicate object name for filtering");
queryParameters.put(OBJECT_KEY, objectName); queryParameters.put(OBJECT_KEY, objectName);
return this; return this;
} }
/* public GetJobListOptions latestJobForObjectByName(String serverName) {
return maxItemsNumber(1).onlyForObjectName(serverName);
}
/*
* This method is intended for testing * This method is intended for testing
*/ */
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (o == null || getClass() != o.getClass()) return false; return true;
if (o == null || getClass() != o.getClass())
return false;
GetJobListOptions options = (GetJobListOptions) o; GetJobListOptions options = (GetJobListOptions) o;
return buildQueryParameters().equals(options.buildQueryParameters()); return buildQueryParameters().equals(options.buildQueryParameters());
} }
public static class Builder { public static class Builder {
public GetJobListOptions create() {
return new GetJobListOptions();
}
public GetJobListOptions latestJobForObjectByName(String serverName) { public static GetJobListOptions maxItems(int maxNumber) {
return new GetJobListOptions(). return new GetJobListOptions().maxItemsNumber(maxNumber);
maxItemsNumber(1). }
onlyForObjectName(serverName);
} public static GetJobListOptions startDate(Date startDate){
} return new GetJobListOptions().withStartDate(startDate);
}
public static GetJobListOptions latestJobForObjectByName(String serverName) {
return new GetJobListOptions().latestJobForObjectByName(serverName);
}
}
} }

View File

@ -18,20 +18,21 @@
*/ */
package org.jclouds.gogrid.predicates; package org.jclouds.gogrid.predicates;
import com.google.common.base.Predicate; import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.Iterables; import static org.jclouds.gogrid.options.GetJobListOptions.Builder.latestJobForObjectByName;
import com.google.inject.Inject;
import org.jclouds.gogrid.domain.Job;
import org.jclouds.gogrid.domain.JobState;
import org.jclouds.gogrid.domain.LoadBalancer;
import org.jclouds.gogrid.options.GetJobListOptions;
import org.jclouds.gogrid.services.GridJobClient;
import org.jclouds.logging.Logger;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Singleton; import javax.inject.Singleton;
import static com.google.common.base.Preconditions.checkNotNull; import org.jclouds.gogrid.domain.Job;
import org.jclouds.gogrid.domain.JobState;
import org.jclouds.gogrid.domain.LoadBalancer;
import org.jclouds.gogrid.services.GridJobClient;
import org.jclouds.logging.Logger;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
/** /**
* @author Oleksiy Yarmula * @author Oleksiy Yarmula
@ -39,23 +40,21 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Singleton @Singleton
public class LoadBalancerLatestJobCompleted implements Predicate<LoadBalancer> { public class LoadBalancerLatestJobCompleted implements Predicate<LoadBalancer> {
protected GridJobClient jobClient; protected GridJobClient jobClient;
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
@Inject @Inject
public LoadBalancerLatestJobCompleted(GridJobClient jobClient) { public LoadBalancerLatestJobCompleted(GridJobClient jobClient) {
this.jobClient = jobClient; this.jobClient = jobClient;
} }
@Override @Override
public boolean apply(LoadBalancer loadBalancer) { public boolean apply(LoadBalancer loadBalancer) {
checkNotNull(loadBalancer, "Load balancer must be a valid instance"); checkNotNull(loadBalancer, "Load balancer must be a valid instance");
checkNotNull(loadBalancer.getName(), "Load balancer must be a valid name"); checkNotNull(loadBalancer.getName(), "Load balancer must be a valid name");
GetJobListOptions jobOptions = new GetJobListOptions.Builder(). Job latestJob = Iterables.getOnlyElement(jobClient.getJobList(latestJobForObjectByName(loadBalancer.getName())));
latestJobForObjectByName(loadBalancer.getName()); return JobState.SUCCEEDED.equals(latestJob.getCurrentState());
Job latestJob = Iterables.getOnlyElement(jobClient.getJobList(jobOptions)); }
return JobState.SUCCEEDED.equals(latestJob.getCurrentState());
}
} }

View File

@ -18,25 +18,26 @@
*/ */
package org.jclouds.gogrid.predicates; package org.jclouds.gogrid.predicates;
import com.google.common.base.Predicate; import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.Iterables; import static org.jclouds.gogrid.options.GetJobListOptions.Builder.latestJobForObjectByName;
import com.google.inject.Inject;
import org.jclouds.gogrid.domain.Job;
import org.jclouds.gogrid.domain.JobState;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.gogrid.options.GetJobListOptions;
import org.jclouds.gogrid.services.GridJobClient;
import org.jclouds.logging.Logger;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Singleton; import javax.inject.Singleton;
import static com.google.common.base.Preconditions.checkNotNull; import org.jclouds.gogrid.domain.Job;
import org.jclouds.gogrid.domain.JobState;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.gogrid.services.GridJobClient;
import org.jclouds.logging.Logger;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
/** /**
* Checks if the latest job for the server is in "Succeeded" state. * Checks if the latest job for the server is in "Succeeded" state. To achieve meaningful results,
* To achieve meaningful results, this must be run in a sequential * this must be run in a sequential environment when a server has only one job related to it at a
* environment when a server has only one job related to it at a time. * time.
* *
* The passed server instance must not be null and must have a name. * The passed server instance must not be null and must have a name.
* *
@ -45,23 +46,21 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Singleton @Singleton
public class ServerLatestJobCompleted implements Predicate<Server> { public class ServerLatestJobCompleted implements Predicate<Server> {
protected GridJobClient jobClient; protected GridJobClient jobClient;
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
@Inject @Inject
public ServerLatestJobCompleted(GridJobClient jobClient) { public ServerLatestJobCompleted(GridJobClient jobClient) {
this.jobClient = jobClient; this.jobClient = jobClient;
} }
@Override @Override
public boolean apply(Server server) { public boolean apply(Server server) {
checkNotNull(server, "Server must be a valid instance"); checkNotNull(server, "Server must be a valid instance");
checkNotNull(server.getName(), "Server must be a valid name"); checkNotNull(server.getName(), "Server must be a valid name");
GetJobListOptions jobOptions = new GetJobListOptions.Builder(). Job latestJob = Iterables.getOnlyElement(jobClient.getJobList(latestJobForObjectByName(server.getName())));
latestJobForObjectByName(server.getName()); return JobState.SUCCEEDED.equals(latestJob.getCurrentState());
Job latestJob = Iterables.getOnlyElement(jobClient.getJobList(jobOptions)); }
return JobState.SUCCEEDED.equals(latestJob.getCurrentState());
}
} }

View File

@ -45,6 +45,7 @@ public interface GridJobClient {
* *
* @return jobs found by request * @return jobs found by request
*/ */
@Timeout(duration = 90, timeUnit = TimeUnit.SECONDS)
Set<Job> getJobList(GetJobListOptions... options); Set<Job> getJobList(GetJobListOptions... options);
/** /**

View File

@ -21,12 +21,12 @@ package org.jclouds.gogrid.predicates;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock; import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay; import static org.easymock.classextension.EasyMock.replay;
import static org.jclouds.gogrid.options.GetJobListOptions.Builder.latestJobForObjectByName;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.jclouds.gogrid.domain.Job; import org.jclouds.gogrid.domain.Job;
import org.jclouds.gogrid.domain.JobState; import org.jclouds.gogrid.domain.JobState;
import org.jclouds.gogrid.domain.Server; import org.jclouds.gogrid.domain.Server;
import org.jclouds.gogrid.options.GetJobListOptions;
import org.jclouds.gogrid.services.GridJobClient; import org.jclouds.gogrid.services.GridJobClient;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -35,33 +35,29 @@ import com.google.common.collect.ImmutableSet;
/** /**
* @author Oleksiy Yarmula * @author Oleksiy Yarmula
*/ */
//NOTE:without testName, this will not call @Before* and fail w/NPE during surefire // NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "unit", testName = "ServerLatestJobCompletedTest") @Test(groups = "unit", testName = "ServerLatestJobCompletedTest")
public class ServerLatestJobCompletedTest { public class ServerLatestJobCompletedTest {
@Test @Test
public void testPredicate() { public void testPredicate() {
final String serverName = "SERVER_NAME"; final String serverName = "SERVER_NAME";
Server server = createMock(Server.class); Server server = createMock(Server.class);
expect(server.getName()).andStubReturn(serverName); expect(server.getName()).andStubReturn(serverName);
GetJobListOptions jobOptions = new GetJobListOptions.Builder(). Job job = createMock(Job.class);
latestJobForObjectByName(serverName); expect(job.getCurrentState()).andReturn(JobState.SUCCEEDED);
Job job = createMock(Job.class); GridJobClient client = createMock(GridJobClient.class);
expect(job.getCurrentState()).andReturn(JobState.SUCCEEDED); expect(client.getJobList(latestJobForObjectByName(serverName))).andReturn(ImmutableSet.<Job> of(job));
GridJobClient client = createMock(GridJobClient.class); replay(job);
expect(client.getJobList(jobOptions)). replay(client);
andReturn(ImmutableSet.<Job>of(job)); replay(server);
replay(job); ServerLatestJobCompleted predicate = new ServerLatestJobCompleted(client);
replay(client); assertTrue(predicate.apply(server), "The result of the predicate should've been 'true'");
replay(server);
ServerLatestJobCompleted predicate = new ServerLatestJobCompleted(client); }
assertTrue(predicate.apply(server), "The result of the predicate should've been 'true'");
}
} }

View File

@ -0,0 +1,90 @@
/**
* 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.gogrid.services;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Properties;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.gogrid.GoGridAsyncClient;
import org.jclouds.gogrid.GoGridClient;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.rest.RestContext;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
/**
* Tests behavior of {@code GoGridClient}
*
* @author Adrian Cole
*/
@Test(groups = "live")
public class BaseGoGridClientLiveTest {
protected RestContext<GoGridClient, GoGridAsyncClient> restContext;
protected String provider = "gogrid";
protected String identity;
protected String credential;
protected String endpoint;
protected String apiversion;
protected ComputeServiceContext context;
protected String prefix = System.getProperty("user.name");
protected void setupCredentials() {
identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider + ".identity");
credential = checkNotNull(System.getProperty("test." + provider + ".credential"), "test." + provider
+ ".credential");
endpoint = System.getProperty("test." + provider + ".endpoint");
apiversion = System.getProperty("test." + provider + ".apiversion");
}
protected Properties setupProperties() {
Properties overrides = new Properties();
overrides.setProperty(provider + ".identity", identity);
overrides.setProperty(provider + ".credential", credential);
if (endpoint != null)
overrides.setProperty(provider + ".endpoint", endpoint);
if (apiversion != null)
overrides.setProperty(provider + ".apiversion", apiversion);
return overrides;
}
@BeforeGroups(groups = { "live" })
public void setupClient() {
setupCredentials();
Properties overrides = setupProperties();
context = new ComputeServiceContextFactory().createContext(provider, ImmutableSet.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), overrides);
restContext = context.getProviderSpecificContext();
}
@AfterGroups(groups = "live")
protected void tearDown() {
if (context != null)
context.close();
}
}

View File

@ -0,0 +1,72 @@
/**
* 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.gogrid.services;
import static org.testng.Assert.assertEquals;
import java.util.Set;
import java.util.logging.Logger;
import org.jclouds.gogrid.domain.ServerImage;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "unit", testName = "GridImageClientLiveTest")
public class GridImageClientLiveTest extends BaseGoGridClientLiveTest {
public void testListImages() throws Exception {
Set<ServerImage> response = restContext.getApi().getImageServices().getImageList();
assert null != response;
for (ServerImage image : response) {
assert image.getId() >= 0 : image;
checkImage(image);
ServerImage query = Iterables.getOnlyElement(restContext.getApi().getImageServices().getImagesById(
image.getId()));
assertEquals(query.getId(), image.getId());
checkImage(query);
}
}
private void checkImage(ServerImage image) {
assert image.getArchitecture() != null : image;
assert image.getBillingTokens() != null : image;
if (image.getCreatedTime() == null)
Logger.getAnonymousLogger().warning("image " + image.getId() + " is missing the createdon field");
assert image.getDescription() != null : image;
assert image.getFriendlyName() != null : image;
assert image.getId() >= 0 : image;
assert image.getLocation() != null : image;
assert image.getName() != null : image;
assert image.getOs() != null : image;
assert image.getOwner() != null : image;
assert image.getPrice() >= 0 : image;
assert image.getState() != null : image;
assert image.getType() != null : image;
if (image.getUpdatedTime() == null)
Logger.getAnonymousLogger().warning("image " + image.getId() + " is missing the updatedon field");
}
}

View File

@ -18,6 +18,8 @@
*/ */
package org.jclouds.gogrid.services; package org.jclouds.gogrid.services;
import static org.jclouds.gogrid.options.GetJobListOptions.Builder.startDate;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Date; import java.util.Date;
@ -45,14 +47,13 @@ public class GridJobAsyncClientTest extends BaseGoGridAsyncClientTest<GridJobAsy
@Test @Test
public void testGetJobListWithOptions() throws NoSuchMethodException, IOException { public void testGetJobListWithOptions() throws NoSuchMethodException, IOException {
Method method = GridJobAsyncClient.class.getMethod("getJobList", GetJobListOptions[].class); Method method = GridJobAsyncClient.class.getMethod("getJobList", GetJobListOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, HttpRequest httpRequest = processor
new GetJobListOptions.Builder().create().withStartDate(new Date(1267385381770L)).withEndDate( .createRequest(method, startDate(new Date(1267385381770L)).withEndDate(new Date(1267385382770L))
new Date(1267385382770L)).onlyForObjectType(ObjectType.VIRTUAL_SERVER).onlyForState( .onlyForObjectType(ObjectType.VIRTUAL_SERVER).onlyForState(JobState.PROCESSING));
JobState.PROCESSING));
assertRequestLineEquals(httpRequest, assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/job/list?v=1.5&startdate=1267385381770&" "GET https://api.gogrid.com/api/grid/job/list?v=1.5&startdate=1267385381770&"
+ "enddate=1267385382770&job.objecttype=VirtualServer&" + "job.state=Processing HTTP/1.1"); + "enddate=1267385382770&job.objecttype=VirtualServer&" + "job.state=Processing HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
@ -64,9 +65,9 @@ public class GridJobAsyncClientTest extends BaseGoGridAsyncClientTest<GridJobAsy
httpRequest = Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest); httpRequest = Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest, assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/job/list?v=1.5&startdate=1267385381770&" "GET https://api.gogrid.com/api/grid/job/list?v=1.5&startdate=1267385381770&"
+ "enddate=1267385382770&job.objecttype=VirtualServer&" + "job.state=Processing&" + "enddate=1267385382770&job.objecttype=VirtualServer&" + "job.state=Processing&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo HTTP/1.1"); + "sig=3f446f171455fbb5574aecff4997b273&api_key=foo HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
} }
@ -87,7 +88,7 @@ public class GridJobAsyncClientTest extends BaseGoGridAsyncClientTest<GridJobAsy
HttpRequest httpRequest = processor.createRequest(method, "MyServer"); HttpRequest httpRequest = processor.createRequest(method, "MyServer");
assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/job/list?v=1.5&" assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/job/list?v=1.5&"
+ "object=MyServer HTTP/1.1"); + "object=MyServer HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
@ -99,7 +100,7 @@ public class GridJobAsyncClientTest extends BaseGoGridAsyncClientTest<GridJobAsy
httpRequest = Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest); httpRequest = Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/job/list?v=1.5&" assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/job/list?v=1.5&"
+ "object=MyServer&sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1"); + "object=MyServer&sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
} }
@ -110,7 +111,7 @@ public class GridJobAsyncClientTest extends BaseGoGridAsyncClientTest<GridJobAsy
HttpRequest httpRequest = processor.createRequest(method, 123L, 456L); HttpRequest httpRequest = processor.createRequest(method, 123L, 456L);
assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/job/get?v=1.5&" assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/job/get?v=1.5&"
+ "id=123&id=456 HTTP/1.1"); + "id=123&id=456 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
@ -122,7 +123,7 @@ public class GridJobAsyncClientTest extends BaseGoGridAsyncClientTest<GridJobAsy
httpRequest = Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest); httpRequest = Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/job/get?v=1.5&" assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/job/get?v=1.5&"
+ "id=123&id=456&sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1"); + "id=123&id=456&sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
} }

View File

@ -0,0 +1,66 @@
/**
* 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.gogrid.services;
import static org.testng.Assert.assertEquals;
import java.util.Set;
import org.jclouds.gogrid.domain.Job;
import org.jclouds.gogrid.options.GetJobListOptions;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "unit", testName = "GridJobClientLiveTest")
public class GridJobClientLiveTest extends BaseGoGridClientLiveTest {
public void testListJobs() throws Exception {
Set<Job> response = restContext.getApi().getJobServices().getJobList(GetJobListOptions.Builder.maxItems(10));
assert null != response;
assert response.size() <= 10 : response;
for (Job job : response) {
assert job.getId() >= 0 : job;
checkJob(job);
Job query = Iterables.getOnlyElement(restContext.getApi().getJobServices().getJobsById(job.getId()));
assertEquals(query.getId(), job.getId());
checkJob(query);
}
}
private void checkJob(Job job) {
assert job.getAttempts() >= 0 : job;
assert job.getCommand() != null : job;
assert job.getCreatedOn() != null : job;
assert job.getCreatedOn() != null : job;
assert job.getDetails() != null : job;
assert job.getHistory() != null : job;
assert job.getId() >= 0 : job;
assert job.getLastUpdatedOn() != null : job;
assert job.getObjectType() != null : job;
assert job.getOwner() != null : job;
}
}