org.asynchttpclient.BoundRequestBuilder Java Examples

The following examples show how to use org.asynchttpclient.BoundRequestBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClusterHealthRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowExceptionIfClusterNameIsMissingInResponse() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.prepareGet("/_cluster/health/someIndex,someOtherIndex")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok",
            "{\n" +
                    "  \"timed_out\": false,\n" +
                    "  \"status\": \"red\"" +
                    "}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    // when
    try {
        testee.execute();
    } catch (InvalidElasticsearchResponseException e) {
        assertThat(e.getMessage(), is("Missing response field: cluster_name"));
    }

    // then
}
 
Example #2
Source File: DeleteRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = HttpServerErrorException.class)
public void shouldThrowExceptionIfStatusIsNot200() {
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(httpClient.prepareDelete("/someIndexName/someType/someId")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(400, "not ok", "errorResponse")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    try {
        testee.setDocumentType("someType")
                .setId("someId")
                .setIndexName("someIndexName")
                .execute();
    } catch (HttpServerErrorException e) {
        assertThat(e.getStatusCode(), is(400));
        assertThat(e.getResponseBody(), is("errorResponse"));
        throw e;
    }
}
 
Example #3
Source File: MultiGetRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildAndReturnDocumentWithTypeAndIndex() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_mget")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", ONE_DOC_FOUND_RESPONSE)));

    // when
    MultiGetResponse response = requestBuilder.setRequestDocuments(asList(multiGetRequestDocumentBuilder().withId("V1").withIndex("I1").withType("T1").build())).execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"docs\":[{\"_id\":\"V1\",\"_type\":\"T1\",\"_index\":\"I1\"}]}");
}
 
Example #4
Source File: MultiGetRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnMultipleMatchedDocument() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_mget")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", TWO_DOC_FOUND_RESPONSE)));

    // when
    MultiGetResponse response = requestBuilder.setRequestDocuments(asList(multiGetRequestDocumentBuilder().withId("V1").build(), multiGetRequestDocumentBuilder().withId("V2").build())).execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"docs\":[{\"_id\":\"V1\"},{\"_id\":\"V2\"}]}");
    verify(httpClient).preparePost("/some-index/_mget");
    assertThat(response.getMultiGetResponseDocuments(), hasSize(2));
    assertThat(response.getMultiGetResponseDocuments().get(0), is(new MultiGetResponseDocument("V1", true, object("variationId", new JsonPrimitive("V1"), "name", new JsonPrimitive("name1")))));
    assertThat(response.getMultiGetResponseDocuments().get(1), is(new MultiGetResponseDocument("V2", true, object("variationId", new JsonPrimitive("V2"), "name", new JsonPrimitive("name2")))));
}
 
Example #5
Source File: MultiGetRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnOneMatchedDocument() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_mget")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", ONE_DOC_FOUND_RESPONSE)));

    // when
    MultiGetResponse response = requestBuilder.setRequestDocuments(asList(multiGetRequestDocumentBuilder().withId("V1").build())).execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"docs\":[{\"_id\":\"V1\"}]}");
    verify(httpClient).preparePost("/some-index/_mget");
    assertThat(response.getMultiGetResponseDocuments(), hasSize(1));
    assertThat(response.getMultiGetResponseDocuments().get(0), is(new MultiGetResponseDocument("V1", true, object("variationId", new JsonPrimitive("V1"), "name", new JsonPrimitive("name1")))));
}
 
Example #6
Source File: SearchRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuilderQueryWithFromAndSize() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", EMPTY_SEARCH_RESPONSE)));

    // when
    SearchResponse response = searchRequestBuilder.setQuery(createSampleQuery()).setSize(42).setFrom(111).execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"query\":{\"term\":{\"someField\":\"someValue\"}},\"from\":111,\"size\":42}");
    verify(httpClient).preparePost("/some-index/_search");
}
 
Example #7
Source File: SearchRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetHttpRequestTimeout() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", EMPTY_SEARCH_RESPONSE)));

    // when
    SearchResponse response = searchRequestBuilder.setQuery(createSampleQuery()).setTimeoutMillis(54321).execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"query\":{\"term\":{\"someField\":\"someValue\"}}}");
    verify(boundRequestBuilderMock).setRequestTimeout(54321);
    verify(httpClient).preparePost("/some-index/_search");
}
 
