ElasticsearchRestClient implements ElasticsearchClient { @Override public ElasticsearchResponse search(ElasticsearchRequest request) { return request.search( req -> { try { return client.search(req, RequestOptions.DEFAULT); } catch (IOException e) { throw new IllegalStateException( "Failed to perform search operation with request " + req, e); } }, req -> { try { return client.scroll(req, RequestOptions.DEFAULT); } catch (IOException e) { throw new IllegalStateException( "Failed to perform scroll operation with request " + req, 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 search() throws IOException { SearchResponse searchResponse = mock(SearchResponse.class); when(restClient.search(any(), any())).thenReturn(searchResponse); when(searchResponse.getScrollId()).thenReturn("scroll123"); when(searchResponse.getHits()) .thenReturn( new SearchHits( new SearchHit[] {searchHit}, new TotalHits(1L, TotalHits.Relation.EQUAL_TO), 1.0F)); when(searchHit.getSourceAsString()).thenReturn("{\"id\", 1}"); when(factory.construct(any())).thenReturn(exprTupleValue); SearchResponse scrollResponse = mock(SearchResponse.class); when(restClient.scroll(any(), any())).thenReturn(scrollResponse); when(scrollResponse.getScrollId()).thenReturn("scroll456"); when(scrollResponse.getHits()).thenReturn(SearchHits.empty()); ElasticsearchScrollRequest request = new ElasticsearchScrollRequest("test", factory); ElasticsearchResponse response1 = client.search(request); assertFalse(response1.isEmpty()); Iterator<ExprValue> hits = response1.iterator(); assertTrue(hits.hasNext()); assertEquals(exprTupleValue, hits.next()); assertFalse(hits.hasNext()); ElasticsearchResponse response2 = client.search(request); assertTrue(response2.isEmpty()); }
@Test void searchWithIOException() throws IOException { when(restClient.search(any(), any())).thenThrow(new IOException()); assertThrows( IllegalStateException.class, () -> client.search(new ElasticsearchScrollRequest("test", factory))); }
@Test void scrollWithIOException() throws IOException { SearchResponse searchResponse = mock(SearchResponse.class); when(restClient.search(any(), any())).thenReturn(searchResponse); when(searchResponse.getScrollId()).thenReturn("scroll123"); when(searchResponse.getHits()) .thenReturn( new SearchHits( new SearchHit[] {new SearchHit(1)}, new TotalHits(1L, TotalHits.Relation.EQUAL_TO), 1.0F)); when(restClient.scroll(any(), any())).thenThrow(new IOException()); ElasticsearchScrollRequest scrollRequest = new ElasticsearchScrollRequest("test", factory); client.search(scrollRequest); assertThrows( IllegalStateException.class, () -> client.search(scrollRequest)); }