private void handleEnrollInTransaction(Operation request) {
      String serviceSelfLink = this.service.getSelfLink();
      if (Action.POST == request.getAction()) {
        ServiceDocument body = request.getBody(this.service.getStateType());
        if (body.documentSelfLink == null) {
          body.documentSelfLink = UUID.randomUUID().toString();
          request.setBody(body);
        }
        serviceSelfLink = UriUtils.buildUriPath(serviceSelfLink, body.documentSelfLink);
      }

      long servicePreviousVersion =
          this.service.getState(request) == null
              ? -1
              : this.service.getState(request).documentVersion;
      Operation enrollRequest =
          SimpleTransactionService.TxUtils.buildEnrollRequest(
                  this.service.getHost(),
                  request.getTransactionId(),
                  serviceSelfLink,
                  request.getAction(),
                  servicePreviousVersion)
              .setCompletion(
                  (o, e) -> {
                    if (e != null) {
                      request.fail(e);
                      return;
                    }
                    this.service
                        .getOperationProcessingChain()
                        .resumeProcessingRequest(request, this);
                  });
      this.service.sendRequest(enrollRequest);
    }
 private void abort(String transactionId) throws Throwable {
   this.host.testStart(1);
   Operation patch = SimpleTransactionService.TxUtils.buildAbortRequest(this.host, transactionId);
   patch.setCompletion(
       (o, e) -> {
         if (operationFailed(o, e)) {
           this.host.failIteration(e);
           return;
         }
         this.host.completeIteration();
       });
   this.host.send(patch);
   this.host.testWait();
 }
  private void sendWithdrawDepositOperationPairs(
      String[] txids, int numOfTransfers, boolean independentTest) throws Throwable {
    Collection<Operation> requests = new ArrayList<Operation>(numOfTransfers);
    Random rand = new Random();
    for (int k = 0; k < numOfTransfers; k++) {
      final String tid = txids[k];
      int i = rand.nextInt(this.accountCount);
      int j = rand.nextInt(this.accountCount);
      if (i == j) {
        j = (j + 1) % this.accountCount;
      }
      final int final_j = j;
      int amount = 1 + rand.nextInt(3);
      this.host.log("Transaction %s: Transferring $%d from %d to %d", tid, amount, i, final_j);
      Operation withdraw = createWithdrawOperation(tid, buildAccountId(i), amount);
      withdraw.setCompletion(
          (o, e) -> {
            if (e != null) {
              this.host.log("Transaction %s: failed to withdraw, aborting...", tid);
              Operation abort = SimpleTransactionService.TxUtils.buildAbortRequest(this.host, tid);
              abort.setCompletion(
                  (op, ex) -> {
                    if (independentTest) {
                      this.host.completeIteration();
                    }
                  });
              this.host.send(abort);
              return;
            }
            Operation deposit = createDepositOperation(tid, buildAccountId(final_j), amount);
            deposit.setCompletion(
                (op, ex) -> {
                  if (ex != null) {
                    this.host.log("Transaction %s: failed to deposit, aborting...", tid);
                    Operation abort =
                        SimpleTransactionService.TxUtils.buildAbortRequest(this.host, tid);
                    abort.setCompletion(
                        (op2, ex2) -> {
                          if (independentTest) {
                            this.host.completeIteration();
                          }
                        });
                    this.host.send(abort);
                    return;
                  }
                  this.host.log("Transaction %s: Committing", tid);
                  Operation commit =
                      SimpleTransactionService.TxUtils.buildCommitRequest(this.host, tid);
                  commit.setCompletion(
                      (op2, ex2) -> {
                        if (independentTest) {
                          this.host.completeIteration();
                        }
                      });
                  this.host.send(commit);
                });
            this.host.send(deposit);
          });
      requests.add(withdraw);
    }

    if (independentTest) {
      this.host.testStart(numOfTransfers);
    }
    for (Operation withdraw : requests) {
      this.host.send(withdraw);
    }
    if (independentTest) {
      this.host.testWait();
    }
  }