Search in sources :

Example 1 with CheckedFunction

use of org.elasticsearch.common.CheckedFunction in project elasticsearch by elastic.

the class ConstructingObjectParserTests method testNullDeclares.

public void testNullDeclares() {
    ConstructingObjectParser<Void, Void> objectParser = new ConstructingObjectParser<>("foo", a -> null);
    Exception e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[consumer] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (ContextParser<Void, Object>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (CheckedFunction<XContentParser, Object, IOException>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, null, ObjectParser.ValueType.STRING));
    assertEquals("[parseField] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, new ParseField("test"), null));
    assertEquals("[type] is required", e.getMessage());
}
Also used : Arrays(java.util.Arrays) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ConstructingObjectParser.constructorArg(org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg) Nullable(org.elasticsearch.common.Nullable) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CheckedFunction(org.elasticsearch.common.CheckedFunction) List(java.util.List) Matcher(org.hamcrest.Matcher) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ConstructingObjectParser.optionalConstructorArg(org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg) ESTestCase(org.elasticsearch.test.ESTestCase) ParseField(org.elasticsearch.common.ParseField) Matchers.anyOf(org.hamcrest.Matchers.anyOf) IOException(java.io.IOException) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) ParseField(org.elasticsearch.common.ParseField)

Example 2 with CheckedFunction

use of org.elasticsearch.common.CheckedFunction in project elasticsearch by elastic.

the class ObjectParserTests method testNullDeclares.

public void testNullDeclares() {
    ObjectParser<Void, Void> objectParser = new ObjectParser<>("foo");
    Exception e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[consumer] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (ContextParser<Void, Object>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (CheckedFunction<XContentParser, Object, IOException>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, null, ObjectParser.ValueType.STRING));
    assertEquals("[parseField] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, new ParseField("test"), null));
    assertEquals("[type] is required", e.getMessage());
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) Arrays(java.util.Arrays) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) CheckedFunction(org.elasticsearch.common.CheckedFunction) NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) List(java.util.List) ValueType(org.elasticsearch.common.xcontent.ObjectParser.ValueType) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) URI(java.net.URI) ParseField(org.elasticsearch.common.ParseField) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ParseField(org.elasticsearch.common.ParseField)

Example 3 with CheckedFunction

use of org.elasticsearch.common.CheckedFunction in project elasticsearch by elastic.

the class RestHighLevelClientTests method testPerformRequestOnSuccess.

