Search in sources :

Example 1 with SimpleObservation

use of ca.nrc.cadc.caom2.SimpleObservation in project caom2db by opencadc.

the class AbstractObservationDAOTest method testGetObservationList.

@Test
public void testGetObservationList() {
    try {
        log.info("testGetObservationList");
        Integer batchSize = new Integer(3);
        String collection = "FOO";
        Observation o1 = new SimpleObservation(collection, "obs1");
        Observation o2 = new SimpleObservation(collection, "obsA");
        Observation o3 = new SimpleObservation(collection, "obs2");
        Observation o4 = new SimpleObservation(collection, "obsB");
        Observation o5 = new SimpleObservation(collection, "obs3");
        // txnManager.startTransaction();
        dao.put(o1);
        Thread.sleep(10L);
        dao.put(o2);
        Thread.sleep(10L);
        dao.put(o3);
        Thread.sleep(10L);
        dao.put(o4);
        Thread.sleep(10L);
        dao.put(o5);
        // txnManager.commitTransaction();
        List<ObservationResponse> obs;
        // get first batch
        obs = dao.getList(collection, null, null, batchSize);
        Assert.assertNotNull(obs);
        Assert.assertEquals(3, obs.size());
        Assert.assertEquals(o1.getURI(), obs.get(0).observation.getURI());
        Assert.assertEquals(o2.getURI(), obs.get(1).observation.getURI());
        Assert.assertEquals(o3.getURI(), obs.get(2).observation.getURI());
        // get next batch
        obs = dao.getList(collection, o3.getMaxLastModified(), null, batchSize);
        Assert.assertNotNull(obs);
        // o3 gets picked up by the >=
        Assert.assertEquals(3, obs.size());
        Assert.assertEquals(o3.getURI(), obs.get(0).observation.getURI());
        Assert.assertEquals(o4.getURI(), obs.get(1).observation.getURI());
        Assert.assertEquals(o5.getURI(), obs.get(2).observation.getURI());
        // txnManager.startTransaction();
        dao.delete(o1.getURI());
        dao.delete(o2.getURI());
        dao.delete(o3.getURI());
        dao.delete(o4.getURI());
        dao.delete(o5.getURI());
    // txnManager.commitTransaction();
    } catch (Exception unexpected) {
        log.error("unexpected exception", unexpected);
        if (txnManager.isOpen())
            try {
                txnManager.rollbackTransaction();
            } catch (Throwable t) {
                log.error("failed to rollback transaction", t);
            }
        Assert.fail("unexpected exception: " + unexpected);
    }
}
Also used : SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) ObservationResponse(ca.nrc.cadc.caom2.ObservationResponse) DerivedObservation(ca.nrc.cadc.caom2.DerivedObservation) Observation(ca.nrc.cadc.caom2.Observation) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) SQLException(java.sql.SQLException) PreconditionFailedException(ca.nrc.cadc.net.PreconditionFailedException) Test(org.junit.Test)

Example 2 with SimpleObservation

use of ca.nrc.cadc.caom2.SimpleObservation in project caom2db by opencadc.

the class AbstractObservationDAOTest method testConditionalUpdate.

