11package com .github .valb3r .springbatch .adapters .dao .repository ;
22
3+ import com .github .valb3r .springbatch .adapters .neo4j .ogm .entity .Neo4jJobExecution ;
4+ import com .github .valb3r .springbatch .adapters .neo4j .ogm .entity .Neo4jStepExecution ;
5+ import com .github .valb3r .springbatch .adapters .neo4j .ogm .repository .Neo4jStepExecutionRepository ;
36import com .github .valb3r .springbatch .adapters .testconfig .common .DbDropper ;
47import com .github .valb3r .springbatch .adapters .testconfig .neo4j .Neo4jTestApplication ;
8+ import lombok .val ;
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 .JobParameters ;
15+ import org .springframework .batch .core .StepExecution ;
16+ import org .springframework .batch .core .repository .dao .JobExecutionDao ;
17+ import org .springframework .batch .core .repository .dao .JobInstanceDao ;
718import org .springframework .batch .core .repository .dao .StepExecutionDao ;
819import org .springframework .beans .factory .annotation .Autowired ;
920import org .springframework .boot .test .context .SpringBootTest ;
1021
22+ import java .util .Arrays ;
23+
24+ import static org .assertj .core .api .Assertions .assertThat ;
25+
1126@ SpringBootTest (classes = Neo4jTestApplication .class , webEnvironment = SpringBootTest .WebEnvironment .NONE )
1227class StepExecutionDaoTest {
1328
29+ private static final String JOB_NAME = "The job" ;
30+ private static final String STEP_NAME = "The step" ;
31+ private static final String STEP_NAME_OTHER = "The other step" ;
32+
33+ @ Autowired
34+ private Neo4jStepExecutionRepository stepExecutionRepository ;
35+
1436 @ Autowired
1537 private StepExecutionDao execDao ;
38+
39+ @ Autowired
40+ private JobInstanceDao instanceDao ;
41+
42+ @ Autowired
43+ private JobExecutionDao jobExecDao ;
1644
1745 @ Autowired
1846 private DbDropper dropper ;
@@ -24,25 +52,99 @@ void dropDatabase() {
2452
2553 @ Test
2654 void saveStepExecution () {
55+ val execution = execution ();
56+ execDao .saveStepExecution (new StepExecution (STEP_NAME , execution ));
57+
58+ val steps = stepExecutionRepository .findAll ();
59+ assertThat (steps ).hasSize (1 );
60+ val step = steps .iterator ().next ();
61+ assertThat (step ).extracting (Neo4jStepExecution ::getStepName ).isEqualTo (STEP_NAME );
62+ assertThat (step )
63+ .extracting (Neo4jStepExecution ::getJobExecution )
64+ .extracting (Neo4jJobExecution ::getId )
65+ .isEqualTo (execution .getId ());
2766 }
2867
2968 @ Test
3069 void saveStepExecutions () {
70+ val execution = execution ();
71+ execDao .saveStepExecutions (Arrays .asList (
72+ new StepExecution (STEP_NAME , execution ),
73+ new StepExecution (STEP_NAME_OTHER , execution )
74+ ));
75+
76+ val steps = stepExecutionRepository .findAll ();
77+ assertThat (steps ).hasSize (2 );
78+ assertThat (steps ).extracting (Neo4jStepExecution ::getStepName )
79+ .containsExactlyInAnyOrder (STEP_NAME , STEP_NAME_OTHER );
80+ assertThat (steps )
81+ .extracting (Neo4jStepExecution ::getJobExecution )
82+ .extracting (Neo4jJobExecution ::getId )
83+ .containsOnly (execution .getId ());
3184 }
3285
3386 @ Test
3487 void updateStepExecution () {
88+ val execution = execution ();
89+ val stepExec = new StepExecution (STEP_NAME , execution );
90+ execDao .saveStepExecution (stepExec );
91+
92+ stepExec .setCommitCount (99 );
93+ stepExec .setStatus (BatchStatus .ABANDONED );
94+ stepExec .setExitStatus (ExitStatus .FAILED );
95+ execDao .updateStepExecution (stepExec );
96+
97+ val steps = stepExecutionRepository .findAll ();
98+ assertThat (steps ).hasSize (1 );
99+ val step = steps .iterator ().next ();
100+ assertThat (step ).extracting (Neo4jStepExecution ::getStepName ).isEqualTo (STEP_NAME );
101+ assertThat (step )
102+ .extracting (Neo4jStepExecution ::getJobExecution )
103+ .extracting (Neo4jJobExecution ::getId )
104+ .isEqualTo (execution .getId ());
105+ assertThat (step ).isEqualToIgnoringGivenFields (stepExec , "jobExecution" , "executionContext" );
35106 }
36107
37108 @ Test
38109 void getStepExecution () {
110+ val execution = execution ();
111+ val stepExec = new StepExecution (STEP_NAME , execution );
112+ execDao .saveStepExecution (stepExec );
113+
114+ assertThat (execDao .getStepExecution (execution , stepExec .getId ()))
115+ .isEqualToIgnoringGivenFields (stepExec , "jobExecution" , "executionContext" );
39116 }
40117
41118 @ Test
42119 void getLastStepExecution () {
120+ val execution = execution ();
121+ val stepExecOne = new StepExecution (STEP_NAME , execution );
122+ execDao .saveStepExecution (stepExecOne );
123+ val stepExecTwo = new StepExecution (STEP_NAME , execution );
124+ execDao .saveStepExecution (stepExecTwo );
125+
126+ assertThat (execDao .getLastStepExecution (execution .getJobInstance (), STEP_NAME ))
127+ .isEqualToIgnoringGivenFields (stepExecTwo , "jobExecution" , "executionContext" );
43128 }
44129
45130 @ Test
46131 void addStepExecutions () {
132+ val execution = execution ();
133+ val stepExec = new StepExecution (STEP_NAME , execution );
134+ execDao .saveStepExecution (stepExec );
135+ assertThat (execution .getStepExecutions ()).isEmpty ();
136+
137+ execDao .addStepExecutions (execution );
138+
139+ assertThat (execution .getStepExecutions ()).hasSize (1 );
140+ val step = execution .getStepExecutions ().iterator ().next ();
141+ assertThat (step ).isEqualToIgnoringGivenFields (stepExec , "startTime" , "jobExecution" , "executionContext" );
142+ }
143+
144+ private JobExecution execution () {
145+ val instance = instanceDao .createJobInstance (JOB_NAME , new JobParameters ());
146+ val execution = new JobExecution (instance , new JobParameters ());
147+ jobExecDao .saveJobExecution (execution );
148+ return execution ;
47149 }
48150}
0 commit comments