|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 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.test.autoconfigure.data.elasticsearch; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.UUID; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.testcontainers.elasticsearch.ElasticsearchContainer; |
| 24 | +import org.testcontainers.junit.jupiter.Container; |
| 25 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.boot.test.context.TestConfiguration; |
| 30 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 31 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration; |
| 32 | +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; |
| 33 | +import org.springframework.context.ApplicationContext; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 39 | +import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration; |
| 40 | + |
| 41 | +/** |
| 42 | + * Integration tests for {@link DataElasticsearchTest @DataElasticsearchTest} with |
| 43 | + * Elasticsearch 8. |
| 44 | + * |
| 45 | + * @author Scott Frederick |
| 46 | + */ |
| 47 | +@Testcontainers(disabledWithoutDocker = true) |
| 48 | +@DataElasticsearchTest(properties = { "spring.elasticsearch.restclient.ssl.bundle=elasticsearch-container" }) |
| 49 | +class DataElasticsearchTestVersion8IntegrationTests { |
| 50 | + |
| 51 | + @Container |
| 52 | + @ServiceConnection |
| 53 | + static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch8()) |
| 54 | + .withEnv("ES_JAVA_OPTS", "-Xms32m -Xmx512m") |
| 55 | + .withStartupAttempts(5) |
| 56 | + .withStartupTimeout(Duration.ofMinutes(10)); |
| 57 | + |
| 58 | + @Autowired |
| 59 | + private ElasticsearchTemplate elasticsearchTemplate; |
| 60 | + |
| 61 | + @Autowired |
| 62 | + private ExampleRepository exampleRepository; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private ApplicationContext applicationContext; |
| 66 | + |
| 67 | + @Test |
| 68 | + void didNotInjectExampleService() { |
| 69 | + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) |
| 70 | + .isThrownBy(() -> this.applicationContext.getBean(ExampleService.class)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testRepository() { |
| 75 | + ExampleDocument document = new ExampleDocument(); |
| 76 | + document.setText("Look, new @DataElasticsearchTest!"); |
| 77 | + String id = UUID.randomUUID().toString(); |
| 78 | + document.setId(id); |
| 79 | + ExampleDocument savedDocument = this.exampleRepository.save(document); |
| 80 | + ExampleDocument getDocument = this.elasticsearchTemplate.get(id, ExampleDocument.class); |
| 81 | + assertThat(getDocument).isNotNull(); |
| 82 | + assertThat(getDocument.getId()).isNotNull(); |
| 83 | + assertThat(getDocument.getId()).isEqualTo(savedDocument.getId()); |
| 84 | + this.exampleRepository.deleteAll(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void serviceConnectionAutoConfigurationWasImported() { |
| 89 | + assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class)); |
| 90 | + } |
| 91 | + |
| 92 | + @TestConfiguration |
| 93 | + static class ElasticsearchContainerSslBundleConfiguration { |
| 94 | + |
| 95 | + @Bean |
| 96 | + ElasticsearchContainerSslBundleRegistrar elasticsearchContainerSslBundleRegistrar() { |
| 97 | + return new ElasticsearchContainerSslBundleRegistrar("elasticsearch-container", elasticsearch); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments