Skip to content

Commit defc0c1

Browse files
committed
Add script to initialize MongoDB with Batch meta-data collections
1 parent 8bfd144 commit defc0c1

File tree

3 files changed

+22
-35
lines changed

3 files changed

+22
-35
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{create:'BATCH_JOB_INSTANCE'}
2+
{create:'BATCH_JOB_EXECUTION'}
3+
{create:'BATCH_STEP_EXECUTION'}
4+
{create:'BATCH_SEQUENCES'}
5+
{insert: "BATCH_SEQUENCES", documents: [ { _id: 'BATCH_JOB_INSTANCE_SEQ', count: NumberLong(0) } ]}
6+
{insert: "BATCH_SEQUENCES", documents: [ { _id: 'BATCH_JOB_EXECUTION_SEQ', count: NumberLong(0) } ]}
7+
{insert: "BATCH_SEQUENCES", documents: [ { _id: 'BATCH_STEP_EXECUTION_SEQ', count: NumberLong(0) } ]}

spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/MongoDBJobRepositoryIntegrationTests.java

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515
*/
1616
package org.springframework.batch.core.repository.support;
1717

18+
import java.io.IOException;
19+
import java.nio.file.Files;
1820
import java.time.LocalDateTime;
19-
import java.util.Map;
2021

2122
import com.mongodb.client.MongoCollection;
2223
import org.bson.Document;
2324
import org.junit.jupiter.api.Assertions;
2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
26-
import org.springframework.test.annotation.DirtiesContext;
27-
import org.springframework.data.domain.Sort;
28-
import org.springframework.data.mongodb.core.index.Index;
29-
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3027
import org.testcontainers.junit.jupiter.Testcontainers;
3128

3229
import org.springframework.batch.core.ExitStatus;
@@ -36,7 +33,10 @@
3633
import org.springframework.batch.core.job.parameters.JobParametersBuilder;
3734
import org.springframework.batch.core.launch.JobOperator;
3835
import org.springframework.beans.factory.annotation.Autowired;
36+
import org.springframework.core.io.ClassPathResource;
3937
import org.springframework.data.mongodb.core.MongoTemplate;
38+
import org.springframework.test.annotation.DirtiesContext;
39+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4040

4141
/**
4242
* @author Mahmoud Ben Hassine
@@ -51,36 +51,9 @@ public class MongoDBJobRepositoryIntegrationTests {
5151
private MongoTemplate mongoTemplate;
5252

5353
@BeforeEach
54-
public void setUp() {
55-
// collections
56-
mongoTemplate.createCollection("BATCH_JOB_INSTANCE");
57-
mongoTemplate.createCollection("BATCH_JOB_EXECUTION");
58-
mongoTemplate.createCollection("BATCH_STEP_EXECUTION");
59-
// sequences
60-
mongoTemplate.createCollection("BATCH_SEQUENCES");
61-
mongoTemplate.getCollection("BATCH_SEQUENCES")
62-
.insertOne(new Document(Map.of("_id", "BATCH_JOB_INSTANCE_SEQ", "count", 0L)));
63-
mongoTemplate.getCollection("BATCH_SEQUENCES")
64-
.insertOne(new Document(Map.of("_id", "BATCH_JOB_EXECUTION_SEQ", "count", 0L)));
65-
mongoTemplate.getCollection("BATCH_SEQUENCES")
66-
.insertOne(new Document(Map.of("_id", "BATCH_STEP_EXECUTION_SEQ", "count", 0L)));
67-
// indices
68-
mongoTemplate.indexOps("BATCH_JOB_INSTANCE")
69-
.createIndex(new Index().on("jobName", Sort.Direction.ASC).named("job_name_idx"));
70-
mongoTemplate.indexOps("BATCH_JOB_INSTANCE")
71-
.createIndex(new Index().on("jobName", Sort.Direction.ASC)
72-
.on("jobKey", Sort.Direction.ASC)
73-
.named("job_name_key_idx"));
74-
mongoTemplate.indexOps("BATCH_JOB_INSTANCE")
75-
.createIndex(new Index().on("jobInstanceId", Sort.Direction.DESC).named("job_instance_idx"));
76-
mongoTemplate.indexOps("BATCH_JOB_EXECUTION")
77-
.createIndex(new Index().on("jobInstanceId", Sort.Direction.ASC).named("job_instance_idx"));
78-
mongoTemplate.indexOps("BATCH_JOB_EXECUTION")
79-
.createIndex(new Index().on("jobInstanceId", Sort.Direction.ASC)
80-
.on("status", Sort.Direction.ASC)
81-
.named("job_instance_status_idx"));
82-
mongoTemplate.indexOps("BATCH_STEP_EXECUTION")
83-
.createIndex(new Index().on("stepExecutionId", Sort.Direction.ASC).named("step_execution_idx"));
54+
public void setUp() throws IOException {
55+
ClassPathResource resource = new ClassPathResource("org/springframework/batch/core/schema-mongodb.json");
56+
Files.lines(resource.getFilePath()).forEach(line -> mongoTemplate.executeCommand(line));
8457
}
8558

8659
@Test
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{create:'BATCH_JOB_INSTANCE'}
2+
{create:'BATCH_JOB_EXECUTION'}
3+
{create:'BATCH_STEP_EXECUTION'}
4+
{create:'BATCH_SEQUENCES'}
5+
{insert: "BATCH_SEQUENCES", documents: [ { _id: 'BATCH_JOB_INSTANCE_SEQ', count: NumberLong(0) } ]}
6+
{insert: "BATCH_SEQUENCES", documents: [ { _id: 'BATCH_JOB_EXECUTION_SEQ', count: NumberLong(0) } ]}
7+
{insert: "BATCH_SEQUENCES", documents: [ { _id: 'BATCH_STEP_EXECUTION_SEQ', count: NumberLong(0) } ]}

0 commit comments

Comments
 (0)