1919import org .elasticsearch .rest .RestStatus ;
2020import org .elasticsearch .test .ESTestCase ;
2121import org .elasticsearch .test .rest .ESRestTestCase ;
22+ import org .elasticsearch .test .rest .ObjectPath ;
2223
2324import java .io .Closeable ;
2425import java .io .IOException ;
2728import java .util .function .UnaryOperator ;
2829import java .util .stream .Collectors ;
2930
30- import static org .hamcrest .Matchers .allOf ;
3131import static org .hamcrest .Matchers .containsString ;
3232import static org .hamcrest .Matchers .equalTo ;
3333
@@ -152,10 +152,9 @@ private void testNonexistentBucket(Boolean readonly) throws Exception {
152152
153153 final var responseException = expectThrows (ResponseException .class , () -> client ().performRequest (registerRequest ));
154154 assertEquals (RestStatus .INTERNAL_SERVER_ERROR .getStatus (), responseException .getResponse ().getStatusLine ().getStatusCode ());
155- assertThat (
156- responseException .getMessage (),
157- allOf (containsString ("repository_verification_exception" ), containsString ("is not accessible on master node" ))
158- );
155+ final var responseObjectPath = ObjectPath .createFromResponse (responseException .getResponse ());
156+ assertThat (responseObjectPath .evaluate ("error.type" ), equalTo ("repository_verification_exception" ));
157+ assertThat (responseObjectPath .evaluate ("error.reason" ), containsString ("is not accessible on master node" ));
159158 }
160159
161160 public void testNonexistentClient () throws Exception {
@@ -181,15 +180,11 @@ private void testNonexistentClient(Boolean readonly) throws Exception {
181180
182181 final var responseException = expectThrows (ResponseException .class , () -> client ().performRequest (registerRequest ));
183182 assertEquals (RestStatus .INTERNAL_SERVER_ERROR .getStatus (), responseException .getResponse ().getStatusLine ().getStatusCode ());
184- assertThat (
185- responseException .getMessage (),
186- allOf (
187- containsString ("repository_verification_exception" ),
188- containsString ("is not accessible on master node" ),
189- containsString ("illegal_argument_exception" ),
190- containsString ("Unknown s3 client name" )
191- )
192- );
183+ final var responseObjectPath = ObjectPath .createFromResponse (responseException .getResponse ());
184+ assertThat (responseObjectPath .evaluate ("error.type" ), equalTo ("repository_verification_exception" ));
185+ assertThat (responseObjectPath .evaluate ("error.reason" ), containsString ("is not accessible on master node" ));
186+ assertThat (responseObjectPath .evaluate ("error.caused_by.type" ), equalTo ("illegal_argument_exception" ));
187+ assertThat (responseObjectPath .evaluate ("error.caused_by.reason" ), containsString ("Unknown s3 client name" ));
193188 }
194189
195190 public void testNonexistentSnapshot () throws Exception {
@@ -212,21 +207,24 @@ private void testNonexistentSnapshot(Boolean readonly) throws Exception {
212207 final var getSnapshotRequest = new Request ("GET" , "/_snapshot/" + repositoryName + "/" + randomIdentifier ());
213208 final var getSnapshotException = expectThrows (ResponseException .class , () -> client ().performRequest (getSnapshotRequest ));
214209 assertEquals (RestStatus .NOT_FOUND .getStatus (), getSnapshotException .getResponse ().getStatusLine ().getStatusCode ());
215- assertThat (getSnapshotException .getMessage (), containsString ("snapshot_missing_exception" ));
210+ final var getResponseObjectPath = ObjectPath .createFromResponse (getSnapshotException .getResponse ());
211+ assertThat (getResponseObjectPath .evaluate ("error.type" ), equalTo ("snapshot_missing_exception" ));
216212
217213 final var restoreRequest = new Request ("POST" , "/_snapshot/" + repositoryName + "/" + randomIdentifier () + "/_restore" );
218214 if (randomBoolean ()) {
219215 restoreRequest .addParameter ("wait_for_completion" , Boolean .toString (randomBoolean ()));
220216 }
221217 final var restoreException = expectThrows (ResponseException .class , () -> client ().performRequest (restoreRequest ));
222218 assertEquals (RestStatus .INTERNAL_SERVER_ERROR .getStatus (), restoreException .getResponse ().getStatusLine ().getStatusCode ());
223- assertThat (restoreException .getMessage (), containsString ("snapshot_restore_exception" ));
219+ final var restoreResponseObjectPath = ObjectPath .createFromResponse (restoreException .getResponse ());
220+ assertThat (restoreResponseObjectPath .evaluate ("error.type" ), equalTo ("snapshot_restore_exception" ));
224221
225222 if (readonly != Boolean .TRUE ) {
226223 final var deleteRequest = new Request ("DELETE" , "/_snapshot/" + repositoryName + "/" + randomIdentifier ());
227224 final var deleteException = expectThrows (ResponseException .class , () -> client ().performRequest (deleteRequest ));
228225 assertEquals (RestStatus .NOT_FOUND .getStatus (), deleteException .getResponse ().getStatusLine ().getStatusCode ());
229- assertThat (deleteException .getMessage (), containsString ("snapshot_missing_exception" ));
226+ final var deleteResponseObjectPath = ObjectPath .createFromResponse (deleteException .getResponse ());
227+ assertThat (deleteResponseObjectPath .evaluate ("error.type" ), equalTo ("snapshot_missing_exception" ));
230228 }
231229 }
232230 }
0 commit comments