Newer
Older
package it.inaf.oats.vospace.persistence;
import it.inaf.oats.vospace.JobService;
import java.util.List;
import javax.sql.DataSource;
import net.ivoa.xml.uws.v1.ExecutionPhase;
import net.ivoa.xml.uws.v1.JobSummary;
import net.ivoa.xml.uws.v1.ShortJobDescription;
import net.ivoa.xml.vospace.v2.Transfer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional;
import java.time.LocalDateTime;
import java.time.Month;
import net.ivoa.xml.uws.v1.ErrorSummary;
import it.inaf.oats.vospace.exception.ErrorSummaryFactory;
import it.inaf.oats.vospace.exception.PermissionDeniedException;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {DataSourceConfig.class})
@TestPropertySource(locations = "classpath:test.properties")
public class JobDAOTest {
@Autowired
private DataSource dataSource;
private JobDAO dao;
@BeforeEach
public void init() {
dao = new JobDAO(dataSource);
}
private JobSummary getJob() {
Transfer transfer = new Transfer();
transfer.setDirection("pushToVoSpace");
transfer.setTarget("vos://example.com!vospace/mynode");
JobSummary job = new JobSummary();
job.setJobId("123");
job.setOwnerId("owner");
job.setPhase(ExecutionPhase.PENDING);
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
return job;
}
// don't want to override equals method just for testing without a use case
private boolean areEqual(ErrorSummary a, ErrorSummary b) {
return (a.getMessage().equals(b.getMessage())
&& a.getType().equals(b.getType())
&& a.isHasDetail() == b.isHasDetail()
&& a.getDetailMessage().equals(b.getDetailMessage()));
}
@Test
public void testJob() {
JobSummary job = getJob();
dao.createJob(job);
assertTrue(dao.getJob("123").isPresent());
assertEquals(ExecutionPhase.PENDING, dao.getJob("123").get().getPhase());
// uses the job retrieved from DAO to perform the update (reproduced a bug in job update)
job = dao.getJob("123").get();
job.setPhase(ExecutionPhase.EXECUTING);
dao.updateJob(job);
assertEquals(ExecutionPhase.EXECUTING, dao.getJob("123").get().getPhase());
}
@Test
public void testCreateJobWithError() {
JobSummary job = getJob();
job.setPhase(ExecutionPhase.ERROR);
// Generate it from exception
ErrorSummary errorSummary
= ErrorSummaryFactory.newErrorSummary(
new PermissionDeniedException("/pippo1/pippo2"));
// Check if properly generated
assertTrue(errorSummary.isHasDetail());
assertEquals("PermissionDenied. Path: /pippo1/pippo2", errorSummary.getDetailMessage());
job.setErrorSummary(errorSummary);
dao.createJob(job);
// Retrieve it back
Optional<JobSummary> retrievedJobOpt = dao.getJob(job.getJobId());
assertTrue(retrievedJobOpt.isPresent());
JobSummary retrievedJob = retrievedJobOpt.get();
assertEquals(ExecutionPhase.ERROR, retrievedJob.getPhase());
assertTrue(areEqual(job.getErrorSummary(), retrievedJob.getErrorSummary()));
}
@Test
public void testUpdateJobWithError() {
JobSummary job = getJob();
dao.createJob(job);
job.setPhase(ExecutionPhase.ERROR);
// Generate it from exception
ErrorSummary errorSummary
= ErrorSummaryFactory.newErrorSummary(
new PermissionDeniedException("/pippo1/pippo2"));
// Check if properly generated
assertTrue(errorSummary.isHasDetail());
assertEquals("PermissionDenied. Path: /pippo1/pippo2", errorSummary.getDetailMessage());
job.setErrorSummary(errorSummary);
dao.updateJob(job);
// Retrieve it back
Optional<JobSummary> retrievedJobOpt = dao.getJob(job.getJobId());
assertTrue(retrievedJobOpt.isPresent());
JobSummary retrievedJob = retrievedJobOpt.get();
assertEquals(ExecutionPhase.ERROR, retrievedJob.getPhase());
assertTrue(areEqual(job.getErrorSummary(), retrievedJob.getErrorSummary()));
}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
@Test
public void testJobsList() {
// Check no arguments
String user = "user1";
List<ExecutionPhase> phaseList = List.of();
List<JobService.JobType> directionList = List.of();
Optional<LocalDateTime> after = Optional.ofNullable(null);
Optional<Integer> last = Optional.ofNullable(null);
Jobs jobs = dao.getJobs(user, phaseList, directionList, after, last);
assertTrue(jobs != null);
List<ShortJobDescription> sjdList = jobs.getJobref();
assertTrue(sjdList != null);
assertTrue(!sjdList.isEmpty());
assertTrue(
sjdList.stream().noneMatch(
(i) -> {
return i.getPhase().equals(ExecutionPhase.ARCHIVED);
}
)
);
assertEquals(3, sjdList.size());
}
@Test
public void testJobsListLimit() {
// Check no arguments
String user = "user1";
List<ExecutionPhase> phaseList = List.of();
List<JobService.JobType> directionList = List.of();
Optional<LocalDateTime> after = Optional.ofNullable(null);
Optional<Integer> last = Optional.of(2);
Jobs jobs = dao.getJobs(user, phaseList, directionList, after, last);
List<ShortJobDescription> sjdList = jobs.getJobref();
assertEquals(2, sjdList.size());
}
@Test
public void testJobsPhase() {
// Check no arguments
String user = "user1";
List<ExecutionPhase> phaseList
= List.of(ExecutionPhase.PENDING, ExecutionPhase.EXECUTING);
List<JobService.JobType> directionList = List.of();
Optional<LocalDateTime> after = Optional.ofNullable(null);
Optional<Integer> last = Optional.ofNullable(null);
Jobs jobs = dao.getJobs(user, phaseList, directionList, after, last);
List<ShortJobDescription> sjdList = jobs.getJobref();
assertEquals(sjdList.size(), 2);
assertEquals("pippo5", sjdList.get(0).getId());
assertEquals("pippo2", sjdList.get(1).getId());
}
@Test
public void testJobsDirection() {
// Check no arguments
String user = "user1";
List<ExecutionPhase> phaseList = List.of();
List<JobService.JobType> directionList
= List.of(JobService.JobType.pullFromVoSpace,
JobService.JobType.pullToVoSpace);
Optional<LocalDateTime> after = Optional.ofNullable(null);
Optional<Integer> last = Optional.ofNullable(null);
Jobs jobs = dao.getJobs(user, phaseList, directionList, after, last);
List<ShortJobDescription> sjdList = jobs.getJobref();
assertEquals(2, sjdList.size());
assertEquals("pippo3", sjdList.get(0).getId());
assertEquals("pippo2", sjdList.get(1).getId());
}
@Test
public void testJobsAfter() {
// Check no arguments
String user = "user1";
List<ExecutionPhase> phaseList = List.of();
List<JobService.JobType> directionList = List.of();
LocalDateTime ldt
= LocalDateTime.of(2013, Month.FEBRUARY, 7, 18, 15);
Optional<LocalDateTime> after = Optional.of(ldt);
Optional<Integer> last = Optional.ofNullable(null);
Jobs jobs = dao.getJobs(user, phaseList, directionList, after, last);
List<ShortJobDescription> sjdList = jobs.getJobref();
assertEquals(2, sjdList.size());
assertEquals("pippo5", sjdList.get(0).getId());
assertEquals("pippo3", sjdList.get(1).getId());
}
@Test
public void testJobsAllchecks() {
// Check no arguments
String user = "user1";
List<ExecutionPhase> phaseList = List.of(ExecutionPhase.QUEUED,
List<JobService.JobType> directionList
= List.of(JobService.JobType.pullFromVoSpace,
JobService.JobType.pullToVoSpace);
LocalDateTime ldt
= LocalDateTime.of(2013, Month.FEBRUARY, 7, 18, 15);
Optional<LocalDateTime> after = Optional.of(ldt);
Optional<Integer> last = Optional.of(2);
Jobs jobs = dao.getJobs(user, phaseList, directionList, after, last);
List<ShortJobDescription> sjdList = jobs.getJobref();
assertEquals(1, sjdList.size());
assertEquals("pippo3", sjdList.get(0).getId());
}