Skip to content

Commit

Permalink
feat : use test containers in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Sep 6, 2024
1 parent 0f55e77 commit fffbc95
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 32 deletions.
13 changes: 1 addition & 12 deletions boot-opensearch-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,9 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>spring-data-opensearch-test-autoconfigure</artifactId>
<artifactId>spring-data-opensearch-testcontainers</artifactId>
<version>${opensearch.version}</version>
<scope>test</scope>
</dependency>
Expand All @@ -105,12 +100,6 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.opensearch</groupId>
<artifactId>opensearch-testcontainers</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

@SpringBootApplication(exclude = {ElasticsearchDataAutoConfiguration.class})
@EnableConfigurationProperties({ApplicationProperties.class})
public class Application {
public class OpenSearchApplication {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(OpenSearchApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.opensearch;

import com.example.opensearch.common.ContainersConfig;
import org.springframework.boot.SpringApplication;

class TestOpenSearchApplication {

public static void main(String[] args) {
SpringApplication.from(OpenSearchApplication::main).with(ContainersConfig.class).run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import org.springframework.test.web.servlet.MockMvc;

@ActiveProfiles({PROFILE_TEST})
@SpringBootTest(webEnvironment = RANDOM_PORT)
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ContainersConfig.class)
@AutoConfigureMockMvc
public abstract class AbstractIntegrationTest extends ContainersConfig {
public abstract class AbstractIntegrationTest {

@Autowired protected MockMvc mockMvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package com.example.opensearch.common;

import java.time.Duration;
import org.opensearch.testcontainers.OpensearchContainer;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.utility.DockerImageName;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;

@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfig {

@Container
public static final OpensearchContainer<?> openSearchContainer =
new OpensearchContainer<>(DockerImageName.parse("opensearchproject/opensearch:2.16.0"))
.withEnv("OPENSEARCH_INITIAL_ADMIN_PASSWORD", "admin");

static {
openSearchContainer.start();
}

@DynamicPropertySource
static void setApplicationProperties(DynamicPropertyRegistry dynamicPropertyRegistry) {
dynamicPropertyRegistry.add("opensearch.uris", openSearchContainer::getHttpHostAddress);
@Bean
@ServiceConnection
OpensearchContainer<?> opensearchContainer() {
return new OpensearchContainer<>("opensearchproject/opensearch:2.16.0")
.withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(10));
}
}

0 comments on commit fffbc95

Please sign in to comment.