|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 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.testcontainers.service.connection.elasticsearch; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.time.Duration; |
| 21 | + |
| 22 | +import co.elastic.clients.elasticsearch.ElasticsearchClient; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.testcontainers.elasticsearch.ElasticsearchContainer; |
| 25 | +import org.testcontainers.junit.jupiter.Container; |
| 26 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 27 | + |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 30 | +import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientAutoConfiguration; |
| 31 | +import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchConnectionDetails; |
| 32 | +import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration; |
| 33 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 34 | +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for {@link ElasticsearchContainerConnectionDetailsFactory}. |
| 42 | + * |
| 43 | + * @author Andy Wilkinson |
| 44 | + */ |
| 45 | +@SpringJUnitConfig |
| 46 | +@Testcontainers(disabledWithoutDocker = true) |
| 47 | +class ElasticsearchContainerConnectionDetailsFactoryTests { |
| 48 | + |
| 49 | + @Container |
| 50 | + @ServiceConnection |
| 51 | + static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch()) |
| 52 | + .withEnv("ES_JAVA_OPTS", "-Xms32m -Xmx512m") |
| 53 | + .withStartupAttempts(5) |
| 54 | + .withStartupTimeout(Duration.ofMinutes(10)); |
| 55 | + |
| 56 | + @Autowired(required = false) |
| 57 | + private ElasticsearchConnectionDetails connectionDetails; |
| 58 | + |
| 59 | + @Autowired |
| 60 | + private ElasticsearchClient client; |
| 61 | + |
| 62 | + @Test |
| 63 | + void connectionCanBeMadeToElasticsearchContainer() throws IOException { |
| 64 | + assertThat(this.connectionDetails).isNotNull(); |
| 65 | + assertThat(this.client.cluster().health().numberOfNodes()).isEqualTo(1); |
| 66 | + } |
| 67 | + |
| 68 | + @Configuration(proxyBeanMethods = false) |
| 69 | + @ImportAutoConfiguration({ ElasticsearchClientAutoConfiguration.class, |
| 70 | + ElasticsearchRestClientAutoConfiguration.class }) |
| 71 | + static class TestConfiguration { |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments