Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish MongoDB integration tests #4796

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,31 @@
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.utility.DockerImageName;

/**
* @author Mahmoud Ben Hassine
* @author Yanming Zhou
*/
@Configuration
@EnableBatchProcessing
class MongoDBIntegrationTestConfiguration {

private static final DockerImageName MONGODB_IMAGE = DockerImageName.parse("mongo:8.0.1");

@Bean(initMethod = "start")
public MongoDBContainer mongoDBContainer() {
return new MongoDBContainer(MONGODB_IMAGE);
}

@Bean
public JobRepository jobRepository(MongoTemplate mongoTemplate, MongoTransactionManager transactionManager)
throws Exception {
Expand All @@ -60,8 +69,8 @@ public JobExplorer jobExplorer(MongoTemplate mongoTemplate, MongoTransactionMana
}

@Bean
public MongoDatabaseFactory mongoDatabaseFactory(@Value("${mongo.connectionString}") String connectionString) {
return new SimpleMongoClientDatabaseFactory(connectionString + "/test");
public MongoDatabaseFactory mongoDatabaseFactory(MongoDBContainer mongoDBContainer) {
return new SimpleMongoClientDatabaseFactory(mongoDBContainer.getConnectionString() + "/test");
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,22 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author Henning Pöttker
* @author Yanming Zhou
*/
@DirtiesContext
@Testcontainers(disabledWithoutDocker = true)
@SpringJUnitConfig(MongoDBIntegrationTestConfiguration.class)
public class MongoDBJobExplorerIntegrationTests {

private static final DockerImageName MONGODB_IMAGE = DockerImageName.parse("mongo:8.0.1");

@Container
public static MongoDBContainer mongodb = new MongoDBContainer(MONGODB_IMAGE);

@DynamicPropertySource
static void setMongoDbConnectionString(DynamicPropertyRegistry registry) {
registry.add("mongo.connectionString", mongodb::getConnectionString);
}

@BeforeAll
static void setUp(@Autowired MongoTemplate mongoTemplate) {
mongoTemplate.createCollection("BATCH_JOB_INSTANCE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
Expand All @@ -45,22 +40,13 @@

/**
* @author Mahmoud Ben Hassine
* @author Yanming Zhou
*/
@DirtiesContext
@Testcontainers(disabledWithoutDocker = true)
@SpringJUnitConfig(MongoDBIntegrationTestConfiguration.class)
public class MongoDBJobRepositoryIntegrationTests {

private static final DockerImageName MONGODB_IMAGE = DockerImageName.parse("mongo:8.0.1");

@Container
public static MongoDBContainer mongodb = new MongoDBContainer(MONGODB_IMAGE);

@DynamicPropertySource
static void setMongoDbConnectionString(DynamicPropertyRegistry registry) {
registry.add("mongo.connectionString", mongodb::getConnectionString);
}

@Autowired
private MongoTemplate mongoTemplate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,22 @@
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author Henning Pöttker
* @author Yanming Zhou
*/
@DirtiesContext
@Testcontainers(disabledWithoutDocker = true)
@SpringJUnitConfig({ MongoDBIntegrationTestConfiguration.class, ExecutionContextDaoConfiguration.class })
public class MongoExecutionContextDaoIntegrationTests {

private static final DockerImageName MONGODB_IMAGE = DockerImageName.parse("mongo:8.0.1");

@Container
public static MongoDBContainer mongodb = new MongoDBContainer(MONGODB_IMAGE);

@DynamicPropertySource
static void setMongoDbConnectionString(DynamicPropertyRegistry registry) {
registry.add("mongo.connectionString", mongodb::getConnectionString);
}

@BeforeAll
static void setUp(@Autowired MongoTemplate mongoTemplate) {
mongoTemplate.createCollection("BATCH_JOB_INSTANCE");
Expand Down