Merge pull request #197 from andreisavu/job-complete

Use enums for status, result code & error code for async jobs
This commit is contained in:
Adrian Cole 2011-12-01 10:19:28 -08:00
commit dc494414ba
13 changed files with 322 additions and 104 deletions

View File

@ -0,0 +1,40 @@
/**
* 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.cloudstack;
/**
* @author Andrei Savu
*/
public class AsyncJobException extends RuntimeException {
public AsyncJobException() {
}
public AsyncJobException(String s) {
super(s);
}
public AsyncJobException(String s, Throwable throwable) {
super(s, throwable);
}
public AsyncJobException(Throwable throwable) {
super(throwable);
}
}

View File

@ -28,6 +28,75 @@ import com.google.gson.annotations.SerializedName;
*/
public class AsyncJob<T> {
/**
* Valid job result codes
*/
public static enum ResultCode {
SUCCESS (0),
FAIL (530),
UNKNOWN (-1);
private final int code;
private ResultCode(int code) {
this.code = code;
}
public int code() { return this.code; }
public static ResultCode fromValue(String value) {
try {
int resultCode = Integer.parseInt(value);
switch (resultCode) {
case 0:
return SUCCESS;
case 530:
return FAIL;
default:
return UNKNOWN;
}
} catch(NumberFormatException e) {
return UNKNOWN;
}
}
}
/**
* Valid async job statuses
*/
public static enum Status {
IN_PROGRESS (0),
SUCCEEDED (1),
FAILED (2),
UNKNOWN (-1);
private final int code;
private Status(int code) {
this.code = code;
}
public int code() { return this.code; }
public static Status fromValue(String value) {
try {
int statusCode = Integer.parseInt(value);
switch (statusCode) {
case 0:
return IN_PROGRESS;
case 1:
return SUCCEEDED;
case 2:
return FAILED;
default:
return UNKNOWN;
}
} catch (NumberFormatException e) {
return UNKNOWN;
}
}
}
public static <T> Builder<T> builder() {
return new Builder<T>();
}
@ -41,10 +110,10 @@ public class AsyncJob<T> {
private String instanceType;
private int progress = -1;
private T result;
private int resultCode = -1;
private ResultCode resultCode = ResultCode.UNKNOWN;
private String resultType;
private AsyncJobError error;
private int status = -1;
private Status status = Status.UNKNOWN;
private int userId = -1;
public Builder<T> accountId(long accountId) {
@ -92,7 +161,7 @@ public class AsyncJob<T> {
return this;
}
public Builder<T> resultCode(int resultCode) {
public Builder<T> resultCode(ResultCode resultCode) {
this.resultCode = resultCode;
return this;
}
@ -102,7 +171,7 @@ public class AsyncJob<T> {
return this;
}
public Builder<T> status(int status) {
public Builder<T> status(Status status) {
this.status = status;
return this;
}
@ -139,17 +208,17 @@ public class AsyncJob<T> {
@SerializedName("jobresult")
private T result;
@SerializedName("jobresultcode")
private int resultCode = -1;
private ResultCode resultCode = ResultCode.UNKNOWN;
@SerializedName("jobresulttype")
private String resultType;
@SerializedName("jobstatus")
private int status = -1;
private Status status = Status.UNKNOWN;
@SerializedName("userid")
private int userId = -1;
private AsyncJobError error;
public AsyncJob(long accountId, String cmd, Date created, long id, long instanceId, String instanceType,
int progress, T result, int resultCode, String resultType, int status, int userId, AsyncJobError error) {
int progress, T result, ResultCode resultCode, String resultType, Status status, int userId, AsyncJobError error) {
this.accountId = accountId;
this.cmd = cmd;
this.created = created;
@ -232,7 +301,7 @@ public class AsyncJob<T> {
/**
* @return the result code for the job
*/
public int getResultCode() {
public ResultCode getResultCode() {
return resultCode;
}
@ -246,7 +315,7 @@ public class AsyncJob<T> {
/**
* @return the current job status-should be 0 for PENDING
*/
public int getStatus() {
public Status getStatus() {
return status;
}
@ -267,6 +336,14 @@ public class AsyncJob<T> {
return error;
}
public boolean hasFailed() {
return getError() != null || getResultCode() == ResultCode.FAIL || getStatus() == Status.FAILED;
}
public boolean hasSucceed() {
return getError() == null && getResultCode() == ResultCode.SUCCESS && getStatus() == Status.SUCCEEDED;
}
@Override
public int hashCode() {
final int prime = 31;
@ -280,9 +357,9 @@ public class AsyncJob<T> {
result = prime * result + ((error == null) ? 0 : error.hashCode());
result = prime * result + progress;
result = prime * result + ((this.result == null) ? 0 : this.result.hashCode());
result = prime * result + resultCode;
result = prime * result + resultCode.code();
result = prime * result + ((resultType == null) ? 0 : resultType.hashCode());
result = prime * result + status;
result = prime * result + status.code();
result = prime * result + userId;
return result;
}

View File

@ -25,8 +25,47 @@ import com.google.gson.annotations.SerializedName;
* @author Adrian Cole
*/
public class AsyncJobError {
/**
* Error codes for job errors
*/
public static enum ErrorCode {
INTERNAL_ERROR (530),
ACCOUNT_ERROR (531),
ACCOUNT_RESOURCE_LIMIT_ERROR(532),
INSUFFICIENT_CAPACITY_ERROR (533),
RESOURCE_UNAVAILABLE_ERROR (534),
RESOURCE_ALLOCATION_ERROR (535),
RESOURCE_IN_USE_ERROR (536),
NETWORK_RULE_CONFLICT_ERROR (537),
UNKNOWN (-1);
private final int code;
private ErrorCode(int code) {
this.code = code;
}
public int code() { return this.code; }
public static ErrorCode fromValue(String value) {
try {
int errorCode = Integer.parseInt(value);
for(ErrorCode candidate : values()) {
if (candidate.code() == errorCode) {
return candidate;
}
}
return UNKNOWN;
} catch(NumberFormatException e) {
return UNKNOWN;
}
}
}
@SerializedName("errorcode")
private int errorCode;
private ErrorCode errorCode;
@SerializedName("errortext")
private String errorText;
@ -38,12 +77,12 @@ public class AsyncJobError {
}
public AsyncJobError(int errorCode, String errorText) {
public AsyncJobError(ErrorCode errorCode, String errorText) {
this.errorCode = errorCode;
this.errorText = errorText;
}
public int getErrorCode() {
public ErrorCode getErrorCode() {
return errorCode;
}
@ -55,7 +94,7 @@ public class AsyncJobError {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + errorCode;
result = prime * result + errorCode.code();
result = prime * result + ((errorText == null) ? 0 : errorText.hashCode());
return result;
}

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudstack.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.cloudstack.domain.AsyncJobError.ErrorCode;
import java.util.Map;
import java.util.Map.Entry;
@ -50,7 +51,6 @@ import com.google.common.collect.Iterables;
import com.google.inject.Inject;
/**
*
* @author Adrian Cole
*/
@Singleton
@ -61,10 +61,13 @@ public class ParseTypedAsyncJob implements Function<AsyncJob<Map<String, JsonBal
@Inject(optional = true)
@VisibleForTesting
@Named("jclouds.cloudstack.jobresult-type-map")
Map<String, Class<?>> typeMap = ImmutableMap.<String, Class<?>> builder().put("securitygroup", SecurityGroup.class)
.put("portforwardingrule", PortForwardingRule.class).put("ipforwardingrule", IPForwardingRule.class)
.put("network", Network.class).put("ipaddress", PublicIPAddress.class)
.put("virtualmachine", VirtualMachine.class).build();
Map<String, Class<?>> typeMap = ImmutableMap.<String, Class<?>>builder()
.put("securitygroup", SecurityGroup.class)
.put("portforwardingrule", PortForwardingRule.class)
.put("ipforwardingrule", IPForwardingRule.class)
.put("network", Network.class)
.put("ipaddress", PublicIPAddress.class)
.put("virtualmachine", VirtualMachine.class).build();
private final Json json;
@Inject
@ -76,7 +79,7 @@ public class ParseTypedAsyncJob implements Function<AsyncJob<Map<String, JsonBal
AsyncJob<?> result = toParse;
if (toParse.getResult() != null) {
if (toParse.getResult().size() == 1) {
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
Builder<Object> builder = AsyncJob.Builder.fromAsyncJobUntyped((AsyncJob) toParse);
if (toParse.getResult().containsKey("success")) {
builder.result(null);
@ -94,23 +97,23 @@ public class ParseTypedAsyncJob implements Function<AsyncJob<Map<String, JsonBal
builder.result(json.fromJson(entry.getValue().toString(), typeMap.get(entry.getKey())));
} else {
logger.warn(
"type key %s not configured. please override default for Map<String, Class<?>> bound to name jclouds.cloudstack.jobresult-type-map",
entry.getKey());
"type key %s not configured. please override default for Map<String, Class<?>> bound to name jclouds.cloudstack.jobresult-type-map",
entry.getKey());
builder.result(entry.getValue().toString());
}
}
result = builder.build();
} else if (toParse.getResult().containsKey("errorcode")) {
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
Builder<Object> builder = AsyncJob.Builder.fromAsyncJobUntyped((AsyncJob) toParse);
builder.result(null);// avoid classcastexceptions
builder.error(new AsyncJobError(Integer.parseInt(toParse.getResult().get("errorcode").toString()), toParse
.getResult().containsKey("errortext") ? toParse.getResult().get("errortext").toString()
.replace("\"", "") : null));
builder.error(new AsyncJobError(ErrorCode.fromValue(toParse.getResult().get("errorcode").toString()), toParse
.getResult().containsKey("errortext") ? toParse.getResult().get("errortext").toString()
.replace("\"", "") : null));
result = builder.build();
} else if (toParse.getResult().size() > 1) {
logger.warn("unexpected size of async job result; expecting a map with a single element",
toParse.getResult());
toParse.getResult());
}
}
return result;

View File

@ -19,10 +19,15 @@
package org.jclouds.cloudstack.predicates;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode.FAIL;
import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode.SUCCESS;
import static org.jclouds.cloudstack.domain.AsyncJob.Status.FAILED;
import static org.jclouds.cloudstack.domain.AsyncJob.Status.SUCCEEDED;
import javax.annotation.Resource;
import javax.inject.Singleton;
import org.jclouds.cloudstack.AsyncJobException;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.domain.AsyncJob;
import org.jclouds.logging.Logger;
@ -50,17 +55,18 @@ public class JobComplete implements Predicate<Long> {
}
public boolean apply(Long jobId) {
logger.trace("looking for status on job %s", checkNotNull(jobId, "jobId"));
logger.trace(">> looking for status on job %s", checkNotNull(jobId, "jobId"));
AsyncJob<?> job = refresh(jobId);
if (job == null)
if (job == null) {
return false;
logger.trace("%s: looking for job status %s: currently: %s", job.getId(), 1, job.getStatus());
if (job.getError() != null) {
// TODO: create a typed error
throw new RuntimeException(String.format("job %s failed with exception %s", job.getId(), job.getError()
.toString()));
}
return job.getStatus() > 0;
logger.trace("%s: looking for job status %s: currently: %s", job.getId(), 1, job.getStatus());
if (job.hasFailed()) {
throw new AsyncJobException(String.format("job %s failed with exception %s",
job.toString(), job.getError().toString()));
}
return job.hasSucceed();
}
private AsyncJob<?> refresh(Long jobId) {

View File

@ -54,15 +54,15 @@ public class AsyncJobClientLiveTest extends BaseCloudStackClientLiveTest {
}
private void checkJob(AsyncJob<?> query) {
assert query.getStatus() >= 0 : query;
assert query.getResultCode() >= 0 : query;
assert query.getStatus().code() >= 0 : query;
assert query.getResultCode().code() >= 0 : query;
assert query.getProgress() >= 0 : query;
if (query.getResultCode() == 0) {
if (query.getResultCode().code() == 0) {
if (query.getResult() != null)// null is ok for result of success =
// true
// ensure we parsed properly
assert (query.getResult().getClass().getPackage().equals(AsyncJob.class.getPackage())) : query;
} else if (query.getResultCode() > 400) {
} else if (query.getResultCode().code() > 400) {
assert query.getResult() == null : query;
assert query.getError() != null : query;
} else {

View File

@ -22,6 +22,7 @@ import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.jclouds.cloudstack.domain.AsyncJobError.ErrorCode;
import static org.testng.Assert.assertEquals;
import org.jclouds.cloudstack.CloudStackClient;
@ -101,7 +102,8 @@ public class BlockUntilJobCompletesAndReturnResultTest {
expect(client.getAsyncJobClient()).andReturn(jobClient).atLeastOnce();
expect(jobClient.getAsyncJob(jobId)).andReturn(
AsyncJob.builder().id(jobId).error(new AsyncJobError(1, "ERRROR")).result("foo").build()).atLeastOnce();
AsyncJob.builder().id(jobId).error(
new AsyncJobError(ErrorCode.INTERNAL_ERROR, "ERRROR")).result("foo").build()).atLeastOnce();
replay(client);
replay(jobClient);

View File

@ -18,6 +18,9 @@
*/
package org.jclouds.cloudstack.functions;
import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode;
import static org.jclouds.cloudstack.domain.AsyncJob.Status;
import static org.jclouds.cloudstack.domain.AsyncJobError.ErrorCode;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
@ -41,7 +44,6 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit")
@ -60,13 +62,16 @@ public class ParseAsyncJobFromHttpResponseTest {
public void testWithNoResult() {
String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":860,\"jobstatus\":0,\"jobprocstatus\":0,\"jobresultcode\":0} }";
AsyncJob<PublicIPAddress> expects = AsyncJob.<PublicIPAddress> builder().id(860).status(0).progress(0)
.resultCode(0).build();
AsyncJob<PublicIPAddress> expects = AsyncJob.<PublicIPAddress>builder()
.id(860)
.status(Status.IN_PROGRESS)
.progress(0)
.resultCode(ResultCode.SUCCESS).build();
ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
@SuppressWarnings("unchecked")
AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
Payloads.newStringPayload(input)));
Payloads.newStringPayload(input)));
assertEquals(response, expects);
}
@ -74,13 +79,17 @@ public class ParseAsyncJobFromHttpResponseTest {
public void testWithSuccessTrueResultSetsNullResult() {
String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":1138,\"jobstatus\":1,\"jobprocstatus\":0,\"jobresultcode\":0,\"jobresulttype\":\"object\",\"jobresult\":{\"success\":true}} }";
AsyncJob<PublicIPAddress> expects = AsyncJob.<PublicIPAddress> builder().id(1138).status(1).progress(0)
.resultType("object").resultCode(0).build();
AsyncJob<PublicIPAddress> expects = AsyncJob.<PublicIPAddress>builder()
.id(1138)
.status(Status.SUCCEEDED)
.progress(0)
.resultType("object")
.resultCode(ResultCode.SUCCESS).build();
ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
@SuppressWarnings("unchecked")
AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
Payloads.newStringPayload(input)));
Payloads.newStringPayload(input)));
assertEquals(response, expects);
}
@ -89,18 +98,19 @@ public class ParseAsyncJobFromHttpResponseTest {
String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":1103,\"jobstatus\":2,\"jobprocstatus\":0,\"jobresultcode\":530,\"jobresulttype\":\"object\",\"jobresult\":{\"errorcode\":530,\"errortext\":\"Internal error executing command, please contact your system administrator\"}} }";
AsyncJob<PublicIPAddress> expects = AsyncJob
.<PublicIPAddress> builder()
.id(1103)
.status(2)
.progress(0)
.resultType("object")
.error(new AsyncJobError(530, "Internal error executing command, please contact your system administrator"))
.resultCode(530).build();
.<PublicIPAddress>builder()
.id(1103)
.status(Status.FAILED)
.progress(0)
.resultType("object")
.error(new AsyncJobError(ErrorCode.INTERNAL_ERROR, "Internal error executing " +
"command, please contact your system administrator"))
.resultCode(ResultCode.FAIL).build();
ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
@SuppressWarnings("unchecked")
AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
Payloads.newStringPayload(input)));
Payloads.newStringPayload(input)));
assertEquals(response, expects);
}
@ -108,13 +118,18 @@ public class ParseAsyncJobFromHttpResponseTest {
public void testWithUnknownResultReturnsStringifiedJson() {
String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":860,\"jobstatus\":0,\"jobprocstatus\":0,\"jobresultcode\":0,\"jobresult\":{\"foo\":{\"bar\":1}}}}";
AsyncJob<?> expects = AsyncJob.builder().id(860).status(0).progress(0).resultCode(0).result("{\"bar\":1}")
.build();
AsyncJob<?> expects = AsyncJob.builder()
.id(860)
.status(Status.IN_PROGRESS)
.progress(0)
.resultCode(ResultCode.SUCCESS)
.result("{\"bar\":1}")
.build();
ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
@SuppressWarnings("unchecked")
AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
Payloads.newStringPayload(input)));
Payloads.newStringPayload(input)));
assertEquals(response, expects);
}
@ -127,13 +142,17 @@ public class ParseAsyncJobFromHttpResponseTest {
// thrown in case they change their minds.
String input = "{ \"queryasyncjobresultresponse\" : {\"jobid\":860,\"jobstatus\":0,\"jobprocstatus\":0,\"jobresultcode\":0,\"jobresult\":{\"foo\":{\"bar\":1},\"foo2\":{\"bar2\":2}}}}";
AsyncJob<?> expects = AsyncJob.builder().id(860).status(0).progress(0).resultCode(0)
.result(ImmutableMap.of("foo", new JsonBall("{\"bar\":1}"), "foo2", new JsonBall("{\"bar2\":2}"))).build();
AsyncJob<?> expects = AsyncJob.builder()
.id(860)
.status(Status.IN_PROGRESS)
.progress(0)
.resultCode(ResultCode.SUCCESS)
.result(ImmutableMap.of("foo", new JsonBall("{\"bar\":1}"), "foo2", new JsonBall("{\"bar2\":2}"))).build();
ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
@SuppressWarnings("unchecked")
AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
Payloads.newStringPayload(input)));
Payloads.newStringPayload(input)));
assertEquals(response, expects);
}
@ -141,29 +160,29 @@ public class ParseAsyncJobFromHttpResponseTest {
public void testPublicIPAddress() {
InputStream is = getClass().getResourceAsStream("/queryasyncjobresultresponse-ipaddress.json");
AsyncJob<PublicIPAddress> expects = AsyncJob
.<PublicIPAddress> builder()
.id(860)
.status(1)
.progress(0)
.resultType("object")
.resultCode(0)
.result(
PublicIPAddress
.builder()
.id(6)
.IPAddress("72.52.126.35")
.allocated(
new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-02-23T20:15:01-0800"))
.zoneId(1).zoneName("San Jose 1").isSourceNAT(false).account("adrian").domainId(1)
.domain("ROOT").usesVirtualNetwork(true).isStaticNAT(false).associatedNetworkId(204)
.networkId(200).state(PublicIPAddress.State.ALLOCATING).build()
.<PublicIPAddress>builder()
.id(860)
.status(Status.SUCCEEDED)
.progress(0)
.resultType("object")
.resultCode(ResultCode.SUCCESS)
.result(
PublicIPAddress
.builder()
.id(6)
.IPAddress("72.52.126.35")
.allocated(
new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-02-23T20:15:01-0800"))
.zoneId(1).zoneName("San Jose 1").isSourceNAT(false).account("adrian").domainId(1)
.domain("ROOT").usesVirtualNetwork(true).isStaticNAT(false).associatedNetworkId(204)
.networkId(200).state(PublicIPAddress.State.ALLOCATING).build()
).build();
).build();
ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
@SuppressWarnings("unchecked")
AsyncJob<PublicIPAddress> response = (AsyncJob<PublicIPAddress>) parser.apply(new HttpResponse(200, "ok",
Payloads.newInputStreamPayload(is)));
Payloads.newInputStreamPayload(is)));
assertEquals(response, expects);
}
@ -171,21 +190,21 @@ public class ParseAsyncJobFromHttpResponseTest {
public void testIPForwardingRule() {
InputStream is = getClass().getResourceAsStream("/queryasyncjobresultresponse-ipforwardingrule.json");
AsyncJob<IPForwardingRule> expects = AsyncJob
.<IPForwardingRule> builder()
.id(1133)
.status(1)
.progress(0)
.resultType("object")
.resultCode(0)
.result(
IPForwardingRule.builder().id(109).protocol("tcp").virtualMachineId(226)
.virtualMachineName("i-3-226-VM").IPAddressId(36).IPAddress("72.52.126.65").startPort(22)
.endPort(22).state("Active").build()).build();
.<IPForwardingRule>builder()
.id(1133)
.status(Status.SUCCEEDED)
.progress(0)
.resultType("object")
.resultCode(ResultCode.SUCCESS)
.result(
IPForwardingRule.builder().id(109).protocol("tcp").virtualMachineId(226)
.virtualMachineName("i-3-226-VM").IPAddressId(36).IPAddress("72.52.126.65").startPort(22)
.endPort(22).state("Active").build()).build();
ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
@SuppressWarnings("unchecked")
AsyncJob<IPForwardingRule> response = (AsyncJob<IPForwardingRule>) parser.apply(new HttpResponse(200, "ok",
Payloads.newInputStreamPayload(is)));
Payloads.newInputStreamPayload(is)));
assertEquals(response, expects);
}
@ -194,10 +213,10 @@ public class ParseAsyncJobFromHttpResponseTest {
InputStream is = getClass().getResourceAsStream("/queryasyncjobresultresponse-createtemplate.json");
ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class);
AsyncJob<?> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertTrue(response.getResult() instanceof Template, "response expected to be Template, actually is "+response.getResult().getClass());
assertTrue(response.getResult() instanceof Template, "response expected to be Template, actually is " + response.getResult().getClass());
is = getClass().getResourceAsStream("/queryasyncjobresultresponse-extracttemplate.json");
response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertTrue(response.getResult() instanceof TemplateExtraction, "response expected to be TemplateExtraction, actually is "+response.getResult().getClass());
assertTrue(response.getResult() instanceof TemplateExtraction, "response expected to be TemplateExtraction, actually is " + response.getResult().getClass());
}
}

