From da0b8702959254a7aada53eee4e09378988031e1 Mon Sep 17 00:00:00 2001 From: Aled Sage Date: Tue, 10 Jan 2012 23:46:26 +0000 Subject: [PATCH] Issue 731: added BaseRestClientExpectTest.orderedRequestsSendResponses, for expecting same request and giving different responses each time --- .../rest/BaseRestClientExpectTest.java | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/org/jclouds/rest/BaseRestClientExpectTest.java b/core/src/test/java/org/jclouds/rest/BaseRestClientExpectTest.java index 30124db3fe..61b54f2796 100644 --- a/core/src/test/java/org/jclouds/rest/BaseRestClientExpectTest.java +++ b/core/src/test/java/org/jclouds/rest/BaseRestClientExpectTest.java @@ -27,10 +27,12 @@ import static org.jclouds.rest.RestContextFactory.createContext; import java.io.IOException; import java.lang.annotation.Retention; import java.lang.annotation.Target; +import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.Map.Entry; +import java.util.Properties; import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicInteger; import javax.inject.Inject; 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.Functions; import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.inject.AbstractModule; @@ -265,7 +268,42 @@ public abstract class BaseRestClientExpectTest { HttpResponse responseB, HttpRequest requestC, HttpResponse responseC, Module 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 requests, final List responses) { + final AtomicInteger counter = new AtomicInteger(0); + + return createClient(new Function() { + @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 * Map parameter.