mirror of
https://github.com/apache/jclouds.git
synced 2025-02-08 19:15:24 +00:00
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 javax.inject.Singleton;
|
||||||
|
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
import org.jclouds.cloudstack.domain.AsyncJob;
|
import org.jclouds.cloudstack.domain.AsyncJob;
|
||||||
import org.jclouds.domain.JsonBall;
|
import org.jclouds.domain.JsonBall;
|
||||||
import org.jclouds.http.HttpResponse;
|
import org.jclouds.http.HttpResponse;
|
||||||
|
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
|
||||||
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue;
|
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
import org.jclouds.json.internal.GsonWrapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
public class ParseAsyncJobsFromHttpResponse implements Function<HttpResponse, Set<AsyncJob<?>>> {
|
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;
|
private final ParseTypedAsyncJob parseTyped;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ParseAsyncJobsFromHttpResponse(ParseTypedAsyncJob parseTyped,
|
public ParseAsyncJobsFromHttpResponse(ParseTypedAsyncJob parseTyped, GsonWrapper gsonWrapper) {
|
||||||
UnwrapOnlyNestedJsonValue<Set<AsyncJob<Map<String, JsonBall>>>> parser) {
|
|
||||||
this.parseTyped = checkNotNull(parseTyped, "parseTyped");
|
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) {
|
public Set<AsyncJob<?>> apply(HttpResponse response) {
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.jclouds.cloudstack.features;
|
package org.jclouds.cloudstack.features;
|
||||||
|
|
||||||
|
import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
import static org.testng.Assert.assertTrue;
|
import static org.testng.Assert.assertTrue;
|
||||||
|
|
||||||
@ -33,13 +34,15 @@ import org.testng.annotations.Test;
|
|||||||
*/
|
*/
|
||||||
@Test(groups = "live", singleThreaded = true, testName = "AsyncJobClientLiveTest")
|
@Test(groups = "live", singleThreaded = true, testName = "AsyncJobClientLiveTest")
|
||||||
public class AsyncJobClientLiveTest extends BaseCloudStackClientLiveTest {
|
public class AsyncJobClientLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
// disabled as it takes too long
|
|
||||||
@Test(enabled = false)
|
@Test(enabled = true)
|
||||||
public void testListAsyncJobs() throws Exception {
|
public void testListAsyncJobs() throws Exception {
|
||||||
Set<AsyncJob<?>> response = client.getAsyncJobClient().listAsyncJobs();
|
Set<AsyncJob<?>> response = client.getAsyncJobClient().listAsyncJobs();
|
||||||
assert null != response;
|
assert null != response;
|
||||||
|
|
||||||
long asyncJobCount = response.size();
|
long asyncJobCount = response.size();
|
||||||
assertTrue(asyncJobCount >= 0);
|
assertTrue(asyncJobCount >= 0);
|
||||||
|
|
||||||
for (AsyncJob<?> asyncJob : response) {
|
for (AsyncJob<?> asyncJob : response) {
|
||||||
assert asyncJob.getCmd() != null : asyncJob;
|
assert asyncJob.getCmd() != null : asyncJob;
|
||||||
assert asyncJob.getUserId() >= 0 : asyncJob;
|
assert asyncJob.getUserId() >= 0 : asyncJob;
|
||||||
@ -57,12 +60,11 @@ public class AsyncJobClientLiveTest extends BaseCloudStackClientLiveTest {
|
|||||||
assert query.getStatus().code() >= 0 : query;
|
assert query.getStatus().code() >= 0 : query;
|
||||||
assert query.getResultCode().code() >= 0 : query;
|
assert query.getResultCode().code() >= 0 : query;
|
||||||
assert query.getProgress() >= 0 : query;
|
assert query.getProgress() >= 0 : query;
|
||||||
if (query.getResultCode().code() == 0) {
|
if (query.getResultCode() == ResultCode.SUCCESS) {
|
||||||
if (query.getResult() != null)// null is ok for result of success =
|
if (query.getResult() != null) {
|
||||||
// true
|
assertEquals(query.getResult().getClass().getPackage(), AsyncJob.class.getPackage());
|
||||||
// ensure we parsed properly
|
}
|
||||||
assert (query.getResult().getClass().getPackage().equals(AsyncJob.class.getPackage())) : query;
|
} else if (query.getResultCode() == ResultCode.FAIL) {
|
||||||
} else if (query.getResultCode().code() > 400) {
|
|
||||||
assert query.getResult() == null : query;
|
assert query.getResult() == null : query;
|
||||||
assert query.getError() != null : query;
|
assert query.getError() != null : query;
|
||||||
} else {
|
} else {
|
||||||
|
@ -39,7 +39,7 @@ import com.google.inject.Injector;
|
|||||||
@Test(groups = "unit")
|
@Test(groups = "unit")
|
||||||
public class ParseAsyncJobsFromHttpResponseTest {
|
public class ParseAsyncJobsFromHttpResponseTest {
|
||||||
|
|
||||||
Injector i = Guice.createInjector(new GsonModule() {
|
Injector injector = Guice.createInjector(new GsonModule() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
@ -52,7 +52,7 @@ public class ParseAsyncJobsFromHttpResponseTest {
|
|||||||
public void testCanParse() {
|
public void testCanParse() {
|
||||||
InputStream is = getClass().getResourceAsStream("/listasyncjobsresponse.json");
|
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)));
|
Set<AsyncJob<?>> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
|
||||||
|
|
||||||
assertEquals(response.size(), 77);
|
assertEquals(response.size(), 77);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"listasyncjobsresponse": {
|
"listasyncjobsresponse": {
|
||||||
|
"count": 77,
|
||||||
"asyncjobs": [{
|
"asyncjobs": [{
|
||||||
"jobid": 1084,
|
"jobid": 1084,
|
||||||
"accountid": 3,
|
"accountid": 3,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user