|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.elasticsearch.client.reactive; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.elasticsearch.search.internal.SearchContext.*; |
| 20 | +import static org.mockito.Mockito.*; |
| 21 | + |
| 22 | +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +import java.util.function.Function; |
| 26 | + |
| 27 | +import org.elasticsearch.action.search.SearchRequest; |
| 28 | +import org.elasticsearch.client.Request; |
| 29 | +import org.elasticsearch.index.query.QueryBuilders; |
| 30 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 34 | +import org.mockito.ArgumentCaptor; |
| 35 | +import org.mockito.Mock; |
| 36 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 37 | +import org.springframework.web.reactive.function.client.ClientResponse; |
| 38 | +import reactor.test.StepVerifier; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Peter-Josef Meisch |
| 42 | + */ |
| 43 | +@ExtendWith(MockitoExtension.class) |
| 44 | +class DefaultReactiveElasticsearchClientTest { |
| 45 | + |
| 46 | + @Mock private HostProvider hostProvider; |
| 47 | + |
| 48 | + @Mock private Function<SearchRequest, Request> searchRequestConverter; |
| 49 | + |
| 50 | + private DefaultReactiveElasticsearchClient client; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + void setUp() { |
| 54 | + client = new DefaultReactiveElasticsearchClient(hostProvider, new RequestCreator() { |
| 55 | + @Override |
| 56 | + public Function<SearchRequest, Request> search() { |
| 57 | + return searchRequestConverter; |
| 58 | + } |
| 59 | + }) { |
| 60 | + @Override |
| 61 | + public Mono<ClientResponse> execute(ReactiveElasticsearchClientCallback callback) { |
| 62 | + return Mono.empty(); |
| 63 | + } |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void shouldSetAppropriateRequestParametersOnCount() { |
| 69 | + |
| 70 | + SearchRequest searchRequest = new SearchRequest("someindex") // |
| 71 | + .source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())); |
| 72 | + |
| 73 | + client.count(searchRequest).as(StepVerifier::create).verifyComplete(); |
| 74 | + |
| 75 | + ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class); |
| 76 | + verify(searchRequestConverter).apply(captor.capture()); |
| 77 | + SearchSourceBuilder source = captor.getValue().source(); |
| 78 | + assertThat(source.size()).isEqualTo(0); |
| 79 | + assertThat(source.trackTotalHitsUpTo()).isEqualTo(TRACK_TOTAL_HITS_ACCURATE); |
| 80 | + assertThat(source.fetchSource()).isEqualTo(FetchSourceContext.DO_NOT_FETCH_SOURCE); |
| 81 | + } |
| 82 | +} |
0 commit comments