Search in sources :

Example 1 with DeletedEntity

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

the class AbstractObservationDAOTest method testPutGetDelete.

@Test
public void testPutGetDelete() {
    try {
        Observation orig = getTestObservation(false, 5, false, true);
        // !EXISTS
        Assert.assertNull(dao.getState(orig.getURI()));
        log.info("not-exists");
        ObservationResponse notFound = dao.getObservationResponse(orig.getURI());
        Assert.assertNotNull("wrapper", notFound);
        Assert.assertNotNull("wrapper", notFound.observationState);
        Assert.assertNull("wrapped", notFound.observation);
        // PUT
        dao.put(orig);
        // this is so we can detect incorrect timestamp round trips
        // caused by assigning something other than what was stored
        Thread.sleep(2 * TIME_TOLERANCE);
        log.info("put + sleep");
        // EXISTS
        Assert.assertNotNull("exists-by-uri", dao.getState(orig.getURI()));
        Assert.assertNotNull("exists-by-uuid", dao.getState(orig.getID()));
        log.info("exists");
        // GET by URI
        ObservationState st = dao.getState(orig.getURI());
        log.info("found: " + st);
        Assert.assertNotNull("found ObservationState by URI", st);
        ObservationResponse rs = dao.getObservationResponse(st);
        log.info("found: " + rs);
        Assert.assertNotNull("found ObservationResponse by URI", rs);
        Observation retrieved = rs.observation;
        log.info("found: " + retrieved);
        Assert.assertNotNull("found Observation by URI", retrieved);
        log.info("retrieved by URI");
        testEqual(orig, retrieved);
        log.info("equal");
        // GET by ID
        retrieved = dao.get(orig.getID());
        Assert.assertNotNull("found by ID", retrieved);
        log.info("retrieved by UUID");
        testEqual(orig, retrieved);
        log.info("equal");
        // DELETE by ID
        dao.delete(orig.getID());
        // EXISTS
        Assert.assertNull(dao.getState(orig.getURI()));
        log.info("delete & not-exists");
        log.info("check deletion track: " + orig.getID());
        DeletedEntity de = ded.get(DeletedObservation.class, orig.getID());
        Assert.assertNotNull("deletion tracker", de);
        Assert.assertEquals("deleted.id", orig.getID(), de.getID());
        Assert.assertNotNull("deleted.lastModified", de.lastModified);
        DeletedObservation doe = (DeletedObservation) de;
        Assert.assertEquals("deleted.uri", orig.getURI(), doe.getURI());
        Assert.assertFalse("open transaction", txnManager.isOpen());
    } catch (Exception unexpected) {
        log.error("unexpected exception", unexpected);
        Assert.fail("unexpected exception: " + unexpected);
    }
}
Also used : 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) ObservationState(ca.nrc.cadc.caom2.ObservationState) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) SQLException(java.sql.SQLException) PreconditionFailedException(ca.nrc.cadc.net.PreconditionFailedException) DeletedEntity(ca.nrc.cadc.caom2.DeletedEntity) Test(org.junit.Test)

Example 2 with DeletedEntity

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

the class DeletedEntityDAO method put.

/**
 * This is intended to be called from ObservationDAO.delete().
 * @param de
 * @param jdbc
 */
void put(DeletedEntity de, JdbcTemplate jdbc) {
    checkInit();
    if (de == null) {
        throw new IllegalArgumentException("arg cannot be null");
    }
    log.debug("PUT: " + de);
    long t = System.currentTimeMillis();
    try {
        DeletedEntity cur = get(de.getClass(), de.getID(), jdbc);
        boolean update = (cur != null);
        if (de.lastModified == null) {
            String tsSQL = gen.getCurrentTimeSQL();
            log.debug("PUT: " + tsSQL);
            // get current timestamp from server so that lastModified is closer to
            // monatonically increasing than if we use client machine clock
            Date now = getCurrentTime(jdbc);
            DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC);
            log.debug("current time: " + df.format(now));
            // if not null, the entity was harvested so keep original timestamp
            Util.assignDeletedLastModified(de, now, "lastModified");
        }
        DeletedEntityPut op = gen.getDeletedEntityPut(de.getClass(), update);
        op.setValue(de);
        op.execute(jdbc);
    } finally {
        long dt = System.currentTimeMillis() - t;
        log.debug("PUT: " + de + " " + dt + "ms");
    }
}
Also used : DateFormat(java.text.DateFormat) Date(java.util.Date) DeletedEntity(ca.nrc.cadc.caom2.DeletedEntity)

Example 3 with DeletedEntity

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

the class DeletedEntityDAO method get.

