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.permissions.intf.tests.PermissionsSOAPImplTester.java

License:Open Source License

/**
 * Test method for {@link PermissionsSOAPImpl#getPermissionsForUser(GetPermissionsForUser)}.
 *//* ww  w  .  ja v  a2 s .  c om*/
@Test
public void testGetPermissionsForUserNotLocked() {
    /**********************************************************************
     ** Test setup.                                                      **
     *********************************************************************/
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    User user = new User("test", "permUser", "USER");
    ses.save(user);
    UserClass uclass1 = new UserClass();
    uclass1.setName("permTestClass1");
    uclass1.setActive(true);
    uclass1.setBookable(false);
    uclass1.setQueuable(true);
    ses.save(uclass1);
    UserAssociation assoc1 = new UserAssociation(new UserAssociationId(user.getId(), uclass1.getId()), uclass1,
            user);
    ses.save(assoc1);
    RigType rigType1 = new RigType("interRigType1", 300, false);
    ses.save(rigType1);
    RigCapabilities rigCaps = new RigCapabilities("not,important");
    ses.save(rigCaps);
    Rig rig1 = new Rig(rigType1, rigCaps, "interRig1", "http://contact", new Date(), true, null, false, true,
            true);
    ses.save(rig1);
    ResourcePermission perm1 = new ResourcePermission();
    perm1.setUserClass(uclass1);
    perm1.setType("RIG");
    perm1.setSessionDuration(3600);
    perm1.setQueueActivityTimeout(300);
    perm1.setAllowedExtensions((short) 10);
    perm1.setSessionActivityTimeout(300);
    perm1.setExtensionDuration(300);
    perm1.setRig(rig1);
    perm1.setStartTime(new Date());
    perm1.setExpiryTime(new Date());
    ses.save(perm1);
    UserLock lock1 = new UserLock(user, perm1, false, "abc123");
    ses.save(lock1);
    ses.getTransaction().commit();

    /**********************************************************************
     ** Actual test.                                                     **
     *********************************************************************/
    GetPermissionsForUser request = new GetPermissionsForUser();
    UserIDType uid = new UserIDType();
    request.setGetPermissionsForUser(uid);
    uid.setNameNamespace(user.getNamespace(), user.getName());

    GetPermissionsForUserResponse resp = this.permissions.getPermissionsForUser(request);
    assertNotNull(resp);

    /**********************************************************************
     ** Test cleanup.                                                    **
     *********************************************************************/
    ses.beginTransaction();
    ses.delete(lock1);
    ses.delete(perm1);
    ses.delete(rig1);
    ses.delete(rigCaps);
    ses.delete(rigType1);
    ses.delete(assoc1);
    ses.delete(uclass1);
    ses.delete(user);
    ses.getTransaction().commit();

    PermissionWithLockListType permListType = resp.getGetPermissionsForUserResponse();
    assertNotNull(permListType);

    PermissionWithLockType permList[] = permListType.getPermission();
    assertNotNull(permList);
    assertEquals(1, permList.length);

    PermissionWithLockType perm = permList[0];
    assertNotNull(perm);
    assertFalse(perm.getIsLocked());

    PermissionType p = perm.getPermission();
    assertNotNull(p);
    assertFalse(p.canBook());
    assertTrue(p.canQueue());
    assertEquals(perm1.getAllowedExtensions(), p.getAllowedExtensions());
    assertEquals(perm1.getExtensionDuration(), p.getExtensionDuration());
    assertEquals(perm1.getQueueActivityTimeout(), p.getQueueActivityTmOut());
    assertEquals(perm1.getSessionActivityTimeout(), p.getSessionActivityTmOut());
    assertEquals(perm1.getSessionDuration(), p.getSessionDuration());
    assertEquals("RIG", p.getResourceClass().getValue());

    ResourceIDType res = p.getResource();
    assertNotNull(res);
    assertEquals(res.getResourceName(), "interRig1");
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsTester.java

License:Open Source License

/**
 * Test method for {@link Permissions#deleteUserAssociation(DeleteUserAssociation)}.
 *//*from   ww  w.  ja va2  s .  c  om*/
@Test
public void testDeleteUserAssociation() throws Exception {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    User user = new User("tuser", "ns", "USER");
    ses.save(user);
    UserClass cls = new UserClass();
    cls.setName("tclass");
    ses.save(cls);
    UserAssociation ass = new UserAssociation();
    ass.setId(new UserAssociationId(user.getId(), cls.getId()));
    ass.setUser(user);
    ass.setUserClass(cls);
    ses.save(ass);
    ses.getTransaction().commit();

    DeleteUserAssociation req = new DeleteUserAssociation();
    UserAssociationType assocType = new UserAssociationType();
    req.setDeleteUserAssociation(assocType);
    assocType.setRequestorQName("TESTNS:mdiponio");
    UserIDType uid = new UserIDType();
    uid.setUserQName("ns:tuser");
    assocType.setUser(uid);
    UserClassIDType cid = new UserClassIDType();
    cid.setUserClassName("tclass");
    assocType.setUserClass(cid);

    DeleteUserAssociationResponse resp = this.permissions.deleteUserAssociation(req);
    assertNotNull(resp);

    OperationResponseType op = resp.getDeleteUserAssociationResponse();
    assertNotNull(op);
    assertTrue(op.getSuccessful());
    assertEquals(0, op.getFailureCode());
    assertNull(op.getFailureReason());

    ses.beginTransaction();
    ses.delete(user);
    ses.delete(cls);
    ses.getTransaction().commit();

    OMElement ele = resp.getOMElement(DeleteUserAssociationResponse.MY_QNAME, OMAbstractFactory.getOMFactory());
    assertNotNull(ele);
    String xml = ele.toStringWithConsume();
    assertNotNull(xml);
    assertTrue(xml.contains("<successful>true</successful>"));
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsTester.java

License:Open Source License

/**
 * Test method for {@link Permissions#deleteUserAssociation(DeleteUserAssociation)}.
 *///  ww  w. ja va 2  s.c  om
@Test
public void testDeleteUserAssociationID() throws Exception {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    User user = new User("tuser", "ns", "USER");
    ses.save(user);
    UserClass cls = new UserClass();
    cls.setName("tclass");
    ses.save(cls);
    UserAssociation ass = new UserAssociation();
    ass.setId(new UserAssociationId(user.getId(), cls.getId()));
    ass.setUser(user);
    ass.setUserClass(cls);
    ses.save(ass);
    ses.getTransaction().commit();

    DeleteUserAssociation req = new DeleteUserAssociation();
    UserAssociationType assocType = new UserAssociationType();
    req.setDeleteUserAssociation(assocType);
    assocType.setRequestorQName("TESTNS:mdiponio");
    UserIDType uid = new UserIDType();
    uid.setUserID(String.valueOf(user.getId()));
    assocType.setUser(uid);
    UserClassIDType cid = new UserClassIDType();
    cid.setUserClassID(cls.getId().intValue());
    assocType.setUserClass(cid);

    DeleteUserAssociationResponse resp = this.permissions.deleteUserAssociation(req);
    assertNotNull(resp);

    OperationResponseType op = resp.getDeleteUserAssociationResponse();
    assertNotNull(op);
    assertTrue(op.getSuccessful());
    assertEquals(0, op.getFailureCode());
    assertNull(op.getFailureReason());

    ses.beginTransaction();
    ses.delete(user);
    ses.delete(cls);
    ses.getTransaction().commit();

    OMElement ele = resp.getOMElement(DeleteUserAssociationResponse.MY_QNAME, OMAbstractFactory.getOMFactory());
    assertNotNull(ele);
    String xml = ele.toStringWithConsume();
    assertNotNull(xml);
    assertTrue(xml.contains("<successful>true</successful>"));
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.tests.RigManagementTester.java

License:Open Source License

@Test
public void testGetTypes() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();//w  w  w.  ja  va  2 s .  c om
    RigType rt = new RigType();
    rt.setName("rigmanagetest");
    ses.save(rt);
    ses.getTransaction().commit();

    GetTypesResponse response = this.service.getTypes(new GetTypes());

    ses.beginTransaction();
    ses.delete(rt);
    ses.getTransaction().commit();

    assertNotNull(response);
    RigTypesType types = response.getGetTypesResponse();
    assertNotNull(response);

    RigTypeType ids[] = types.getTypes();
    assertNotNull(ids);
    assertTrue(ids.length > 0);

    boolean typeFound = false;
    for (RigTypeType ty : ids) {
        assertNotNull(ty.getName());
        if (ty.getName().equals(rt.getName())) {
            typeFound = true;
            assertFalse(ty.getIsOnline());
            assertFalse(ty.getIsAlarmed());
            assertEquals(0, ty.getNumberRigs());
        }
    }

    assertTrue(typeFound);
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.tests.RigManagementTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.RigManagement#getTypeStatus(au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.types.GetTypeStatus)}.
 *//*from w ww .j a  va2  s.c om*/
@Test
public void testGetTypeStatus() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType rt = new RigType();
    rt.setName("rigmanagetest");
    ses.save(rt);
    RigType rt2 = new RigType();
    rt2.setName("rigmanagetest2");
    ses.save(rt2);
    RigCapabilities caps = new RigCapabilities("rig,management,test");
    ses.save(caps);
    Rig r = new Rig();
    r.setOnline(false);
    r.setOfflineReason("test reason");
    r.setName("rigmanagementrig");
    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);
    Rig r2 = new Rig();
    r2.setName("rigmanagementrig2");
    r2.setLastUpdateTimestamp(new Date());
    r2.setRigCapabilities(caps);
    r2.setRigType(rt);
    r2.setActive(true);
    r2.setOnline(true);
    r2.setContactUrl("http://remotelabs/rig");
    ses.save(r2);
    Rig r3 = new Rig();
    r3.setName("rigmanagementrig3");
    r3.setLastUpdateTimestamp(new Date());
    r3.setRigCapabilities(caps);
    r3.setRigType(rt2);
    r3.setActive(true);
    r3.setOnline(true);
    r3.setContactUrl("http://remotelabs/rig");
    ses.save(r3);
    ses.getTransaction().commit();

    ses.refresh(rt);
    ses.refresh(r);
    ses.refresh(ro);

    GetTypeStatus request = new GetTypeStatus();
    RigTypeIDType id = new RigTypeIDType();
    id.setName(rt.getName());
    request.setGetTypeStatus(id);

    GetTypeStatusResponse response = this.service.getTypeStatus(request);

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

    assertNotNull(response);

    RigTypeType rigType = response.getGetTypeStatusResponse();
    assertEquals(rt.getName(), rigType.getName());
    assertNotNull(rigType);
    assertTrue(rigType.getIsOnline());
    assertFalse(rigType.getIsAlarmed());
    assertEquals(2, rigType.getNumberRigs());

    au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.types.RigType rigs[] = rigType.getRigs();
    assertNotNull(rigs);
    assertEquals(rigs.length, 2);

    for (au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.types.RigType rig : rigs) {
        assertNotNull(r.getName());
        RigTypeIDType rtid = rig.getRigType();
        assertNotNull(rtid);
        assertEquals(rt.getName(), rtid.getName());
        assertEquals(caps.getCapabilities(), rig.getCapabilities());
        assertNull(rig.getLastLogs());
        assertNull(rig.getOfflinePeriods());

        if (rig.getName().equals(r.getName())) {
            assertFalse(rig.getIsRegistered());
            assertFalse(rig.getIsOnline());
            assertFalse(rig.getIsInSession());
            assertFalse(rig.getIsAlarmed());
            assertNull(rig.getSessionUser());
            assertEquals(r.getOfflineReason(), rig.getOfflineReason());
            assertNull(rig.getContactURL());

        } else if (rig.getName().equals(r2.getName())) {
            assertTrue(rig.getIsRegistered());
            assertTrue(rig.getIsOnline());
            assertFalse(rig.getIsInSession());
            assertFalse(rig.getIsAlarmed());
            assertNull(rig.getSessionUser());
            assertNull(rig.getOfflineReason());
            assertEquals(r2.getContactUrl(), rig.getContactURL());
        } else {
            fail("Unknown rig name");
        }
    }
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.tests.RigManagementTester.java

License:Open Source License

@Test
public void testGetRig() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();// ww  w. j  a va2s .  com
    RigType rt = new RigType();
    rt.setName("rigmanagetest");
    ses.save(rt);
    RigCapabilities caps = new RigCapabilities("rig,management,test");
    ses.save(caps);
    Rig r = new Rig();
    r.setActive(true);
    r.setOnline(true);
    r.setContactUrl("foo://bar");
    r.setName("rigmanagementrig");
    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);
    RigLog l1 = new RigLog();
    l1.setTimeStamp(new Date());
    l1.setReason("register received");
    l1.setRig(r);
    l1.setOldState(RigLog.NOT_REGISTERED);
    l1.setNewState(RigLog.OFFLINE);
    ses.save(l1);
    RigLog l2 = new RigLog();
    l2.setTimeStamp(new Date(System.currentTimeMillis() + 100000));
    l2.setReason("online received");
    l2.setRig(r);
    l2.setOldState(RigLog.OFFLINE);
    l2.setNewState(RigLog.ONLINE);
    ses.save(l2);
    ses.getTransaction().commit();

    ses.refresh(rt);
    ses.refresh(r);
    ses.refresh(ro);

    GetRig request = new GetRig();
    RigIDType rid = new RigIDType();
    rid.setName(r.getName());
    request.setGetRig(rid);

    GetRigResponse response = this.service.getRig(request);

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

    assertNotNull(response);
    au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.types.RigType rig = response.getGetRigResponse();
    assertNotNull(rig);

    assertEquals(r.getName(), rig.getName());
    assertEquals(caps.getCapabilities(), rig.getCapabilities());

    RigTypeIDType tid = rig.getRigType();
    assertNotNull(tid);
    assertEquals(rt.getName(), tid.getName());

    assertTrue(rig.getIsOnline());
    assertTrue(rig.getIsRegistered());
    assertFalse(rig.getIsInSession());
    assertNull(rig.getSessionUser());
    assertFalse(rig.getIsAlarmed());
    assertNull(rig.getOfflineReason());
    assertEquals(r.getContactUrl(), rig.getContactURL());

    RigLogType logs[] = rig.getLastLogs();
    assertNotNull(logs);
    assertEquals(2, logs.length);

    RigLogType log = logs[1];
    assertEquals(l1.getReason(), log.getReason());
    assertEquals(log.getOldState(), RigStateType.NOT_REGISTERED);
    assertEquals(log.getNewState(), RigStateType.OFFLINE);

    log = logs[0];
    assertEquals(l2.getReason(), log.getReason());
    assertEquals(log.getOldState(), RigStateType.OFFLINE);
    assertEquals(log.getNewState(), RigStateType.ONLINE);

    OfflinePeriodType offs[] = rig.getOfflinePeriods();
    assertNotNull(offs);
    assertEquals(1, offs.length);

    OfflinePeriodType off = offs[0];
    assertEquals(ro.getId().intValue(), off.getId());
    assertEquals(ro.getReason(), off.getReason());
    assertEquals(ro.getStartTime().getTime(), off.getStart().getTimeInMillis());
    assertEquals(ro.getEndTime().getTime(), off.getEnd().getTimeInMillis());
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.tests.RigManagementTester.java

License:Open Source License

@Test
public void testGetRigAlarmed() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();/*from  w ww .ja  v a  2  s. com*/
    RigType rt = new RigType();
    rt.setName("rigmanagetest");
    ses.save(rt);
    RigType rt2 = new RigType();
    rt2.setName("rigmanagetest2");
    ses.save(rt2);
    RigCapabilities caps = new RigCapabilities("rig,management,test");
    ses.save(caps);
    Rig r = new Rig();
    r.setActive(true);
    r.setOnline(false);
    r.setContactUrl("foo://bar");
    r.setOfflineReason("smothing broke");
    r.setName("rigmanagementrig");
    r.setLastUpdateTimestamp(new Date());
    r.setRigCapabilities(caps);
    r.setRigType(rt);
    ses.save(r);
    ses.getTransaction().commit();

    ses.refresh(rt);
    ses.refresh(r);

    GetRig request = new GetRig();
    RigIDType rid = new RigIDType();
    rid.setName(r.getName());
    request.setGetRig(rid);

    GetRigResponse response = this.service.getRig(request);

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

    assertNotNull(response);
    au.edu.uts.eng.remotelabs.schedserver.rigmanagement.intf.types.RigType rig = response.getGetRigResponse();
    assertNotNull(rig);

    assertEquals(r.getName(), rig.getName());
    assertEquals(caps.getCapabilities(), rig.getCapabilities());

    RigTypeIDType tid = rig.getRigType();
    assertNotNull(tid);
    assertEquals(rt.getName(), tid.getName());

    assertFalse(rig.getIsOnline());
    assertTrue(rig.getIsRegistered());
    assertFalse(rig.getIsInSession());
    assertNull(rig.getSessionUser());
    assertTrue(rig.getIsAlarmed());
    assertEquals(r.getOfflineReason(), rig.getOfflineReason());
    assertEquals(r.getContactUrl(), rig.getContactURL());
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RegisterLocalRigTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.RegisterLocalRig#registerRig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
 *//*from w w  w  . j a  v  a2  s  . c  om*/
@Test
public void testAddRigToSchedServerNewRigExistingTypeNewCaps() {
    Session ses = DataAccessActivator.getNewSession();

    String name = "register1";
    String type = "registertest";
    String caps = "a,b,c,d,e,f";
    String contactUrl = "http://lbremote1.eng.uts.edu.au:7070/services/RigClientService";

    RigType existType = new RigType(type, 180, false);
    ses.beginTransaction();
    ses.save(existType);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existType);

    assertTrue(this.register.registerRig(name, type, caps, contactUrl));
    assertNull(this.register.getFailedReason());

    Rig rig = this.register.getRegisteredRig();
    assertNotNull(rig);

    assertEquals(name, rig.getName());
    assertEquals(type, rig.getRigType().getName());
    assertEquals(contactUrl, rig.getContactUrl());
    assertNotNull(rig.getRigCapabilities());

    /* Default values. */
    assertTrue(rig.isActive());
    assertFalse(rig.isInSession());
    assertFalse(rig.isOnline());
    assertTrue(rig.isManaged());
    assertNotNull(rig.getOfflineReason());
    assertNull(rig.getMeta());
    assertNull(rig.getSession());

    assertTrue(new Date(System.currentTimeMillis()).after(rig.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rig.getLastUpdateTimestamp()));

    /* Test caps record was created and added. */
    RigCapabilities capsRec = new RigCapabilitiesDao(ses).findCapabilites(caps);
    assertNotNull(capsRec);
    assertEquals(caps, capsRec.getCapabilities());

    /* Test type record was created and added. */
    RigType typeRec = new RigTypeDao(ses).findByName(type);
    assertNotNull(typeRec);
    assertEquals(type, typeRec.getName());
    assertEquals(180, typeRec.getLogoffGraceDuration());
    assertFalse(typeRec.isCodeAssignable());

    /* Test rig record was created and added. */
    Rig rigRec = new RigDao(ses).findByName(name);
    assertNotNull(rigRec);
    assertNotNull(rigRec.getId());
    assertEquals(name, rigRec.getName());
    assertEquals(type, rigRec.getRigType().getName());
    assertTrue(rigRec.isActive());
    assertEquals(contactUrl, rigRec.getContactUrl());
    assertFalse(rigRec.isInSession());
    assertNotNull(rigRec.getLastUpdateTimestamp());
    assertTrue(rigRec.isManaged());
    assertNull(rigRec.getMeta());
    assertNotNull(rigRec.getOfflineReason());
    assertFalse(rigRec.isOnline());
    assertEquals(caps, rigRec.getRigCapabilities().getCapabilities());

    assertTrue(new Date(System.currentTimeMillis()).after(rigRec.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rigRec.getLastUpdateTimestamp()));

    /* Cleanup. */
    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rig.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

    ses.beginTransaction();
    ses.delete(rigRec);
    ses.delete(capsRec);
    ses.delete(typeRec);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RegisterLocalRigTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.RegisterLocalRig#registerRig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
 *//*from   w ww. j  av  a 2 s.c o  m*/
@Test
public void testAddRigToSchedServerNewRigExistingTypeExistingCaps() {
    Session ses = DataAccessActivator.getNewSession();

    String name = "register1";
    String type = "registertest";
    String caps = "a,b,c,d,e,f";
    String contactUrl = "http://lbremote1.eng.uts.edu.au:7070/services/RigClientService";

    RigType existType = new RigType(type, 180, false);
    RigCapabilities existCaps = new RigCapabilities(caps);
    ses.beginTransaction();
    ses.save(existType);
    ses.save(existCaps);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existType);

    assertTrue(this.register.registerRig(name, type, caps, contactUrl));
    assertNull(this.register.getFailedReason());

    Rig rig = this.register.getRegisteredRig();
    assertNotNull(rig);

    assertEquals(name, rig.getName());
    assertEquals(type, rig.getRigType().getName());
    assertEquals(contactUrl, rig.getContactUrl());
    assertNotNull(rig.getRigCapabilities());

    /* Default values. */
    assertTrue(rig.isActive());
    assertFalse(rig.isInSession());
    assertFalse(rig.isOnline());
    assertTrue(rig.isManaged());
    assertNotNull(rig.getOfflineReason());
    assertNull(rig.getMeta());
    assertNull(rig.getSession());

    assertTrue(new Date(System.currentTimeMillis()).after(rig.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rig.getLastUpdateTimestamp()));

    /* Test caps record was created and added. */
    RigCapabilities capsRec = new RigCapabilitiesDao(ses).findCapabilites(caps);
    assertNotNull(capsRec);
    assertEquals(caps, capsRec.getCapabilities());

    /* Test type record was created and added. */
    RigType typeRec = new RigTypeDao(ses).findByName(type);
    assertNotNull(typeRec);
    assertEquals(type, typeRec.getName());
    assertEquals(180, typeRec.getLogoffGraceDuration());
    assertFalse(typeRec.isCodeAssignable());

    /* Test rig record was created and added. */
    Rig rigRec = new RigDao(ses).findByName(name);
    assertNotNull(rigRec);
    assertNotNull(rigRec.getId());
    assertEquals(name, rigRec.getName());
    assertEquals(type, rigRec.getRigType().getName());
    assertTrue(rigRec.isActive());
    assertEquals(contactUrl, rigRec.getContactUrl());
    assertFalse(rigRec.isInSession());
    assertNotNull(rigRec.getLastUpdateTimestamp());
    assertTrue(rigRec.isManaged());
    assertNull(rigRec.getMeta());
    assertNotNull(rigRec.getOfflineReason());
    assertFalse(rigRec.isOnline());
    assertEquals(caps, rigRec.getRigCapabilities().getCapabilities());

    assertTrue(new Date(System.currentTimeMillis()).after(rigRec.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rigRec.getLastUpdateTimestamp()));

    /* Cleanup. */
    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rig.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

    ses.beginTransaction();
    ses.delete(rigRec);
    ses.delete(capsRec);
    ses.delete(typeRec);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RegisterLocalRigTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.RegisterLocalRig#registerRig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
 *//*  w  w w.  j av a  2s. c o  m*/
@Test
public void testAddRigToSchedServerInactiveRig() {
    Session ses = DataAccessActivator.getNewSession();

    String name = "register1";
    String type = "registertest";
    String caps = "a,b,c,d,e,f";
    String contactUrl = "http://lbremote1.eng.uts.edu.au:7070/services/RigClientService";

    RigType existType = new RigType(type, 180, false);
    RigCapabilities existCaps = new RigCapabilities(caps);
    ses.beginTransaction();
    ses.save(existType);
    ses.save(existCaps);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existType);
    ses.evict(existCaps);

    Rig existRig = new Rig(existType, existCaps, name, contactUrl, new Date(), false, "Broken.", false, true,
            false);
    ses.beginTransaction();
    ses.save(existRig);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existRig);

    assertTrue(this.register.registerRig(name, type, caps, contactUrl));
    assertNull(this.register.getFailedReason());

    Rig rig = this.register.getRegisteredRig();
    assertNotNull(rig);

    assertEquals(name, rig.getName());
    assertEquals(type, rig.getRigType().getName());
    assertEquals(contactUrl, rig.getContactUrl());
    assertNotNull(rig.getRigCapabilities());

    /* Default values. */
    assertTrue(rig.isActive());
    assertFalse(rig.isInSession());
    assertFalse(rig.isOnline());
    assertTrue(rig.isManaged());
    assertNotNull(rig.getOfflineReason());
    assertNull(rig.getMeta());
    assertNull(rig.getSession());

    assertTrue(new Date(System.currentTimeMillis()).after(rig.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rig.getLastUpdateTimestamp()));

    /* Test caps record was created and added. */
    RigCapabilities capsRec = new RigCapabilitiesDao(ses).findCapabilites(caps);
    assertNotNull(capsRec);
    assertEquals(caps, capsRec.getCapabilities());

    /* Test type record was created and added. */
    RigType typeRec = new RigTypeDao(ses).findByName(type);
    assertNotNull(typeRec);
    assertEquals(type, typeRec.getName());
    assertEquals(180, typeRec.getLogoffGraceDuration());
    assertFalse(typeRec.isCodeAssignable());

    /* Test rig record was created and added. */
    Rig rigRec = new RigDao(ses).findByName(name);
    assertNotNull(rigRec);
    assertNotNull(rigRec.getId());
    assertEquals(name, rigRec.getName());
    assertEquals(type, rigRec.getRigType().getName());
    assertTrue(rigRec.isActive());
    assertEquals(contactUrl, rigRec.getContactUrl());
    assertFalse(rigRec.isInSession());
    assertNotNull(rigRec.getLastUpdateTimestamp());
    assertTrue(rigRec.isManaged());
    assertNull(rigRec.getMeta());
    assertNotNull(rigRec.getOfflineReason());
    assertFalse(rigRec.isOnline());
    assertEquals(caps, rigRec.getRigCapabilities().getCapabilities());

    assertTrue(new Date(System.currentTimeMillis()).after(rigRec.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rigRec.getLastUpdateTimestamp()));

    /* Cleanup. */
    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rig.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

    ses.beginTransaction();
    ses.delete(rigRec);
    ses.delete(capsRec);
    ses.delete(typeRec);
    ses.getTransaction().commit();
}