mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-30 16:52:11 +00:00
Improve integration test setup.
Original Pull Request #2072 Closes #2068
This commit is contained in:
parent
1331e26279
commit
cf3e46bf68
1
.gitignore
vendored
1
.gitignore
vendored
@ -20,3 +20,4 @@ target
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea
|
||||
/.env
|
||||
|
21
pom.xml
21
pom.xml
@ -19,7 +19,7 @@
|
||||
|
||||
<properties>
|
||||
<elasticsearch>7.16.3</elasticsearch>
|
||||
<log4j>2.17.0</log4j>
|
||||
<log4j>2.17.1</log4j>
|
||||
<netty>4.1.65.Final</netty>
|
||||
<springdata.commons>2.7.0-SNAPSHOT</springdata.commons>
|
||||
<testcontainers>1.16.2</testcontainers>
|
||||
@ -208,6 +208,25 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${slf4j}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-to-slf4j</artifactId>
|
||||
<version>${log4j}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor.tools</groupId>
|
||||
<artifactId>blockhound-junit-platform</artifactId>
|
||||
|
@ -83,9 +83,6 @@ import org.springframework.data.elasticsearch.annotations.JoinTypeRelations;
|
||||
import org.springframework.data.elasticsearch.annotations.MultiField;
|
||||
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
||||
import org.springframework.data.elasticsearch.annotations.Setting;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.core.query.ScriptField;
|
||||
import org.springframework.data.elasticsearch.core.document.Explanation;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
import org.springframework.data.elasticsearch.core.index.AliasAction;
|
||||
@ -150,7 +147,7 @@ public abstract class ElasticsearchTemplateTests {
|
||||
@Test
|
||||
@Order(java.lang.Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
|
||||
}
|
||||
|
||||
@Test // DATAES-106
|
||||
|
@ -125,7 +125,7 @@ public class ReactiveElasticsearchTemplateIntegrationTests {
|
||||
@Test
|
||||
@Order(java.lang.Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete().block();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete().block();
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -55,7 +55,7 @@ public abstract class RuntimeFieldsIntegrationTests {
|
||||
@Test
|
||||
@Order(java.lang.Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
|
||||
}
|
||||
|
||||
@Test // #1971
|
||||
|
@ -48,8 +48,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.*;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.IndexOperations;
|
||||
import org.springframework.data.elasticsearch.core.MappingContextBaseTests;
|
||||
@ -57,6 +55,8 @@ import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
|
||||
import org.springframework.data.elasticsearch.core.suggest.Completion;
|
||||
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchRestTemplateConfiguration;
|
||||
@ -107,7 +107,7 @@ public class MappingBuilderIntegrationTests extends MappingContextBaseTests {
|
||||
@Test
|
||||
@Order(java.lang.Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -61,7 +61,7 @@ public abstract class CriteriaQueryIntegrationTests {
|
||||
@Test
|
||||
@Order(java.lang.Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
|
||||
}
|
||||
|
||||
@Test // DATAES-706
|
||||
|
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.junit.jupiter;
|
||||
|
||||
import static org.springframework.util.StringUtils.*;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -24,6 +26,8 @@ import java.util.Properties;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.testcontainers.elasticsearch.ElasticsearchContainer;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
@ -40,6 +44,9 @@ public class ClusterConnection implements ExtensionContext.Store.CloseableResour
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(ClusterConnection.class);
|
||||
|
||||
private static final String ENV_ELASTICSEARCH_HOST = "DATAES_ELASTICSEARCH_HOST";
|
||||
private static final String ENV_ELASTICSEARCH_PORT = "DATAES_ELASTICSEARCH_PORT";
|
||||
|
||||
private static final String SDE_TESTCONTAINER_IMAGE_NAME = "sde.testcontainers.image-name";
|
||||
private static final String SDE_TESTCONTAINER_IMAGE_VERSION = "sde.testcontainers.image-version";
|
||||
private static final int ELASTICSEARCH_DEFAULT_PORT = 9200;
|
||||
@ -52,7 +59,7 @@ public class ClusterConnection implements ExtensionContext.Store.CloseableResour
|
||||
* creates the ClusterConnection, starting a container
|
||||
*/
|
||||
public ClusterConnection() {
|
||||
clusterConnectionInfo = startElasticsearchContainer();
|
||||
clusterConnectionInfo = createClusterConnectionInfo();
|
||||
|
||||
if (clusterConnectionInfo != null) {
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
@ -79,6 +86,32 @@ public class ClusterConnection implements ExtensionContext.Store.CloseableResour
|
||||
return clusterConnectionInfo;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private ClusterConnectionInfo createClusterConnectionInfo() {
|
||||
|
||||
String host = System.getenv(ENV_ELASTICSEARCH_HOST);
|
||||
|
||||
if (hasText(host)) {
|
||||
|
||||
String envPort = System.getenv(ENV_ELASTICSEARCH_PORT);
|
||||
|
||||
int port = 9200;
|
||||
|
||||
try {
|
||||
if (hasText(envPort)) {
|
||||
port = Integer.parseInt(envPort);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
LOGGER.warn("DATAES_ELASTICSEARCH_PORT does not contain a number");
|
||||
}
|
||||
|
||||
return ClusterConnectionInfo.builder().withIntegrationtestEnvironment(IntegrationtestEnvironment.get())
|
||||
.withHostAndPort(host, port).build();
|
||||
}
|
||||
|
||||
return startElasticsearchContainer();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private ClusterConnectionInfo startElasticsearchContainer() {
|
||||
|
||||
@ -97,7 +130,7 @@ public class ClusterConnection implements ExtensionContext.Store.CloseableResour
|
||||
|
||||
DockerImageName dockerImageName = getDockerImageName(testcontainersProperties);
|
||||
|
||||
ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(dockerImageName)
|
||||
ElasticsearchContainer elasticsearchContainer = new SpringDataElasticsearchContainer(dockerImageName)
|
||||
.withEnv(testcontainersProperties);
|
||||
elasticsearchContainer.start();
|
||||
|
||||
@ -169,4 +202,19 @@ public class ClusterConnection implements ExtensionContext.Store.CloseableResour
|
||||
LOGGER.debug("closed");
|
||||
}
|
||||
}
|
||||
|
||||
private static class SpringDataElasticsearchContainer extends ElasticsearchContainer {
|
||||
|
||||
public SpringDataElasticsearchContainer(DockerImageName dockerImageName) {
|
||||
super(dockerImageName);
|
||||
}
|
||||
|
||||
/*
|
||||
* don't need that fancy docker whale in the logger name, this makes configuration of the log level impossible
|
||||
*/
|
||||
@Override
|
||||
protected Logger logger() {
|
||||
return LoggerFactory.getLogger(SpringDataElasticsearchContainer.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.junit.jupiter;
|
||||
|
||||
import static org.springframework.util.StringUtils.*;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
@ -60,6 +62,13 @@ public class ElasticsearchRestTemplateConfiguration extends AbstractElasticsearc
|
||||
.usingSsl();
|
||||
}
|
||||
|
||||
String user = System.getenv("DATAES_ELASTICSEARCH_USER");
|
||||
String password = System.getenv("DATAES_ELASTICSEARCH_PASSWORD");
|
||||
|
||||
if (hasText(user) && hasText(password)) {
|
||||
configurationBuilder.withBasicAuth(user, password);
|
||||
}
|
||||
|
||||
return RestClients.create(configurationBuilder //
|
||||
.withConnectTimeout(Duration.ofSeconds(20)) //
|
||||
.withSocketTimeout(Duration.ofSeconds(20)) //
|
||||
|
@ -15,14 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.junit.jupiter;
|
||||
|
||||
import static org.springframework.util.StringUtils.*;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
|
||||
import org.springframework.data.elasticsearch.config.AbstractReactiveElasticsearchConfiguration;
|
||||
import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
||||
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
|
||||
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
|
||||
import org.springframework.data.elasticsearch.config.AbstractReactiveElasticsearchConfiguration;
|
||||
import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
||||
|
||||
/**
|
||||
@ -53,6 +55,13 @@ public class ReactiveElasticsearchRestTemplateConfiguration extends AbstractReac
|
||||
.usingSsl();
|
||||
}
|
||||
|
||||
String user = System.getenv("DATAES_ELASTICSEARCH_USER");
|
||||
String password = System.getenv("DATAES_ELASTICSEARCH_PASSWORD");
|
||||
|
||||
if (hasText(user) && hasText(password)) {
|
||||
configurationBuilder.withBasicAuth(user, password);
|
||||
}
|
||||
|
||||
return ReactiveRestClients.create(configurationBuilder //
|
||||
.withConnectTimeout(Duration.ofSeconds(20)) //
|
||||
.withSocketTimeout(Duration.ofSeconds(20)) //
|
||||
|
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repositories.cdi;
|
||||
|
||||
import static org.springframework.util.StringUtils.*;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.inject.Produces;
|
||||
@ -56,8 +58,17 @@ class ElasticsearchOperationsProducer {
|
||||
// we rely on the tests being run with the SpringDataElasticsearchExtension class that sets up a containerized ES.
|
||||
ClusterConnectionInfo connectionInfo = ClusterConnection.clusterConnectionInfo();
|
||||
|
||||
ClientConfiguration clientConfiguration = ClientConfiguration.builder() //
|
||||
.connectedTo(connectionInfo.getHost() + ':' + connectionInfo.getHttpPort()) //
|
||||
ClientConfiguration.TerminalClientConfigurationBuilder configurationBuilder = ClientConfiguration.builder() //
|
||||
.connectedTo(connectionInfo.getHost() + ':' + connectionInfo.getHttpPort());
|
||||
|
||||
String user = System.getenv("DATAES_ELASTICSEARCH_USER");
|
||||
String password = System.getenv("DATAES_ELASTICSEARCH_PASSWORD");
|
||||
|
||||
if (hasText(user) && hasText(password)) {
|
||||
configurationBuilder.withBasicAuth(user, password);
|
||||
}
|
||||
|
||||
ClientConfiguration clientConfiguration = configurationBuilder //
|
||||
.build();
|
||||
|
||||
return RestClients.create(clientConfiguration).rest();
|
||||
|
@ -52,7 +52,7 @@ public abstract class ComplexCustomMethodRepositoryIntegrationTests {
|
||||
@Test
|
||||
@Order(java.lang.Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -51,7 +51,7 @@ public abstract class ComplexCustomMethodRepositoryManualWiringIntegrationTests
|
||||
@Test
|
||||
@Order(java.lang.Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -93,7 +93,7 @@ public abstract class CustomMethodRepositoryIntegrationTests {
|
||||
@Test
|
||||
@org.junit.jupiter.api.Order(java.lang.Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -1654,9 +1654,7 @@ public abstract class CustomMethodRepositoryIntegrationTests {
|
||||
void shouldBeAbleToUseCollectionInQueryAnnotatedMethod() {
|
||||
List<SampleEntity> entities = createSampleEntities("abc", 20);
|
||||
repository.saveAll(entities);
|
||||
List<String> ids = entities.stream()
|
||||
.map(SampleEntity::getId)
|
||||
.limit(7) // Just get subset
|
||||
List<String> ids = entities.stream().map(SampleEntity::getId).limit(7) // Just get subset
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<SampleEntity> sampleEntities = repository.getByIds(ids);
|
||||
@ -1667,7 +1665,6 @@ public abstract class CustomMethodRepositoryIntegrationTests {
|
||||
assertThat(returnedIds).containsAll(ids);
|
||||
}
|
||||
|
||||
|
||||
private List<SampleEntity> createSampleEntities(String type, int numberOfEntities) {
|
||||
|
||||
List<SampleEntity> entities = new ArrayList<>();
|
||||
|
@ -58,7 +58,7 @@ public abstract class DoubleIDRepositoryIntegrationTests {
|
||||
@Test
|
||||
@Order(Integer.MAX_VALUE)
|
||||
public void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -64,7 +64,7 @@ public abstract class DynamicSettingAndMappingEntityRepositoryIntegrationTests {
|
||||
@Test
|
||||
@Order(Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
|
||||
}
|
||||
|
||||
@Test // DATAES-64
|
||||
|
@ -75,7 +75,7 @@ public class ReactiveQueryKeywordsIntegrationTests {
|
||||
@Test
|
||||
@Order(java.lang.Integer.MAX_VALUE)
|
||||
void cleanup() {
|
||||
operations.indexOps(IndexCoordinates.of("*")).delete().block();
|
||||
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete().block();
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
package org.springframework.data.elasticsearch.utils;
|
||||
|
||||
/**
|
||||
* Class providing an index name with a prefix and a index number
|
||||
* Class providing an index name witInCo
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
*/
|
||||
@ -41,4 +41,11 @@ public class IndexNameProvider {
|
||||
public String indexName() {
|
||||
return indexName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.4
|
||||
*/
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
}
|
||||
|
@ -15,26 +15,22 @@
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<!-- needed for getting testcontainers log -->
|
||||
|
||||
<configuration>
|
||||
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
|
||||
<pattern>%d %5p %c - %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.apache.http" level="info"/>
|
||||
<logger name="org.apache.http.wire" level="info"/>
|
||||
|
||||
<logger name="org.springframework.data.elasticsearch.support.VersionInfo" level="info"/>
|
||||
<logger name="org.springframework.data.elasticsearch.junit" level="debug"/>
|
||||
<logger name="org.springframework.data.elasticsearch.junit.junit4.ElasticsearchVersionRule" level="info"/>
|
||||
<logger name="org.springframework.data.elasticsearch.client.WIRE" level="info"/>
|
||||
|
||||
<logger name="org.springframework.web" level="info"/>
|
||||
|
||||
<logger name="com.github.dockerjava" level="WARN"/>
|
||||
<logger name="org.testcontainers" level="INFO"/>
|
||||
<logger name="com.github.dockerjava" level="INFO"/>
|
||||
|
||||
<root level="error">
|
||||
<appender-ref ref="console"/>
|
||||
|
Loading…
x
Reference in New Issue
Block a user