TransactionServiceImpl implements TransactionService { protected <T extends AbstractRepositoryDomain> void populateCache(TransactionElement<T> element, OpState currentOpState) { fillCacheIfNeeded(element); int nextOpIndex = getNextOpsIndex(element.getTxId()); TransactionStoreKey key = factory.createTransactionStoreKey(); key.setOpTimestamp(System.currentTimeMillis()); key.setTransactionId(element.getTxId()); key.setObjectType(element.getDto().getClass()); key.setObjectId((String) element.getDto().getId()); Pair<TransactionStoreKey, TransactionStoreValue> pair = memCache.getValueForIsolatedTransaction(key); if (pair == null) { TransactionStoreValue value = factory.createTransactionStoreValue(); value.setCurrentState(element.getDto()); value.setOpSequence(nextOpIndex); value.setOpState(currentOpState); if (!OpState.SAVE.equals(currentOpState)) { value.setOriginalState(element.getReadDao().getById(key.getObjectId())); } memCache.storeTransactionValue(key, value); } else { TransactionStoreValue value = pair.getValue(); value.setCurrentState(element.getDto()); value.setOpState(getActualStateAfterTransition(value.getOpState(), currentOpState)); } } @Inject  TransactionServiceImpl(TransactionInMemoryCache memCache,
                                TransactionFactory factory); String getNextTransactionId(); void commit(String txId); void rollback(String txId); void save(TransactionElement<T> element); void update(TransactionElement<T> element); void delete(TransactionElement<T> element);  }
@Test public void testPopulateTransactionCacheWithNonExistingValueForSave() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; CommonReadDao<DemoDomain, String> readDao = mockery.mock(CommonReadDao.class); CommonWriteDao<DemoDomain> writeDao = mockery.mock(CommonWriteDao.class); final TransactionElement<DemoDomain> element = getTransactionElement(readDao, writeDao); mockery.checking(new Expectations() { { Sequence seq = mockery.sequence("serial"); exactly(1).of(mockFactory).createTransactionStoreKey(); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); will(returnValue(key)); inSequence(seq); exactly(1).of(key).setOpTimestamp(with(any(Long.class))); inSequence(seq); exactly(1).of(key).setTransactionId("1"); inSequence(seq); exactly(1).of(key).setObjectType(DemoDomain.class); inSequence(seq); exactly(1).of(key).setObjectId("1"); inSequence(seq); exactly(1).of(mockMemCache).getValueForIsolatedTransaction(with(same(key))); will(returnValue(null)); inSequence(seq); exactly(1).of(mockFactory).createTransactionStoreValue(); TransactionStoreValue val = mockery.mock(TransactionStoreValue.class); will(returnValue(val)); inSequence(seq); exactly(1).of(val).setCurrentState(with(same(element.getDto()))); inSequence(seq); exactly(1).of(val).setOpSequence(with(equal(0))); inSequence(seq); exactly(1).of(val).setOpState(with(equal(OpState.SAVE))); inSequence(seq); exactly(1).of(mockMemCache).storeTransactionValue(with(same(key)), with(same(val))); inSequence(seq); } }); serviceImpl.populateCache(element, OpState.SAVE); mockery.assertIsSatisfied(); }
@Test public void testPopulateTransactionCacheWithNonExistingValueForNotSave() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; final CommonReadDao<DemoDomain, String> readDao = mockery.mock(CommonReadDao.class); final CommonWriteDao<DemoDomain> writeDao = mockery.mock(CommonWriteDao.class); final TransactionElement<DemoDomain> element = getTransactionElement(readDao, writeDao); mockery.checking(new Expectations() { { Sequence seq = mockery.sequence("serial"); exactly(1).of(mockFactory).createTransactionStoreKey(); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); will(returnValue(key)); inSequence(seq); exactly(1).of(key).setOpTimestamp(with(any(Long.class))); inSequence(seq); exactly(1).of(key).setTransactionId("1"); inSequence(seq); exactly(1).of(key).setObjectType(DemoDomain.class); inSequence(seq); exactly(1).of(key).setObjectId("1"); inSequence(seq); exactly(1).of(mockMemCache).getValueForIsolatedTransaction(with(same(key))); will(returnValue(null)); inSequence(seq); exactly(1).of(mockFactory).createTransactionStoreValue(); TransactionStoreValue val = mockery.mock(TransactionStoreValue.class); will(returnValue(val)); inSequence(seq); exactly(1).of(val).setCurrentState(with(same(element.getDto()))); inSequence(seq); exactly(1).of(val).setOpSequence(with(equal(0))); inSequence(seq); exactly(1).of(val).setOpState(with(equal(OpState.UPDATE))); inSequence(seq); exactly(1).of(key).getObjectId(); will(returnValue("1")); DemoDomain orgDemo = new DemoDomain(); exactly(1).of(readDao).getById("1"); will(returnValue(orgDemo)); inSequence(seq); exactly(1).of(val).setOriginalState(with(same(orgDemo))); inSequence(seq); exactly(1).of(mockMemCache).storeTransactionValue(with(same(key)), with(same(val))); inSequence(seq); } }); serviceImpl.populateCache(element, OpState.UPDATE); mockery.assertIsSatisfied(); }
@Test public void testPopulateTransactionCacheWithExistingValue() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; CommonReadDao<DemoDomain, String> readDao = mockery.mock(CommonReadDao.class); CommonWriteDao<DemoDomain> writeDao = mockery.mock(CommonWriteDao.class); final TransactionElement<DemoDomain> element = getTransactionElement(readDao, writeDao); mockery.checking(new Expectations() { { Sequence seq = mockery.sequence("serial"); exactly(1).of(mockFactory).createTransactionStoreKey(); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); will(returnValue(key)); inSequence(seq); exactly(1).of(key).setOpTimestamp(with(any(Long.class))); inSequence(seq); exactly(1).of(key).setTransactionId("1"); inSequence(seq); exactly(1).of(key).setObjectType(DemoDomain.class); inSequence(seq); exactly(1).of(key).setObjectId("1"); inSequence(seq); TransactionStoreValue val = mockery.mock(TransactionStoreValue.class); Pair<TransactionStoreKey, TransactionStoreValue> pair = new Pair<TransactionStoreKey, TransactionStoreValue>( key, val); exactly(1).of(mockMemCache).getValueForIsolatedTransaction(with(same(key))); will(returnValue(pair)); inSequence(seq); exactly(1).of(val).setCurrentState(with(same(element.getDto()))); inSequence(seq); exactly(1).of(val).getOpState(); will(returnValue(OpState.SAVE)); inSequence(seq); exactly(1).of(val).setOpState(with(equal(OpState.SAVE))); inSequence(seq); } }); serviceImpl.populateCache(element, OpState.UPDATE); mockery.assertIsSatisfied(); }