mirror of https://github.com/apache/jclouds.git
Merge pull request #202 from andreisavu/list-async
Fix json parsing for listAsyncJobs
This commit is contained in:
commit
8626682535
|
@ -25,30 +25,34 @@ import java.util.Set;
|
|||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import com.google.inject.TypeLiteral;
|
||||
import org.jclouds.cloudstack.domain.AsyncJob;
|
||||
import org.jclouds.domain.JsonBall;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
|
||||
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.inject.Inject;
|
||||
import org.jclouds.json.internal.GsonWrapper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Singleton
|
||||
public class ParseAsyncJobsFromHttpResponse implements Function<HttpResponse, Set<AsyncJob<?>>> {
|
||||
private final UnwrapOnlyNestedJsonValue<Set<AsyncJob<Map<String, JsonBall>>>> parser;
|
||||
private final ParseFirstJsonValueNamed<Set<AsyncJob<Map<String, JsonBall>>>> parser;
|
||||
private final ParseTypedAsyncJob parseTyped;
|
||||
|
||||
@Inject
|
||||
public ParseAsyncJobsFromHttpResponse(ParseTypedAsyncJob parseTyped,
|
||||
UnwrapOnlyNestedJsonValue<Set<AsyncJob<Map<String, JsonBall>>>> parser) {
|
||||
public ParseAsyncJobsFromHttpResponse(ParseTypedAsyncJob parseTyped, GsonWrapper gsonWrapper) {
|
||||
this.parseTyped = checkNotNull(parseTyped, "parseTyped");
|
||||
this.parser = checkNotNull(parser, "parser");
|
||||
this.parser = new ParseFirstJsonValueNamed<Set<AsyncJob<Map<String, JsonBall>>>>(
|
||||
checkNotNull(gsonWrapper, "gsonWrapper"),
|
||||
new TypeLiteral<Set<AsyncJob<Map<String, JsonBall>>>>() { },
|
||||
"asyncjobs");
|
||||
}
|
||||
|
||||
public Set<AsyncJob<?>> apply(HttpResponse response) {
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
package org.jclouds.cloudstack.features;
|
||||
|
||||
import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
|
@ -28,18 +29,20 @@ import org.testng.annotations.Test;
|
|||
|
||||
/**
|
||||
* Tests behavior of {@code AsyncJobClientLiveTest}
|
||||
*
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = "live", singleThreaded = true, testName = "AsyncJobClientLiveTest")
|
||||
public class AsyncJobClientLiveTest extends BaseCloudStackClientLiveTest {
|
||||
// disabled as it takes too long
|
||||
@Test(enabled = false)
|
||||
|
||||
@Test(enabled = true)
|
||||
public void testListAsyncJobs() throws Exception {
|
||||
Set<AsyncJob<?>> response = client.getAsyncJobClient().listAsyncJobs();
|
||||
assert null != response;
|
||||
|
||||
long asyncJobCount = response.size();
|
||||
assertTrue(asyncJobCount >= 0);
|
||||
|
||||
for (AsyncJob<?> asyncJob : response) {
|
||||
assert asyncJob.getCmd() != null : asyncJob;
|
||||
assert asyncJob.getUserId() >= 0 : asyncJob;
|
||||
|
@ -57,12 +60,11 @@ public class AsyncJobClientLiveTest extends BaseCloudStackClientLiveTest {
|
|||
assert query.getStatus().code() >= 0 : query;
|
||||
assert query.getResultCode().code() >= 0 : query;
|
||||
assert query.getProgress() >= 0 : query;
|
||||
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().code() > 400) {
|
||||
if (query.getResultCode() == ResultCode.SUCCESS) {
|
||||
if (query.getResult() != null) {
|
||||
assertEquals(query.getResult().getClass().getPackage(), AsyncJob.class.getPackage());
|
||||
}
|
||||
} else if (query.getResultCode() == ResultCode.FAIL) {
|
||||
assert query.getResult() == null : query;
|
||||
assert query.getError() != null : query;
|
||||
} else {
|
||||
|
|
|
@ -39,7 +39,7 @@ import com.google.inject.Injector;
|
|||
@Test(groups = "unit")
|
||||
public class ParseAsyncJobsFromHttpResponseTest {
|
||||
|
||||
Injector i = Guice.createInjector(new GsonModule() {
|
||||
Injector injector = Guice.createInjector(new GsonModule() {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
|
@ -52,7 +52,7 @@ public class ParseAsyncJobsFromHttpResponseTest {
|
|||
public void testCanParse() {
|
||||
InputStream is = getClass().getResourceAsStream("/listasyncjobsresponse.json");
|
||||
|
||||
ParseAsyncJobsFromHttpResponse parser = i.getInstance(ParseAsyncJobsFromHttpResponse.class);
|
||||
ParseAsyncJobsFromHttpResponse parser = injector.getInstance(ParseAsyncJobsFromHttpResponse.class);
|
||||
Set<AsyncJob<?>> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
|
||||
|
||||
assertEquals(response.size(), 77);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"listasyncjobsresponse": {
|
||||
"count": 77,
|
||||
"asyncjobs": [{
|
||||
"jobid": 1084,
|
||||
"accountid": 3,
|
||||
|
|
Loading…
Reference in New Issue