class PortfolioServiceImpl implements PortfolioService {

  @Override
  public void buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) {
    if (amount <= 0) {
      resultHandler.handle(Future.failedFuture("Cannot buy " + quote.getString("name") + " - the amount must be " +
          "greater than 0"));
    }

    if (quote.getInteger("shares") < amount) {
      resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " - not enough " +
          "stocks on the market (" + quote.getInteger("shares") + ")"));
    }

    double price = amount * quote.getDouble("ask");
    String name = quote.getString("name");
    // 1) do we have enough money
    if (portfolio.getCash() >= price) {
      // Yes, buy it
      portfolio.setCash(portfolio.getCash() - price);
      int current = portfolio.getAmount(name);
      int newAmount = current + amount;
      portfolio.getShares().put(name, newAmount);
      sendActionOnTheEventBus("BUY", amount, quote, newAmount);
      resultHandler.handle(Future.succeededFuture(portfolio));
    } else {
      resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " of " + name + " - " + "not enough money, " +
          "need " + price + ", has " + portfolio.getCash()));
    }
  }

}

class PortfolioServiceImplTest {

  private Vertx vertx;
  private PortfolioService service;
  private Portfolio original;

  @Test
  public void testThatYouCannotBuyIfThereIsNotEnoughShare(TestContext tc) {

    Async async = tc.async();
    service.buy(100, quote("A", 10, 20, 10), ar -> {
      tc.assertTrue(ar.failed());
      tc.assertTrue(ar.cause().getMessage().contains("not enough stocks"));
      async.complete();
    });
  }

}