public void testConvert() throws Exception {
    expect(mockExchangeDas.findExchange(ENTITY_ID, 200))
        .andReturn(_mockCurrencyExchangeDTO("0.98"))
        .once();
    expect(mockExchangeDas.findExchange(ENTITY_ID, 201))
        .andReturn(_mockCurrencyExchangeDTO("1.20"))
        .once();
    replay(mockCurrencyDas, mockExchangeDas);

    // convert $20.00 CAD to AUD - approximated conversion rates ;)
    CurrencyBL bl = new CurrencyBL(mockCurrencyDas, mockExchangeDas);
    BigDecimal amount = bl.convert(200, 201, new BigDecimal("20.00"), ENTITY_ID);

    verify(mockCurrencyDas, mockExchangeDas);

    assertEquals(new BigDecimal("24.48979"), amount);
  }
  public void testConvertRepeatingDecimal() throws Exception {
    expect(mockExchangeDas.findExchange(ENTITY_ID, 200))
        .andReturn(_mockCurrencyExchangeDTO("3.00"))
        .once();
    expect(mockExchangeDas.findExchange(ENTITY_ID, 201))
        .andReturn(_mockCurrencyExchangeDTO("1.00"))
        .once();
    replay(mockCurrencyDas, mockExchangeDas);

    /*
       Pivot calculation will result in a value of 3.333333~ repeating, which will
       cause an ArithmeticException if not handled correctly.

       10.00 / 3.00 = 3.333333~
    */
    CurrencyBL bl = new CurrencyBL(mockCurrencyDas, mockExchangeDas);
    BigDecimal amount = bl.convert(200, 201, new BigDecimal("10.00"), ENTITY_ID);

    verify(mockCurrencyDas, mockExchangeDas);

    assertEquals(new BigDecimal("3.33"), amount);
  }
  private CurrencyExchangeDTO findExchange(Integer entityId, Integer currencyId, Date toDate)
      throws SessionInternalError {

    // check for system currency exchange
    if (SYSTEM_CURRENCY_ID.equals(currencyId)) {
      return new CurrencyExchangeDTO(
          0, currency, entityId, SYSTEM_CURRENCY_RATE_DEFAULT, new Date());
    }
    LOG.debug("Get exchange rate for %s for entity %s for date %s", currencyId, entityId, toDate);

    CurrencyExchangeDTO exchange = exchangeDas.getExchangeRateForDate(entityId, currencyId, toDate);
    if (exchange == null) {
      // this entity doesn't have this exchange defined
      // 0 is the default, don't try to use null, it won't work
      exchange = exchangeDas.findExchange(SYSTEM_RATE_ENTITY_ID, currencyId);
      if (exchange == null) {
        throw new SessionInternalError(
            "Currency " + currencyId + " doesn't have a default exchange");
      }
    }
    LOG.debug("Exchange found %s", exchange.getId());
    return exchange;
  }