Newer
Older
package net.ivoa.xml.uws.v1;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXB;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import net.ivoa.xml.uws.v1.JobSummary.JobInfo;
import net.ivoa.xml.vospace.v2.Protocol;
import net.ivoa.xml.vospace.v2.Transfer;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class JobSummaryTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
@Test
public void testXmlSerialization() throws Exception {
JobSummary job = getJobSummary();
String xml;
try ( StringWriter sw = new StringWriter()) {
JAXB.marshal(job, sw);
xml = sw.toString();
System.out.println(xml);
}
assertTrue(xml.contains("<uws:job"));
assertTrue(xml.contains("<uws:jobInfo"));
assertTrue(xml.contains("<vos:transfer"));
try ( StringReader sr = new StringReader(xml)) {
JobSummary deserialized = JAXB.unmarshal(sr, JobSummary.class);
verifyJobsAreEquals(deserialized);
}
}
@Test
public void testJsonSerialization() throws Exception {
JobSummary job = getJobSummary();
String json = MAPPER.writeValueAsString(job);
System.out.println(json);
JobSummary deserialized = MAPPER.readValue(json, JobSummary.class);
verifyJobsAreEquals(deserialized);
}
private JobSummary getJobSummary() {
JobSummary job = new JobSummary();
job.setJobId("job_id");
job.setPhase(ExecutionPhase.PENDING);
job.setQuote(getXmlDate("2015-05-26T11:06:45.713"));
JobInfo jobInfo = new JobInfo();
Transfer transfer = new Transfer();
transfer.setVersion("2.1");
transfer.setTarget("vos://example.com!vospace/mydata1");
transfer.setDirection("pullFromVoSpace");
Protocol protocol = new Protocol();
protocol.setUri("ivo://ivoa.net/vospace/core#httpget");
transfer.getProtocol().add(protocol);
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
return job;
}
private XMLGregorianCalendar getXmlDate(String dateTimeString) {
try {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(dateTimeString);
} catch (DatatypeConfigurationException ex) {
throw new RuntimeException(ex);
}
}
private void verifyJobsAreEquals(JobSummary deserializedJob) {
assertEquals("job_id", deserializedJob.getJobId());
assertEquals(ExecutionPhase.PENDING, deserializedJob.getPhase());
Transfer transfer = (Transfer) deserializedJob.getJobInfo().getAny().get(0);
assertEquals("2.1", transfer.getVersion());
assertEquals("pullFromVoSpace", transfer.getDirection());
assertEquals("vos://example.com!vospace/mydata1", transfer.getTarget());
Protocol protocol = transfer.getProtocol().get(0);
assertEquals("ivo://ivoa.net/vospace/core#httpget", protocol.getUri());
}
}