|
| 1 | +package org.testcontainers.typesense; |
| 2 | + |
| 3 | +import org.testcontainers.containers.GenericContainer; |
| 4 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 5 | +import org.testcontainers.utility.DockerImageName; |
| 6 | + |
| 7 | +/** |
| 8 | + * Testcontainers implementation for Typesense. |
| 9 | + * <p> |
| 10 | + * Supported image: {@code typesense/typesense} |
| 11 | + * <p> |
| 12 | + * Exposed ports: 8108 |
| 13 | + */ |
| 14 | +public class TypesenseContainer extends GenericContainer<TypesenseContainer> { |
| 15 | + |
| 16 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("typesense/typesense"); |
| 17 | + |
| 18 | + private static final int PORT = 8108; |
| 19 | + |
| 20 | + private static final String DEFAULT_API_KEY = "testcontainers"; |
| 21 | + |
| 22 | + private String apiKey = DEFAULT_API_KEY; |
| 23 | + |
| 24 | + public TypesenseContainer(String dockerImageName) { |
| 25 | + this(DockerImageName.parse(dockerImageName)); |
| 26 | + } |
| 27 | + |
| 28 | + public TypesenseContainer(DockerImageName dockerImageName) { |
| 29 | + super(dockerImageName); |
| 30 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 31 | + withExposedPorts(PORT); |
| 32 | + withEnv("TYPESENSE_DATA_DIR", "/tmp"); |
| 33 | + waitingFor( |
| 34 | + Wait |
| 35 | + .forHttp("/health") |
| 36 | + .forStatusCode(200) |
| 37 | + .forResponsePredicate(response -> response.contains("\"ok\":true")) |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected void configure() { |
| 43 | + withEnv("TYPESENSE_API_KEY", this.apiKey); |
| 44 | + } |
| 45 | + |
| 46 | + public TypesenseContainer withApiKey(String apiKey) { |
| 47 | + this.apiKey = apiKey; |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + public String getHttpPort() { |
| 52 | + return String.valueOf(getMappedPort(PORT)); |
| 53 | + } |
| 54 | + |
| 55 | + public String getApiKey() { |
| 56 | + return this.apiKey; |
| 57 | + } |
| 58 | +} |
0 commit comments