Example usage for org.hibernate Session save

List of usage examples for org.hibernate Session save

Introduction

In this page you can find the example usage for org.hibernate Session save.

Prototype

Serializable save(Object object);

Source Link

Document

Persist the given transient instance, first assigning a generated identifier.

Usage

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigDao#findFreeinType(au.edu.uts.eng.remotelabs.schedserver.dataaccess.entities.RigType)}.
 *//*from  w  w  w .j  a  va  2s.  c o m*/
@Test
public void testFindFreeinType() {
    /* Add a rig. */
    long ids[] = new long[5];
    Session ses = this.dao.getSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);
    ses.getTransaction().commit();

    Rig rig = new Rig();
    rig.setName("rig_name_test_1");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    Timestamp ts = new Timestamp(System.currentTimeMillis());
    ts.setNanos(0); // Need to trunc time as nanoseconds aren't stored in the DB.
    rig.setLastUpdateTimestamp(ts);
    rig.setOnline(true);
    rig.setActive(true);
    rig.setManaged(true);
    this.dao.persist(rig);
    ids[0] = rig.getId();

    rig = new Rig();
    rig.setName("rig_name_test_2");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(ts);
    rig.setOnline(true);
    rig.setActive(true);
    rig.setManaged(true);
    this.dao.persist(rig);
    ids[1] = rig.getId();

    rig = new Rig();
    rig.setName("rig_name_test_3");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(ts);
    rig.setActive(true);
    rig.setOnline(true);
    rig.setInSession(true);
    this.dao.persist(rig);
    ids[2] = rig.getId();

    rig = new Rig();
    rig.setName("rig_name_test_4");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(ts);
    rig.setOnline(true);
    rig.setInSession(true);
    this.dao.persist(rig);
    ids[3] = rig.getId();

    rig = new Rig();
    rig.setName("rig_name_test_5");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(ts);
    rig.setActive(true);
    rig.setInSession(true);
    this.dao.persist(rig);
    ids[4] = rig.getId();

    this.dao.closeSession();
    this.dao = new RigDao();

    List<Rig> free = this.dao.findFreeinType(type);
    assertNotNull(free);
    assertEquals(2, free.size());
    Rig fr = free.get(0);
    assertTrue(fr.isActive());
    assertTrue(fr.isOnline());
    assertFalse(fr.isInSession());
    assertTrue(fr.isManaged());
    assertNull(fr.getMeta());
    fr = free.get(1);
    assertTrue(fr.isActive());
    assertTrue(fr.isOnline());
    assertFalse(fr.isInSession());
    assertTrue(fr.isManaged());
    assertNull(fr.getMeta());

    List<String> names = new ArrayList<String>(2);
    for (Rig r : free) {
        names.add(r.getName());
    }
    assertTrue(names.contains("rig_name_test_1"));
    assertTrue(names.contains("rig_name_test_2"));

    ses = this.dao.getSession();
    ses.beginTransaction();
    for (long i : ids) {
        ses.createSQLQuery("DELETE FROM rig WHERE id=" + i).executeUpdate();
    }
    ses.createSQLQuery("DELETE FROM rig_capabilities WHERE id=" + caps.getId()).executeUpdate();
    ses.createSQLQuery("DELETE FROM rig_type WHERE id=" + type.getId()).executeUpdate();
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#addRegisteredLog()}
 *///from w ww.jav a2s  .  com
