@Test public void checkLockOwner() throws Exception { ledger.enableCache(true); StateRecord existing = ledger.findOrCreate(HashId.createRandom()); existing.approve(); StateRecord r = ledger.findOrCreate(HashId.createRandom()); StateRecord r1 = r.lockToRevoke(existing.getId()); existing.reload(); r.reload(); assertSameRecords(existing, r1); assertEquals(ItemState.LOCKED, existing.getState()); assertEquals(r.getRecordId(), existing.getLockedByRecordId()); StateRecord currentOwner = ledger.getLockOwnerOf(existing); System.out.println("existing: " + existing.getId()); System.out.println("locker: " + r.getId()); System.out.println("locked: " + r1.getId()); System.out.println("currentOwner: " + currentOwner.getId()); assertSameRecords(r, currentOwner); }
}); assertEquals(5, x); r1.reload(); StateRecord r3 = ledger.getRecord(r1.getId()); assertEquals(ItemState.APPROVED, r1.getState()); assertEquals(ItemState.APPROVED, r3.getState()); r2.reload(); assertEquals(ItemState.DECLINED, r2.getState()); Object y = ledger.transaction(() -> { }); assert (y == null); r1.reload(); assertEquals(ItemState.APPROVED, r1.getState()); r2.reload(); assertEquals(ItemState.DECLINED, r2.getState());
@Test public void lockForRevoking() throws Exception { ledger.enableCache(true); StateRecord existing = ledger.findOrCreate(HashId.createRandom()); existing.approve(); StateRecord existing2 = ledger.findOrCreate(HashId.createRandom()); existing2.approve(); StateRecord r = ledger.findOrCreate(HashId.createRandom()); StateRecord r1 = r.lockToRevoke(existing.getId()); existing.reload(); r.reload(); assertSameRecords(existing, r1); assertEquals(ItemState.LOCKED, existing.getState()); assertEquals(r.getRecordId(), existing.getLockedByRecordId()); StateRecord r2 = r.lockToRevoke(existing.getId()); existing.reload(); r.reload(); assertSameRecords(existing, r1); assertSameRecords(existing, r2); assertSame(r1, r2); assertEquals(ItemState.LOCKED, existing.getState()); assertEquals(r.getRecordId(), existing.getLockedByRecordId()); StateRecord r3 = r.lockToRevoke(existing2.getId()); existing2.reload(); assertSameRecords(existing2, r3); assertEquals(ItemState.LOCKED, existing2.getState()); assertEquals(r.getRecordId(), existing2.getLockedByRecordId()); }
}); assertEquals(5, x); r1.reload(); StateRecord r3 = ledger.getRecord(r1.getId()); assertEquals(ItemState.APPROVED, r1.getState()); assertEquals(ItemState.APPROVED, r3.getState()); r2.reload(); assertEquals(ItemState.DECLINED, r2.getState()); Object y = ledger.transaction(() -> { }); assert (y == null); r1.reload(); assertEquals(ItemState.APPROVED, r1.getState()); r2.reload(); assertEquals(ItemState.DECLINED, r2.getState());
@Test public void lockForRevoking() throws Exception { ledger.enableCache(true); StateRecord existing = ledger.findOrCreate(HashId.createRandom()); existing.approve(); StateRecord existing2 = ledger.findOrCreate(HashId.createRandom()); existing2.approve(); StateRecord r = ledger.findOrCreate(HashId.createRandom()); StateRecord r1 = r.lockToRevoke(existing.getId()); existing.reload(); r.reload(); assertSameRecords(existing, r1); assertEquals(ItemState.LOCKED, existing.getState()); assertEquals(r.getRecordId(), existing.getLockedByRecordId()); // we lock again the same record, everything should be still ok: StateRecord r2 = r.lockToRevoke(existing.getId()); assertNotNull(r2); existing.reload(); r.reload(); assertSameRecords(existing, r1); assertSameRecords(existing, r2); assertSame(r1, r2); assertEquals(ItemState.LOCKED, existing.getState()); assertEquals(r.getRecordId(), existing.getLockedByRecordId()); StateRecord r3 = r.lockToRevoke(existing2.getId()); existing2.reload(); assertSameRecords(existing2, r3); assertEquals(ItemState.LOCKED, existing2.getState()); assertEquals(r.getRecordId(), existing2.getLockedByRecordId()); }
@Test public void approve() throws Exception { StateRecord r1 = ledger.findOrCreate(HashId.createRandom()); assertFalse(r1.isApproved()); r1.approve(); assertEquals(ItemState.APPROVED, r1.getState()); assert (r1.isApproved()); r1.reload(); assert (r1.isApproved()); assertThrows(IllegalStateException.class, () -> { r1.approve(); return null; }); }
@Test public void approve() throws Exception { StateRecord r1 = ledger.findOrCreate(HashId.createRandom()); assertFalse(r1.isApproved()); r1.approve(); assertEquals(ItemState.APPROVED, r1.getState()); assert (r1.isApproved()); r1.reload(); assert (r1.isApproved()); assertThrows(IllegalStateException.class, () -> { r1.approve(); return null; }); }
@Test public void revoke() throws Exception { StateRecord r1 = ledger.findOrCreate(HashId.createRandom()); assertFalse(r1.isApproved()); assertTrue(r1.isPending()); assertFalse(r1.isArchived()); r1.approve(); r1.reload(); assertTrue(r1.isApproved()); assertFalse(r1.isPending()); assertFalse(r1.isArchived()); r1.setState(ItemState.LOCKED); r1.revoke(); assertFalse(r1.isPending()); assertFalse(r1.isApproved()); assertTrue(r1.isArchived()); }
@Test public void createOutputLockRecord() throws Exception { ledger.enableCache(true); StateRecord owner = ledger.findOrCreate(HashId.createRandom()); StateRecord other = ledger.findOrCreate(HashId.createRandom()); HashId id = HashId.createRandom(); StateRecord r1 = owner.createOutputLockRecord(id); r1.reload(); assertEquals(id, r1.getId()); assertEquals(ItemState.LOCKED_FOR_CREATION, r1.getState()); assertEquals(owner.getRecordId(), r1.getLockedByRecordId()); StateRecord r2 = owner.createOutputLockRecord(id); assertSame(r2, r1); assertNull(owner.createOutputLockRecord(other.getId())); // And hacked low level operation must fail too assertNull(ledger.createOutputLockRecord(owner.getRecordId(), other.getId())); }
@Test public void createOutputLockRecord() throws Exception { ledger.enableCache(true); StateRecord owner = ledger.findOrCreate(HashId.createRandom()); StateRecord other = ledger.findOrCreate(HashId.createRandom()); HashId id = HashId.createRandom(); StateRecord r1 = owner.createOutputLockRecord(id); r1.reload(); assertEquals(id, r1.getId()); assertEquals(ItemState.LOCKED_FOR_CREATION, r1.getState()); assertEquals(owner.getRecordId(), r1.getLockedByRecordId()); StateRecord r2 = owner.createOutputLockRecord(id); assertSame(r2, r1); assertNull(owner.createOutputLockRecord(other.getId())); // And hacked low level operation must fail too assertNull(ledger.createOutputLockRecord(owner.getRecordId(), other.getId())); }
@Test public void revoke() throws Exception { StateRecord r1 = ledger.findOrCreate(HashId.createRandom()); assertFalse(r1.isApproved()); assertTrue(r1.isPending()); assertFalse(r1.isArchived()); r1.approve(); r1.reload(); assertTrue(r1.isApproved()); assertFalse(r1.isPending()); assertFalse(r1.isArchived()); r1.setState(ItemState.LOCKED); r1.revoke(); assertFalse(r1.isPending()); assertFalse(r1.isApproved()); assertTrue(r1.isArchived()); }