ElasticsearchRestClient implements ElasticsearchClient { @Override public void cleanup(ElasticsearchRequest request) { request.clean(scrollId -> { try { ClearScrollRequest clearRequest = new ClearScrollRequest(); clearRequest.addScrollId(scrollId); client.clearScroll(clearRequest, RequestOptions.DEFAULT); } catch (IOException e) { throw new IllegalStateException( "Failed to clean up resources for search request " + request, e); } }); }  @Override Map<String, IndexMapping> getIndexMappings(String indexExpression); @Override ElasticsearchResponse search(ElasticsearchRequest request); @Override void cleanup(ElasticsearchRequest request); @Override void schedule(Runnable task);  }
@Test void cleanup() throws IOException { ElasticsearchScrollRequest request = new ElasticsearchScrollRequest("test", factory); request.setScrollId("scroll123"); client.cleanup(request); verify(restClient).clearScroll(any(), any()); assertFalse(request.isScrollStarted()); }
@Test void cleanupWithoutScrollId() throws IOException { ElasticsearchScrollRequest request = new ElasticsearchScrollRequest("test", factory); client.cleanup(request); verify(restClient, never()).clearScroll(any(), any()); }
@Test void cleanupWithIOException() throws IOException { when(restClient.clearScroll(any(), any())).thenThrow(new IOException()); ElasticsearchScrollRequest request = new ElasticsearchScrollRequest("test", factory); request.setScrollId("scroll123"); assertThrows(IllegalStateException.class, () -> client.cleanup(request)); }