Search in sources :

Example 1 with Ping

use of io.searchbox.core.Ping in project graylog2-server by Graylog2.

the class JestUtilsTest method executeWithUnsuccessfulResponseAndErrorDetails.

@Test
public void executeWithUnsuccessfulResponseAndErrorDetails() throws Exception {
    final Ping request = new Ping.Builder().build();
    final JestResult resultMock = mock(JestResult.class);
    when(resultMock.isSucceeded()).thenReturn(false);
    final ObjectNode rootCauseStub = objectMapper.createObjectNode();
    rootCauseStub.set("reason", new TextNode("foobar"));
    final ArrayNode rootCausesStub = objectMapper.createArrayNode();
    rootCausesStub.add(rootCauseStub);
    final ObjectNode errorStub = objectMapper.createObjectNode();
    errorStub.set("root_cause", rootCausesStub);
    final ObjectNode responseStub = objectMapper.createObjectNode();
    responseStub.set("error", errorStub);
    when(resultMock.getJsonObject()).thenReturn(responseStub);
    when(clientMock.execute(request)).thenReturn(resultMock);
    try {
        JestUtils.execute(clientMock, request, () -> "BOOM");
        fail("Expected ElasticsearchException to be thrown");
    } catch (ElasticsearchException e) {
        assertThat(e).hasMessageStartingWith("BOOM").hasMessageEndingWith("foobar").hasNoSuppressedExceptions();
        assertThat(e.getErrorDetails()).containsExactly("foobar");
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Ping(io.searchbox.core.Ping) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Example 2 with Ping

use of io.searchbox.core.Ping in project graylog2-server by Graylog2.

the class JestUtilsTest method executeWithIOException.

@Test
public void executeWithIOException() throws Exception {
    final Ping request = new Ping.Builder().build();
    when(clientMock.execute(request)).thenThrow(IOException.class);
    expectedException.expect(ElasticsearchException.class);
    expectedException.expectMessage("BOOM");
    JestUtils.execute(clientMock, request, () -> "BOOM");
}
Also used : Ping(io.searchbox.core.Ping) Test(org.junit.Test)

Example 3 with Ping

use of io.searchbox.core.Ping in project graylog2-server by Graylog2.

the class JestUtilsTest method executeWithSuccessfulResponse.

@Test
public void executeWithSuccessfulResponse() throws Exception {
    final Ping request = new Ping.Builder().build();
    final JestResult resultMock = mock(JestResult.class);
    when(resultMock.isSucceeded()).thenReturn(true);
    when(clientMock.execute(request)).thenReturn(resultMock);
    final JestResult result = JestUtils.execute(clientMock, request, () -> "BOOM");
    assertThat(result.isSucceeded()).isTrue();
}
Also used : Ping(io.searchbox.core.Ping) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Example 4 with Ping

use of io.searchbox.core.Ping in project graylog2-server by Graylog2.

the class JestUtilsTest method executeFailsWithCustomMessage.

@Test
public void executeFailsWithCustomMessage() throws Exception {
    final Ping request = new Ping.Builder().build();
    final JestResult resultMock = mock(JestResult.class);
    when(resultMock.isSucceeded()).thenReturn(false);
    final ObjectNode responseStub = objectMapper.createObjectNode();
    final ObjectNode errorStub = objectMapper.createObjectNode();
    responseStub.set("Message", new TextNode("Authorization header requires 'Credential' parameter."));
    errorStub.set("error", responseStub);
    when(resultMock.getJsonObject()).thenReturn(errorStub);
    when(clientMock.execute(request)).thenReturn(resultMock);
    try {
        JestUtils.execute(clientMock, request, () -> "BOOM");
        fail("Expected ElasticsearchException to be thrown");
    } catch (ElasticsearchException e) {
        assertThat(e).hasMessageStartingWith("BOOM").hasMessageEndingWith("{\"Message\":\"Authorization header requires 'Credential' parameter.\"}").hasNoSuppressedExceptions();
        assertThat(e.getErrorDetails()).containsExactly("{\"Message\":\"Authorization header requires 'Credential' parameter.\"}");
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Ping(io.searchbox.core.Ping) TextNode(com.fasterxml.jackson.databind.node.TextNode) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Example 5 with Ping

use of io.searchbox.core.Ping in project graylog2-server by Graylog2.

the class JestUtilsTest method executeWithUnsuccessfulResponse.

@Test
public void executeWithUnsuccessfulResponse() throws Exception {
    final Ping request = new Ping.Builder().build();
    final JestResult resultMock = mock(JestResult.class);
    when(resultMock.isSucceeded()).thenReturn(false);
    when(resultMock.getJsonObject()).thenReturn(objectMapper.createObjectNode());
    when(clientMock.execute(request)).thenReturn(resultMock);
    expectedException.expect(ElasticsearchException.class);
    expectedException.expectMessage("BOOM");
    JestUtils.execute(clientMock, request, () -> "BOOM");
}
Also used : Ping(io.searchbox.core.Ping) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Aggregations

Ping (io.searchbox.core.Ping)7 JestResult (io.searchbox.client.JestResult)6 Test (org.junit.Test)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 TextNode (com.fasterxml.jackson.databind.node.TextNode)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ElasticsearchException (org.graylog2.indexer.ElasticsearchException)2 IntNode (com.fasterxml.jackson.databind.node.IntNode)1 QueryParsingException (org.graylog2.indexer.QueryParsingException)1 SearchVersion (org.graylog2.storage.SearchVersion)1