18
18
19
19
package org .elasticsearch .test .rest .section ;
20
20
21
+ import com .google .common .collect .Maps ;
21
22
import org .elasticsearch .common .Strings ;
23
+ import org .elasticsearch .common .collect .Tuple ;
22
24
import org .elasticsearch .common .logging .ESLogger ;
23
25
import org .elasticsearch .common .logging .Loggers ;
24
26
import org .elasticsearch .test .rest .RestTestExecutionContext ;
25
27
import org .elasticsearch .test .rest .client .RestException ;
26
28
import org .elasticsearch .test .rest .client .RestResponse ;
27
29
28
30
import java .io .IOException ;
31
+ import java .util .Map ;
29
32
import java .util .regex .Matcher ;
30
33
import java .util .regex .Pattern ;
31
34
35
+ import static org .elasticsearch .common .collect .Tuple .tuple ;
32
36
import static org .hamcrest .Matchers .*;
33
37
import static org .junit .Assert .assertThat ;
34
38
import static org .junit .Assert .fail ;
@@ -71,27 +75,31 @@ public void setApiCallSection(ApiCallSection apiCallSection) {
71
75
@ Override
72
76
public void execute (RestTestExecutionContext executionContext ) throws IOException {
73
77
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
+ }
76
84
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
+ }
77
98
} catch (RestException e ) {
78
99
if (!Strings .hasLength (catchParam )) {
79
100
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 ());
95
103
} else if (catchParam .startsWith ("/" ) && catchParam .endsWith ("/" )) {
96
104
//the text of the error message matches regular expression
97
105
assertThat (formatStatusCodeMessage (e .restResponse (), "4xx|5xx" ), e .statusCode (), greaterThanOrEqualTo (400 ));
@@ -109,8 +117,23 @@ public void execute(RestTestExecutionContext executionContext) throws IOExceptio
109
117
}
110
118
}
111
119
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
+
112
126
private String formatStatusCodeMessage (RestResponse restResponse , String expected ) {
113
127
return "expected [" + expected + "] status code but api [" + apiCallSection .getApi () + "] returned ["
114
128
+ restResponse .getStatusCode () + " " + restResponse .getReasonPhrase () + "] [" + restResponse .getBody () + "]" ;
115
129
}
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
+ }
116
139
}
0 commit comments