11package com .github .valb3r .springbatch .adapters .dao .repository ;
22
3+ import com .github .valb3r .springbatch .adapters .neo4j .ogm .entity .Neo4jJobInstance ;
4+ import com .github .valb3r .springbatch .adapters .neo4j .ogm .repository .Neo4jJobExecutionRepository ;
35import com .github .valb3r .springbatch .adapters .testconfig .common .DbDropper ;
46import com .github .valb3r .springbatch .adapters .testconfig .neo4j .Neo4jTestApplication ;
7+ import lombok .val ;
8+ import lombok .var ;
59import org .junit .jupiter .api .AfterEach ;
610import org .junit .jupiter .api .Test ;
11+ import org .springframework .batch .core .BatchStatus ;
12+ import org .springframework .batch .core .ExitStatus ;
13+ import org .springframework .batch .core .JobExecution ;
14+ import org .springframework .batch .core .JobInstance ;
15+ import org .springframework .batch .core .JobParameters ;
16+ import org .springframework .batch .core .JobParametersBuilder ;
17+ import org .springframework .batch .core .StepExecution ;
718import org .springframework .batch .core .repository .dao .JobExecutionDao ;
19+ import org .springframework .batch .core .repository .dao .JobInstanceDao ;
20+ import org .springframework .batch .core .repository .dao .StepExecutionDao ;
21+ import org .springframework .batch .item .ExecutionContext ;
822import org .springframework .beans .factory .annotation .Autowired ;
923import org .springframework .boot .test .context .SpringBootTest ;
1024
25+ import java .util .Date ;
26+ import java .util .HashMap ;
27+ import java .util .Map ;
28+
29+ import static org .assertj .core .api .Assertions .assertThat ;
30+
1131@ SpringBootTest (classes = Neo4jTestApplication .class , webEnvironment = SpringBootTest .WebEnvironment .NONE )
1232class JobExecutionDaoTest {
1333
34+ private static final String JOB_NAME = "JOB_NAME" ;
35+ private static final String PARAM = "LONG_PARAM_VALUE" ;
36+ private static final long LONG_PARAM_VAL = 1L ;
37+
1438 @ Autowired
1539 private JobExecutionDao execDao ;
1640
41+ @ Autowired
42+ private StepExecutionDao stepExecDao ;
43+
44+ @ Autowired
45+ private Neo4jJobExecutionRepository neo4jExecs ;
46+
47+ @ Autowired
48+ private JobInstanceDao instanceDao ;
49+
1750 @ Autowired
1851 private DbDropper dropper ;
1952
@@ -22,35 +55,149 @@ void dropDatabase() {
2255 dropper .dropDatabase ();
2356 }
2457
58+ /**
59+ * Currently we can't assign id to neo4j entity, so there is impedance between spring-batch and neo4j.
60+ * Problems are not observed at this moment.
61+ */
2562 @ Test
2663 void saveJobExecution () {
27-
64+ var newExec = execution ();
65+ execDao .saveJobExecution (newExec );
66+
67+ var execs = neo4jExecs .findAll ();
68+ assertThat (execs ).hasSize (1 );
69+ var exec = execs .iterator ().next ();
70+ assertThat (exec .getJobInstance ()).extracting (Neo4jJobInstance ::getJobName ).isEqualTo (JOB_NAME );
71+ assertThat (exec .getExitStatus ()).isEqualTo (ExitStatus .UNKNOWN );
72+ assertThat (exec .getStatus ()).isEqualTo (BatchStatus .STARTING );
73+ assertThat (exec .getJobParameters ()).isEqualToComparingFieldByField (newExec .getJobParameters ());
2874 }
2975
3076 @ Test
3177 void updateJobExecution () {
78+ var newExec = execution ();
79+ execDao .saveJobExecution (newExec );
80+
81+ ExecutionContext newCtx = new ExecutionContext (params ());
82+ newExec .setExitStatus (ExitStatus .COMPLETED );
83+ newExec .setStatus (BatchStatus .ABANDONED );
84+ newExec .setExecutionContext (newCtx );
85+
86+ execDao .updateJobExecution (newExec );
87+
88+ var execs = neo4jExecs .findAll ();
89+ assertThat (execs ).hasSize (1 );
90+ var exec = execs .iterator ().next ();
91+ assertThat (exec .getJobInstance ()).extracting (Neo4jJobInstance ::getJobName ).isEqualTo (JOB_NAME );
92+ assertThat (exec .getExitStatus ()).isEqualTo (ExitStatus .COMPLETED );
93+ assertThat (exec .getStatus ()).isEqualTo (BatchStatus .ABANDONED );
94+ assertThat (exec .getExecutionContext ()).isEqualTo (params ());
95+ assertThat (exec .getJobParameters ()).isEqualToComparingFieldByField (newExec .getJobParameters ());
3296
3397 }
3498
99+ /**
100+ * Currently, it is required to have step executions in order to find the job execution here.
101+ */
35102 @ Test
36103 void findJobExecutions () {
104+ JobParameters parameters = new JobParametersBuilder ().addLong (PARAM , LONG_PARAM_VAL ).toJobParameters ();
105+ val instance = instanceDao .createJobInstance (JOB_NAME , parameters );
106+
107+ var execToSave = execution (instance );
108+ execDao .saveJobExecution (execToSave );
109+ addStepExecutions (execToSave );
110+
111+ execToSave = execution (instance );
112+ execDao .saveJobExecution (execToSave );
113+ addStepExecutions (execToSave );
114+
115+ assertThat (execDao .findJobExecutions (instance )).hasSize (2 );
37116 }
38117
39118 @ Test
40119 void getLastJobExecution () {
120+ JobParameters parameters = new JobParametersBuilder ().addLong (PARAM , LONG_PARAM_VAL ).toJobParameters ();
121+ val instance = instanceDao .createJobInstance (JOB_NAME , parameters );
122+
123+ var execToSaveOne = execution (instance );
124+ execDao .saveJobExecution (execToSaveOne );
125+ addStepExecutions (execToSaveOne );
126+
127+ var execToSaveTwo = execution (instance );
128+ execDao .saveJobExecution (execToSaveTwo );
129+ addStepExecutions (execToSaveTwo );
130+
131+ assertThat (execDao .getLastJobExecution (instance )).isEqualTo (execToSaveTwo );
41132 }
42133
43134 @ Test
44135 void findRunningJobExecutions () {
136+ JobParameters parameters = new JobParametersBuilder ().addLong (PARAM , LONG_PARAM_VAL ).toJobParameters ();
137+ val instance = instanceDao .createJobInstance (JOB_NAME , parameters );
138+
139+ var execToSaveOne = execution (instance );
140+ execToSaveOne .setStartTime (new Date ());
141+ execDao .saveJobExecution (execToSaveOne );
142+ addStepExecutions (execToSaveOne );
143+
144+ var execToSaveTwo = execution (instance );
145+ execToSaveTwo .setStartTime (new Date ());
146+ execDao .saveJobExecution (execToSaveTwo );
147+ addStepExecutions (execToSaveTwo );
148+
149+ assertThat (execDao .findRunningJobExecutions (JOB_NAME )).containsExactlyInAnyOrder (execToSaveOne , execToSaveTwo );
45150 }
46151
47152 @ Test
48153 void getJobExecution () {
154+ JobParameters parameters = new JobParametersBuilder ().addLong (PARAM , LONG_PARAM_VAL ).toJobParameters ();
155+ val instance = instanceDao .createJobInstance (JOB_NAME , parameters );
156+
157+ var execToSave = execution (instance );
158+ execToSave .setStartTime (new Date ());
159+ execDao .saveJobExecution (execToSave );
49160
161+ assertThat (execDao .getJobExecution (execToSave .getId ())).isEqualTo (execToSave );
50162 }
51163
164+ /**
165+ * This one is not exactly complete as it does not use version field.
166+ */
52167 @ Test
53168 void synchronizeStatus () {
169+ JobParameters parameters = new JobParametersBuilder ().addLong (PARAM , LONG_PARAM_VAL ).toJobParameters ();
170+ val instance = instanceDao .createJobInstance (JOB_NAME , parameters );
171+
172+ var execToSave = execution (instance );
173+ execToSave .setStartTime (new Date ());
174+ execDao .saveJobExecution (execToSave );
175+
176+ execToSave .setStatus (BatchStatus .ABANDONED );
177+ execDao .synchronizeStatus (execToSave );
178+
179+ assertThat (execDao .getJobExecution (execToSave .getId ())).extracting (JobExecution ::getStatus )
180+ .isEqualTo (BatchStatus .ABANDONED );
181+ }
182+
183+ private JobExecution execution (JobInstance instance ) {
184+ JobParameters parameters = new JobParametersBuilder ().addLong (PARAM , LONG_PARAM_VAL ).toJobParameters ();
185+ return new JobExecution (instance , parameters );
186+ }
187+
188+ private JobExecution execution () {
189+ JobParameters parameters = new JobParametersBuilder ().addLong (PARAM , LONG_PARAM_VAL ).toJobParameters ();
190+ return execution (instanceDao .createJobInstance (JOB_NAME , parameters ));
191+ }
192+
193+ private Map <String , Object > params () {
194+ val params = new HashMap <String , Object >();
195+ params .put ("test" , "value" );
196+ return params ;
197+ }
54198
199+ // this one should be removed when we are able to read job execution without step execution
200+ private void addStepExecutions (JobExecution execution ) {
201+ stepExecDao .saveStepExecution (new StepExecution ("step" , execution ));
55202 }
56203}
0 commit comments