@Test
  public void testDisallowed() throws Exception {
    final InvocationContext context = mock(InvocationContext.class);

    final Invocation invocation = mock(Invocation.class);
    when(invocation.getInvocationContext()).thenReturn(context);

    CashRepository cashRepository = mock(CashRepository.class);
    final CoinReceiveCash cash1 = new CoinReceiveCash();
    cash1.confirm(BigMoney.of(CurrencyUnit.of("BTC"), BigDecimal.TEN), new Date());
    final CoinReceiveCash cash2 = new CoinReceiveCash();
    cash2.confirm(BigMoney.of(CurrencyUnit.of("BTC"), BigDecimal.ONE), new Date());
    when(cashRepository.findByDueDate(any(Date.class))).thenReturn(ImmutableList.of(cash1, cash2));

    DueDateService dateService = mock(DueDateService.class);

    final GlobalMaxAmountPerDayExecutingInterceptor interceptor =
        new GlobalMaxAmountPerDayExecutingInterceptor("BTC 10");
    interceptor.setCashRepository(cashRepository);
    interceptor.setService(dateService);

    final ValidationCode validationCode = interceptor.intercept(invocation);

    assertThat(validationCode, is(ValidationCode.EXCEEDED_GLOBAL_MAX_AMOUNT_PER_DAY));

    verify(dateService).computeDueDate(any(Date.class));
    verify(invocation, never()).invoke();
  }
  @Test
  public void testAllowed() throws Exception {
    final InvocationContext context = mock(InvocationContext.class);

    final Invocation invocation = mock(Invocation.class);
    when(invocation.getInvocationContext()).thenReturn(context);

    CashRepository cashRepository = mock(CashRepository.class);
    final CoinReceiveCash cash1 = new CoinReceiveCash();
    cash1.confirm(BigMoney.of(CurrencyUnit.of("BTC"), BigDecimal.TEN), new Date());
    final CoinReceiveCash cash2 = new CoinReceiveCash();
    cash2.confirm(BigMoney.of(CurrencyUnit.of("BTC"), BigDecimal.ONE), new Date());
    when(cashRepository.findByDueDate(any(Date.class)))
        .thenReturn(ImmutableList.of(cash1), ImmutableList.of(cash2));

    DueDateService dateService = mock(DueDateService.class);

    final GlobalMaxAmountPerDayExecutingInterceptor interceptor =
        new GlobalMaxAmountPerDayExecutingInterceptor("BTC 10");
    interceptor.setCashRepository(cashRepository);
    interceptor.setService(dateService);

    final ValidationCode validationCode1 = interceptor.intercept(invocation);
    final ValidationCode validationCode2 = interceptor.intercept(invocation);

    assertThat(validationCode1, anyOf(nullValue(), is(ValidationCode.SUCCESSFUL)));
    assertThat(validationCode2, anyOf(nullValue(), is(ValidationCode.SUCCESSFUL)));

    verify(invocation, times(2)).invoke();
    verify(dateService, times(2)).computeDueDate(any(Date.class));
    verify(invocation, times(2)).invoke();
  }
Esempio n. 3
0
  private static LimitOrder createOrder(
      String tradableIdentifier,
      String currency,
      List<BigDecimal> priceAndAmount,
      Order.OrderType orderType) {

    return new LimitOrder(
        orderType,
        priceAndAmount.get(1),
        tradableIdentifier,
        currency,
        BigMoney.of(CurrencyUnit.USD, priceAndAmount.get(0)));
  }
Esempio n. 4
0
  /**
   * Adapts a Transaction[] to a Trades Object
   *
   * @param transactions The Bitstamp transactions
   * @param tradableIdentifier The tradeable identifier (e.g. BTC in BTC/USD)
   * @param currency The currency (e.g. USD in BTC/USD)
   * @return The XChange Trades
   */
  public static Trades adaptTrades(
      BitstampTransaction[] transactions, String tradableIdentifier, String currency) {

    List<Trade> trades = new ArrayList<Trade>();
    for (BitstampTransaction tx : transactions) {
      trades.add(
          new Trade(
              null,
              tx.getAmount(),
              tradableIdentifier,
              currency,
              BigMoney.of(CurrencyUnit.of(currency), tx.getPrice()),
              DateUtils.fromMillisUtc(tx.getDate() * 1000L),
              tx.getTid()));
    }

    return new Trades(trades);
  }