|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 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 | + |
| 17 | +package org.springframework.boot.autoconfigure.data.elasticsearch; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.elasticsearch.action.get.GetRequest; |
| 24 | +import org.elasticsearch.action.index.IndexRequest; |
| 25 | +import org.elasticsearch.index.get.GetResult; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.testcontainers.elasticsearch.ElasticsearchContainer; |
| 28 | +import org.testcontainers.junit.jupiter.Container; |
| 29 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 30 | + |
| 31 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 32 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 33 | +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; |
| 34 | +import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | + |
| 38 | +/** |
| 39 | + * Integration tests for {@link ReactiveElasticsearchRestClientAutoConfiguration}. |
| 40 | + * |
| 41 | + * @author Brian Clozel |
| 42 | + */ |
| 43 | +@Testcontainers(disabledWithoutDocker = true) |
| 44 | +class ReactiveElasticsearchRestClientAutoConfigurationIntegrationTests { |
| 45 | + |
| 46 | + @Container |
| 47 | + static ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch()) |
| 48 | + .withStartupAttempts(5).withStartupTimeout(Duration.ofMinutes(10)); |
| 49 | + |
| 50 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 51 | + .withConfiguration(AutoConfigurations.of(ReactiveElasticsearchRestClientAutoConfiguration.class)); |
| 52 | + |
| 53 | + @Test |
| 54 | + void restClientCanQueryElasticsearchNode() { |
| 55 | + this.contextRunner.withPropertyValues( |
| 56 | + "spring.data.elasticsearch.client.reactive.endpoints=" + elasticsearch.getHost() + ":" |
| 57 | + + elasticsearch.getFirstMappedPort(), |
| 58 | + "spring.data.elasticsearch.client.reactive.connection-timeout=120s", |
| 59 | + "spring.data.elasticsearch.client.reactive.socket-timeout=120s").run((context) -> { |
| 60 | + ReactiveElasticsearchClient client = context.getBean(ReactiveElasticsearchClient.class); |
| 61 | + Map<String, String> source = new HashMap<>(); |
| 62 | + source.put("a", "alpha"); |
| 63 | + source.put("b", "bravo"); |
| 64 | + IndexRequest indexRequest = new IndexRequest("foo").id("1").source(source); |
| 65 | + GetRequest getRequest = new GetRequest("foo").id("1"); |
| 66 | + GetResult getResult = client.index(indexRequest).then(client.get(getRequest)).block(); |
| 67 | + assertThat(getResult).isNotNull(); |
| 68 | + assertThat(getResult.isExists()).isTrue(); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments