Search in sources :

Example 1 with Receive

use of org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive in project spring-data-elasticsearch by spring-projects.

the class MultiNodeHostProviderUnitTests method triesDeadHostsIfNoActiveFound.

// DATAES-488
@Test
public void triesDeadHostsIfNoActiveFound() {
    multiNodeDelegatingHostProvider.when(HOST_1).receive(Receive::error);
    multiNodeDelegatingHostProvider.when(HOST_2).get(requestHeadersUriSpec -> {
        ClientResponse response1 = mock(ClientResponse.class);
        when(response1.releaseBody()).thenReturn(Mono.empty());
        Receive.error(response1);
        ClientResponse response2 = mock(ClientResponse.class);
        when(response2.releaseBody()).thenReturn(Mono.empty());
        Receive.ok(response2);
        // 
        when(requestHeadersUriSpec.exchangeToMono(any())).thenAnswer(// 
        invocation -> getAnswer(invocation, response1)).thenAnswer(invocation -> getAnswer(invocation, response2));
    });
    multiNodeDelegatingHostProvider.when(HOST_3).receive(Receive::error);
    delegateHostProvider.clusterInfo().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    assertThat(delegateHostProvider.getCachedHostState()).extracting(ElasticsearchHost::getState).containsExactly(State.OFFLINE, State.OFFLINE, State.OFFLINE);
    delegateHostProvider.getActive().as(StepVerifier::create).expectNext(multiNodeDelegatingHostProvider.client(HOST_2)).verifyComplete();
    verify(multiNodeDelegatingHostProvider.client(HOST_2), times(2)).head();
}
Also used : ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) BeforeEach(org.junit.jupiter.api.BeforeEach) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) StepVerifier(reactor.test.StepVerifier) Mono(reactor.core.publisher.Mono) Verification(org.springframework.data.elasticsearch.client.reactive.HostProvider.Verification) Function(java.util.function.Function) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ElasticsearchHost(org.springframework.data.elasticsearch.client.ElasticsearchHost) State(org.springframework.data.elasticsearch.client.ElasticsearchHost.State) Assertions(org.assertj.core.api.Assertions) MockDelegatingElasticsearchHostProvider(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockDelegatingElasticsearchHostProvider) Receive(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive) Receive(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 2 with Receive

use of org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchClientUnitTests method pingShouldHitMainEndpoint.

// --> PING
@Test
public void pingShouldHitMainEndpoint() {
    // 
    hostProvider.when(HOST).receive(Receive::ok);
    // 
    client.ping().then().as(// 
    StepVerifier::create).verifyComplete();
    URI uri = hostProvider.when(HOST).captureUri();
    assertThat(uri.getRawPath()).isEqualTo("/");
}
Also used : Receive(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 3 with Receive

use of org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchClientUnitTests method searchShouldReturnMultipleResultsCorrectly.

// DATAES-488
@Test
public void searchShouldReturnMultipleResultsCorrectly() {
    // 
    hostProvider.when(HOST).receive(// 
    Receive::json).body(fromPath("search-ok-multiple-hits"));
    // 
    client.search(new SearchRequest("twitter")).as(// 
    StepVerifier::create).consumeNextWith(hit -> {
        assertThat(hit.getId()).isEqualTo("2");
        assertThat(hit.getIndex()).isEqualTo("twitter");
        // 
        assertThat(hit.getSourceAsMap()).containsEntry("user", // 
        "kimchy").containsEntry("message", // 
        "Another tweet, will it be indexed?").containsKey("post_date");
    }).consumeNextWith(hit -> {
        assertThat(hit.getId()).isEqualTo("1");
        assertThat(hit.getIndex()).isEqualTo("twitter");
        // 
        assertThat(hit.getSourceAsMap()).containsEntry("user", // 
        "kimchy").containsEntry("message", // 
        "Trying out Elasticsearch, so far so good?").containsKey("post_date");
    }).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) RestStatusException(org.springframework.data.elasticsearch.RestStatusException) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) VersionType(org.elasticsearch.index.VersionType) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) IndexRequest(org.elasticsearch.action.index.IndexRequest) Assertions(org.assertj.core.api.Assertions) URI(java.net.URI) Result(org.elasticsearch.action.DocWriteResponse.Result) Receive(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive) TimeValue(org.elasticsearch.core.TimeValue) StreamUtils(org.springframework.util.StreamUtils) GetRequest(org.elasticsearch.action.get.GetRequest) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Instant(java.time.Instant) ParsedMax(org.elasticsearch.search.aggregations.metrics.ParsedMax) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) XContentType(org.elasticsearch.xcontent.XContentType) HttpStatus(org.springframework.http.HttpStatus) RestStatus(org.elasticsearch.rest.RestStatus) Collections(java.util.Collections) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) MockDelegatingElasticsearchHostProvider(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockDelegatingElasticsearchHostProvider) SearchRequest(org.elasticsearch.action.search.SearchRequest) Test(org.junit.jupiter.api.Test)

