Skip to content
JobSummaryTest.java 4.16 KiB
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;
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);
    }
    
    /**
     * Uses JSON extracted from real job executed by transfer service. Contains extra field jobType.
     */
    @Test
    public void testDeserializeTransferServiceResponse() throws Exception {
        String response = "{\"jobId\": \"917c784f814c4a1a91a9d5d1af07dbe9\", \"ownerId\": \"2386\", \"jobType\": \"pullToVoSpace\", \"phase\": \"PENDING\", \"startTime\": null, \"endTime\": null, \"creationTime\": \"2021-02-03T15:05:57.233602\", \"jobInfo\": {\"transfer\": {\"view\": null, \"target\": \"vos://example.com!vospace/szorba/aaa\", \"version\": null, \"direction\": \"pullToVoSpace\", \"keepBytes\": null, \"protocols\": [{\"uri\": \"ia2:async-recall\", \"param\": [{\"uri\": \"ia2:node-type\", \"value\": \"single\"}], \"endpoint\": null}]}}, \"results\": null}";
        MAPPER.readValue(response, JobSummary.class);
    }

    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 protocol1 = new Protocol();
        protocol1.setUri("ivo://ivoa.net/vospace/core#httpget");
        Protocol protocol2 = new Protocol();
        protocol2.setUri("ivo://ivoa.net/vospace/core#httpsget");
        transfer.getProtocols().add(protocol1);
        transfer.getProtocols().add(protocol2);

        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.getProtocols().get(0);
        assertEquals("ivo://ivoa.net/vospace/core#httpget", protocol.getUri());
    }
}