Example #8
Source File: SearchRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildRequestWithOneSort() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", EMPTY_SEARCH_RESPONSE)));

    // when
    SearchResponse response = searchRequestBuilder.setQuery(createSampleQuery()).addSort("someKey", SortOrder.DESC).execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"query\":{\"term\":{\"someField\":\"someValue\"}},\"sort\":[{\"someKey\":{\"order\":\"desc\"}}]}");
    verify(httpClient).preparePost("/some-index/_search");
}
 
Example #9
Source File: SearchRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildRequestWithMultipleSort() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", EMPTY_SEARCH_RESPONSE)));

    // when
    SearchResponse response = searchRequestBuilder.setQuery(createSampleQuery()).addSort("someKey", SortOrder.DESC).addSort("someOtherKey", ASC).execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"query\":{\"term\":{\"someField\":\"someValue\"}},\"sort\":[{\"someKey\":{\"order\":\"desc\"}},{\"someOtherKey\":{\"order\":\"asc\"}}]}");
    verify(httpClient).preparePost("/some-index/_search");
}
 
Example #10
Source File: SearchRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildRequestWithPostFilter() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", EMPTY_SEARCH_RESPONSE)));

    // when
    SearchResponse response = searchRequestBuilder.setQuery(createSampleQuery()).setPostFilter(QueryBuilders.termQuery("someField", "someValue")).execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"query\":{\"term\":{\"someField\":\"someValue\"}},\"post_filter\":{\"term\":{\"someField\":\"someValue\"}}}");
    verify(httpClient).preparePost("/some-index/_search");
}
 
Example #11
Source File: SearchRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildRequestWithAggregations() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", EMPTY_SEARCH_RESPONSE)));

    // when
    SearchResponse response = searchRequestBuilder.setQuery(createSampleQuery()).addAggregation(new TermsBuilder("Katzenklo")
            .field("someField"))
            .execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"query\":{\"term\":{\"someField\":\"someValue\"}}," +
            "\"aggregations\":{\"Katzenklo\":{\"terms\":{\"field\":\"someField\"}}}}");
    verify(httpClient).preparePost("/some-index/_search");
}
 
Example #12
Source File: SearchRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuildRequestWithScroll() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", SEARCH_RESPONSE_WITH_SCROLL_ID)));

    // when
    SearchResponse response = searchRequestBuilder.setQuery(createSampleQuery())
            .setScroll("1m")
            .execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"query\":{\"term\":{\"someField\":\"someValue\"}}}");
    verify(httpClient).preparePost("/some-index/_search");
    verify(boundRequestBuilderMock).addQueryParam("scroll", "1m");
    assertThat(response.getScrollId(), is("some_scroll_id"));
}
 
Example #13
Source File: FlummiTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldPrepareBulk() throws ExecutionException, InterruptedException, IOException {
    //Given
    final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
    when(asyncHttpClient.preparePost(anyString())).thenReturn(boundRequestBuilder);
    final ListenableFuture listenableFuture = mock(ListenableFuture.class);
    when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
    when(boundRequestBuilder.setBody(anyString())).thenReturn(boundRequestBuilder);
    when(boundRequestBuilder.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilder);
    when(boundRequestBuilder.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilder);
    final Response response = mock(Response.class);
    when(listenableFuture.get()).thenReturn(response);
    when(response.getStatusCode()).thenReturn(200);
    when(response.getResponseBody()).thenReturn("{\"errors\":false}");
    final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    bulkRequestBuilder.add(new DeleteActionBuilder("someIndexName", "someId", "someType"));

    //When
    bulkRequestBuilder.execute();

    //Then
    verify(asyncHttpClient).preparePost("http://someHost:9200/_bulk");
}
 
Example #14
Source File: SearchRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBuilderQueryWithSourceFilter() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture<>(new MockResponse(200, "ok", EMPTY_SEARCH_RESPONSE)));

    // when
    SearchResponse response = searchRequestBuilder.setQuery(createSampleQuery()).addSourceFilter("someField").execute();

    //then
    verify(boundRequestBuilderMock).setBody("{\"query\":{\"term\":{\"someField\":\"someValue\"}},\"_source\":[\"someField\"]}");
    verify(httpClient).preparePost("/some-index/_search");
}
 
Example #15
Source File: ClusterHealthRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowExceptionIfTimedOutIsTrue() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.prepareGet("/_cluster/health/someIndex,someOtherIndex")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok",
            "{\n" +
                    "  \"cluster_name\": \"myCluster\",\n" +
                    "  \"timed_out\": true,\n" +
                    "  \"status\": \"red\"" +
                    "}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    // when
    try {
        testee.execute();
    } catch (InvalidElasticsearchResponseException e) {
        assertThat(e.getMessage(), is("Timed out waiting for yellow cluster status"));
    }

    // then
}
 