public void testPerformRequestOnSuccess() throws IOException {
    MainRequest mainRequest = new MainRequest();
    CheckedFunction<MainRequest, Request, IOException> requestConverter = request -> new Request("GET", "/", Collections.emptyMap(), null);
    RestStatus restStatus = randomFrom(RestStatus.values());
    HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
    Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
    when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class), anyObject(), anyVararg())).thenReturn(mockResponse);
    {
        Integer result = restHighLevelClient.performRequest(mainRequest, requestConverter, response -> response.getStatusLine().getStatusCode(), Collections.emptySet());
        assertEquals(restStatus.getStatus(), result.intValue());
    }
    {
        IOException ioe = expectThrows(IOException.class, () -> restHighLevelClient.performRequest(mainRequest, requestConverter, response -> {
            throw new IllegalStateException();
        }, Collections.emptySet()));
        assertEquals("Unable to parse response body for Response{requestLine=GET / http/1.1, host=http://localhost:9200, " + "response=http/1.1 " + restStatus.getStatus() + " " + restStatus.name() + "}", ioe.getMessage());
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Header(org.apache.http.Header) StatusLine(org.apache.http.StatusLine) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) ArgumentMatcher(org.mockito.ArgumentMatcher) RequestLine(org.apache.http.RequestLine) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Matchers.eq(org.mockito.Matchers.eq) ClusterName(org.elasticsearch.cluster.ClusterName) JsonParseException(com.fasterxml.jackson.core.JsonParseException) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Matchers.anyVararg(org.mockito.Matchers.anyVararg) ActionRequest(org.elasticsearch.action.ActionRequest) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) StringEntity(org.apache.http.entity.StringEntity) XContentHelper.toXContent(org.elasticsearch.common.xcontent.XContentHelper.toXContent) MainResponse(org.elasticsearch.action.main.MainResponse) CheckedFunction(org.elasticsearch.common.CheckedFunction) List(java.util.List) Version(org.elasticsearch.Version) ArrayEquals(org.mockito.internal.matchers.ArrayEquals) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) Matchers.argThat(org.mockito.Matchers.argThat) RestStatus(org.elasticsearch.rest.RestStatus) Mockito.mock(org.mockito.Mockito.mock) XContentType(org.elasticsearch.common.xcontent.XContentType) BasicStatusLine(org.apache.http.message.BasicStatusLine) Matchers(org.mockito.Matchers) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) Matchers.anyMapOf(org.mockito.Matchers.anyMapOf) SocketTimeoutException(java.net.SocketTimeoutException) Matchers.anyObject(org.mockito.Matchers.anyObject) ESTestCase(org.elasticsearch.test.ESTestCase) MainRequest(org.elasticsearch.action.main.MainRequest) Before(org.junit.Before) CborXContent(org.elasticsearch.common.xcontent.cbor.CborXContent) BasicRequestLine(org.apache.http.message.BasicRequestLine) SmileXContent(org.elasticsearch.common.xcontent.smile.SmileXContent) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ProtocolVersion(org.apache.http.ProtocolVersion) HttpResponse(org.apache.http.HttpResponse) HttpHost(org.apache.http.HttpHost) Build(org.elasticsearch.Build) VarargMatcher(org.mockito.internal.matchers.VarargMatcher) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) MainRequest(org.elasticsearch.action.main.MainRequest) ActionRequest(org.elasticsearch.action.ActionRequest) MainRequest(org.elasticsearch.action.main.MainRequest) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) MainResponse(org.elasticsearch.action.main.MainResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) RestStatus(org.elasticsearch.rest.RestStatus) HttpHost(org.apache.http.HttpHost)

Example 4 with CheckedFunction

use of org.elasticsearch.common.CheckedFunction in project elasticsearch by elastic.

the class RestHighLevelClientTests method testParseEntity.

public void testParseEntity() throws IOException {
    {
        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));
        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));
        assertEquals("Unsupported Content-Type: " + entity.getContentType().getValue(), ise.getMessage());
    }
    {
        CheckedFunction<XContentParser, String, IOException> entityParser = parser -> {
            assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
            assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
            assertTrue(parser.nextToken().isValue());
            String value = parser.text();
            assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
            return value;
        };
        HttpEntity jsonEntity = new StringEntity("{\"field\":\"value\"}", ContentType.APPLICATION_JSON);
        assertEquals("value", restHighLevelClient.parseEntity(jsonEntity, entityParser));
        HttpEntity yamlEntity = new StringEntity("---\nfield: value\n", ContentType.create("application/yaml"));
        assertEquals("value", restHighLevelClient.parseEntity(yamlEntity, entityParser));
        HttpEntity smileEntity = createBinaryEntity(SmileXContent.contentBuilder(), ContentType.create("application/smile"));
        assertEquals("value", restHighLevelClient.parseEntity(smileEntity, entityParser));
        HttpEntity cborEntity = createBinaryEntity(CborXContent.contentBuilder(), ContentType.create("application/cbor"));
        assertEquals("value", restHighLevelClient.parseEntity(cborEntity, entityParser));
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) ContentType(org.apache.http.entity.ContentType) XContentType(org.elasticsearch.common.xcontent.XContentType) HttpEntity(org.apache.http.HttpEntity) Matchers.anyString(org.mockito.Matchers.anyString) CheckedFunction(org.elasticsearch.common.CheckedFunction)

Example 5 with CheckedFunction

use of org.elasticsearch.common.CheckedFunction in project elasticsearch by elastic.

the class RestHighLevelClientTests method testPerformRequestOnResponseExceptionWithIgnoresErrorNoBody.

