|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.core.repository.support; |
| 17 | + |
| 18 | +import java.time.LocalDateTime; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.bson.Document; |
| 22 | +import org.junit.jupiter.api.BeforeAll; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.batch.core.Job; |
| 25 | +import org.springframework.batch.core.JobExecution; |
| 26 | +import org.springframework.batch.core.JobParameters; |
| 27 | +import org.springframework.batch.core.JobParametersBuilder; |
| 28 | +import org.springframework.batch.core.StepExecution; |
| 29 | +import org.springframework.batch.core.explore.JobExplorer; |
| 30 | +import org.springframework.batch.core.launch.JobLauncher; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 33 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 34 | +import org.springframework.test.context.DynamicPropertySource; |
| 35 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 36 | +import org.testcontainers.containers.MongoDBContainer; |
| 37 | +import org.testcontainers.junit.jupiter.Container; |
| 38 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 39 | +import org.testcontainers.utility.DockerImageName; |
| 40 | + |
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Henning Pöttker |
| 47 | + */ |
| 48 | +@Testcontainers(disabledWithoutDocker = true) |
| 49 | +@SpringJUnitConfig(MongoDBIntegrationTestConfiguration.class) |
| 50 | +public class MongoDBJobExplorerIntegrationTests { |
| 51 | + |
| 52 | + private static final DockerImageName MONGODB_IMAGE = DockerImageName.parse("mongo:8.0.1"); |
| 53 | + |
| 54 | + @Container |
| 55 | + public static MongoDBContainer mongodb = new MongoDBContainer(MONGODB_IMAGE); |
| 56 | + |
| 57 | + @DynamicPropertySource |
| 58 | + static void setMongoDbConnectionString(DynamicPropertyRegistry registry) { |
| 59 | + registry.add("mongo.connectionString", mongodb::getConnectionString); |
| 60 | + } |
| 61 | + |
| 62 | + @BeforeAll |
| 63 | + static void setUp(@Autowired MongoTemplate mongoTemplate) { |
| 64 | + mongoTemplate.createCollection("BATCH_JOB_INSTANCE"); |
| 65 | + mongoTemplate.createCollection("BATCH_JOB_EXECUTION"); |
| 66 | + mongoTemplate.createCollection("BATCH_STEP_EXECUTION"); |
| 67 | + mongoTemplate.createCollection("BATCH_SEQUENCES"); |
| 68 | + mongoTemplate.getCollection("BATCH_SEQUENCES") |
| 69 | + .insertOne(new Document(Map.of("_id", "BATCH_JOB_INSTANCE_SEQ", "count", 0L))); |
| 70 | + mongoTemplate.getCollection("BATCH_SEQUENCES") |
| 71 | + .insertOne(new Document(Map.of("_id", "BATCH_JOB_EXECUTION_SEQ", "count", 0L))); |
| 72 | + mongoTemplate.getCollection("BATCH_SEQUENCES") |
| 73 | + .insertOne(new Document(Map.of("_id", "BATCH_STEP_EXECUTION_SEQ", "count", 0L))); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testGetJobExecutionById(@Autowired JobLauncher jobLauncher, @Autowired Job job, |
| 78 | + @Autowired JobExplorer jobExplorer) throws Exception { |
| 79 | + // given |
| 80 | + JobParameters jobParameters = new JobParametersBuilder().addString("name", "testGetJobExecutionById") |
| 81 | + .addLocalDateTime("runtime", LocalDateTime.now()) |
| 82 | + .toJobParameters(); |
| 83 | + JobExecution jobExecution = jobLauncher.run(job, jobParameters); |
| 84 | + |
| 85 | + // when |
| 86 | + JobExecution actual = jobExplorer.getJobExecution(jobExecution.getId()); |
| 87 | + |
| 88 | + // then |
| 89 | + assertNotNull(actual); |
| 90 | + assertNotNull(actual.getJobInstance()); |
| 91 | + assertEquals(jobExecution.getJobId(), actual.getJobId()); |
| 92 | + assertFalse(actual.getExecutionContext().isEmpty()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testGetStepExecutionByIds(@Autowired JobLauncher jobLauncher, @Autowired Job job, |
| 97 | + @Autowired JobExplorer jobExplorer) throws Exception { |
| 98 | + // given |
| 99 | + JobParameters jobParameters = new JobParametersBuilder().addString("name", "testGetStepExecutionByIds") |
| 100 | + .addLocalDateTime("runtime", LocalDateTime.now()) |
| 101 | + .toJobParameters(); |
| 102 | + JobExecution jobExecution = jobLauncher.run(job, jobParameters); |
| 103 | + StepExecution stepExecution = jobExecution.getStepExecutions().stream().findFirst().orElseThrow(); |
| 104 | + |
| 105 | + // when |
| 106 | + StepExecution actual = jobExplorer.getStepExecution(jobExecution.getId(), stepExecution.getId()); |
| 107 | + |
| 108 | + // then |
| 109 | + assertNotNull(actual); |
| 110 | + assertEquals(stepExecution.getId(), actual.getId()); |
| 111 | + assertFalse(actual.getExecutionContext().isEmpty()); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments