Issue 731: added BaseRestClientExpectTest.orderedRequestsSendResponses, for expecting same request and giving different responses each time

This commit is contained in:
Aled Sage 2012-01-10 23:46:26 +00:00 committed by Adrian Cole
parent 15621101da
commit da0b870295
1 changed files with 40 additions and 2 deletions

View File

@ -27,10 +27,12 @@ import static org.jclouds.rest.RestContextFactory.createContext;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -60,6 +62,7 @@ import com.google.common.annotations.Beta;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Functions; import com.google.common.base.Functions;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
@ -266,6 +269,41 @@ public abstract class BaseRestClientExpectTest<S> {
return requestsSendResponses(ImmutableMap.of(requestA, responseA, requestB, responseB, requestC, responseC), module); return requestsSendResponses(ImmutableMap.of(requestA, responseA, requestB, responseB, requestC, responseC), module);
} }
public S orderedRequestsSendResponses(HttpRequest requestA, HttpResponse responseA) {
return orderedRequestsSendResponses(ImmutableList.of(requestA), ImmutableList.of(responseA));
}
public S orderedRequestsSendResponses(HttpRequest requestA, HttpResponse responseA, HttpRequest requestB,
HttpResponse responseB) {
return orderedRequestsSendResponses(ImmutableList.of(requestA, requestB), ImmutableList.of(responseA, responseB));
}
public S orderedRequestsSendResponses(HttpRequest requestA, HttpResponse responseA, HttpRequest requestB,
HttpResponse responseB, HttpRequest requestC, HttpResponse responseC) {
return orderedRequestsSendResponses(ImmutableList.of(requestA, requestB, requestC), ImmutableList.of(responseA, responseB, responseC));
}
public S orderedRequestsSendResponses(HttpRequest requestA, HttpResponse responseA, HttpRequest requestB,
HttpResponse responseB, HttpRequest requestC, HttpResponse responseC, HttpRequest requestD, HttpResponse responseD) {
return orderedRequestsSendResponses(ImmutableList.of(requestA, requestB, requestC, requestD), ImmutableList.of(responseA, responseB, responseC, responseD));
}
public S orderedRequestsSendResponses(final List<HttpRequest> requests, final List<HttpResponse> responses) {
final AtomicInteger counter = new AtomicInteger(0);
return createClient(new Function<HttpRequest,HttpResponse>() {
@Override
public HttpResponse apply(HttpRequest input) {
int index = counter.getAndIncrement();
if (index >= requests.size()) {
throw new IndexOutOfBoundsException("request index "+index+", but only "+requests.size()+" request/response pairs");
}
assert input.equals(requests.get(index)) : "expected="+requests.get(index)+"; actual="+input;
return responses.get(index);
}
});
}
/** /**
* creates a client for a mock server which returns responses for requests based on the supplied * creates a client for a mock server which returns responses for requests based on the supplied
* Map parameter. * Map parameter.