<test>[testlib]: add final handler for ut test
Add final handler as the last handler of http call simulator and used
to test random return of a http call
@@ -92,6 +92,7 @@ class EnvSpec extends ApiHelper implements Node {
private boolean hasCreated
private ConcurrentHashMap<String, Closure> httpHandlers = [:]
private ConcurrentHashMap<String, Closure> httpPostHandlers = [:]
+ private ConcurrentHashMap<String, Closure> httpFinalHandlers = [:]
private ConcurrentHashMap<String, AtomicInteger> httpHandlerCounters = [:]
private ConcurrentHashMap<String, AtomicInteger> httpPostHandlerCounters = [:]
private ConcurrentHashMap<String, Closure> defaultHttpHandlers = [:]
@@ -252,6 +253,10 @@ class EnvSpec extends ApiHelper implements Node {
httpPostHandlers.putAll(defaultHttpPostHandlers)
}
+ void cleanFinalSimulatorHandlers() {
+ httpFinalHandlers.clear()
+ }
+
void cleanMessageHandlers() {
messageHandlers.clear()
messageHandlerCounters.clear()
@@ -878,6 +883,10 @@ class EnvSpec extends ApiHelper implements Node {
httpPostHandlers[path] = c
}
+ void hijackSimulator(String path, Closure c) {
+ httpFinalHandlers[path] = c
+ }
+
void conditionSimulator(String path, Closure condition, Closure c) {
def lst = httpConditionHandlers[path]
if (lst == null) {
@@ -999,6 +1008,27 @@ class EnvSpec extends ApiHelper implements Node {
httpPostHandlerCounters.get(url).incrementAndGet()
}
+ Closure finalHandler = httpFinalHandlers[url]
+
+ if (finalHandler == null) {
+ for (String httpUrl : httpFinalHandlers.keys()) {
+ if (Pattern.matches(httpUrl, url)) {
+ finalHandler = httpFinalHandlers.get(httpUrl)
+ break
+ }
+ }
+ }
+
+ if (finalHandler != null) {
+ if (finalHandler.maximumNumberOfParameters <= 1) {
+ ret = finalHandler(ret)
+ } else if (finalHandler.maximumNumberOfParameters == 2) {
+ ret = finalHandler(ret, entity)
+ } else {
+ ret = finalHandler(ret, entity, this)
+ }
+ }
+
if (ret == null) {
ret = [:]
}
