TransactionServiceImpl implements TransactionService { public <T extends AbstractRepositoryDomain> void save(TransactionElement<T> element) { String id = (String) element.getDto().getId(); if (id == null) { id = UUID.randomUUID().toString(); element.getDto().setId(id); } populateCache(element, OpState.SAVE); } @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 testSaveWithId() { final TransactionService service = injector.getInstance(TransactionService.class); 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() { { exactly(1).of(mockFactory).createTransactionStoreKey(); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); will(returnValue(key)); exactly(1).of(key).setOpTimestamp(with(any(Long.class))); exactly(1).of(key).setTransactionId("1"); exactly(1).of(key).setObjectType(DemoDomain.class); exactly(1).of(key).setObjectId("1"); 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)); exactly(1).of(val).setCurrentState(with(same(element.getDto()))); exactly(1).of(val).getOpState(); will(returnValue(null)); exactly(1).of(val).setOpState(with(equal(OpState.SAVE))); } }); service.save(element); mockery.assertIsSatisfied(); }
@Test public void testSaveWithoutId() { final TransactionService service = injector.getInstance(TransactionService.class); final CommonReadDao<DemoDomain, String> readDao = mockery.mock(CommonReadDao.class); final CommonWriteDao<DemoDomain> writeDao = mockery.mock(CommonWriteDao.class); final TransactionElement<DemoDomain> element = getTransactionElement(readDao, writeDao); element.getDto().setId(null); mockery.checking(new Expectations() { { exactly(1).of(mockFactory).createTransactionStoreKey(); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); will(returnValue(key)); exactly(1).of(key).setOpTimestamp(with(any(Long.class))); exactly(1).of(key).setTransactionId("1"); exactly(1).of(key).setObjectType(DemoDomain.class); exactly(1).of(key).setObjectId(with(aNonNull(String.class))); 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)); exactly(1).of(val).setCurrentState(with(same(element.getDto()))); exactly(1).of(val).getOpState(); will(returnValue(null)); exactly(1).of(val).setOpState(with(equal(OpState.SAVE))); } }); service.save(element); mockery.assertIsSatisfied(); }