View File

@ -18,35 +18,67 @@
*/
package org.jclouds.cloudstack.predicates;
import org.jclouds.cloudstack.AsyncJobException;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.domain.AsyncJob;
import org.jclouds.cloudstack.domain.AsyncJobError;
import org.jclouds.cloudstack.features.AsyncJobClient;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode;
import static org.jclouds.cloudstack.domain.AsyncJob.Status;
import static org.jclouds.cloudstack.domain.AsyncJobError.ErrorCode;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
/**
* @author Andrei Savu
*/
@Test(groups = "unit")
@Test(groups = "unit", singleThreaded = true)
public class JobCompleteTest {
CloudStackClient client;
AsyncJobClient asyncJobClient;
@BeforeMethod
public void setUp() {
client = createMock(CloudStackClient.class);
asyncJobClient = createMock(AsyncJobClient.class);
expect(client.getAsyncJobClient()).andReturn(asyncJobClient);
}
@Test
public void testJobComplete() {
CloudStackClient client = createMock(CloudStackClient.class);
AsyncJobClient asyncJobClient = createMock(AsyncJobClient.class);
expect(client.getAsyncJobClient()).andReturn(asyncJobClient);
AsyncJob job = AsyncJob.builder().id(100L).status(1).build();
AsyncJob job = AsyncJob.builder().id(100L)
.status(Status.SUCCEEDED).resultCode(ResultCode.SUCCESS).build();
expect(asyncJobClient.getAsyncJob(job.getId())).andReturn(job);
replay(client, asyncJobClient);
assertTrue(new JobComplete(client).apply(job.getId()));
verify(client, asyncJobClient);
}
@Test
public void testFailedJobComplete() {
AsyncJob job = AsyncJob.builder().id(100L)
.status(Status.FAILED).resultCode(ResultCode.FAIL)
.error(new AsyncJobError(ErrorCode.INTERNAL_ERROR, "Dummy test error")).build();
expect(asyncJobClient.getAsyncJob(job.getId())).andReturn(job);
replay(client, asyncJobClient);
try {
new JobComplete(client).apply(job.getId());
fail("No exception thrown");
} catch (AsyncJobException e) {
assertTrue(e.toString().contains("Dummy test error"));
}
verify(client, asyncJobClient);
}
}

