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 net.ivoa.xml.uws.v1.ErrorSummaryFactory;
@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());
}
@Test
public void testJob() {
JobSummary job = getJob();
dao.createJob(job);
assertTrue(dao.getJob("123").isPresent());
assertEquals(ExecutionPhase.PENDING, dao.getJob("123").get().getPhase());
job.setPhase(ExecutionPhase.EXECUTING);
dao.updateJob(job);
assertEquals(ExecutionPhase.EXECUTING, dao.getJob("123").get().getPhase());
}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
@Test
public void testCreateJobWithError() {
JobSummary job = getJob();
job.setPhase(ExecutionPhase.ERROR);
// A generic picked randomly
ErrorSummary errorSummary
= ErrorSummaryFactory.newErrorSummary(
ErrorSummaryFactory.VOSpaceFault.PERMISSION_DENIED);
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);
// A generic picked randomly
ErrorSummary errorSummary
= ErrorSummaryFactory.newErrorSummary(
ErrorSummaryFactory.VOSpaceFault.PERMISSION_DENIED);
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()));
}
129
130
131
132
133
134
135
136
137
138
139
140
141
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
@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());
}