From 09de9bf00ed4779bd1e873c16745a5d53ef37412 Mon Sep 17 00:00:00 2001 From: andreisavu Date: Wed, 30 Nov 2011 17:42:15 +0200 Subject: [PATCH] Use enums for status, result code & error code for async jobs --- .../jclouds/cloudstack/domain/AsyncJob.java | 64 +++++++-- .../cloudstack/domain/AsyncJobError.java | 27 +++- .../functions/ParseTypedAsyncJob.java | 29 +++-- .../cloudstack/predicates/JobComplete.java | 7 +- .../features/AsyncJobClientLiveTest.java | 8 +- ...kUntilJobCompletesAndReturnResultTest.java | 4 +- .../ParseAsyncJobFromHttpResponseTest.java | 123 ++++++++++-------- .../predicates/JobCompleteTest.java | 43 +++++- .../predicates/OSCategoryInTest.java | 2 +- .../VirtualMachineDestroyedTest.java | 2 +- .../VirtualMachineExpungedTest.java | 2 +- .../predicates/VirtualMachineRunningTest.java | 2 +- 12 files changed, 210 insertions(+), 103 deletions(-) diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/AsyncJob.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/AsyncJob.java index add5c9f7a2..8cd7d08201 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/AsyncJob.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/AsyncJob.java @@ -43,6 +43,22 @@ public class AsyncJob { } 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; + } + } } /** @@ -61,6 +77,24 @@ public class AsyncJob { } 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 Builder builder() { @@ -76,10 +110,10 @@ public class AsyncJob { 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 accountId(long accountId) { @@ -127,7 +161,7 @@ public class AsyncJob { return this; } - public Builder resultCode(int resultCode) { + public Builder resultCode(ResultCode resultCode) { this.resultCode = resultCode; return this; } @@ -137,7 +171,7 @@ public class AsyncJob { return this; } - public Builder status(int status) { + public Builder status(Status status) { this.status = status; return this; } @@ -174,17 +208,17 @@ public class AsyncJob { @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; @@ -267,7 +301,7 @@ public class AsyncJob { /** * @return the result code for the job */ - public int getResultCode() { + public ResultCode getResultCode() { return resultCode; } @@ -281,7 +315,7 @@ public class AsyncJob { /** * @return the current job status-should be 0 for PENDING */ - public int getStatus() { + public Status getStatus() { return status; } @@ -302,6 +336,14 @@ public class AsyncJob { 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; @@ -315,9 +357,9 @@ public class AsyncJob { 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; } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/AsyncJobError.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/AsyncJobError.java index 09ae856dc7..8bfeb5a6d0 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/AsyncJobError.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/AsyncJobError.java @@ -29,7 +29,7 @@ public class AsyncJobError { /** * Error codes for job errors */ - public static enum Code { + public static enum ErrorCode { INTERNAL_ERROR (530), ACCOUNT_ERROR (531), ACCOUNT_RESOURCE_LIMIT_ERROR(532), @@ -42,15 +42,30 @@ public class AsyncJobError { private final int code; - private Code(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; @@ -62,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; } @@ -79,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; } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/ParseTypedAsyncJob.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/ParseTypedAsyncJob.java index 4f358b32e0..8b1410ada5 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/ParseTypedAsyncJob.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/ParseTypedAsyncJob.java @@ -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> typeMap = ImmutableMap.> 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> typeMap = ImmutableMap.>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 result = toParse; if (toParse.getResult() != null) { if (toParse.getResult().size() == 1) { - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) Builder builder = AsyncJob.Builder.fromAsyncJobUntyped((AsyncJob) toParse); if (toParse.getResult().containsKey("success")) { builder.result(null); @@ -94,23 +97,23 @@ public class ParseTypedAsyncJob implements Function> bound to name jclouds.cloudstack.jobresult-type-map", - entry.getKey()); + "type key %s not configured. please override default for Map> 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 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; diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/JobComplete.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/JobComplete.java index b5605d4c0a..b288205327 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/JobComplete.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/JobComplete.java @@ -61,15 +61,12 @@ public class JobComplete implements Predicate { return false; } logger.trace("%s: looking for job status %s: currently: %s", job.getId(), 1, job.getStatus()); - if (job.getError() != null || - job.getStatus() == FAILED.code() || - job.getResultCode() == FAIL.code()) { + if (job.hasFailed()) { throw new AsyncJobException(String.format("job %s failed with exception %s", job.toString(), job.getError().toString())); } - return job.getStatus() == SUCCEEDED.code() && - job.getResultCode() == SUCCESS.code(); + return job.hasSucceed(); } private AsyncJob refresh(Long jobId) { diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AsyncJobClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AsyncJobClientLiveTest.java index f36e4f722f..135a36b00a 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AsyncJobClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/AsyncJobClientLiveTest.java @@ -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 { diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/BlockUntilJobCompletesAndReturnResultTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/BlockUntilJobCompletesAndReturnResultTest.java index a0a1a60e43..54f6c32c32 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/BlockUntilJobCompletesAndReturnResultTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/BlockUntilJobCompletesAndReturnResultTest.java @@ -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); diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/ParseAsyncJobFromHttpResponseTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/ParseAsyncJobFromHttpResponseTest.java index f4178ba968..d3b7c038fc 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/ParseAsyncJobFromHttpResponseTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/functions/ParseAsyncJobFromHttpResponseTest.java @@ -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 expects = AsyncJob. builder().id(860).status(0).progress(0) - .resultCode(0).build(); + AsyncJob expects = AsyncJob.builder() + .id(860) + .status(Status.IN_PROGRESS) + .progress(0) + .resultCode(ResultCode.SUCCESS).build(); ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class); @SuppressWarnings("unchecked") AsyncJob response = (AsyncJob) 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 expects = AsyncJob. builder().id(1138).status(1).progress(0) - .resultType("object").resultCode(0).build(); + AsyncJob expects = AsyncJob.builder() + .id(1138) + .status(Status.SUCCEEDED) + .progress(0) + .resultType("object") + .resultCode(ResultCode.SUCCESS).build(); ParseAsyncJobFromHttpResponse parser = i.getInstance(ParseAsyncJobFromHttpResponse.class); @SuppressWarnings("unchecked") AsyncJob response = (AsyncJob) 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 expects = AsyncJob - . 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(); + .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 response = (AsyncJob) 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 response = (AsyncJob) 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 response = (AsyncJob) 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 expects = AsyncJob - . 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() + .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 response = (AsyncJob) 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 expects = AsyncJob - . 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(); + .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 response = (AsyncJob) 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()); } } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/JobCompleteTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/JobCompleteTest.java index 3dfcf086eb..073accf2f0 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/JobCompleteTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/JobCompleteTest.java @@ -18,18 +18,23 @@ */ 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.SUCCESS; -import static org.jclouds.cloudstack.domain.AsyncJob.Status.SUCCEEDED; +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 @@ -37,19 +42,43 @@ import static org.testng.Assert.assertTrue; @Test(groups = "unit", singleThreaded = true) public class JobCompleteTest { - @Test - public void testJobComplete() { - CloudStackClient client = createMock(CloudStackClient.class); - AsyncJobClient asyncJobClient = createMock(AsyncJobClient.class); + 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() { AsyncJob job = AsyncJob.builder().id(100L) - .status(SUCCEEDED.code()).resultCode(SUCCESS.code()).build(); + .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); + } } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/OSCategoryInTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/OSCategoryInTest.java index cc16c3afdd..009ef211d4 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/OSCategoryInTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/OSCategoryInTest.java @@ -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; diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineDestroyedTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineDestroyedTest.java index f2c179830b..d5be70f9b5 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineDestroyedTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineDestroyedTest.java @@ -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; diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineExpungedTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineExpungedTest.java index 11bdbdd702..c583848f5a 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineExpungedTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineExpungedTest.java @@ -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; diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineRunningTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineRunningTest.java index c9485916ad..7a373f5686 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineRunningTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineRunningTest.java @@ -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;