1
+ package de .otto .edison .jobs .service ;
2
+
3
+ import org .junit .jupiter .api .BeforeEach ;
4
+ import org .junit .jupiter .api .Test ;
5
+ import org .mockito .ArgumentCaptor ;
6
+ import org .mockito .Mock ;
7
+ import org .springframework .scheduling .TaskScheduler ;
8
+ import org .springframework .scheduling .Trigger ;
9
+ import org .springframework .scheduling .support .CronTrigger ;
10
+ import org .springframework .scheduling .support .PeriodicTrigger ;
11
+
12
+ import java .time .Duration ;
13
+ import java .util .List ;
14
+ import java .util .Optional ;
15
+
16
+ import static de .otto .edison .jobs .definition .DefaultJobDefinition .*;
17
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
18
+ import static org .mockito .Mockito .*;
19
+ import static org .mockito .MockitoAnnotations .openMocks ;
20
+
21
+ class LocalJobSchedulerTest {
22
+
23
+ @ Mock
24
+ private JobRunnable fixedDelayJobRunnable ;
25
+ @ Mock
26
+ private JobRunnable manualJobRunnable ;
27
+ @ Mock
28
+ private JobRunnable cronJobRunnable ;
29
+
30
+ @ Mock
31
+ private JobService jobService ;
32
+ @ Mock
33
+ private TaskScheduler taskScheduler ;
34
+
35
+ @ BeforeEach
36
+ public void setUp () {
37
+ openMocks (this );
38
+ when (fixedDelayJobRunnable .getJobDefinition ()).thenReturn (
39
+ fixedDelayJobDefinition ("FIXED" , "" , "" , Duration .ofSeconds (2 ), 0 , Optional .empty ())
40
+ );
41
+ when (manualJobRunnable .getJobDefinition ()).thenReturn (
42
+ manuallyTriggerableJobDefinition ("MANUAL" , "" , "" , 0 , Optional .empty ())
43
+ );
44
+ when (cronJobRunnable .getJobDefinition ()).thenReturn (
45
+ cronJobDefinition ("CRON" , "" , "" , "0 0 * * * *" , 0 , Optional .empty ())
46
+ );
47
+ }
48
+
49
+ @ Test
50
+ public void shouldScheduleRunnable () {
51
+ // given
52
+ LocalJobScheduler localJobScheduler = new LocalJobScheduler (List .of (fixedDelayJobRunnable , cronJobRunnable ), jobService , taskScheduler );
53
+
54
+ // when
55
+ localJobScheduler .schedule ();
56
+
57
+ // then
58
+ ArgumentCaptor <Trigger > triggerCaptor = ArgumentCaptor .forClass (Trigger .class );
59
+ verify (taskScheduler , times (2 )).schedule (any (), triggerCaptor .capture ());
60
+
61
+ assertTrue (triggerCaptor .getAllValues ().stream ().anyMatch (trigger -> trigger instanceof CronTrigger ));
62
+ assertTrue (triggerCaptor .getAllValues ().stream ().anyMatch (trigger -> trigger instanceof PeriodicTrigger ));
63
+ }
64
+
65
+ @ Test
66
+ public void shouldStartJobFromRunnable () {
67
+ // given
68
+ doAnswer (invocation -> {
69
+ Runnable runnable = invocation .getArgument (0 );
70
+ runnable .run ();
71
+ return null ;
72
+ }).when (taskScheduler ).schedule (any (), (Trigger ) any ());
73
+ LocalJobScheduler localJobScheduler = new LocalJobScheduler (List .of (fixedDelayJobRunnable , cronJobRunnable ), jobService , taskScheduler );
74
+
75
+ // when
76
+ localJobScheduler .schedule ();
77
+
78
+ // then
79
+ ArgumentCaptor <String > stringCaptor = ArgumentCaptor .forClass (String .class );
80
+ verify (jobService , times (2 )).startAsyncJob (stringCaptor .capture ());
81
+ assertTrue (stringCaptor .getAllValues ().contains ("FIXED" ));
82
+ assertTrue (stringCaptor .getAllValues ().contains ("CRON" ));
83
+ }
84
+
85
+ @ Test
86
+ public void shouldFilterManuallyTriggeredJobs () {
87
+ // given
88
+ LocalJobScheduler localJobScheduler = new LocalJobScheduler (List .of (fixedDelayJobRunnable , cronJobRunnable , manualJobRunnable ), jobService , taskScheduler );
89
+
90
+ // when
91
+ localJobScheduler .schedule ();
92
+
93
+ // then
94
+ verify (taskScheduler , times (2 )).schedule (any (), (Trigger ) any ());
95
+ }
96
+ }
0 commit comments