Upgrade mockito to 3.12.4 (#79669) (#79686)

Securemock is a wrapper around Mockito that monkey patches internals of
Mockito to work with the SecurityManager. However, the library has not
been updated in several years due to the complicated nature of this
monkey patching. This has left us with an ancient version of Mockito,
missing out on updates to the library in the last half decade.

While Securemock currently works with Mockito 1.x, in 2.x an official
means of plugging into mockito was added, MockMaker. This commit removes
securemock as a dependnecy of the test framework, replacing it with a
modern version of Mockito, and implementing a MockMaker that integrates
with SecurityManager.

Note that while there is a newer version of Mockito available, 4.0, it
has several deprecations removed that are used throughout Elasticsearch.
Those can be addressed in followups, and then a subsequent upgrade to
4.0 should be possible.

relates #79567
closes #40334
This commit is contained in:
Ryan Ernst 2021-10-24 10:46:03 -07:00 committed by GitHub
parent 262db33192
commit 09c88797d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
86 changed files with 509 additions and 312 deletions

View file

@ -68,13 +68,13 @@ public class CustomRestHighLevelClientTests extends ESTestCase {
doAnswer(inv -> mockPerformRequest((Request) inv.getArguments()[0]))
.when(restClient)
.performRequest(argThat(new RequestMatcher("GET", ENDPOINT)));
.performRequest(argThat(new RequestMatcher("GET", ENDPOINT)::matches));
doAnswer(inv -> mockPerformRequestAsync(
((Request) inv.getArguments()[0]),
(ResponseListener) inv.getArguments()[1]))
.when(restClient)
.performRequestAsync(argThat(new RequestMatcher("GET", ENDPOINT)), any(ResponseListener.class));
.performRequestAsync(argThat(new RequestMatcher("GET", ENDPOINT)::matches), any(ResponseListener.class));
}
}

View file

@ -236,7 +236,7 @@ public class RestHighLevelClientTests extends ESTestCase {
}
when(restClient
.performRequestAsync(argThat(new RequestMatcher("GET", "/")), any()))
.performRequestAsync(argThat(new RequestMatcher("GET", "/")::matches), any()))
.thenAnswer(i -> {
((ResponseListener)i.getArguments()[1]).onSuccess(response);
return Cancellable.NO_OP;
@ -1039,7 +1039,8 @@ public class RestHighLevelClientTests extends ESTestCase {
Build build = new Build(Build.Flavor.DEFAULT, Build.Type.UNKNOWN, "hash", "date", false, version);
mockGetRoot(restClient, build, setProductHeader);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")))).thenReturn(apiResponse);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches)))
.thenReturn(apiResponse);
RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList());
@ -1086,7 +1087,8 @@ public class RestHighLevelClientTests extends ESTestCase {
when(apiStatus.getStatusCode()).thenReturn(200);
Response apiResponse = mock(Response.class);
when(apiResponse.getStatusLine()).thenReturn(apiStatus);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")))).thenReturn(apiResponse);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches)))
.thenReturn(apiResponse);
RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList());
@ -1124,7 +1126,8 @@ public class RestHighLevelClientTests extends ESTestCase {
when(apiStatus.getStatusCode()).thenReturn(200);
Response apiResponse = mock(Response.class);
when(apiResponse.getStatusLine()).thenReturn(apiStatus);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")))).thenReturn(apiResponse);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches)))
.thenReturn(apiResponse);
RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList());
@ -1166,10 +1169,11 @@ public class RestHighLevelClientTests extends ESTestCase {
when(apiStatus.getStatusCode()).thenReturn(200);
Response apiResponse = mock(Response.class);
when(apiResponse.getStatusLine()).thenReturn(apiStatus);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")))).thenReturn(apiResponse);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches)))
.thenReturn(apiResponse);
// Have the verification request fail
when(restClient.performRequestAsync(argThat(new RequestMatcher("GET", "/")), any()))
when(restClient.performRequestAsync(argThat(new RequestMatcher("GET", "/")::matches), any()))
.thenAnswer(i -> {
((ResponseListener)i.getArguments()[1]).onFailure(new IOException("Something bad happened"));
return Cancellable.NO_OP;
@ -1198,10 +1202,11 @@ public class RestHighLevelClientTests extends ESTestCase {
when(apiStatus.getStatusCode()).thenReturn(200);
Response apiResponse = mock(Response.class);
when(apiResponse.getStatusLine()).thenReturn(apiStatus);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")))).thenReturn(apiResponse);
when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches)))
.thenReturn(apiResponse);
// Have the info endpoint used for verification return a 403 (forbidden)
when(restClient.performRequestAsync(argThat(new RequestMatcher("GET", "/")), any()))
when(restClient.performRequestAsync(argThat(new RequestMatcher("GET", "/")::matches), any()))
.thenAnswer(i -> {
StatusLine infoStatus = mock(StatusLine.class);
when(apiStatus.getStatusCode()).thenReturn(HttpStatus.SC_FORBIDDEN);
@ -1224,7 +1229,8 @@ public class RestHighLevelClientTests extends ESTestCase {
mockGetRoot(restClient);
Cancellable cancellable = mock(Cancellable.class);
when(restClient.performRequestAsync(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")), any())).thenReturn(cancellable);
when(restClient.performRequestAsync(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches), any()))
.thenReturn(cancellable);
Cancellable result = restHighLevelClient.existsSourceAsync(
new GetSourceRequest("foo", "bar"),