@Test
public void testConditionalUpdate() {
    try {
        SimpleObservation orig = new SimpleObservation("FOO", "bar");
        dao.put(orig);
        Observation c = dao.get(orig.getURI());
        Assert.assertNotNull("found", c);
        testEqual(orig, c);
        URI cs1 = c.getAccMetaChecksum();
        c.sequenceNumber = 1;
        dao.put(c, cs1);
        Observation c2 = dao.get(c.getURI());
        Assert.assertNotNull("found", c2);
        URI cs2 = c.getAccMetaChecksum();
        // conditional update fails when expected checksum does not match
        c.type = "OBJECT";
        try {
            dao.put(c, cs1);
        } catch (PreconditionFailedException expected) {
            log.info("caught expected exception: " + expected);
        }
        // conditional update succeeds when expected checksum does match
        dao.put(c, cs2);
        Observation c3 = dao.get(c.getURI());
        Assert.assertNotNull("found", c3);
        URI cs3 = c.getAccMetaChecksum();
        dao.delete(orig.getID());
        // copnditional update fails when not found
        try {
            dao.put(c, cs3);
        } catch (PreconditionFailedException expected) {
            log.info("caught expected exception: " + expected);
        }
    } catch (Exception unexpected) {
        log.error("unexpected exception", unexpected);
        if (txnManager.isOpen())
            try {
                txnManager.rollbackTransaction();
            } catch (Throwable t) {
                log.error("failed to rollback transaction", t);
            }
        Assert.fail("unexpected exception: " + unexpected);
    }
}
Also used : SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) DerivedObservation(ca.nrc.cadc.caom2.DerivedObservation) Observation(ca.nrc.cadc.caom2.Observation) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) PreconditionFailedException(ca.nrc.cadc.net.PreconditionFailedException) PlaneURI(ca.nrc.cadc.caom2.PlaneURI) URI(java.net.URI) ObservationURI(ca.nrc.cadc.caom2.ObservationURI) SQLException(java.sql.SQLException) PreconditionFailedException(ca.nrc.cadc.net.PreconditionFailedException) Test(org.junit.Test)

Example 3 with SimpleObservation

use of ca.nrc.cadc.caom2.SimpleObservation in project caom2db by opencadc.

the class ReadAccessGeneratorTest method getSampleObservation.

Observation getSampleObservation(final String id, final int numPlanes, Date now, long dateOffset) {
    Observation obs = new SimpleObservation("obsID_" + id, "collectionID_" + id);
    obs.proposal = new Proposal("proposalID_" + id);
    Date rd = null;
    if (now != null) {
        rd = new Date(now.getTime() + dateOffset);
    }
    obs.metaRelease = rd;
    for (int i = 0; i < numPlanes; i++) {
        Plane p = new Plane("productID_" + id + i);
        if (now != null) {
            p.metaRelease = rd;
            p.dataRelease = rd;
        }
        obs.getPlanes().add(p);
        Artifact a = new Artifact(URI.create("cadc:FOO/bar"), ProductType.SCIENCE, ReleaseType.DATA);
        p.getArtifacts().add(a);
    }
    return obs;
}
Also used : Plane(ca.nrc.cadc.caom2.Plane) SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) Observation(ca.nrc.cadc.caom2.Observation) SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) Proposal(ca.nrc.cadc.caom2.Proposal) Date(java.util.Date) Artifact(ca.nrc.cadc.caom2.Artifact)

Example 4 with SimpleObservation

use of ca.nrc.cadc.caom2.SimpleObservation in project caom2db by opencadc.

the class CaomRepoConditionalUpdateTest method testConditonalUpdate.

@Test
public void testConditonalUpdate() throws Throwable {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    Observation orig = new SimpleObservation(TEST_COLLECTION, "testConditionalUpdate");
    final URI origAcc = orig.computeAccMetaChecksum(md5);
    deleteObservation(orig.getURI().getURI().toASCIIString(), subject1, null, null);
    log.info("put: orig");
    sendObservation("PUT", orig, subject1, 200, "OK", null);
    // get the observation using subject2
    log.info("get: o1");
    Observation o1 = getObservation(orig.getURI().getURI().toASCIIString(), subject1, 200, null, EXPECTED_CAOM_VERSION);
    Assert.assertNotNull(o1);
    final URI acc1 = o1.computeAccMetaChecksum(md5);
    // modify
    orig.intent = ObservationIntentType.SCIENCE;
    // update
    log.info("update: orig");
    sendObservation("POST", orig, subject1, 200, "OK", null, acc1.toASCIIString());
    // get and verify
    log.info("get: updated");
    Observation o2 = getObservation(orig.getURI().getURI().toASCIIString(), subject1, 200, null, EXPECTED_CAOM_VERSION);
    Assert.assertNotNull(o2);
    final URI acc2 = o2.computeAccMetaChecksum(md5);
    // attempt second update from orig: rejected
    log.info("update: orig [race loser]");
    sendObservation("POST", orig, subject1, 412, "update blocked", null, origAcc.toASCIIString());
    // attempt update from current: accepted
    log.info("update: o2");
    sendObservation("POST", orig, subject1, 200, "OK", null, acc2.toASCIIString());
}
Also used : SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) Observation(ca.nrc.cadc.caom2.Observation) MessageDigest(java.security.MessageDigest) URI(java.net.URI) Test(org.junit.Test)