@Test
public void testAddRegisteredLog() {
    Date now = new Date();

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("rig_name_test");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(now);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOnline(true);
    ses.save(rig);
    ses.getTransaction().commit();

    RigLogDao dao = new RigLogDao(ses);
    RigLog log = dao.addRegisteredLog(rig, "Newly registered");
    assertNotNull(log);

    Session ses2 = DataAccessActivator.getNewSession();
    RigLog loaded = (RigLog) ses2.load(RigLog.class, log.getId());
    assertNotNull(loaded);

    assertEquals(RigLog.NOT_REGISTERED, loaded.getOldState());
    assertEquals(RigLog.ONLINE, loaded.getNewState());
    assertEquals("Newly registered", loaded.getReason());
    assertEquals(Math.floor(now.getTime() / 1000), Math.floor(loaded.getTimeStamp().getTime() / 1000));
    assertEquals(rig.getId().longValue(), loaded.getRig().getId().longValue());

    ses.beginTransaction();
    ses.delete(log);
    ses.delete(rig);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();
    ses2.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#addOnlineLog()}
 *//*from w w w .j  a  va  2  s  .com*/
@Test
public void testAddOnlineLog() {
    Date now = new Date();

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("rig_name_test");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(now);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOnline(true);
    ses.save(rig);
    ses.getTransaction().commit();

    RigLogDao dao = new RigLogDao(ses);
    RigLog log = dao.addOnlineLog(rig, "Fixed");
    assertNotNull(log);

    Session ses2 = DataAccessActivator.getNewSession();
    RigLog loaded = (RigLog) ses2.load(RigLog.class, log.getId());
    assertNotNull(loaded);

    assertEquals(RigLog.OFFLINE, loaded.getOldState());
    assertEquals(RigLog.ONLINE, loaded.getNewState());
    assertEquals("Fixed", loaded.getReason());
    assertEquals(Math.floor(now.getTime() / 1000), Math.floor(loaded.getTimeStamp().getTime() / 1000));
    assertEquals(rig.getId().longValue(), loaded.getRig().getId().longValue());

    ses.beginTransaction();
    ses.delete(log);
    ses.delete(rig);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();
    ses2.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#addOfflineLog()}
 *//*from   w  ww.j a  v  a 2  s. c  o m*/
@Test
public void testAddOfflineLog() {
    Date now = new Date();

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("rig_name_test");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(now);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOnline(true);
    ses.save(rig);
    ses.getTransaction().commit();

    RigLogDao dao = new RigLogDao(ses);
    RigLog log = dao.addOfflineLog(rig, "Broken");
    assertNotNull(log);

    Session ses2 = DataAccessActivator.getNewSession();
    RigLog loaded = (RigLog) ses2.load(RigLog.class, log.getId());
    assertNotNull(loaded);

    assertEquals(RigLog.ONLINE, loaded.getOldState());
    assertEquals(RigLog.OFFLINE, loaded.getNewState());
    assertEquals("Broken", loaded.getReason());
    assertEquals(Math.floor(now.getTime() / 1000), Math.floor(loaded.getTimeStamp().getTime() / 1000));
    assertEquals(rig.getId().longValue(), loaded.getRig().getId().longValue());

    ses.beginTransaction();
    ses.delete(log);
    ses.delete(rig);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();
    ses2.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#addUnRegisteredLog()}
 *//*  ww  w .j  a va 2 s.  c o  m*/
@Test
public void testAddUnRegisteredLog() {
    Date now = new Date();

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("rig_name_test");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(now);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOnline(false);
    ses.save(rig);
    ses.getTransaction().commit();

    RigLogDao dao = new RigLogDao(ses);
    RigLog log = dao.addUnRegisteredLog(rig, "Un registered");
    assertNotNull(log);

    Session ses2 = DataAccessActivator.getNewSession();
    RigLog loaded = (RigLog) ses2.load(RigLog.class, log.getId());
    assertNotNull(loaded);

    assertEquals(RigLog.OFFLINE, loaded.getOldState());
    assertEquals(RigLog.NOT_REGISTERED, loaded.getNewState());
    assertEquals("Un registered", loaded.getReason());
    assertEquals(Math.floor(now.getTime() / 1000), Math.floor(loaded.getTimeStamp().getTime() / 1000));
    assertEquals(rig.getId().longValue(), loaded.getRig().getId().longValue());

    ses.beginTransaction();
    ses.delete(log);
    ses.delete(rig);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();
    ses2.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#findLogs(au.edu.uts.eng.remotelabs.schedserver.dataaccess.entities.Rig, java.util.Date, java.util.Date)}.
 *//*from  ww w  .j  av  a 2s  . c  o m*/
@Test
public void testFindLogsRigDateDate() {
    Date before = new Date(System.currentTimeMillis() - 1000000);
    Date now = new Date();
    Date after = new Date(System.currentTimeMillis() + 1000000);

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("rig_name_test");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(after);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOfflineReason("Tomorrows problem");
    ses.save(rig);

    RigLog online = new RigLog();
    online.setOldState(RigLog.NOT_REGISTERED);
    online.setNewState(RigLog.ONLINE);
    online.setReason("First registration");
    online.setTimeStamp(before);
    online.setRig(rig);
    ses.save(online);

    RigLog offline = new RigLog();
    offline.setOldState(RigLog.OFFLINE);
    offline.setNewState(RigLog.ONLINE);
    offline.setReason("Offline");
    offline.setTimeStamp(now);
    offline.setRig(rig);
    ses.save(offline);

    RigLog notreg = new RigLog();
    notreg.setOldState(RigLog.OFFLINE);
    notreg.setNewState(RigLog.NOT_REGISTERED);
    notreg.setReason("Not reg");
    notreg.setTimeStamp(after);
    notreg.setRig(rig);
    ses.save(notreg);
    ses.getTransaction().commit();

    List<RigLog> logs = this.dao.findLogs(rig, new Date(now.getTime() - 1000),
            new Date(after.getTime() + 1000));

    ses.beginTransaction();
    ses.delete(notreg);
    ses.delete(offline);
    ses.delete(online);
    ses.delete(rig);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();

    assertEquals(2, logs.size());
    assertEquals(offline.getId().longValue(), logs.get(0).getId().longValue());
    assertEquals(notreg.getId().longValue(), logs.get(1).getId().longValue());
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#findLogsOfState(au.edu.uts.eng.remotelabs.schedserver.dataaccess.entities.Rig, java.lang.String, java.util.Date, java.util.Date)}.
 *//*from w  w w  .  ja  v  a  2 s  . c o m*/
@Test
public void testFindLogsOfStateRigStringDateDate() {
    Date before = new Date(System.currentTimeMillis() - 1000000);
    Date now = new Date();
    Date after = new Date(System.currentTimeMillis() + 1000000);

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("rig_name_test");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(after);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOfflineReason("Tomorrows problem");
    ses.save(rig);

    RigLog online = new RigLog();
    online.setOldState(RigLog.NOT_REGISTERED);
    online.setNewState(RigLog.ONLINE);
    online.setReason("First registration");
    online.setTimeStamp(before);
    online.setRig(rig);
    ses.save(online);

    RigLog offline = new RigLog();
    offline.setOldState(RigLog.ONLINE);
    offline.setNewState(RigLog.OFFLINE);
    offline.setReason("Offline");
    offline.setTimeStamp(now);
    offline.setRig(rig);
    ses.save(offline);

    RigLog notreg = new RigLog();
    notreg.setOldState(RigLog.OFFLINE);
    notreg.setNewState(RigLog.NOT_REGISTERED);
    notreg.setReason("Not reg");
    notreg.setTimeStamp(after);
    notreg.setRig(rig);
    ses.save(notreg);
    ses.getTransaction().commit();

    List<RigLog> logs = this.dao.findLogsOfState(rig, RigLog.OFFLINE, new Date(now.getTime() - 1000),
            new Date(after.getTime() + 1000));

    ses.beginTransaction();
    ses.delete(notreg);
    ses.delete(offline);
    ses.delete(online);
    ses.delete(rig);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();

    assertEquals(1, logs.size());
    assertEquals(offline.getId().longValue(), logs.get(0).getId().longValue());
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#findLogs(au.edu.uts.eng.remotelabs.schedserver.dataaccess.entities.RigType, java.util.Date, java.util.Date)}.
 *//*from w w w . j av  a2 s . com*/
@Test
public void testFindLogsRigTypeDateDate() {
    Date before = new Date(System.currentTimeMillis() - 1000000);
    Date now = new Date();
    Date after = new Date(System.currentTimeMillis() + 1000000);

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();

    RigType type = new RigType();
    type.setName("log_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("log_name_test1");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(after);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOfflineReason("Tomorrows problem");
    ses.save(rig);

    Rig rig2 = new Rig();
    rig2.setName("log_name_test2");
    rig2.setRigType(type);
    rig2.setContactUrl("http://url");
    rig2.setRigCapabilities(caps);
    rig2.setLastUpdateTimestamp(after);
    rig2.setManaged(false);
    rig2.setMeta("iLabs");
    rig2.setOfflineReason("Tomorrows problem");
    ses.save(rig2);

    RigLog online = new RigLog();
    online.setOldState(RigLog.NOT_REGISTERED);
    online.setNewState(RigLog.ONLINE);
    online.setReason("First registration");
    online.setTimeStamp(before);
    online.setRig(rig);
    ses.save(online);

    RigLog offline = new RigLog();
    offline.setOldState(RigLog.ONLINE);
    offline.setNewState(RigLog.OFFLINE);
    offline.setReason("Offline");
    offline.setTimeStamp(now);
    offline.setRig(rig2);
    ses.save(offline);

    RigLog off2 = new RigLog();
    off2.setOldState(RigLog.ONLINE);
    off2.setNewState(RigLog.OFFLINE);
    off2.setReason("One Offline");
    off2.setTimeStamp(now);
    off2.setRig(rig);
    ses.save(off2);

    RigLog notreg = new RigLog();
    notreg.setOldState(RigLog.OFFLINE);
    notreg.setNewState(RigLog.NOT_REGISTERED);
    notreg.setReason("Not reg");
    notreg.setTimeStamp(after);
    notreg.setRig(rig2);
    ses.save(notreg);
    ses.getTransaction().commit();

    ses.refresh(type);
    System.out.println(type.getRigs().size());
    Map<Rig, List<RigLog>> logs = this.dao.findLogs(type, new Date(before.getTime() - 1000),
            new Date(after.getTime() + 1000));

    ses.beginTransaction();
    ses.delete(notreg);
    ses.delete(offline);
    ses.delete(off2);
    ses.delete(online);
    ses.delete(rig);
    ses.delete(rig2);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();

    assertEquals(2, logs.size());
    for (Entry<Rig, List<RigLog>> rlogs : logs.entrySet()) {
        assertEquals(2, rlogs.getValue().size());
        if (rig.getId().longValue() == rlogs.getKey().getId().longValue()) {
            assertEquals(online.getId().longValue(), rlogs.getValue().get(0).getId().longValue());
            assertEquals(off2.getId().longValue(), rlogs.getValue().get(1).getId().longValue());
            assertEquals("First registration", rlogs.getValue().get(0).getReason());
            assertEquals("One Offline", rlogs.getValue().get(1).getReason());
        } else if (rig2.getId().longValue() == rlogs.getKey().getId().longValue()) {
            assertEquals(offline.getId().longValue(), rlogs.getValue().get(0).getId().longValue());
            assertEquals(notreg.getId().longValue(), rlogs.getValue().get(1).getId().longValue());
            assertEquals("Offline", rlogs.getValue().get(0).getReason());
            assertEquals("Not reg", rlogs.getValue().get(1).getReason());
        } else {
            fail("Impossible rig.");
        }
    }
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#findLogsOfState(au.edu.uts.eng.remotelabs.schedserver.dataaccess.entities.RigType, java.lang.String, java.util.Date, java.util.Date)}.
 *///from   w ww.  ja  v a2 s  .  com
@Test
public void testFindLogsOfStateRigTypeStringDateDate() {
    Date before = new Date(System.currentTimeMillis() - 1000000);
    Date now = new Date();
    Date after = new Date(System.currentTimeMillis() + 1000000);

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();

    RigType type = new RigType();
    type.setName("log_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("log_name_test1");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(after);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOfflineReason("Tomorrows problem");
    ses.save(rig);

    Rig rig2 = new Rig();
    rig2.setName("log_name_test2");
    rig2.setRigType(type);
    rig2.setContactUrl("http://url");
    rig2.setRigCapabilities(caps);
    rig2.setLastUpdateTimestamp(after);
    rig2.setManaged(false);
    rig2.setMeta("iLabs");
    rig2.setOfflineReason("Tomorrows problem");
    ses.save(rig2);

    RigLog online = new RigLog();
    online.setOldState(RigLog.NOT_REGISTERED);
    online.setNewState(RigLog.ONLINE);
    online.setReason("First registration");
    online.setTimeStamp(before);
    online.setRig(rig);
    ses.save(online);

    RigLog offline = new RigLog();
    offline.setOldState(RigLog.ONLINE);
    offline.setNewState(RigLog.OFFLINE);
    offline.setReason("Offline");
    offline.setTimeStamp(now);
    offline.setRig(rig2);
    ses.save(offline);

    RigLog off2 = new RigLog();
    off2.setOldState(RigLog.ONLINE);
    off2.setNewState(RigLog.OFFLINE);
    off2.setReason("One Offline");
    off2.setTimeStamp(now);
    off2.setRig(rig);
    ses.save(off2);

    RigLog notreg = new RigLog();
    notreg.setOldState(RigLog.OFFLINE);
    notreg.setNewState(RigLog.NOT_REGISTERED);
    notreg.setReason("Not reg");
    notreg.setTimeStamp(after);
    notreg.setRig(rig2);
    ses.save(notreg);
    ses.getTransaction().commit();

    ses.refresh(type);
    System.out.println(type.getRigs().size());
    Map<Rig, List<RigLog>> logs = this.dao.findLogsOfState(type, RigLog.OFFLINE,
            new Date(before.getTime() - 1000), new Date(after.getTime() + 1000));

    ses.beginTransaction();
    ses.delete(notreg);
    ses.delete(offline);
    ses.delete(off2);
    ses.delete(online);
    ses.delete(rig);
    ses.delete(rig2);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();

    assertEquals(2, logs.size());
    for (Entry<Rig, List<RigLog>> rlogs : logs.entrySet()) {
        assertEquals(1, rlogs.getValue().size());
        if (rig.getId().longValue() == rlogs.getKey().getId().longValue()) {
            assertEquals(off2.getId().longValue(), rlogs.getValue().get(0).getId().longValue());
            assertEquals("One Offline", rlogs.getValue().get(0).getReason());
        } else if (rig2.getId().longValue() == rlogs.getKey().getId().longValue()) {
            assertEquals(offline.getId().longValue(), rlogs.getValue().get(0).getId().longValue());
            assertEquals("Offline", rlogs.getValue().get(0).getReason());
        } else {
            fail("Impossible rig.");
        }
    }
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigOfflineScheduleDaoTester.java

License:Open Source License

@Test
public void testIsOffline() {
    Session ses = this.dao.getSession();
    ses.beginTransaction();//w  ww . ja  va  2s. com
    RigType rt = new RigType();
    rt.setName("offlineschedtype");
    ses.save(rt);
    RigCapabilities caps = new RigCapabilities("offline,schedul,test");
    ses.save(caps);
    Rig r = new Rig();
    r.setName("offlineschedtest");
    r.setLastUpdateTimestamp(new Date());
    r.setRigCapabilities(caps);
    r.setRigType(rt);
    ses.save(r);
    RigOfflineSchedule ro = new RigOfflineSchedule();
    ro.setActive(true);
    ro.setStartTime(new Date(System.currentTimeMillis() - 60000));
    ro.setEndTime(new Date(System.currentTimeMillis() + 60000));
    ro.setRig(r);
    ro.setReason("testcase");
    ses.save(ro);
    ses.getTransaction().commit();

    boolean off = this.dao.isOffline(r);

    ses.beginTransaction();
    ses.delete(ro);
    ses.delete(r);
    ses.delete(rt);
    ses.delete(caps);
    ses.getTransaction().commit();

    assertTrue(off);
}