Example #16
Source File: ClusterHealthRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = HttpServerErrorException.class)
public void shouldThrowExceptionIfStatusCodeNotOk() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.prepareGet("/_cluster/health/someIndex,someOtherIndex")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(400, "not ok",
            "{\n" +
                    "  \"cluster_name\": \"myCluster\",\n" +
                    "  \"timed_out\": false,\n" +
                    "  \"status\": \"red\"" +
                    "}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    // when
    try {
        testee.execute();
    }
    // then
    catch (HttpServerErrorException e) {
        assertThat(e.getMessage(), is("400 not ok: {\n  \"cluster_name\": \"myCluster\",\n  \"timed_out\": false,\n  \"status\": \"red\"}"));
        assertThat(e.getResponseBody(), is("{\n  \"cluster_name\": \"myCluster\",\n  \"timed_out\": false,\n  \"status\": \"red\"}"));
        throw e;
    }
}
 
Example #17
Source File: ClusterHealthRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowExceptionIfTimedOutIsMissingInResponse() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.prepareGet("/_cluster/health/someIndex,someOtherIndex")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok",
            "{\n" +
                    "  \"cluster_name\": \"myCluster\",\n" +
                    "  \"status\": \"red\"" +
                    "}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    // when
    try {
        testee.execute();
    } catch (InvalidElasticsearchResponseException e) {
        assertThat(e.getMessage(), is("Missing response field: timed_out"));
    }

    // then
}
 
Example #18
Source File: FlummiTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldGetAdminClientCluster() throws ExecutionException, InterruptedException, IOException {
    // Given
    final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
    final ListenableFuture<Response> listenableFuture = mock(ListenableFuture.class);
    final Response response = mock(Response.class);
    when(response.getStatusCode()).thenReturn(200);
    when(response.getResponseBody()).thenReturn("{\"status\":\"GREEN\", \"cluster_name\":\"someClusterName\", \"timed_out\":\"someTimedOut\"}");
    when(listenableFuture.get()).thenReturn(response);
    when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
    when(boundRequestBuilder.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilder);
    when(asyncHttpClient.prepareGet(anyString())).thenReturn(boundRequestBuilder);
    final ClusterAdminClient cluster = client.admin().cluster();
    final ClusterHealthRequestBuilder clusterHealthRequestBuilder = cluster.prepareHealth("someIndexName");

    //When
    clusterHealthRequestBuilder.execute();

    //Then
    Mockito.verify(asyncHttpClient).prepareGet("http://someHost:9200/_cluster/health/someIndexName");
    assertThat(cluster, notNullValue());
}
 
Example #19
Source File: ClusterHealthRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteRequestWithWaitForYellowStatus() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.prepareGet("/_cluster/health/someIndex,someOtherIndex")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addQueryParam(anyString(), anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok",
            "{\n" +
                    "  \"cluster_name\": \"myCluster\",\n" +
                    "  \"status\": \"green\",\n" +
                    "  \"timed_out\": false" +
                    "}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    // when
    ClusterHealthResponse healthResponse = testee.setWaitForYellowStatus().execute();

    // then
    assertThat(healthResponse.getStatus(), is(ClusterHealthStatus.GREEN));
    assertThat(healthResponse.getCluster_name(), is("myCluster"));
    assertThat(healthResponse.isTimedOut(), is(false));
    Mockito.verify(boundRequestBuilderMock).addQueryParam("wait_for_status", "yellow");
}
 
Example #20
Source File: ClusterHealthRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteRequestWithTimeout() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.prepareGet("/_cluster/health/someIndex,someOtherIndex")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addQueryParam(anyString(), anyString())).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok",
            "{\n" +
                    "  \"cluster_name\": \"myCluster\",\n" +
                    "  \"status\": \"green\",\n" +
                    "  \"timed_out\": false" +
                    "}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    // when
    ClusterHealthResponse healthResponse = testee.setTimeout(1000).execute();

    // then
    assertThat(healthResponse.getStatus(), is(ClusterHealthStatus.GREEN));
    assertThat(healthResponse.getCluster_name(), is("myCluster"));
    assertThat(healthResponse.isTimedOut(), is(false));
    Mockito.verify(boundRequestBuilderMock).addQueryParam("timeout", "1000ms");
}
 