View File

@ -41,7 +41,7 @@ import static org.testng.Assert.assertTrue;
/**
* @author Andrei Savu
*/
@Test(groups = "unit", singleThreaded=true)
@Test(groups = "unit", singleThreaded = true)
public class OSCategoryInTest {
private CloudStackClient client;

View File

@ -35,7 +35,7 @@ import static org.testng.Assert.assertTrue;
/**
* @author Andrei Savu
*/
@Test(groups = "unit", singleThreaded=true, testName = "VirtualMachineDestroyedTest")
@Test(groups = "unit", singleThreaded = true, testName = "VirtualMachineDestroyedTest")
public class VirtualMachineDestroyedTest {
CloudStackClient client;

View File

@ -35,7 +35,7 @@ import static org.testng.Assert.assertTrue;
/**
* @author Andrei Savu
*/
@Test(groups = "unit", singleThreaded=true, testName = "VirtualMachineExpungedTest")
@Test(groups = "unit", singleThreaded = true, testName = "VirtualMachineExpungedTest")
public class VirtualMachineExpungedTest {
CloudStackClient client;

View File

@ -36,7 +36,7 @@ import static org.testng.Assert.assertEquals;
/**
* @author Andrei Savu
*/
@Test(groups = "unit", singleThreaded=true, testName = "VirtualMachineRunningTest")
@Test(groups = "unit", singleThreaded = true, testName = "VirtualMachineRunningTest")
public class VirtualMachineRunningTest {
CloudStackClient client;