-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix job execution retrieval by id for MongoDB
Resolves #4722 Signed-off-by: Mahmoud Ben Hassine <[email protected]>
- Loading branch information
1 parent
ba2202a
commit 82d9c15
Showing
7 changed files
with
396 additions
and
93 deletions.
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
98 changes: 98 additions & 0 deletions
98
...rg/springframework/batch/core/repository/support/MongoDBIntegrationTestConfiguration.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,98 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.core.repository.support; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.explore.JobExplorer; | ||
import org.springframework.batch.core.explore.support.MongoJobExplorerFactoryBean; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
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; | ||
|
||
/** | ||
* @author Mahmoud Ben Hassine | ||
*/ | ||
@Configuration | ||
@EnableBatchProcessing | ||
class MongoDBIntegrationTestConfiguration { | ||
|
||
@Bean | ||
public JobRepository jobRepository(MongoTemplate mongoTemplate, MongoTransactionManager transactionManager) | ||
throws Exception { | ||
MongoJobRepositoryFactoryBean jobRepositoryFactoryBean = new MongoJobRepositoryFactoryBean(); | ||
jobRepositoryFactoryBean.setMongoOperations(mongoTemplate); | ||
jobRepositoryFactoryBean.setTransactionManager(transactionManager); | ||
jobRepositoryFactoryBean.afterPropertiesSet(); | ||
return jobRepositoryFactoryBean.getObject(); | ||
} | ||
|
||
@Bean | ||
public JobExplorer jobExplorer(MongoTemplate mongoTemplate, MongoTransactionManager transactionManager) | ||
throws Exception { | ||
MongoJobExplorerFactoryBean jobExplorerFactoryBean = new MongoJobExplorerFactoryBean(); | ||
jobExplorerFactoryBean.setMongoOperations(mongoTemplate); | ||
jobExplorerFactoryBean.setTransactionManager(transactionManager); | ||
jobExplorerFactoryBean.afterPropertiesSet(); | ||
return jobExplorerFactoryBean.getObject(); | ||
} | ||
|
||
@Bean | ||
public MongoDatabaseFactory mongoDatabaseFactory(@Value("${mongo.connectionString}") String connectionString) { | ||
MongoClient mongoClient = MongoClients.create(connectionString); | ||
return new SimpleMongoClientDatabaseFactory(mongoClient, "test"); | ||
} | ||
|
||
@Bean | ||
public MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDatabaseFactory) { | ||
MongoTemplate template = new MongoTemplate(mongoDatabaseFactory); | ||
MappingMongoConverter converter = (MappingMongoConverter) template.getConverter(); | ||
converter.setMapKeyDotReplacement("."); | ||
return template; | ||
} | ||
|
||
@Bean | ||
public MongoTransactionManager transactionManager(MongoDatabaseFactory mongoDatabaseFactory) { | ||
MongoTransactionManager mongoTransactionManager = new MongoTransactionManager(); | ||
mongoTransactionManager.setDatabaseFactory(mongoDatabaseFactory); | ||
mongoTransactionManager.afterPropertiesSet(); | ||
return mongoTransactionManager; | ||
} | ||
|
||
@Bean | ||
public Job job(JobRepository jobRepository, MongoTransactionManager transactionManager) { | ||
return new JobBuilder("job", jobRepository) | ||
.start(new StepBuilder("step1", jobRepository) | ||
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED, transactionManager) | ||
.build()) | ||
.next(new StepBuilder("step2", jobRepository) | ||
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED, transactionManager) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
...org/springframework/batch/core/repository/support/MongoDBJobExplorerIntegrationTests.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,114 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.core.repository.support; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Map; | ||
|
||
import org.bson.Document; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.JobParametersBuilder; | ||
import org.springframework.batch.core.StepExecution; | ||
import org.springframework.batch.core.explore.JobExplorer; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
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 | ||
*/ | ||
@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"); | ||
mongoTemplate.createCollection("BATCH_JOB_EXECUTION"); | ||
mongoTemplate.createCollection("BATCH_STEP_EXECUTION"); | ||
mongoTemplate.createCollection("BATCH_SEQUENCES"); | ||
mongoTemplate.getCollection("BATCH_SEQUENCES") | ||
.insertOne(new Document(Map.of("_id", "BATCH_JOB_INSTANCE_SEQ", "count", 0L))); | ||
mongoTemplate.getCollection("BATCH_SEQUENCES") | ||
.insertOne(new Document(Map.of("_id", "BATCH_JOB_EXECUTION_SEQ", "count", 0L))); | ||
mongoTemplate.getCollection("BATCH_SEQUENCES") | ||
.insertOne(new Document(Map.of("_id", "BATCH_STEP_EXECUTION_SEQ", "count", 0L))); | ||
} | ||
|
||
@Test | ||
void testGetJobExecutionById(@Autowired JobLauncher jobLauncher, @Autowired Job job, | ||
@Autowired JobExplorer jobExplorer) throws Exception { | ||
// given | ||
JobParameters jobParameters = new JobParametersBuilder().addString("name", "testGetJobExecutionById") | ||
.addLocalDateTime("runtime", LocalDateTime.now()) | ||
.toJobParameters(); | ||
JobExecution jobExecution = jobLauncher.run(job, jobParameters); | ||
|
||
// when | ||
JobExecution actual = jobExplorer.getJobExecution(jobExecution.getId()); | ||
|
||
// then | ||
assertNotNull(actual); | ||
assertNotNull(actual.getJobInstance()); | ||
assertEquals(jobExecution.getJobId(), actual.getJobId()); | ||
assertFalse(actual.getExecutionContext().isEmpty()); | ||
} | ||
|
||
@Test | ||
void testGetStepExecutionByIds(@Autowired JobLauncher jobLauncher, @Autowired Job job, | ||
@Autowired JobExplorer jobExplorer) throws Exception { | ||
// given | ||
JobParameters jobParameters = new JobParametersBuilder().addString("name", "testGetStepExecutionByIds") | ||
.addLocalDateTime("runtime", LocalDateTime.now()) | ||
.toJobParameters(); | ||
JobExecution jobExecution = jobLauncher.run(job, jobParameters); | ||
StepExecution stepExecution = jobExecution.getStepExecutions().stream().findFirst().orElseThrow(); | ||
|
||
// when | ||
StepExecution actual = jobExplorer.getStepExecution(jobExecution.getId(), stepExecution.getId()); | ||
|
||
// then | ||
assertNotNull(actual); | ||
assertEquals(stepExecution.getId(), actual.getId()); | ||
assertFalse(actual.getExecutionContext().isEmpty()); | ||
} | ||
|
||
} |
Oops, something went wrong.