-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Christoph Schmid <christoph.schmid@cloudflight.io>
- Loading branch information
Showing
9 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
69 changes: 69 additions & 0 deletions
69
...cloudflight/testresources/springboot/elasticsearch/ElasticSearchTestResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...rc/main/resources/META-INF/services/io.micronaut.testresources.core.TestResourcesResolver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.cloudflight.testresources.springboot.elasticsearch.ElasticSearchTestResourceProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
20 changes: 20 additions & 0 deletions
20
...arch/src/main/kotlin/io/cloudflight/testresources/springboot/elasticsearch/Application.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?>? | ||
} |
32 changes: 32 additions & 0 deletions
32
.../src/test/kotlin/io/cloudflight/testresources/springboot/elasticsearch/ApplicationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |