### Question:
SagaSQLTransport implements SQLTransport { @Override public SagaResponse with(final String datasourceName, final String sql, final List<List<String>> sagaParameters) { if (Strings.isNullOrEmpty(sql)) { return new SuccessfulSagaResponse("Skip empty transaction/compensation"); } if (SagaDefinitionFactory.ROLLBACK_TAG.equals(sql)) { shardingSQLTransaction.changeAllLogicTransactionStatus(ExecuteStatus.COMPENSATING); throw new TransportFailedException("Forced Rollback tag has been checked, saga will rollback this transaction"); } Optional<SQLTransaction> sqlTransaction = shardingSQLTransaction.findSQLTransaction(datasourceName, sql, sagaParameters); return sqlTransaction.isPresent() && isExecuteSQL(sqlTransaction.get().getExecuteStatus()) ? executeSQL(datasourceName, sql, sagaParameters) : new JsonSuccessfulSagaResponse("{}"); }  @Override SagaResponse with(final String datasourceName, final String sql, final List<List<String>> sagaParameters);  }### Answer:
@Test(expected = TransportFailedException.class) public void assertTransportWithRollback() { try { sagaSQLTransport.with("ds1", SagaDefinitionFactory.ROLLBACK_TAG, Lists.<List<String>>newLinkedList()); } catch (final TransportFailedException ex) { verify(shardingSQLTransaction).changeAllLogicTransactionStatus(ExecuteStatus.COMPENSATING); throw ex; } }
@Test public void assertWithBranchTransactionNotPresent() { when(shardingSQLTransaction.findSQLTransaction(anyString(), anyString(), ArgumentMatchers.<List<String>>anyList())).thenReturn(Optional.<SQLTransaction>absent()); sagaSQLTransport.with("ds1", "xxx", Lists.<List<String>>newLinkedList()); verify(shardingSQLTransaction).findSQLTransaction(anyString(), anyString(), ArgumentMatchers.<List<String>>anyList()); }
@Test public void assertWithExecuteStatusSuccess() throws SQLException { when(sqlTransaction.getExecuteStatus()).thenReturn(ExecuteStatus.SUCCESS); when(shardingSQLTransaction.findSQLTransaction(anyString(), anyString(), ArgumentMatchers.<List<String>>anyList())).thenReturn(Optional.of(sqlTransaction)); sagaSQLTransport.with("ds1", "xxx", Lists.<List<String>>newLinkedList()); verify(connection, never()).prepareStatement("xxx"); }
@Test public void assertWithExecuteStatusFailedOfRollback() throws SQLException { when(sqlTransaction.getExecuteStatus()).thenReturn(ExecuteStatus.FAILURE); when(shardingSQLTransaction.getOperationType()).thenReturn(TransactionOperationType.ROLLBACK); when(shardingSQLTransaction.findSQLTransaction(anyString(), anyString(), ArgumentMatchers.<List<String>>anyList())).thenReturn(Optional.of(sqlTransaction)); sagaSQLTransport.with("ds1", "xxx", Lists.<List<String>>newLinkedList()); verify(connection, never()).prepareStatement("xxx"); }
@Test public void assertWithExecuteSQL() throws SQLException { when(sqlTransaction.getExecuteStatus()).thenReturn(ExecuteStatus.COMPENSATING); when(shardingSQLTransaction.findSQLTransaction(anyString(), anyString(), ArgumentMatchers.<List<String>>anyList())).thenReturn(Optional.of(sqlTransaction)); sagaSQLTransport.with("ds1", "xxx", Lists.<List<String>>newLinkedList()); verify(connection).prepareStatement("xxx"); verify(preparedStatement).executeUpdate(); }