|
@@ -39,6 +39,7 @@ import org.elasticsearch.action.ActionRequest;
|
|
|
import org.elasticsearch.action.ActionRequestValidationException;
|
|
|
import org.elasticsearch.action.main.MainRequest;
|
|
|
import org.elasticsearch.common.CheckedFunction;
|
|
|
+import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
import org.elasticsearch.common.xcontent.cbor.CborXContent;
|
|
@@ -54,9 +55,9 @@ import org.mockito.internal.matchers.VarargMatcher;
|
|
|
import java.io.IOException;
|
|
|
import java.net.SocketTimeoutException;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
-import java.util.function.Function;
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
|
|
import static org.mockito.Matchers.anyMapOf;
|
|
@@ -138,17 +139,17 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
|
|
|
public void testParseEntity() throws IOException {
|
|
|
{
|
|
|
- IllegalStateException ise = expectThrows(IllegalStateException.class, () -> RestHighLevelClient.parseEntity(null, null));
|
|
|
+ IllegalStateException ise = expectThrows(IllegalStateException.class, () -> restHighLevelClient.parseEntity(null, null));
|
|
|
assertEquals("Response body expected but not returned", ise.getMessage());
|
|
|
}
|
|
|
{
|
|
|
IllegalStateException ise = expectThrows(IllegalStateException.class,
|
|
|
- () -> RestHighLevelClient.parseEntity(new StringEntity("", (ContentType) null), null));
|
|
|
+ () -> restHighLevelClient.parseEntity(new StringEntity("", (ContentType) null), null));
|
|
|
assertEquals("Elasticsearch didn't return the [Content-Type] header, unable to parse response body", ise.getMessage());
|
|
|
}
|
|
|
{
|
|
|
StringEntity entity = new StringEntity("", ContentType.APPLICATION_SVG_XML);
|
|
|
- IllegalStateException ise = expectThrows(IllegalStateException.class, () -> RestHighLevelClient.parseEntity(entity, null));
|
|
|
+ IllegalStateException ise = expectThrows(IllegalStateException.class, () -> restHighLevelClient.parseEntity(entity, null));
|
|
|
assertEquals("Unsupported Content-Type: " + entity.getContentType().getValue(), ise.getMessage());
|
|
|
}
|
|
|
{
|
|
@@ -161,13 +162,13 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
return value;
|
|
|
};
|
|
|
HttpEntity jsonEntity = new StringEntity("{\"field\":\"value\"}", ContentType.APPLICATION_JSON);
|
|
|
- assertEquals("value", RestHighLevelClient.parseEntity(jsonEntity, entityParser));
|
|
|
+ assertEquals("value", restHighLevelClient.parseEntity(jsonEntity, entityParser));
|
|
|
HttpEntity yamlEntity = new StringEntity("---\nfield: value\n", ContentType.create("application/yaml"));
|
|
|
- assertEquals("value", RestHighLevelClient.parseEntity(yamlEntity, entityParser));
|
|
|
+ assertEquals("value", restHighLevelClient.parseEntity(yamlEntity, entityParser));
|
|
|
HttpEntity smileEntity = createBinaryEntity(SmileXContent.contentBuilder(), ContentType.create("application/smile"));
|
|
|
- assertEquals("value", RestHighLevelClient.parseEntity(smileEntity, entityParser));
|
|
|
+ assertEquals("value", restHighLevelClient.parseEntity(smileEntity, entityParser));
|
|
|
HttpEntity cborEntity = createBinaryEntity(CborXContent.contentBuilder(), ContentType.create("application/cbor"));
|
|
|
- assertEquals("value", RestHighLevelClient.parseEntity(cborEntity, entityParser));
|
|
|
+ assertEquals("value", restHighLevelClient.parseEntity(cborEntity, entityParser));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -194,7 +195,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
|
|
|
Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
|
|
|
ResponseException responseException = new ResponseException(response);
|
|
|
- ElasticsearchException elasticsearchException = RestHighLevelClient.parseResponseException(responseException);
|
|
|
+ ElasticsearchException elasticsearchException = restHighLevelClient.parseResponseException(responseException);
|
|
|
assertEquals(responseException.getMessage(), elasticsearchException.getMessage());
|
|
|
assertEquals(restStatus, elasticsearchException.status());
|
|
|
assertSame(responseException, elasticsearchException.getCause());
|
|
@@ -206,7 +207,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
ContentType.APPLICATION_JSON));
|
|
|
Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
|
|
|
ResponseException responseException = new ResponseException(response);
|
|
|
- ElasticsearchException elasticsearchException = RestHighLevelClient.parseResponseException(responseException);
|
|
|
+ ElasticsearchException elasticsearchException = restHighLevelClient.parseResponseException(responseException);
|
|
|
assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage());
|
|
|
assertEquals(restStatus, elasticsearchException.status());
|
|
|
assertSame(responseException, elasticsearchException.getSuppressed()[0]);
|
|
@@ -217,7 +218,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
httpResponse.setEntity(new StringEntity("{\"error\":", ContentType.APPLICATION_JSON));
|
|
|
Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
|
|
|
ResponseException responseException = new ResponseException(response);
|
|
|
- ElasticsearchException elasticsearchException = RestHighLevelClient.parseResponseException(responseException);
|
|
|
+ ElasticsearchException elasticsearchException = restHighLevelClient.parseResponseException(responseException);
|
|
|
assertEquals("Unable to parse response body", elasticsearchException.getMessage());
|
|
|
assertEquals(restStatus, elasticsearchException.status());
|
|
|
assertSame(responseException, elasticsearchException.getCause());
|
|
@@ -229,7 +230,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
httpResponse.setEntity(new StringEntity("{\"status\":" + restStatus.getStatus() + "}", ContentType.APPLICATION_JSON));
|
|
|
Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
|
|
|
ResponseException responseException = new ResponseException(response);
|
|
|
- ElasticsearchException elasticsearchException = RestHighLevelClient.parseResponseException(responseException);
|
|
|
+ ElasticsearchException elasticsearchException = restHighLevelClient.parseResponseException(responseException);
|
|
|
assertEquals("Unable to parse response body", elasticsearchException.getMessage());
|
|
|
assertEquals(restStatus, elasticsearchException.status());
|
|
|
assertSame(responseException, elasticsearchException.getCause());
|
|
@@ -390,7 +391,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
public void testWrapResponseListenerOnSuccess() throws IOException {
|
|
|
{
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet());
|
|
|
RestStatus restStatus = randomFrom(RestStatus.values());
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
|
|
@@ -400,7 +401,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
}
|
|
|
{
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> {throw new IllegalStateException();}, trackingActionListener, Collections.emptySet());
|
|
|
RestStatus restStatus = randomFrom(RestStatus.values());
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
|
|
@@ -415,7 +416,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
|
|
|
public void testWrapResponseListenerOnException() throws IOException {
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet());
|
|
|
IllegalStateException exception = new IllegalStateException();
|
|
|
responseListener.onFailure(exception);
|
|
@@ -424,7 +425,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
|
|
|
public void testWrapResponseListenerOnResponseExceptionWithoutEntity() throws IOException {
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet());
|
|
|
RestStatus restStatus = randomFrom(RestStatus.values());
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
|
|
@@ -440,7 +441,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
|
|
|
public void testWrapResponseListenerOnResponseExceptionWithEntity() throws IOException {
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet());
|
|
|
RestStatus restStatus = randomFrom(RestStatus.values());
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
|
|
@@ -459,7 +460,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
public void testWrapResponseListenerOnResponseExceptionWithBrokenEntity() throws IOException {
|
|
|
{
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet());
|
|
|
RestStatus restStatus = randomFrom(RestStatus.values());
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
|
|
@@ -476,7 +477,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
}
|
|
|
{
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet());
|
|
|
RestStatus restStatus = randomFrom(RestStatus.values());
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
|
|
@@ -495,7 +496,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
|
|
|
public void testWrapResponseListenerOnResponseExceptionWithIgnores() throws IOException {
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.singleton(404));
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND));
|
|
|
Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
|
|
@@ -510,7 +511,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
//response parsing throws exception while handling ignores. same as when GetResponse#fromXContent throws error when trying
|
|
|
//to parse a 404 response which contains an error rather than a valid document not found response.
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> { throw new IllegalStateException(); }, trackingActionListener, Collections.singleton(404));
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND));
|
|
|
Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
|
|
@@ -527,7 +528,7 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
TrackingActionListener trackingActionListener = new TrackingActionListener();
|
|
|
//response parsing throws exception while handling ignores. same as when GetResponse#fromXContent throws error when trying
|
|
|
//to parse a 404 response which contains an error rather than a valid document not found response.
|
|
|
- ResponseListener responseListener = RestHighLevelClient.wrapResponseListener(
|
|
|
+ ResponseListener responseListener = restHighLevelClient.wrapResponseListener(
|
|
|
response -> { throw new IllegalStateException(); }, trackingActionListener, Collections.singleton(404));
|
|
|
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND));
|
|
|
httpResponse.setEntity(new StringEntity("{\"error\":\"test error message\",\"status\":404}",
|
|
@@ -542,6 +543,11 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|
|
assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage());
|
|
|
}
|
|
|
|
|
|
+ public void testNamedXContents() throws IOException {
|
|
|
+ List<NamedXContentRegistry.Entry> namedXContents = RestHighLevelClient.getNamedXContents();
|
|
|
+ assertEquals(0, namedXContents.size());
|
|
|
+ }
|
|
|
+
|
|
|
private static class TrackingActionListener implements ActionListener<Integer> {
|
|
|
private final AtomicInteger statusCode = new AtomicInteger(-1);
|
|
|
private final AtomicReference<Exception> exception = new AtomicReference<>();
|