Skip to content

Commit

Permalink
Add Elasticsearch support (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Christoph Schmid <christoph.schmid@cloudflight.io>
  • Loading branch information
ngChris and Christoph Schmid authored Mar 29, 2023
1 parent 03b6241 commit 4ac05d2
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,18 @@ dependencies {
* `spring.mail.host`
* `spring.mail.port`
* `test-resources.mailhog.api-url`

## Elasticsearch

* **Module-ID**: elasticsearch
* **Default-Image**: docker.elastic.co/elasticsearch/elasticsearch

````kotlin
dependencies {
testResourcesImplementation ("io.cloudflight.testresources.springboot:springboot-testresources-elasticsearch:0.1.2")
}
````

* **Provided properties**:
* `spring.elasticsearch.uris`
* `spring.elasticsearch.password`
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "j
jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }

assertj = { module = "org.assertj:assertj-core", version = "3.23.1" }

testcontainers-elasticsearch = { module = "org.testcontainers:elasticsearch", version = "" }
testcontainers-junit4-mock = { module = "io.quarkus:quarkus-junit4-mock", version = "2.9.2.Final" }
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include("springboot-testresources-jdbc:springboot-testresources-jdbc-postgres")
include("springboot-testresources-rabbitmq")
include("springboot-testresources-redis")
include("springboot-testresources-mailhog")
include("springboot-testresources-elasticsearch")

include("testprojects:jdbc:mariadb")
include("testprojects:jdbc:mssql")
Expand All @@ -23,3 +24,4 @@ include("testprojects:minio")
include("testprojects:rabbitmq")
include("testprojects:redis")
include("testprojects:mailhog")
include("testprojects:elasticsearch")
6 changes: 6 additions & 0 deletions springboot-testresources-elasticsearch/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

description = "Spring Boot TestResourceProvider for Elasticsearc"

dependencies {
implementation("org.testcontainers:elasticsearch")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.cloudflight.testresources.springboot.elasticsearch;

import io.micronaut.testresources.testcontainers.AbstractTestContainersProvider;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.utility.DockerImageName;

import java.util.*;
import java.util.stream.Collectors;

public class ElasticSearchTestResourceProvider extends AbstractTestContainersProvider<ElasticsearchContainer> {

private static final String SIMPLE_NAME = "elasticsearch";
private static final String DEFAULT_IMAGE = "docker.elastic.co/elasticsearch/elasticsearch";
private static final String DEFAULT_TAG = "8.4.3";
private static final String PREFIX = "spring.elasticsearch";
private static final String URIS = "uris";
private static final String PASSWORD = "password";
private static final String ENV_PASSWORD = "ELASTIC_PASSWORD";
private static final List<String> SUPPORTED_LIST = Collections.unmodifiableList(
Arrays.asList(URIS, PASSWORD)
);

@Override
protected String getSimpleName() {
return SIMPLE_NAME;
}

@Override
protected String getDefaultImageName() {
return DEFAULT_IMAGE;
}

@Override
protected ElasticsearchContainer createContainer(DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfiguration) {
if ("latest".equals(imageName.getVersionPart())) {
// ElasticSearch does't provide a latest tag, so we use a hardcoded version
imageName = imageName.withTag(DEFAULT_TAG);
}
ElasticsearchContainer container = new ElasticsearchContainer(imageName);
container.withEnv("xpack.security.enabled", "false");
return container;
}

@Override
protected Optional<String> resolveProperty(String propertyName, ElasticsearchContainer container) {
String value = switch (configurationPropertyFrom(propertyName)) {
case URIS -> container.getHttpHostAddress();
case PASSWORD -> container.getEnvMap().get(ENV_PASSWORD);
default -> null;
};
return Optional.ofNullable(value);
}

private String configurationPropertyFrom(String expression) {
String[] propertyParts = expression.split("\\.");
return propertyParts[propertyParts.length - 1];
}

@Override
public boolean shouldAnswer(String propertyName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfiguration) {
return propertyName.startsWith(PREFIX);
}

@Override
public List<String> getResolvableProperties(Map<String, Collection<String>> propertyEntries, Map<String, Object> testResourcesConfig) {
return SUPPORTED_LIST.stream().map(p -> PREFIX + "." + p).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.cloudflight.testresources.springboot.elasticsearch.ElasticSearchTestResourceProvider
14 changes: 14 additions & 0 deletions testprojects/elasticsearch/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id("io.micronaut.test-resources")
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-elasticsearch")

runtimeOnly("org.jetbrains.kotlin:kotlin-reflect")

testImplementation("org.springframework.boot:spring-boot-starter-test")

testRuntimeOnly(project(":springboot-testresources-client"))
testResourcesImplementation(project(":springboot-testresources-elasticsearch"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.cloudflight.testresources.springboot.elasticsearch

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.data.annotation.Id
import org.springframework.data.elasticsearch.annotations.Document
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
import org.springframework.stereotype.Repository


@SpringBootApplication
class Application

@Document(indexName = "test_index")
class TestDocument( @Id var key: String? = null, var title: String? = null) {
}

@Repository
interface TestDocumentRepository : ElasticsearchRepository<TestDocument?, String?> {
fun findByTitle(title: String?): List<TestDocument?>?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.cloudflight.testresources.springboot.elasticsearch

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class ApplicationTest(
@Value("\${spring.elasticsearch.uris}") private val uris: String,
@Autowired private val testDocumentRepository: TestDocumentRepository
) {

@Test
fun urisPropertyIsBound() {
assertThat(uris).matches("^localhost:[1-9][0-9]{3,4}\$")
}

@Test
fun springDataShouldWork() {
val key = "test::1"
val title = "myvalue"
assertThat(testDocumentRepository).isNotNull()
assertThat(testDocumentRepository.findById(key).isPresent()).isFalse()
val testDocument = TestDocument(key, title)
testDocumentRepository.save(testDocument)
val foundDocument = testDocumentRepository.findById(key).get()
assertThat(foundDocument.key).isEqualTo(key)
assertThat(foundDocument.title).isEqualTo(title)
}
}

0 comments on commit 4ac05d2

Please sign in to comment.