-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SimpleJobOperatorTests.java
449 lines (379 loc) · 15.6 KB
/
SimpleJobOperatorTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* Copyright 2006-2022 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.launch.support;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersIncrementer;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.support.MapJobRegistry;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.launch.JobInstanceAlreadyExistsException;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.launch.NoSuchJobExecutionException;
import org.springframework.batch.core.launch.NoSuchJobInstanceException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.StoppableTasklet;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.support.PropertiesConverter;
import org.springframework.lang.Nullable;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Dave Syer
* @author Will Schipp
* @author Mahmoud Ben Hassine
*
*/
class SimpleJobOperatorTests {
private SimpleJobOperator jobOperator;
protected Job job;
private JobExplorer jobExplorer;
private JobRepository jobRepository;
private JobParameters jobParameters;
@BeforeEach
void setUp() throws Exception {
job = new JobSupport("foo") {
@Nullable
@Override
public JobParametersIncrementer getJobParametersIncrementer() {
return parameters -> jobParameters;
}
};
jobOperator = new SimpleJobOperator();
jobOperator.setJobRegistry(new MapJobRegistry() {
@Override
public Job getJob(@Nullable String name) throws NoSuchJobException {
if (name.equals("foo")) {
return job;
}
throw new NoSuchJobException("foo");
}
@Override
public Set<String> getJobNames() {
return new HashSet<>(Arrays.asList(new String[] { "foo", "bar" }));
}
});
jobOperator.setJobLauncher(
(job, jobParameters) -> new JobExecution(new JobInstance(123L, job.getName()), 999L, jobParameters));
jobExplorer = mock(JobExplorer.class);
jobOperator.setJobExplorer(jobExplorer);
jobRepository = mock(JobRepository.class);
jobOperator.setJobRepository(jobRepository);
jobOperator.setJobParametersConverter(new DefaultJobParametersConverter() {
@Override
public JobParameters getJobParameters(@Nullable Properties properties) {
assertTrue(properties.containsKey("a"), "Wrong properties");
return jobParameters;
}
@Override
public Properties getProperties(@Nullable JobParameters params) {
return PropertiesConverter.stringToProperties("a=b");
}
});
jobOperator.afterPropertiesSet();
}
@Test
void testMandatoryProperties() {
jobOperator = new SimpleJobOperator();
assertThrows(IllegalStateException.class, jobOperator::afterPropertiesSet);
}
/**
* Test method for
* {@link org.springframework.batch.core.launch.support.SimpleJobOperator#startNextInstance(java.lang.String)}
* .
*/
@Test
void testStartNextInstanceSunnyDay() throws Exception {
jobParameters = new JobParameters();
JobInstance jobInstance = new JobInstance(321L, "foo");
when(jobExplorer.getJobInstances("foo", 0, 1)).thenReturn(Collections.singletonList(jobInstance));
when(jobExplorer.getJobExecutions(jobInstance))
.thenReturn(Collections.singletonList(new JobExecution(jobInstance, new JobParameters())));
Long value = jobOperator.startNextInstance("foo");
assertEquals(999, value.longValue());
}
@Test
void testStartNewInstanceSunnyDay() throws Exception {
jobParameters = new JobParameters();
jobRepository.isJobInstanceExists("foo", jobParameters);
Long value = jobOperator.start("foo", "a=b");
assertEquals(999, value.longValue());
}
@Test
void testStartNewInstanceAlreadyExists() {
jobParameters = new JobParameters();
when(jobRepository.isJobInstanceExists("foo", jobParameters)).thenReturn(true);
jobRepository.isJobInstanceExists("foo", jobParameters);
assertThrows(JobInstanceAlreadyExistsException.class, () -> jobOperator.start("foo", "a=b"));
}
@Test
void testResumeSunnyDay() throws Exception {
jobParameters = new JobParameters();
when(jobExplorer.getJobExecution(111L))
.thenReturn(new JobExecution(new JobInstance(123L, job.getName()), 111L, jobParameters));
jobExplorer.getJobExecution(111L);
Long value = jobOperator.restart(111L);
assertEquals(999, value.longValue());
}
@Test
void testGetSummarySunnyDay() throws Exception {
jobParameters = new JobParameters();
JobExecution jobExecution = new JobExecution(new JobInstance(123L, job.getName()), 111L, jobParameters);
when(jobExplorer.getJobExecution(111L)).thenReturn(jobExecution);
jobExplorer.getJobExecution(111L);
String value = jobOperator.getSummary(111L);
assertEquals(jobExecution.toString(), value);
}
@Test
void testGetSummaryNoSuchExecution() {
jobParameters = new JobParameters();
jobExplorer.getJobExecution(111L);
assertThrows(NoSuchJobExecutionException.class, () -> jobOperator.getSummary(111L));
}
@Test
void testGetStepExecutionSummariesSunnyDay() throws Exception {
jobParameters = new JobParameters();
JobExecution jobExecution = new JobExecution(new JobInstance(123L, job.getName()), 111L, jobParameters);
jobExecution.createStepExecution("step1");
jobExecution.createStepExecution("step2");
jobExecution.getStepExecutions().iterator().next().setId(21L);
when(jobExplorer.getJobExecution(111L)).thenReturn(jobExecution);
Map<Long, String> value = jobOperator.getStepExecutionSummaries(111L);
assertEquals(2, value.size());
}
@Test
void testGetStepExecutionSummariesNoSuchExecution() {
jobParameters = new JobParameters();
jobExplorer.getJobExecution(111L);
assertThrows(NoSuchJobExecutionException.class, () -> jobOperator.getStepExecutionSummaries(111L));
}
@Test
void testFindRunningExecutionsSunnyDay() throws Exception {
jobParameters = new JobParameters();
JobExecution jobExecution = new JobExecution(new JobInstance(123L, job.getName()), 111L, jobParameters);
when(jobExplorer.findRunningJobExecutions("foo")).thenReturn(Collections.singleton(jobExecution));
Set<Long> value = jobOperator.getRunningExecutions("foo");
assertEquals(111L, value.iterator().next().longValue());
}
@Test
@SuppressWarnings("unchecked")
void testFindRunningExecutionsNoSuchJob() {
jobParameters = new JobParameters();
when(jobExplorer.findRunningJobExecutions("no-such-job")).thenReturn(Collections.EMPTY_SET);
assertThrows(NoSuchJobException.class, () -> jobOperator.getRunningExecutions("no-such-job"));
}
@Test
void testGetJobParametersSunnyDay() throws Exception {
final JobParameters jobParameters = new JobParameters();
when(jobExplorer.getJobExecution(111L))
.thenReturn(new JobExecution(new JobInstance(123L, job.getName()), 111L, jobParameters));
String value = jobOperator.getParameters(111L);
assertEquals("a=b", value);
}
@Test
void testGetJobParametersNoSuchExecution() {
jobExplorer.getJobExecution(111L);
assertThrows(NoSuchJobExecutionException.class, () -> jobOperator.getParameters(111L));
}
@Test
void testGetLastInstancesSunnyDay() throws Exception {
jobParameters = new JobParameters();
JobInstance jobInstance = new JobInstance(123L, job.getName());
when(jobExplorer.getJobInstances("foo", 0, 2)).thenReturn(Collections.singletonList(jobInstance));
jobExplorer.getJobInstances("foo", 0, 2);
List<Long> value = jobOperator.getJobInstances("foo", 0, 2);
assertEquals(123L, value.get(0).longValue());
}
@Test
void testGetLastInstancesNoSuchJob() {
jobParameters = new JobParameters();
jobExplorer.getJobInstances("no-such-job", 0, 2);
assertThrows(NoSuchJobException.class, () -> jobOperator.getJobInstances("no-such-job", 0, 2));
}
@Test
public void testGetJobInstanceWithNameAndParameters() {
// given
String jobName = "job";
JobParameters jobParameters = new JobParameters();
JobInstance jobInstance = mock(JobInstance.class);
// when
when(this.jobExplorer.getJobInstance(jobName, jobParameters)).thenReturn(jobInstance);
JobInstance actualJobInstance = this.jobOperator.getJobInstance(jobName, jobParameters);
// then
verify(this.jobExplorer).getJobInstance(jobName, jobParameters);
assertEquals(jobInstance, actualJobInstance);
}
@Test
void testGetJobNames() {
Set<String> names = jobOperator.getJobNames();
assertEquals(2, names.size());
assertTrue(names.contains("foo"), "Wrong names: " + names);
}
@Test
void testGetExecutionsSunnyDay() throws Exception {
JobInstance jobInstance = new JobInstance(123L, job.getName());
when(jobExplorer.getJobInstance(123L)).thenReturn(jobInstance);
JobExecution jobExecution = new JobExecution(jobInstance, 111L, jobParameters);
when(jobExplorer.getJobExecutions(jobInstance)).thenReturn(Collections.singletonList(jobExecution));
List<Long> value = jobOperator.getExecutions(123L);
assertEquals(111L, value.iterator().next().longValue());
}
@Test
void testGetExecutionsNoSuchInstance() {
jobExplorer.getJobInstance(123L);
assertThrows(NoSuchJobInstanceException.class, () -> jobOperator.getExecutions(123L));
}
@Test
void testStop() throws Exception {
JobInstance jobInstance = new JobInstance(123L, job.getName());
JobExecution jobExecution = new JobExecution(jobInstance, 111L, jobParameters);
when(jobExplorer.getJobExecution(111L)).thenReturn(jobExecution);
jobExplorer.getJobExecution(111L);
jobRepository.update(jobExecution);
jobOperator.stop(111L);
assertEquals(BatchStatus.STOPPING, jobExecution.getStatus());
}
@Test
void testStopTasklet() throws Exception {
JobInstance jobInstance = new JobInstance(123L, job.getName());
JobExecution jobExecution = new JobExecution(jobInstance, 111L, jobParameters);
StoppableTasklet tasklet = mock(StoppableTasklet.class);
TaskletStep taskletStep = new TaskletStep();
taskletStep.setTasklet(tasklet);
MockJob job = new MockJob();
job.taskletStep = taskletStep;
JobRegistry jobRegistry = mock(JobRegistry.class);
TaskletStep step = mock(TaskletStep.class);
when(step.getTasklet()).thenReturn(tasklet);
when(step.getName()).thenReturn("test_job.step1");
when(jobRegistry.getJob(any(String.class))).thenReturn(job);
when(jobExplorer.getJobExecution(111L)).thenReturn(jobExecution);
jobOperator.setJobRegistry(jobRegistry);
jobExplorer.getJobExecution(111L);
jobRepository.update(jobExecution);
jobOperator.stop(111L);
assertEquals(BatchStatus.STOPPING, jobExecution.getStatus());
}
@Test
void testStopTaskletWhenJobNotRegistered() throws Exception {
JobInstance jobInstance = new JobInstance(123L, job.getName());
JobExecution jobExecution = new JobExecution(jobInstance, 111L, jobParameters);
StoppableTasklet tasklet = mock(StoppableTasklet.class);
JobRegistry jobRegistry = mock(JobRegistry.class);
TaskletStep step = mock(TaskletStep.class);
when(step.getTasklet()).thenReturn(tasklet);
when(jobRegistry.getJob(job.getName())).thenThrow(new NoSuchJobException("Unable to find job"));
when(jobExplorer.getJobExecution(111L)).thenReturn(jobExecution);
jobOperator.setJobRegistry(jobRegistry);
jobOperator.stop(111L);
assertEquals(BatchStatus.STOPPING, jobExecution.getStatus());
verify(tasklet, never()).stop();
}
@Test
void testStopTaskletException() throws Exception {
JobInstance jobInstance = new JobInstance(123L, job.getName());
JobExecution jobExecution = new JobExecution(jobInstance, 111L, jobParameters);
StoppableTasklet tasklet = new StoppableTasklet() {
@Nullable
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
return null;
}
@Override
public void stop() {
throw new IllegalStateException();
}
};
TaskletStep taskletStep = new TaskletStep();
taskletStep.setTasklet(tasklet);
MockJob job = new MockJob();
job.taskletStep = taskletStep;
JobRegistry jobRegistry = mock(JobRegistry.class);
TaskletStep step = mock(TaskletStep.class);
when(step.getTasklet()).thenReturn(tasklet);
when(step.getName()).thenReturn("test_job.step1");
when(jobRegistry.getJob(any(String.class))).thenReturn(job);
when(jobExplorer.getJobExecution(111L)).thenReturn(jobExecution);
jobOperator.setJobRegistry(jobRegistry);
jobExplorer.getJobExecution(111L);
jobRepository.update(jobExecution);
jobOperator.stop(111L);
assertEquals(BatchStatus.STOPPING, jobExecution.getStatus());
}
@Test
void testAbort() throws Exception {
JobInstance jobInstance = new JobInstance(123L, job.getName());
JobExecution jobExecution = new JobExecution(jobInstance, 111L, jobParameters);
jobExecution.setStatus(BatchStatus.STOPPING);
when(jobExplorer.getJobExecution(123L)).thenReturn(jobExecution);
jobRepository.update(jobExecution);
jobOperator.abandon(123L);
assertEquals(BatchStatus.ABANDONED, jobExecution.getStatus());
assertNotNull(jobExecution.getEndTime());
}
@Test
void testAbortNonStopping() {
JobInstance jobInstance = new JobInstance(123L, job.getName());
JobExecution jobExecution = new JobExecution(jobInstance, 111L, jobParameters);
jobExecution.setStatus(BatchStatus.STARTED);
when(jobExplorer.getJobExecution(123L)).thenReturn(jobExecution);
jobRepository.update(jobExecution);
assertThrows(JobExecutionAlreadyRunningException.class, () -> jobOperator.abandon(123L));
}
static class MockJob extends AbstractJob {
private TaskletStep taskletStep;
@Override
public Step getStep(String stepName) {
return taskletStep;
}
@Override
public Collection<String> getStepNames() {
return Collections.singletonList("test_job.step1");
}
@Override
protected void doExecute(JobExecution execution) throws JobExecutionException {
}
}
}