DeletedEntity get(Class<? extends DeletedEntity> c, UUID id, JdbcTemplate jdbc) {
    checkInit();
    log.debug("GET: " + id);
    long t = System.currentTimeMillis();
    try {
        // JdbcTemplate jdbc = new JdbcTemplate(dataSource);
        String sql = gen.getSelectSQL(c, id);
        log.debug("GET SQL: " + sql);
        Object result = jdbc.query(sql, gen.getDeletedEntityMapper(c));
        if (result == null) {
            return null;
        }
        if (result instanceof List) {
            List obs = (List) result;
            if (obs.isEmpty()) {
                return null;
            }
            if (obs.size() > 1) {
                throw new RuntimeException("BUG: get " + c.getSimpleName() + " " + id + " query returned " + obs.size() + " DeletedEntity(s)");
            }
            Object o = obs.get(0);
            if (o instanceof DeletedEntity) {
                DeletedEntity ret = (DeletedEntity) obs.get(0);
                return ret;
            } else {
                throw new RuntimeException("BUG: query returned an unexpected type " + o.getClass().getName());
            }
        }
        throw new RuntimeException("BUG: query returned an unexpected list type " + result.getClass().getName());
    } finally {
        long dt = System.currentTimeMillis() - t;
        log.debug("GET: " + id + " " + dt + "ms");
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) DeletedEntity(ca.nrc.cadc.caom2.DeletedEntity)

Example 4 with DeletedEntity

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

the class AbstractDeletedEntityDAOTest method testPutGetDelete.

@Test
public void testPutGetDelete() {
    try {
        UUID id1 = new UUID(0L, 100L);
        Date d1 = new Date();
        ObservationURI u1 = new ObservationURI("FOO", "bar1");
        DeletedObservation o1 = new DeletedObservation(id1, u1);
        log.info("put: " + o1);
        dao.put(o1);
        Assert.assertNotNull("side effect: DeletedObservation.lastModified", o1.lastModified);
        DeletedEntity per = dao.get(DeletedObservation.class, id1);
        Assert.assertNotNull("found DeletedObservation " + id1, per);
        Assert.assertNotNull("DeletedObservation.lastModified", per.lastModified);
        dao.delete(per);
        DeletedEntity gone = dao.get(DeletedObservation.class, id1);
        Assert.assertNull(gone);
    } catch (Exception unexpected) {
        log.error("unexpected exception", unexpected);
        Assert.fail("unexpected exception: " + unexpected);
    }
}
Also used : ObservationURI(ca.nrc.cadc.caom2.ObservationURI) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) UUID(java.util.UUID) Date(java.util.Date) DeletedEntity(ca.nrc.cadc.caom2.DeletedEntity) Test(org.junit.Test)

Example 5 with DeletedEntity

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

the class AbstractDeletedEntityDAOTest method testGetListDeletedObservation.

@Test
public void testGetListDeletedObservation() {
    try {
        UUID id1 = new UUID(0L, 100L);
        UUID id2 = new UUID(0L, 200L);
        UUID id3 = new UUID(0L, 300L);
        UUID id4 = new UUID(0L, 400L);
        UUID id5 = new UUID(0L, 500L);
        Date d1 = new Date();
        Date d2 = new Date(d1.getTime() + 10L);
        Date d3 = new Date(d2.getTime() + 10L);
        Date d4 = new Date(d3.getTime() + 10L);
        Date d5 = new Date(d4.getTime() + 10L);
        ObservationURI u1 = new ObservationURI("FOO", "bar1");
        ObservationURI u2 = new ObservationURI("FOO", "bar2");
        ObservationURI u3 = new ObservationURI("FOO", "bar3");
        ObservationURI u4 = new ObservationURI("FOO", "bar4");
        ObservationURI u5 = new ObservationURI("FOO", "bar5");
        DeletedObservation o1 = new DeletedObservation(id1, u1);
        DeletedObservation o2 = new DeletedObservation(id2, u2);
        DeletedObservation o3 = new DeletedObservation(id3, u3);
        DeletedObservation o4 = new DeletedObservation(id4, u4);
        DeletedObservation o5 = new DeletedObservation(id5, u5);
        log.info("put: \n" + o1 + "\n" + o2 + "\n" + o3 + "\n" + o4 + "\n" + o5 + "\n");
        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);
        Assert.assertNotNull("DeletedObservation.lastModified", o1.lastModified);
        // past
        Date start = new Date(o1.lastModified.getTime() - 100L);
        Date end = null;
        Integer batchSize = new Integer(3);
        List<DeletedEntity> dels;
        // get first batch
        dels = dao.getList(DeletedObservation.class, start, end, batchSize);
        Assert.assertNotNull("deleted list", dels);
        Assert.assertEquals(3, dels.size());
        Assert.assertEquals(o1.getID(), dels.get(0).getID());
        Assert.assertEquals(o2.getID(), dels.get(1).getID());
        Assert.assertEquals(o3.getID(), dels.get(2).getID());
        // get next batch
        dels = dao.getList(DeletedObservation.class, o3.lastModified, end, batchSize);
        Assert.assertNotNull("deleted list/next batch", dels);
        // o3 gets picked up by the >=
        Assert.assertEquals(3, dels.size());
        Assert.assertEquals(o3.getID(), dels.get(0).getID());
        Assert.assertEquals(o4.getID(), dels.get(1).getID());
        Assert.assertEquals(o5.getID(), dels.get(2).getID());
    } catch (Exception unexpected) {
        log.error("unexpected exception", unexpected);
        Assert.fail("unexpected exception: " + unexpected);
    }
}
Also used : ObservationURI(ca.nrc.cadc.caom2.ObservationURI) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) UUID(java.util.UUID) Date(java.util.Date) DeletedEntity(ca.nrc.cadc.caom2.DeletedEntity) Test(org.junit.Test)

Aggregations

DeletedEntity (ca.nrc.cadc.caom2.DeletedEntity)8 DeletedObservation (ca.nrc.cadc.caom2.DeletedObservation)4 Test (org.junit.Test)4 Date (java.util.Date)3 UUID (java.util.UUID)3 DerivedObservation (ca.nrc.cadc.caom2.DerivedObservation)2 Observation (ca.nrc.cadc.caom2.Observation)2 ObservationResponse (ca.nrc.cadc.caom2.ObservationResponse)2 ObservationState (ca.nrc.cadc.caom2.ObservationState)2 ObservationURI (ca.nrc.cadc.caom2.ObservationURI)2 SimpleObservation (ca.nrc.cadc.caom2.SimpleObservation)2 PreconditionFailedException (ca.nrc.cadc.net.PreconditionFailedException)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DateFormat (java.text.DateFormat)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1