BusinessLocalTransactionManagerImpl implements BusinessLocalTransactionManager { Optional<LocalTransaction> getLocalTransaction(String gtx, String name, Optional<List<String>> businessKey) { Optional<LocalTransaction> localTransaction = Optional.empty(); Long key = BusinessKey.getKey(businessKey); Optional<GlobalTransaction> globalTransaction = this.globalTransactionRepository.findById(gtx); if (globalTransaction.isPresent()) { GlobalTransaction gt = globalTransaction.get(); localTransaction = gt.getLocalTransactions().stream().filter(ltx -> { return name.equals(ltx.getName()) && (key != null ? key.equals(ltx.getbKey()) : true); }).findFirst(); } return localTransaction; } BusinessLocalTransactionManagerImpl(GlobalTransactionRepository globalTransactionRepository,
			CommandOrchestrator commandOrchestrator, ApplicationEventPublisher applicationEventPublisher,
			BubbleCache bubbleCache, TransactionUtilities txUtilities); @Override void begin(final LocalTransactionStepDefinition localTransactionStepDefinition); @Override void commit(
			final LocalTransactionStepDefinition<? extends BaseTransactionEvent> localTransactionStepDefinition); @Override void rollBack(
			LocalTransactionStepDefinition<? extends BaseTransactionEvent> localTransactionStepDefinition); @Override Optional<GlobalTransaction> findGlobalTransaction(final String GTX);  }
@Test public void testGetLocalTransactionWhenGlobalTxExist() { final String gtx = "gtx"; final String name = "name"; final LocalTransaction localTransaction = mock(LocalTransaction.class); final GlobalTransaction globalTransaction = mock(GlobalTransaction.class); when(mockGlobalTransactionRepository.findById(gtx)).thenReturn(Optional.of(globalTransaction)); when(globalTransaction.getLocalTransactions()).thenReturn(Arrays.asList(localTransaction)); when(localTransaction.getName()).thenReturn(name); final Optional<LocalTransaction> result = businessLocalTransactionManagerImplUnderTest.getLocalTransaction(gtx, name,null); assertEquals(localTransaction, result.get()); }
@Test public void testGetLocalTransactionWhenGlobalTxExistAndLocalLtxDifferent() { final String gtx = "gtx"; final String name = "name"; final LocalTransaction localTransaction = mock(LocalTransaction.class); final GlobalTransaction globalTransaction = mock(GlobalTransaction.class); when(mockGlobalTransactionRepository.findById(gtx)).thenReturn(Optional.of(globalTransaction)); when(globalTransaction.getLocalTransactions()).thenReturn(Arrays.asList(localTransaction)); when(localTransaction.getName()).thenReturn("anothername"); final Optional<LocalTransaction> result = businessLocalTransactionManagerImplUnderTest.getLocalTransaction(gtx, name,null); assertEquals(Optional.empty(), result); }
@Test public void testGetLocalTransactionWhenGlobalTxNotExist() { when(mockGlobalTransactionRepository.findById(anyString())).thenReturn(Optional.empty()); final Optional<LocalTransaction> result = businessLocalTransactionManagerImplUnderTest.getLocalTransaction(anyString(), anyString(),any()); assertEquals(Optional.empty(), result); }