Skip to content

Commit 8ca6193

Browse files
committed
SNA-7. Added StepExecutionDao tests
1 parent 2af2855 commit 8ca6193

File tree

4 files changed

+138
-4
lines changed

4 files changed

+138
-4
lines changed

neo4j-adapter/src/main/java/com/github/valb3r/springbatch/adapters/neo4j/dao/Neo4jStepExecutionDao.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.valb3r.springbatch.adapters.neo4j.dao;
22

3+
import com.github.valb3r.springbatch.adapters.neo4j.ogm.entity.Neo4jStepExecution;
34
import com.github.valb3r.springbatch.adapters.neo4j.ogm.repository.Neo4jStepExecutionRepository;
45
import lombok.RequiredArgsConstructor;
56
import lombok.val;
6-
import com.github.valb3r.springbatch.adapters.neo4j.ogm.entity.Neo4jStepExecution;
77
import org.springframework.batch.core.JobExecution;
88
import org.springframework.batch.core.JobInstance;
99
import org.springframework.batch.core.StepExecution;
@@ -46,7 +46,7 @@ public void updateStepExecution(StepExecution stepExecution) {
4646
@Transactional
4747
public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecutionId) {
4848
return stepExecs.findBy(jobExecution.getId(), stepExecutionId)
49-
.map(Neo4jStepExecution.MAP::map)
49+
.map(it -> Neo4jStepExecution.MAP.map(it, jobExecution))
5050
.orElse(null);
5151
}
5252

@@ -65,6 +65,11 @@ public StepExecution getLastStepExecution(JobInstance jobInstance, String stepNa
6565
@Override
6666
@Transactional
6767
public void addStepExecutions(JobExecution jobExecution) {
68-
stepExecs.findStepExecutions(jobExecution.getId()).forEach(Neo4jStepExecution.MAP::map);
68+
stepExecs.findStepExecutions(jobExecution.getId())
69+
.forEach(it -> new StepExecution(
70+
it.getStepName(),
71+
jobExecution,
72+
it.getId())
73+
);
6974
}
7075
}

neo4j-adapter/src/main/java/com/github/valb3r/springbatch/adapters/neo4j/ogm/entity/Neo4jStepExecution.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.github.valb3r.springbatch.adapters.neo4j.dao.converters.ExitStatusConverter;
1919
import org.springframework.batch.core.BatchStatus;
2020
import org.springframework.batch.core.ExitStatus;
21+
import org.springframework.batch.core.JobExecution;
2122
import org.springframework.batch.core.StepExecution;
2223
import org.springframework.batch.item.ExecutionContext;
2324

@@ -102,6 +103,27 @@ default StepExecution map(Neo4jStepExecution source) {
102103
);
103104
}
104105

106+
default StepExecution map(Neo4jStepExecution source, JobExecution execution) {
107+
if (null != source.getId()) {
108+
return map(
109+
source,
110+
new StepExecution(
111+
source.getStepName(),
112+
execution,
113+
source.getId()
114+
)
115+
);
116+
}
117+
118+
return map(
119+
source,
120+
new StepExecution(
121+
source.getStepName(),
122+
execution
123+
)
124+
);
125+
}
126+
105127
default Map<String, Object> map(ExecutionContext value) {
106128
Map<String, Object> result = new HashMap<>();
107129
for (Map.Entry<String, Object> me : value.entrySet()) {

neo4j-adapter/src/test/java/com/github/valb3r/springbatch/adapters/dao/repository/JobExecutionDaoTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
@SpringBootTest(classes = Neo4jTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
3232
class JobExecutionDaoTest {
3333

34-
private static final String JOB_NAME = "JOB_NAME";
34+
private static final String JOB_NAME = "The job";
3535
private static final String PARAM = "LONG_PARAM_VALUE";
3636
private static final long LONG_PARAM_VAL = 1L;
3737

@@ -58,6 +58,7 @@ void dropDatabase() {
5858
/**
5959
* Currently we can't assign id to neo4j entity, so there is impedance between spring-batch and neo4j.
6060
* Problems are not observed at this moment.
61+
* TODO: https://github.com/valb3r/springbatch-neo4j-adapter/issues/13
6162
*/
6263
@Test
6364
void saveJobExecution() {
@@ -98,6 +99,7 @@ void updateJobExecution() {
9899

99100
/**
100101
* Currently, it is required to have step executions in order to find the job execution here.
102+
* TODO: https://github.com/valb3r/springbatch-neo4j-adapter/issues/14
101103
*/
102104
@Test
103105
void findJobExecutions() {
@@ -115,6 +117,7 @@ void findJobExecutions() {
115117
assertThat(execDao.findJobExecutions(instance)).hasSize(2);
116118
}
117119

120+
// TODO: https://github.com/valb3r/springbatch-neo4j-adapter/issues/14
118121
@Test
119122
void getLastJobExecution() {
120123
JobParameters parameters = new JobParametersBuilder().addLong(PARAM, LONG_PARAM_VAL).toJobParameters();
@@ -131,6 +134,7 @@ void getLastJobExecution() {
131134
assertThat(execDao.getLastJobExecution(instance)).isEqualTo(execToSaveTwo);
132135
}
133136

137+
// TODO: https://github.com/valb3r/springbatch-neo4j-adapter/issues/14
134138
@Test
135139
void findRunningJobExecutions() {
136140
JobParameters parameters = new JobParametersBuilder().addLong(PARAM, LONG_PARAM_VAL).toJobParameters();
@@ -163,6 +167,7 @@ void getJobExecution() {
163167

164168
/**
165169
* This one is not exactly complete as it does not use version field.
170+
* TODO: https://github.com/valb3r/springbatch-neo4j-adapter/issues/14
166171
*/
167172
@Test
168173
void synchronizeStatus() {

neo4j-adapter/src/test/java/com/github/valb3r/springbatch/adapters/dao/repository/StepExecutionDaoTest.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
11
package 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;
36
import com.github.valb3r.springbatch.adapters.testconfig.common.DbDropper;
47
import com.github.valb3r.springbatch.adapters.testconfig.neo4j.Neo4jTestApplication;
8+
import lombok.val;
59
import org.junit.jupiter.api.AfterEach;
610
import 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;
718
import org.springframework.batch.core.repository.dao.StepExecutionDao;
819
import org.springframework.beans.factory.annotation.Autowired;
920
import 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)
1227
class 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

Comments
 (0)