### Question:
SagaDefinitionFactory { public static SagaDefinition newInstance(final String recoveryPolicy, final SagaConfiguration configuration, final ShardingSQLTransaction shardingSQLTransaction) { Collection<SagaRequest> sagaRequests = new LinkedList<>(); Collection<String> requestIds = new LinkedList<>(); for (LogicSQLTransaction each : shardingSQLTransaction.getLogicSQLTransactions()) { requestIds = addLogicSQLTransactionRequest(new LinkedList<>(requestIds), sagaRequests, each, configuration); } if (RecoveryPolicy.SAGA_BACKWARD_RECOVERY_POLICY.equals(recoveryPolicy)) { sagaRequests.add(newRollbackRequest(new LinkedList<>(requestIds), configuration)); } return new SagaDefinition(recoveryPolicy, sagaRequests); }  static SagaDefinition newInstance(final String recoveryPolicy, final SagaConfiguration configuration, final ShardingSQLTransaction shardingSQLTransaction); static final String ROLLBACK_TAG; }### Answer:
@Test public void assertNewInstanceOfBackwardRecovery() { shardingSQLTransaction.getLogicSQLTransactions().addAll(mockLogicSQLTransactions(3, 2)); SagaDefinition actual = SagaDefinitionFactory.newInstance(RecoveryPolicy.SAGA_BACKWARD_RECOVERY_POLICY, sagaConfiguration, shardingSQLTransaction); assertThat(actual.getPolicy(), is(RecoveryPolicy.SAGA_BACKWARD_RECOVERY_POLICY)); assertThat(actual.getRequests().size(), is(7)); int i = 0; for (SagaRequest each : actual.getRequests()) { if (i <= 1) { assertThat(each.getParents().size(), is(0)); } else { assertThat(each.getParents().size(), is(2)); } if (i <= 5) { assertThat(each.getDatasource(), is("ds")); assertThat(each.getTransaction().getSql(), is("tx-sql")); assertThat(each.getTransaction().getParams().size(), is(3)); assertThat(each.getId(), instanceOf(String.class)); assertThat(each.getType(), is("sql")); assertThat(each.getFailRetryDelayMilliseconds(), is(sagaConfiguration.getTransactionRetryDelayMilliseconds())); assertThat(each.getCompensation().getSql(), is("revert-sql")); assertThat(each.getCompensation().getParams().size(), is(3)); assertThat(each.getCompensation().getRetries(), is(sagaConfiguration.getCompensationMaxRetries())); } else { assertThat(each.getDatasource(), is("rollbackTag")); assertThat(each.getTransaction().getSql(), is("rollbackTag")); assertThat(each.getTransaction().getParams().size(), is(0)); assertThat(each.getCompensation().getSql(), is("rollbackTag")); assertThat(each.getCompensation().getParams().size(), is(0)); } i++; } }
@Test public void assertNewInstanceOfForwardRecovery() { shardingSQLTransaction.getLogicSQLTransactions().addAll(mockLogicSQLTransactions(2, 4)); SagaDefinition actual = SagaDefinitionFactory.newInstance(RecoveryPolicy.SAGA_FORWARD_RECOVERY_POLICY, sagaConfiguration, shardingSQLTransaction); assertThat(actual.getPolicy(), is(RecoveryPolicy.SAGA_FORWARD_RECOVERY_POLICY)); assertThat(actual.getRequests().size(), is(8)); for (SagaRequest each : actual.getRequests()) { assertThat(each.getDatasource(), is("ds")); assertThat(each.getTransaction().getSql(), is("tx-sql")); assertThat(each.getTransaction().getParams().size(), is(3)); assertThat(each.getCompensation().getSql(), is("revert-sql")); assertThat(each.getCompensation().getParams().size(), is(3)); } }