diff --git a/pom.xml b/pom.xml index 872689c..23be52a 100644 --- a/pom.xml +++ b/pom.xml @@ -50,11 +50,6 @@ spring-boot-starter-test test - - org.springframework.batch - spring-batch-test - test - diff --git a/src/main/java/com/springbatch/excel/tutorial/batch/BatchConfiguration.java b/src/main/java/com/springbatch/excel/tutorial/batch/BatchConfiguration.java index 4fd0338..f9ece9f 100644 --- a/src/main/java/com/springbatch/excel/tutorial/batch/BatchConfiguration.java +++ b/src/main/java/com/springbatch/excel/tutorial/batch/BatchConfiguration.java @@ -12,7 +12,10 @@ import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.job.CompositeJobParametersValidator; +import org.springframework.batch.core.job.builder.JobBuilder; import org.springframework.batch.core.launch.support.RunIdIncrementer; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.builder.StepBuilder; import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.data.MongoItemWriter; @@ -20,6 +23,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.transaction.PlatformTransactionManager; import java.util.Collections; diff --git a/src/test/java/com/springbatch/excel/tutorial/BachIntegrationTest.java b/src/test/java/com/springbatch/excel/tutorial/BachIntegrationTest.java new file mode 100644 index 0000000..130a434 --- /dev/null +++ b/src/test/java/com/springbatch/excel/tutorial/BachIntegrationTest.java @@ -0,0 +1,61 @@ +package com.springbatch.excel.tutorial; +import com.springbatch.excel.tutorial.batch.BatchConfiguration; +import com.springbatch.excel.tutorial.batch.listeners.JobCompletionListener; +import com.springbatch.excel.tutorial.domain.Employee; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameter; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.*; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + + +@ExtendWith(SpringExtension.class) +@EnableBatchProcessing +@ComponentScan(basePackages = "com.springbatch.excel.tutorial") +@SpringBootTest(classes = {BatchConfiguration.class, JobCompletionListener.class}) +class BatchIntegrationTest { + + @Autowired + private JobLauncher jobLauncher; + + @Autowired + private Job jsonFileProcessingJob; + + @Autowired + private MongoTemplate mongoTemplate; + + @Test + void testBatchJob() throws Exception { + String absolutePath = Objects.requireNonNull(getClass().getResource("/data/processing/employee.xlsx")).getPath(); + + // Créez les paramètres du job + Map jobParameters = new HashMap<>(); + jobParameters.put("filePath", new JobParameter(absolutePath)); + jobParameters.put("currentTime", new JobParameter(new Date())); + + // Lancez le job avec les paramètres + JobExecution jobExecution = jobLauncher.run(jsonFileProcessingJob,new JobParameters(jobParameters)); + + // Vérifiez que le job s'est terminé avec succès + assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo("COMPLETED"); + + List couponOffers = mongoTemplate.findAll(Employee.class); + assertThat(couponOffers.size()).isEqualTo(100); + + } +} + + diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml new file mode 100644 index 0000000..a0fd8c1 --- /dev/null +++ b/src/test/resources/application.yml @@ -0,0 +1,26 @@ +spring: + data: + mongodb: + host: localhost + port: 27017 + database: spring_batch_excel_mongodb_db +# username: admin +# password: pass + auto-index-creation: true + + jackson: + default-property-inclusion: NON_NULL +#disabled job run at startup + batch: + job: + enabled: false + +# Enable Logging mongo queries +logging: + level: + org.springframework.data.mongodb.core.MongoTemplate: DEBUG + +employee: + excel: + processingfolder: data/processing/ + resultsfolder: data/results/ diff --git a/src/test/resources/data/processing/employee.txt b/src/test/resources/data/processing/employee.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/data/processing/employee.xlsx b/src/test/resources/data/processing/employee.xlsx new file mode 100644 index 0000000..312ae9a Binary files /dev/null and b/src/test/resources/data/processing/employee.xlsx differ diff --git a/src/test/resources/data/results/readme.txt b/src/test/resources/data/results/readme.txt new file mode 100644 index 0000000..e69de29