Example #21
Source File: SearchRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddQueryToException() throws Exception {
    //given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
    when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);
    TimeoutException timeoutException = new TimeoutException();
    when(boundRequestBuilderMock.execute()).thenReturn(new ListenableFuture.CompletedFailure<>(timeoutException));

    //when
    JsonObject query = createSampleQuery();
    try {
        searchRequestBuilder.setQuery(query).execute();
    } catch (RuntimeException e) {
        //then
        assertThat(e.getCause().getCause(), is(timeoutException));
        assertThat(e.getMessage(), is("{\"query\":" + query.toString() + "}"));
    }

}
 
Example #22
Source File: BulkRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotThrowExceptionForResponseItemWithoutErrors() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.preparePost("/_bulk")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody("{\"index\":{\"_index\":\"someIndex\",\"_type\":\"Flutschfinger\"}}\n{\"Eis\":\"am Stiel\"}\n")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok", "{\"errors\":true,\"items\":[{\"update\":{\"_index\":\"mytestindex\",\"_type\":\"product\",\"_id\":\"340891232\",\"status\":409}}]}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    testee.add(new IndexActionBuilder("someIndex").setOpType(IndexOpType.INDEX).setType("Flutschfinger").setSource(object("Eis", "am Stiel")));

    // when
    testee.execute();

    // then
    verify(asyncHttpClient).preparePost("/_bulk");
    verify(boundRequestBuilderMock).execute();
}
 
Example #23
Source File: BulkRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotThrowAnExceptionIfTwoOpsArePresentAndUpdateHasAn404Error() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.preparePost("/_bulk")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody("{\"index\":{\"_index\":\"someIndex\",\"_type\":\"Flutschfinger\"}}\n{\"Eis\":\"am Stiel\"}\n")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok", "{\"errors\":true,\"items\":[{\"create\":{\"status\":200}},{\"update\":{\"status\":404,\"error\":\"someError\"}}]}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    testee.add(new IndexActionBuilder("someIndex").setOpType(IndexOpType.INDEX).setType("Flutschfinger").setSource(object("Eis", "am Stiel")));

    // when
    testee.execute();
    // then
}
 
Example #24
Source File: BulkRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowAnExceptionIfTwoOpsArePresentAndDeleteHasAnError() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.preparePost("/_bulk")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody("{\"index\":{\"_index\":\"someIndex\",\"_type\":\"Flutschfinger\"}}\n{\"Eis\":\"am Stiel\"}\n")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok", "{\"errors\":true,\"items\":[{\"create\":{\"status\":200}},{\"delete\":{\"status\":503,\"error\":\"someError\"}}]}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    testee.add(new IndexActionBuilder("someIndex").setOpType(IndexOpType.INDEX).setType("Flutschfinger").setSource(object("Eis", "am Stiel")));

    // when
    try {
        testee.execute();
    } catch (InvalidElasticsearchResponseException e) {
        assertThat(e.getMessage(), is("Response contains errors': {\"errors\":true,\"items\":[{\"create\":{\"status\":200}},{\"delete\":{\"status\":503,\"error\":\"someError\"}}]}"));
    }
    // then
}
 
Example #25
Source File: BulkRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowAnExceptionIfOneOpIsPresentAndItIsNotAnUpdate503() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.preparePost("/_bulk")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody("{\"index\":{\"_index\":\"someIndex\",\"_type\":\"Flutschfinger\"}}\n{\"Eis\":\"am Stiel\"}\n")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok", "{\"errors\":true,\"items\":[{\"index\":{\"status\":503,\"error\":\"someError\"}}]}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    testee.add(new IndexActionBuilder("someIndex").setOpType(IndexOpType.INDEX).setType("Flutschfinger").setSource(object("Eis", "am Stiel")));

    // when
    try {
        testee.execute();
    } catch (InvalidElasticsearchResponseException e) {
        assertThat(e.getMessage(), is("Response contains errors': {\"errors\":true,\"items\":[{\"index\":{\"status\":503,\"error\":\"someError\"}}]}"));
    }
    // then
}
 
Example #26
Source File: FlummiTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldPrepareDelete() throws ExecutionException, InterruptedException, IOException {
    //Given
    final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
    when(asyncHttpClient.prepareDelete(anyString())).thenReturn(boundRequestBuilder);
    final ListenableFuture listenableFuture = mock(ListenableFuture.class);
    when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
    when(boundRequestBuilder.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilder);
    final Response response = mock(Response.class);
    when(listenableFuture.get()).thenReturn(response);
    when(response.getStatusCode()).thenReturn(200);
    final DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete();
    deleteRequestBuilder.setIndexName("someIndexName");
    deleteRequestBuilder.setDocumentType("someDocumentType");
    deleteRequestBuilder.setId("someId");

    //When
    deleteRequestBuilder.execute();

    //Then
    verify(asyncHttpClient).prepareDelete("http://someHost:9200/someIndexName/someDocumentType/someId");
}
 