public void testPerformRequestOnResponseExceptionWithIgnoresErrorNoBody() throws IOException {
    MainRequest mainRequest = new MainRequest();
    CheckedFunction<MainRequest, Request, IOException> requestConverter = request -> new Request("GET", "/", Collections.emptyMap(), null);
    HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND));
    Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
    ResponseException responseException = new ResponseException(mockResponse);
    when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class), anyObject(), anyVararg())).thenThrow(responseException);
    ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, () -> restHighLevelClient.performRequest(mainRequest, requestConverter, response -> {
        throw new IllegalStateException();
    }, Collections.singleton(404)));
    assertEquals(RestStatus.NOT_FOUND, elasticsearchException.status());
    assertSame(responseException, elasticsearchException.getCause());
    assertEquals(responseException.getMessage(), elasticsearchException.getMessage());
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Header(org.apache.http.Header) StatusLine(org.apache.http.StatusLine) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) ArgumentMatcher(org.mockito.ArgumentMatcher) RequestLine(org.apache.http.RequestLine) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Matchers.eq(org.mockito.Matchers.eq) ClusterName(org.elasticsearch.cluster.ClusterName) JsonParseException(com.fasterxml.jackson.core.JsonParseException) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Matchers.anyVararg(org.mockito.Matchers.anyVararg) ActionRequest(org.elasticsearch.action.ActionRequest) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) StringEntity(org.apache.http.entity.StringEntity) XContentHelper.toXContent(org.elasticsearch.common.xcontent.XContentHelper.toXContent) MainResponse(org.elasticsearch.action.main.MainResponse) CheckedFunction(org.elasticsearch.common.CheckedFunction) List(java.util.List) Version(org.elasticsearch.Version) ArrayEquals(org.mockito.internal.matchers.ArrayEquals) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) Matchers.argThat(org.mockito.Matchers.argThat) RestStatus(org.elasticsearch.rest.RestStatus) Mockito.mock(org.mockito.Mockito.mock) XContentType(org.elasticsearch.common.xcontent.XContentType) BasicStatusLine(org.apache.http.message.BasicStatusLine) Matchers(org.mockito.Matchers) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) Matchers.anyMapOf(org.mockito.Matchers.anyMapOf) SocketTimeoutException(java.net.SocketTimeoutException) Matchers.anyObject(org.mockito.Matchers.anyObject) ESTestCase(org.elasticsearch.test.ESTestCase) MainRequest(org.elasticsearch.action.main.MainRequest) Before(org.junit.Before) CborXContent(org.elasticsearch.common.xcontent.cbor.CborXContent) BasicRequestLine(org.apache.http.message.BasicRequestLine) SmileXContent(org.elasticsearch.common.xcontent.smile.SmileXContent) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ProtocolVersion(org.apache.http.ProtocolVersion) HttpResponse(org.apache.http.HttpResponse) HttpHost(org.apache.http.HttpHost) Build(org.elasticsearch.Build) VarargMatcher(org.mockito.internal.matchers.VarargMatcher) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) MainRequest(org.elasticsearch.action.main.MainRequest) ActionRequest(org.elasticsearch.action.ActionRequest) MainRequest(org.elasticsearch.action.main.MainRequest) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) ElasticsearchException(org.elasticsearch.ElasticsearchException) MainResponse(org.elasticsearch.action.main.MainResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpHost(org.apache.http.HttpHost)

Aggregations

CheckedFunction (org.elasticsearch.common.CheckedFunction)14 IOException (java.io.IOException)13 List (java.util.List)11 XContentParser (org.elasticsearch.common.xcontent.XContentParser)10 XContentType (org.elasticsearch.common.xcontent.XContentType)10 ESTestCase (org.elasticsearch.test.ESTestCase)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 HttpEntity (org.apache.http.HttpEntity)9 ContentType (org.apache.http.entity.ContentType)9 StringEntity (org.apache.http.entity.StringEntity)9 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 Version (org.elasticsearch.Version)9 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)9 JsonParseException (com.fasterxml.jackson.core.JsonParseException)8 SocketTimeoutException (java.net.SocketTimeoutException)8 Collections (java.util.Collections)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Header (org.apache.http.Header)8 HttpHost (org.apache.http.HttpHost)8 HttpResponse (org.apache.http.HttpResponse)8