Example 5 with SimpleObservation

use of ca.nrc.cadc.caom2.SimpleObservation in project caom2db by opencadc.

the class AbstractNestedTransactionTest method testNestedTransaction.

@Test
public void testNestedTransaction() {
    // can still proceed -- eg it is really nested
    try {
        String uniqID1 = "bar-" + UUID.randomUUID().toString();
        String uniqID2 = "bar2-" + UUID.randomUUID().toString();
        Observation obs1 = new SimpleObservation("FOO", uniqID1);
        dao.put(obs1);
        Assert.assertNotNull(dao.getState(obs1.getURI()));
        log.info("created: " + obs1);
        Observation dupe = new SimpleObservation("FOO", uniqID1);
        Observation obs2 = new SimpleObservation("FOO", uniqID2);
        log.info("start outer");
        // outer txn
        dao.getTransactionManager().startTransaction();
        try {
            // nested txn
            dao.put(dupe);
            Assert.fail("expected exception, successfully put duplicate observation");
        } catch (DataIntegrityViolationException expected) {
            log.info("caught expected: " + expected);
        }
        // another nested txn
        dao.put(obs2);
        log.info("created: " + obs2);
        Assert.assertNotNull(dao.getState(obs2.getURI()));
        log.info("commit outer");
        // outer txn
        dao.getTransactionManager().commitTransaction();
        log.info("commit outer [OK]");
        Observation check1 = dao.get(obs1.getID());
        Assert.assertNotNull(check1);
        Assert.assertEquals(obs1.getURI(), check1.getURI());
        Observation check2 = dao.get(obs2.getID());
        Assert.assertNotNull(check2);
        Assert.assertEquals(obs2.getURI(), check2.getURI());
    } catch (Exception unexpected) {
        log.error("unexpected exception", unexpected);
        Assert.fail("unexpected exception: " + unexpected);
    }
}
Also used : SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) Observation(ca.nrc.cadc.caom2.Observation) SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Aggregations

SimpleObservation (ca.nrc.cadc.caom2.SimpleObservation)54 Test (org.junit.Test)45 Observation (ca.nrc.cadc.caom2.Observation)20 Plane (ca.nrc.cadc.caom2.Plane)16 Artifact (ca.nrc.cadc.caom2.Artifact)9 Chunk (ca.nrc.cadc.caom2.Chunk)7 DeletedObservation (ca.nrc.cadc.caom2.DeletedObservation)7 DerivedObservation (ca.nrc.cadc.caom2.DerivedObservation)7 Instrument (ca.nrc.cadc.caom2.Instrument)7 Part (ca.nrc.cadc.caom2.Part)7 URI (java.net.URI)7 PreconditionFailedException (ca.nrc.cadc.net.PreconditionFailedException)6 SQLException (java.sql.SQLException)6 Proposal (ca.nrc.cadc.caom2.Proposal)5 ObservationURI (ca.nrc.cadc.caom2.ObservationURI)4 Axis (ca.nrc.cadc.caom2.wcs.Axis)4 CoordAxis1D (ca.nrc.cadc.caom2.wcs.CoordAxis1D)4 CoordFunction1D (ca.nrc.cadc.caom2.wcs.CoordFunction1D)4 RefCoord (ca.nrc.cadc.caom2.wcs.RefCoord)4 SpectralWCS (ca.nrc.cadc.caom2.wcs.SpectralWCS)4