Example 4 with Receive

use of org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive in project spring-data-elasticsearch by spring-projects.

the class ReactiveElasticsearchClientUnitTests method aggregateShouldReturnMultipleResultsCorrectly.

// DATAES-567
@Test
public void aggregateShouldReturnMultipleResultsCorrectly() {
    // 
    hostProvider.when(HOST).receive(// 
    Receive::json).body(fromPath("aggregate-ok-multiple-results"));
    // 
    client.aggregate(new SearchRequest("twitter")).as(// 
    StepVerifier::create).consumeNextWith(aggregation -> {
        assertThat(aggregation.getName()).isEqualTo("users");
        assertThat(aggregation instanceof ParsedStringTerms);
        ParsedStringTerms parsedStringTerms = (ParsedStringTerms) aggregation;
        assertThat(parsedStringTerms.getBuckets().size()).isEqualTo(2);
        assertThat(parsedStringTerms.getBucketByKey("kimchy").getDocCount()).isEqualTo(2);
        assertThat(parsedStringTerms.getBucketByKey("elastic").getDocCount()).isEqualTo(1);
    }).consumeNextWith(aggregation -> {
        assertThat(aggregation.getName()).isEqualTo("max_post_date");
        assertThat(aggregation instanceof ParsedMax);
        ParsedMax parsedMax = (ParsedMax) aggregation;
        assertThat(Instant.ofEpochMilli((long) parsedMax.getValue())).isEqualTo(Instant.parse("2010-01-15T01:46:38Z"));
    }).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) RestStatusException(org.springframework.data.elasticsearch.RestStatusException) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) VersionType(org.elasticsearch.index.VersionType) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) IndexRequest(org.elasticsearch.action.index.IndexRequest) Assertions(org.assertj.core.api.Assertions) URI(java.net.URI) Result(org.elasticsearch.action.DocWriteResponse.Result) Receive(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive) TimeValue(org.elasticsearch.core.TimeValue) StreamUtils(org.springframework.util.StreamUtils) GetRequest(org.elasticsearch.action.get.GetRequest) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Instant(java.time.Instant) ParsedMax(org.elasticsearch.search.aggregations.metrics.ParsedMax) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) XContentType(org.elasticsearch.xcontent.XContentType) HttpStatus(org.springframework.http.HttpStatus) RestStatus(org.elasticsearch.rest.RestStatus) Collections(java.util.Collections) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) MockDelegatingElasticsearchHostProvider(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockDelegatingElasticsearchHostProvider) SearchRequest(org.elasticsearch.action.search.SearchRequest) ParsedMax(org.elasticsearch.search.aggregations.metrics.ParsedMax) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 Receive (org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive)4 URI (java.net.URI)3 Assertions (org.assertj.core.api.Assertions)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Mockito (org.mockito.Mockito)3 MockDelegatingElasticsearchHostProvider (org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockDelegatingElasticsearchHostProvider)3 Mono (reactor.core.publisher.Mono)3 StepVerifier (reactor.test.StepVerifier)3 IOException (java.io.IOException)2 Instant (java.time.Instant)2 Collections (java.util.Collections)2 Result (org.elasticsearch.action.DocWriteResponse.Result)2 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2