|
@@ -33,6 +33,7 @@ import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.message.BasicHttpResponse;
|
|
|
import org.apache.http.message.BasicRequestLine;
|
|
|
import org.apache.http.message.BasicStatusLine;
|
|
|
+import org.apache.http.nio.entity.NStringEntity;
|
|
|
import org.elasticsearch.Build;
|
|
|
import org.elasticsearch.ElasticsearchException;
|
|
|
import org.elasticsearch.Version;
|
|
@@ -41,21 +42,26 @@ import org.elasticsearch.action.ActionRequest;
|
|
|
import org.elasticsearch.action.ActionRequestValidationException;
|
|
|
import org.elasticsearch.action.main.MainRequest;
|
|
|
import org.elasticsearch.action.main.MainResponse;
|
|
|
+import org.elasticsearch.action.search.SearchResponse;
|
|
|
+import org.elasticsearch.action.search.SearchResponseSections;
|
|
|
+import org.elasticsearch.action.search.SearchScrollRequest;
|
|
|
+import org.elasticsearch.action.search.ShardSearchFailure;
|
|
|
import org.elasticsearch.cluster.ClusterName;
|
|
|
import org.elasticsearch.common.CheckedFunction;
|
|
|
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
|
|
+import org.elasticsearch.common.xcontent.ToXContent;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
-import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.common.xcontent.cbor.CborXContent;
|
|
|
import org.elasticsearch.common.xcontent.smile.SmileXContent;
|
|
|
import org.elasticsearch.rest.RestStatus;
|
|
|
+import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
+import org.elasticsearch.search.aggregations.InternalAggregations;
|
|
|
import org.elasticsearch.search.suggest.Suggest;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
import org.junit.Before;
|
|
|
import org.mockito.ArgumentMatcher;
|
|
|
-import org.mockito.Matchers;
|
|
|
import org.mockito.internal.matchers.ArrayEquals;
|
|
|
import org.mockito.internal.matchers.VarargMatcher;
|
|
|
|
|
@@ -68,6 +74,7 @@ import java.util.Map;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
+import static org.elasticsearch.client.RestClientTestUtil.randomHeaders;
|
|
|
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
|
|
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
|
|
import static org.mockito.Matchers.anyMapOf;
|
|
@@ -76,6 +83,8 @@ import static org.mockito.Matchers.anyString;
|
|
|
import static org.mockito.Matchers.anyVararg;
|
|
|
import static org.mockito.Matchers.argThat;
|
|
|
import static org.mockito.Matchers.eq;
|
|
|
+import static org.mockito.Matchers.isNotNull;
|
|
|
+import static org.mockito.Matchers.isNull;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
import static org.mockito.Mockito.when;
|
|
@@ -95,49 +104,70 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testPingSuccessful() throws IOException {
|
|
|
- Header[] headers = RestClientTestUtil.randomHeaders(random(), "Header");
|
|
|
+ Header[] headers = randomHeaders(random(), "Header");
|
|
|
Response response = mock(Response.class);
|
|
|
when(response.getStatusLine()).thenReturn(newStatusLine(RestStatus.OK));
|
|
|
when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class),
|
|
|
anyObject(), anyVararg())).thenReturn(response);
|
|
|
assertTrue(restHighLevelClient.ping(headers));
|
|
|
verify(restClient).performRequest(eq("HEAD"), eq("/"), eq(Collections.emptyMap()),
|
|
|
- Matchers.isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
|
|
|
+ isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
|
|
|
}
|
|
|
|
|
|
public void testPing404NotFound() throws IOException {
|
|
|
- Header[] headers = RestClientTestUtil.randomHeaders(random(), "Header");
|
|
|
+ Header[] headers = randomHeaders(random(), "Header");
|
|
|
Response response = mock(Response.class);
|
|
|
when(response.getStatusLine()).thenReturn(newStatusLine(RestStatus.NOT_FOUND));
|
|
|
when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class),
|
|
|
anyObject(), anyVararg())).thenReturn(response);
|
|
|
assertFalse(restHighLevelClient.ping(headers));
|
|
|
verify(restClient).performRequest(eq("HEAD"), eq("/"), eq(Collections.emptyMap()),
|
|
|
- Matchers.isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
|
|
|
+ isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
|
|
|
}
|
|
|
|
|
|
public void testPingSocketTimeout() throws IOException {
|
|
|
- Header[] headers = RestClientTestUtil.randomHeaders(random(), "Header");
|
|
|
+ Header[] headers = randomHeaders(random(), "Header");
|
|
|
when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class),
|
|
|
anyObject(), anyVararg())).thenThrow(new SocketTimeoutException());
|
|
|
expectThrows(SocketTimeoutException.class, () -> restHighLevelClient.ping(headers));
|
|
|
verify(restClient).performRequest(eq("HEAD"), eq("/"), eq(Collections.emptyMap()),
|
|
|
- Matchers.isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
|
|
|
+ isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
|
|
|
}
|
|
|
|
|
|
public void testInfo() throws IOException {
|
|
|
- Header[] headers = RestClientTestUtil.randomHeaders(random(), "Header");
|
|
|
- Response response = mock(Response.class);
|
|
|
+ Header[] headers = randomHeaders(random(), "Header");
|
|
|
MainResponse testInfo = new MainResponse("nodeName", Version.CURRENT, new ClusterName("clusterName"), "clusterUuid",
|
|
|
Build.CURRENT, true);
|
|
|
- when(response.getEntity()).thenReturn(
|
|
|
- new StringEntity(toXContent(testInfo, XContentType.JSON, false).utf8ToString(), ContentType.APPLICATION_JSON));
|
|
|
- when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class),
|
|
|
- anyObject(), anyVararg())).thenReturn(response);
|
|
|
+ mockResponse(testInfo);
|
|
|
MainResponse receivedInfo = restHighLevelClient.info(headers);
|
|
|
assertEquals(testInfo, receivedInfo);
|
|
|
verify(restClient).performRequest(eq("GET"), eq("/"), eq(Collections.emptyMap()),
|
|
|
- Matchers.isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
|
|
|
+ isNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testSearchScroll() throws IOException {
|
|
|
+ Header[] headers = randomHeaders(random(), "Header");
|
|
|
+ SearchResponse mockSearchResponse = new SearchResponse(new SearchResponseSections(SearchHits.empty(), InternalAggregations.EMPTY,
|
|
|
+ null, false, false, null, 1), randomAlphaOfLengthBetween(5, 10), 5, 5, 100, new ShardSearchFailure[0]);
|
|
|
+ mockResponse(mockSearchResponse);
|
|
|
+ SearchResponse searchResponse = restHighLevelClient.searchScroll(new SearchScrollRequest(randomAlphaOfLengthBetween(5, 10)),
|
|
|
+ headers);
|
|
|
+ assertEquals(mockSearchResponse.getScrollId(), searchResponse.getScrollId());
|
|
|
+ assertEquals(0, searchResponse.getHits().totalHits);
|
|
|
+ assertEquals(5, searchResponse.getTotalShards());
|
|
|
+ assertEquals(5, searchResponse.getSuccessfulShards());
|
|
|
+ assertEquals(100, searchResponse.getTook().getMillis());
|
|
|
+ verify(restClient).performRequest(eq("GET"), eq("/_search/scroll"), eq(Collections.emptyMap()),
|
|
|
+ isNotNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void mockResponse(ToXContent toXContent) throws IOException {
|
|
|
+ Response response = mock(Response.class);
|
|
|
+ ContentType contentType = ContentType.parse(Request.REQUEST_BODY_CONTENT_TYPE.mediaType());
|
|
|
+ String requestBody = toXContent(toXContent, Request.REQUEST_BODY_CONTENT_TYPE, false).utf8ToString();
|
|
|
+ when(response.getEntity()).thenReturn(new NStringEntity(requestBody, contentType));
|
|
|
+ when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class),
|
|
|
+ anyObject(), anyVararg())).thenReturn(response);
|
|
|
}
|
|
|
|
|
|
public void testRequestValidation() {
|