Example #27
Source File: BulkRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowAnExceptionIfOneOpIsPresentAndItIsNotAnUpdate404() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.preparePost("/_bulk")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody("{\"index\":{\"_index\":\"someIndex\",\"_type\":\"Flutschfinger\"}}\n{\"Eis\":\"am Stiel\"}\n")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok", "{\"errors\":true,\"items\":[{\"index\":{\"status\":400,\"error\":\"someError\"}}]}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    testee.add(new IndexActionBuilder("someIndex").setOpType(IndexOpType.INDEX).setType("Flutschfinger").setSource(object("Eis", "am Stiel")));

    // when
    try {
        testee.execute();
    } catch (InvalidElasticsearchResponseException e) {
        assertThat(e.getMessage(), is("Response contains errors': {\"errors\":true,\"items\":[{\"index\":{\"status\":400,\"error\":\"someError\"}}]}"));
    }
    // then
}
 
Example #28
Source File: BulkRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = HttpServerErrorException.class)
public void shouldThrowExceptionIfHttpStatusIsNot200() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.preparePost("/_bulk")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody("{\"index\":{\"_index\":\"someIndex\",\"_type\":\"Flutschfinger\"}}\n{\"Eis\":\"am Stiel\"}\n")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(400, "not ok", "{\"errors\":false}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    testee.add(new IndexActionBuilder("someIndex").setOpType(IndexOpType.INDEX).setType("Flutschfinger").setSource(object("Eis", "am Stiel")));

    // when
    try {
        testee.execute();
    }
    // then
    catch (HttpServerErrorException e) {
        assertThat(e.getStatusCode(), is(400));
        assertThat(e.getResponseBody(), is("{\"errors\":false}"));
        throw e;
    }
}
 
Example #29
Source File: BulkRequestBuilderTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFireBulkRequest() throws Exception {
    // given
    BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);

    when(asyncHttpClient.preparePost("/_bulk")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setBody("{\"index\":{\"_index\":\"someIndex\",\"_type\":\"Flutschfinger\"}}\n{\"Eis\":\"am Stiel\"}\n")).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
    when(boundRequestBuilderMock.execute()).thenReturn(new CompletedFuture(new MockResponse(200, "ok", "{\"errors\":false}")));
    when(boundRequestBuilderMock.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilderMock);

    testee.add(new IndexActionBuilder("someIndex").setOpType(IndexOpType.INDEX).setType("Flutschfinger").setSource(object("Eis", "am Stiel")));

    // when
    testee.execute();

    // then
    verify(asyncHttpClient).preparePost("/_bulk");
    verify(boundRequestBuilderMock).execute();
    verify(boundRequestBuilderMock).setBody("{\"index\":{\"_index\":\"someIndex\",\"_type\":\"Flutschfinger\"}}\n{\"Eis\":\"am Stiel\"}\n");
}
 
Example #30
Source File: AdminClientTest.java    From flummi with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCreateIndicesAdminClient() throws ExecutionException, InterruptedException, IOException {
    //Given
    final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
    when(httpClient.preparePut(anyString())).thenReturn(boundRequestBuilder);
    when(boundRequestBuilder.setBody(anyString())).thenReturn(boundRequestBuilder);
    when(boundRequestBuilder.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilder);
    final ListenableFuture<Response> listenableFuture = mock(ListenableFuture.class);
    when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
    when(boundRequestBuilder.addHeader(anyString(),anyString())).thenReturn(boundRequestBuilder);
    final Response response = mock(Response.class);
    when(listenableFuture.get()).thenReturn(response);
    when(response.getStatusCode()).thenReturn(200);
    when(response.getResponseBody()).thenReturn("{\"acknowledged\":\"true\"}");
    final IndicesAdminClient indicesAdminClient = adminClient.indices();
    final CreateIndexRequestBuilder createIndexRequestBuilder = indicesAdminClient.prepareCreate("someIndexName");

    //When
    createIndexRequestBuilder.execute();

    //Then
    verify(httpClient).preparePut("/someIndexName");
    assertThat(indicesAdminClient, notNullValue());
}