|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.fineract.infrastructure.jobs.service; |
| 20 | + |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.Mockito.times; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.stream.Stream; |
| 30 | +import org.apache.fineract.infrastructure.jobs.data.JobParameterDTO; |
| 31 | +import org.apache.fineract.infrastructure.jobs.domain.JobParameter; |
| 32 | +import org.apache.fineract.infrastructure.jobs.domain.JobParameterRepository; |
| 33 | +import org.apache.fineract.infrastructure.jobs.domain.ScheduledJobDetail; |
| 34 | +import org.apache.fineract.infrastructure.jobs.service.jobname.JobNameData; |
| 35 | +import org.apache.fineract.infrastructure.jobs.service.jobname.JobNameService; |
| 36 | +import org.apache.fineract.infrastructure.jobs.service.jobparameterprovider.JobParameterProvider; |
| 37 | +import org.junit.jupiter.api.Assertions; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 40 | +import org.mockito.ArgumentCaptor; |
| 41 | +import org.mockito.Captor; |
| 42 | +import org.mockito.InjectMocks; |
| 43 | +import org.mockito.Mock; |
| 44 | +import org.mockito.Mockito; |
| 45 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 46 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 47 | +import org.mockito.quality.Strictness; |
| 48 | +import org.quartz.JobExecutionException; |
| 49 | +import org.springframework.batch.core.BatchStatus; |
| 50 | +import org.springframework.batch.core.ExitStatus; |
| 51 | +import org.springframework.batch.core.Job; |
| 52 | +import org.springframework.batch.core.JobExecution; |
| 53 | +import org.springframework.batch.core.JobParameters; |
| 54 | +import org.springframework.batch.core.JobParametersIncrementer; |
| 55 | +import org.springframework.batch.core.JobParametersInvalidException; |
| 56 | +import org.springframework.batch.core.explore.JobExplorer; |
| 57 | +import org.springframework.batch.core.launch.JobLauncher; |
| 58 | +import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; |
| 59 | +import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; |
| 60 | +import org.springframework.batch.core.repository.JobRestartException; |
| 61 | + |
| 62 | +@ExtendWith(MockitoExtension.class) |
| 63 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 64 | +public class JobStarterTest { |
| 65 | + |
| 66 | + @Mock |
| 67 | + private JobExplorer jobExplorer; |
| 68 | + @Mock |
| 69 | + private JobLauncher jobLauncher; |
| 70 | + @Mock |
| 71 | + private JobParameterRepository jobParameterRepository; |
| 72 | + @Mock |
| 73 | + private List<JobParameterProvider<?>> jobParameterProviders; |
| 74 | + @Mock |
| 75 | + private JobNameService jobNameService; |
| 76 | + @Captor |
| 77 | + private ArgumentCaptor<Set<JobParameterDTO>> jobParameterDTOCaptor; |
| 78 | + |
| 79 | + @InjectMocks |
| 80 | + private JobStarter underTest; |
| 81 | + |
| 82 | + @Test |
| 83 | + public void getJobParameterTest() { |
| 84 | + ScheduledJobDetail scheduledJobDetail = Mockito.mock(ScheduledJobDetail.class); |
| 85 | + when(scheduledJobDetail.getId()).thenReturn(1L); |
| 86 | + when(jobParameterRepository.findJobParametersByJobId(1L)) |
| 87 | + .thenReturn(List.of(new JobParameter().setJobId(1L).setParameterName("testParamKey").setParameterValue("testParamValue"))); |
| 88 | + Map<String, org.springframework.batch.core.JobParameter<?>> result = underTest.getJobParameter(scheduledJobDetail); |
| 89 | + Assertions.assertEquals("testParamValue", result.get("testParamKey").getValue()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void provideCustomJobParameters() { |
| 94 | + JobParameterProvider<?> jobParameterProvider = Mockito.mock(JobParameterProvider.class); |
| 95 | + when(jobParameterProvider.canProvideParametersForJob("testJobName")).thenReturn(true); |
| 96 | + when(jobParameterProviders.stream()).thenReturn(Stream.of(jobParameterProvider)); |
| 97 | + underTest.provideCustomJobParameters("testJobName", Set.of(new JobParameterDTO("testKey", "testValue"))); |
| 98 | + verify(jobParameterProvider, times(1)).provide(jobParameterDTOCaptor.capture()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void runWithComplete() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, |
| 103 | + JobParametersInvalidException, JobRestartException, JobExecutionException { |
| 104 | + JobExecution jobExecution = Mockito.mock(JobExecution.class); |
| 105 | + Job job = Mockito.mock(Job.class); |
| 106 | + ScheduledJobDetail scheduledJobDetail = Mockito.mock(ScheduledJobDetail.class); |
| 107 | + when(jobExecution.getStatus()).thenReturn(BatchStatus.COMPLETED); |
| 108 | + setupMocks(jobExecution, job, scheduledJobDetail); |
| 109 | + JobExecution result = underTest.run(job, scheduledJobDetail, Set.of()); |
| 110 | + Assertions.assertEquals(jobExecution, result); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void runWithFailed() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, |
| 115 | + JobParametersInvalidException, JobRestartException, JobExecutionException { |
| 116 | + JobExecution jobExecution = Mockito.mock(JobExecution.class); |
| 117 | + Job job = Mockito.mock(Job.class); |
| 118 | + ScheduledJobDetail scheduledJobDetail = Mockito.mock(ScheduledJobDetail.class); |
| 119 | + |
| 120 | + for (BatchStatus failedStatus : JobStarter.FAILED_STATUSES) { |
| 121 | + setupMocks(jobExecution, job, scheduledJobDetail); |
| 122 | + when(jobExecution.getStatus()).thenReturn(BatchStatus.FAILED); |
| 123 | + when(jobExecution.getExitStatus()).thenReturn(new ExitStatus(failedStatus.name(), "testException")); |
| 124 | + JobExecutionException exception = Assertions.assertThrows(JobExecutionException.class, |
| 125 | + () -> underTest.run(job, scheduledJobDetail, Set.of())); |
| 126 | + Assertions.assertEquals(String.format("exitCode=%s;exitDescription=%s", failedStatus.name(), "testException"), |
| 127 | + exception.getMessage()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private void setupMocks(JobExecution jobExecution, Job job, ScheduledJobDetail scheduledJobDetail) throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException { |
| 132 | + when(scheduledJobDetail.getId()).thenReturn(1L); |
| 133 | + when(scheduledJobDetail.getJobName()).thenReturn("testJobName"); |
| 134 | + when(jobParameterRepository.findJobParametersByJobId(1L)).thenReturn(List.of(new JobParameter().setJobId(1L).setParameterName("testParamKey").setParameterValue("testParamValue"))); |
| 135 | + when(jobLauncher.run(any(Job.class), any(JobParameters.class))).thenReturn(jobExecution); |
| 136 | + JobParametersIncrementer jobParametersIncrementer = Mockito.mock(JobParametersIncrementer.class); |
| 137 | + when(jobParametersIncrementer.getNext(any(JobParameters.class))).thenReturn(new JobParameters()); |
| 138 | + when(job.getJobParametersIncrementer()).thenReturn(jobParametersIncrementer); |
| 139 | + JobParameterProvider<?> jobParameterProvider = Mockito.mock(JobParameterProvider.class); |
| 140 | + when(jobParameterProvider.canProvideParametersForJob("testJobName")).thenReturn(true); |
| 141 | + when(jobParameterProviders.stream()).thenReturn(Stream.of(jobParameterProvider)); |
| 142 | + when(jobNameService.getJobByHumanReadableName(any(String.class))).thenReturn(new JobNameData("testEnumstyleName", "testHumanReadableName")); |
| 143 | + } |
| 144 | +} |
0 commit comments