Skip to content

Commit ebb9884

Browse files
committed
[REST tests] Improved DoSection behaviour
No error wa previously thrown when a catch section was specified and a the rest call returned no error Also, moved the catch:request on top to avoid even sending requests in that case
1 parent dc728b0 commit ebb9884

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

src/test/java/org/elasticsearch/test/rest/section/DoSection.java

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818

1919
package org.elasticsearch.test.rest.section;
2020

21+
import com.google.common.collect.Maps;
2122
import org.elasticsearch.common.Strings;
23+
import org.elasticsearch.common.collect.Tuple;
2224
import org.elasticsearch.common.logging.ESLogger;
2325
import org.elasticsearch.common.logging.Loggers;
2426
import org.elasticsearch.test.rest.RestTestExecutionContext;
2527
import org.elasticsearch.test.rest.client.RestException;
2628
import org.elasticsearch.test.rest.client.RestResponse;
2729

2830
import java.io.IOException;
31+
import java.util.Map;
2932
import java.util.regex.Matcher;
3033
import java.util.regex.Pattern;
3134

35+
import static org.elasticsearch.common.collect.Tuple.tuple;
3236
import static org.hamcrest.Matchers.*;
3337
import static org.junit.Assert.assertThat;
3438
import static org.junit.Assert.fail;
@@ -71,27 +75,31 @@ public void setApiCallSection(ApiCallSection apiCallSection) {
7175
@Override
7276
public void execute(RestTestExecutionContext executionContext) throws IOException {
7377

74-
try {
75-
executionContext.callApi(apiCallSection.getApi(), apiCallSection.getParams(), apiCallSection.getBody());
78+
if ("param".equals(catchParam)) {
79+
//client should throw validation error before sending request
80+
//lets just return without doing anything as we don't have any client to test here
81+
logger.info("found [catch: param], no request sent");
82+
return;
83+
}
7684

85+
try {
86+
RestResponse restResponse = executionContext.callApi(apiCallSection.getApi(), apiCallSection.getParams(), apiCallSection.getBody());
87+
if (Strings.hasLength(catchParam)) {
88+
String catchStatusCode;
89+
if (catches.containsKey(catchParam)) {
90+
catchStatusCode = catches.get(catchParam).v1();
91+
} else if (catchParam.startsWith("/") && catchParam.endsWith("/")) {
92+
catchStatusCode = "4xx|5xx";
93+
} else {
94+
throw new UnsupportedOperationException("catch value [" + catchParam + "] not supported");
95+
}
96+
fail(formatStatusCodeMessage(restResponse, catchStatusCode));
97+
}
7798
} catch(RestException e) {
7899
if (!Strings.hasLength(catchParam)) {
79100
fail(formatStatusCodeMessage(e.restResponse(), "2xx"));
80-
}
81-
82-
if ("param".equals(catchParam)) {
83-
//client should throw validation error before sending request
84-
//lets just return without doing anything as we don't have any client to test here
85-
logger.info("found [catch: param], no request sent");
86-
} else if ("missing".equals(catchParam)) {
87-
assertThat(formatStatusCodeMessage(e.restResponse(), "404"), e.statusCode(), equalTo(404));
88-
} else if ("conflict".equals(catchParam)) {
89-
assertThat(formatStatusCodeMessage(e.restResponse(), "409"), e.statusCode(), equalTo(409));
90-
} else if ("forbidden".equals(catchParam)) {
91-
assertThat(formatStatusCodeMessage(e.restResponse(), "403"), e.statusCode(), equalTo(403));
92-
} else if ("request".equals(catchParam)) {
93-
//generic error response from ES
94-
assertThat(formatStatusCodeMessage(e.restResponse(), "4xx|5xx"), e.statusCode(), greaterThanOrEqualTo(400));
101+
} else if (catches.containsKey(catchParam)) {
102+
assertStatusCode(e.restResponse());
95103
} else if (catchParam.startsWith("/") && catchParam.endsWith("/")) {
96104
//the text of the error message matches regular expression
97105
assertThat(formatStatusCodeMessage(e.restResponse(), "4xx|5xx"), e.statusCode(), greaterThanOrEqualTo(400));
@@ -109,8 +117,23 @@ public void execute(RestTestExecutionContext executionContext) throws IOExceptio
109117
}
110118
}
111119

120+
private void assertStatusCode(RestResponse restResponse) {
121+
Tuple<String, org.hamcrest.Matcher<Integer>> stringMatcherTuple = catches.get(catchParam);
122+
assertThat(formatStatusCodeMessage(restResponse, stringMatcherTuple.v1()),
123+
restResponse.getStatusCode(), stringMatcherTuple.v2());
124+
}
125+
112126
private String formatStatusCodeMessage(RestResponse restResponse, String expected) {
113127
return "expected [" + expected + "] status code but api [" + apiCallSection.getApi() + "] returned ["
114128
+ restResponse.getStatusCode() + " " + restResponse.getReasonPhrase() + "] [" + restResponse.getBody() + "]";
115129
}
130+
131+
private static Map<String, Tuple<String, org.hamcrest.Matcher<Integer>>> catches = Maps.newHashMap();
132+
133+
static {
134+
catches.put("missing", tuple("404", equalTo(404)));
135+
catches.put("conflict", tuple("409", equalTo(409)));
136+
catches.put("forbidden", tuple("403", equalTo(403)));
137+
catches.put("request", tuple("4xx|5xx", greaterThanOrEqualTo(400)));
138+
}
116139
}

0 commit comments

Comments
 (0)