|
| 1 | +package com.github.valb3r.springbatch.adapters.dao.repository; |
| 2 | + |
| 3 | +import com.github.valb3r.springbatch.adapters.testconfig.common.DbDropper; |
| 4 | +import com.github.valb3r.springbatch.adapters.testconfig.neo4j.Neo4jTestApplication; |
| 5 | +import lombok.val; |
| 6 | +import lombok.var; |
| 7 | +import org.junit.jupiter.api.AfterEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.batch.core.JobExecution; |
| 10 | +import org.springframework.batch.core.JobParameters; |
| 11 | +import org.springframework.batch.core.StepExecution; |
| 12 | +import org.springframework.batch.core.repository.dao.ExecutionContextDao; |
| 13 | +import org.springframework.batch.core.repository.dao.JobExecutionDao; |
| 14 | +import org.springframework.batch.core.repository.dao.JobInstanceDao; |
| 15 | +import org.springframework.batch.core.repository.dao.StepExecutionDao; |
| 16 | +import org.springframework.batch.item.ExecutionContext; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | +@SpringBootTest(classes = Neo4jTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE) |
| 27 | +public class ExecutionContextDaoTest { |
| 28 | + |
| 29 | + private static final String STEP_NAME = "The step"; |
| 30 | + private static final String STEP_NAME_OTHER = "The other step"; |
| 31 | + private static final String JOB_NAME = "The job"; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private ExecutionContextDao ctxDao; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private JobInstanceDao instanceDao; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private JobExecutionDao jobExecDao; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private StepExecutionDao stepExecDao; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private DbDropper dropper; |
| 47 | + |
| 48 | + @AfterEach |
| 49 | + void dropDatabase() { |
| 50 | + dropper.dropDatabase(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void getExecutionContextFromJobExecution() { |
| 55 | + var newExec = execution(); |
| 56 | + jobExecDao.saveJobExecution(newExec); |
| 57 | + |
| 58 | + assertThat(ctxDao.getExecutionContext(newExec)).isEqualToComparingFieldByField(execCtx()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void getExecutionContextFromStepExecution() { |
| 63 | + var newExec = execution(); |
| 64 | + jobExecDao.saveJobExecution(newExec); |
| 65 | + var stepExec = new StepExecution(STEP_NAME, newExec); |
| 66 | + stepExec.setExecutionContext(stepExecCtx()); |
| 67 | + stepExecDao.saveStepExecution(stepExec); |
| 68 | + |
| 69 | + assertThat(ctxDao.getExecutionContext(stepExec)).isEqualToComparingFieldByField(stepExecCtx()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void saveExecutionContextForJobExecution() { |
| 74 | + var newExec = execution(); |
| 75 | + jobExecDao.saveJobExecution(newExec); |
| 76 | + |
| 77 | + newExec.setExecutionContext(newExecCtx()); |
| 78 | + ctxDao.saveExecutionContext(newExec); |
| 79 | + |
| 80 | + assertThat(ctxDao.getExecutionContext(newExec)).isEqualToComparingFieldByField(newExecCtx()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void saveExecutionContextForStepExecution() { |
| 85 | + var newExec = execution(); |
| 86 | + jobExecDao.saveJobExecution(newExec); |
| 87 | + var stepExec = new StepExecution(STEP_NAME, newExec); |
| 88 | + stepExec.setExecutionContext(stepExecCtx()); |
| 89 | + stepExecDao.saveStepExecution(stepExec); |
| 90 | + |
| 91 | + stepExec.setExecutionContext(newStepExecCtx()); |
| 92 | + ctxDao.saveExecutionContext(stepExec); |
| 93 | + |
| 94 | + assertThat(ctxDao.getExecutionContext(stepExec)).isEqualToComparingFieldByField(newStepExecCtx()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void saveExecutionContexts() { |
| 99 | + var newExec = execution(); |
| 100 | + jobExecDao.saveJobExecution(newExec); |
| 101 | + var stepExecOne = new StepExecution(STEP_NAME, newExec); |
| 102 | + stepExecDao.saveStepExecution(stepExecOne); |
| 103 | + var stepExecTwo = new StepExecution(STEP_NAME_OTHER, newExec); |
| 104 | + stepExecDao.saveStepExecution(stepExecTwo); |
| 105 | + |
| 106 | + stepExecOne.setExecutionContext(stepExecCtx()); |
| 107 | + stepExecTwo.setExecutionContext(newStepExecCtx()); |
| 108 | + ctxDao.saveExecutionContexts(Arrays.asList(stepExecOne, stepExecTwo)); |
| 109 | + |
| 110 | + assertThat(ctxDao.getExecutionContext(stepExecOne)).isEqualToComparingFieldByField(stepExecCtx()); |
| 111 | + assertThat(ctxDao.getExecutionContext(stepExecTwo)).isEqualToComparingFieldByField(newStepExecCtx()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void updateExecutionContextForJobExecution() { |
| 116 | + var newExec = execution(); |
| 117 | + newExec.setExecutionContext(execCtx()); |
| 118 | + jobExecDao.saveJobExecution(newExec); |
| 119 | + |
| 120 | + newExec.setExecutionContext(newExecCtx()); |
| 121 | + ctxDao.updateExecutionContext(newExec); |
| 122 | + |
| 123 | + assertThat(ctxDao.getExecutionContext(newExec)).isEqualToComparingFieldByField(newExecCtx()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void updateExecutionContextForStepExecution() { |
| 128 | + var newExec = execution(); |
| 129 | + jobExecDao.saveJobExecution(newExec); |
| 130 | + var stepExec = new StepExecution(STEP_NAME, newExec); |
| 131 | + stepExec.setExecutionContext(stepExecCtx()); |
| 132 | + stepExecDao.saveStepExecution(stepExec); |
| 133 | + |
| 134 | + stepExec.setExecutionContext(newStepExecCtx()); |
| 135 | + stepExecDao.updateStepExecution(stepExec); |
| 136 | + |
| 137 | + assertThat(ctxDao.getExecutionContext(stepExec)).isEqualToComparingFieldByField(newStepExecCtx()); |
| 138 | + } |
| 139 | + |
| 140 | + private JobExecution execution() { |
| 141 | + val instance = instanceDao.createJobInstance(JOB_NAME, new JobParameters()); |
| 142 | + val execution = new JobExecution(instance, new JobParameters()); |
| 143 | + execution.setExecutionContext(execCtx()); |
| 144 | + jobExecDao.saveJobExecution(execution); |
| 145 | + return execution; |
| 146 | + } |
| 147 | + |
| 148 | + private ExecutionContext execCtx() { |
| 149 | + return new ExecutionContext(params()); |
| 150 | + } |
| 151 | + |
| 152 | + private ExecutionContext stepExecCtx() { |
| 153 | + return new ExecutionContext(paramsStep()); |
| 154 | + } |
| 155 | + |
| 156 | + private ExecutionContext newExecCtx() { |
| 157 | + return new ExecutionContext(newParams()); |
| 158 | + } |
| 159 | + |
| 160 | + private ExecutionContext newStepExecCtx() { |
| 161 | + return new ExecutionContext(newParamsStep()); |
| 162 | + } |
| 163 | + |
| 164 | + private Map<String, Object> params() { |
| 165 | + val params = new HashMap<String, Object>(); |
| 166 | + params.put("test", "value"); |
| 167 | + return params; |
| 168 | + } |
| 169 | + |
| 170 | + private Map<String, Object> paramsStep() { |
| 171 | + val params = new HashMap<String, Object>(); |
| 172 | + params.put("test123", "value123"); |
| 173 | + return params; |
| 174 | + } |
| 175 | + |
| 176 | + private Map<String, Object> newParams() { |
| 177 | + val params = new HashMap<String, Object>(); |
| 178 | + params.put("new-test", "new-value"); |
| 179 | + return params; |
| 180 | + } |
| 181 | + |
| 182 | + private Map<String, Object> newParamsStep() { |
| 183 | + val params = new HashMap<String, Object>(); |
| 184 | + params.put("new-test123", "new-value123"); |
| 185 | + return params; |
| 186 | + } |
| 187 | +} |
0 commit comments