Skip to content

Commit

Permalink
Fixed exception on 1-to-1 currency conversion (closes #97)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy committed Feb 16, 2016
1 parent 7b08764 commit b505abe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
42 changes: 25 additions & 17 deletions src/main/scala/com.snowplowanalytics/forex/Forex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ package com.snowplowanalytics.forex
// Java
import java.math.BigDecimal
import java.math.RoundingMode

// Joda
import org.joda.time._
import org.joda.money._
// Scala
import scala.collection.JavaConversions._

// OerClient
import com.snowplowanalytics.forex.oerclient._

Expand Down Expand Up @@ -60,34 +60,37 @@ object Forex {

/**
* Starts building the fluent interface for currency look-up and conversion,
* Forex class has methods rate and convert, which set the source currency to
* the currency we are interested in.
* They return ForexLookupTo object which is then passed to methods
* in ForexLookupTo class in the fluent interface.
* Forex class has methods rate and convert, which set the source currency to
* the currency we are interested in.
* They return ForexLookupTo object which is then passed to methods
* in ForexLookupTo class in the fluent interface.
* @param config - a configurator for Forex object
* @param clientConfig - a configurator for Forex Client object
* @param clientConfig - a configurator for Forex Client object
* @param nowishCache - user defined nowishCache
* @param eodCache - user defined eodCache
*/
case class Forex(config: ForexConfig, clientConfig: ForexClientConfig,
nowishCache: MaybeNowishCache = None, eodCache: MaybeEodCache = None) {
case class Forex(
config: ForexConfig,
clientConfig: ForexClientConfig,
nowishCache: MaybeNowishCache = None,
eodCache: MaybeEodCache = None) {

// For now, we are hard-wired to the OER Client library
val client = ForexClient.getClient(config, clientConfig, nowish = nowishCache, eod = eodCache)

def rate: ForexLookupTo = {
ForexLookupTo(1, config.baseCurrency, this)
}

/**
* Starts building a fluent interface,
* Starts building a fluent interface,
* performs currency look up from *source* currency.
* (The target currency will be supplied
* to the ForexLookupTo later).
* to the ForexLookupTo later).
* If not specified, it is set to base currency by default.
* @param currency(optional) - source currency
* @return ForexLookupTo object which is the part of the fluent interface
*/
def rate: ForexLookupTo = {
ForexLookupTo(1, config.baseCurrency, this)
}

def rate(currency: String): ForexLookupTo = {
ForexLookupTo(1, currency, this)
}
Expand Down Expand Up @@ -332,8 +335,13 @@ case class ForexLookupWhen(conversionAmount: Double, fromCurr: String, toCurr: S
private def returnMoneyOrJodaError(rate: BigDecimal): Either[OerResponseError, Money] = {
if (fromCurrencyUnit.isRight && toCurrencyUnit.isRight) {
// Money in a given amount
val moneyInSourceCurrency = BigMoney.of(fromCurrencyUnit.right.get, conversionAmt)
Right(moneyInSourceCurrency.convertedTo(toCurrencyUnit.right.get, rate).toMoney(RoundingMode.HALF_EVEN))
val moneyInSourceCurrency = BigMoney.of(fromCurrencyUnit.right.get, conversionAmt)
// prevent weird JodaTime exception when converting equal currencies
if (fromCurrencyUnit.right.get == toCurrencyUnit.right.get) {
Right(moneyInSourceCurrency.toMoney(RoundingMode.HALF_EVEN))
} else {
Right(moneyInSourceCurrency.convertedTo(toCurrencyUnit.right.get, rate).toMoney(RoundingMode.HALF_EVEN))
}
} else {
var errMessage = "The exchange rate of [" + fromCurr + "]:["+ toCurr + "] " +
"is " + rate + ". However, "
Expand Down
17 changes: 15 additions & 2 deletions src/test/scala/com.snowplowanalytics.forex/ForexNowSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Snowplow Analytics Ltd. All rights reserved.
* Copyright (c) 2013-2016 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
Expand Down Expand Up @@ -62,5 +62,18 @@ class ForexNowSpec extends Specification {
"be greater than 1 SGD" in {
sgdMoneyWithBaseGbp.isGreaterThan(Money.of(CurrencyUnit.getInstance("SGD"), 1))
}
}
}

/**
* GBP with GBP as base currency
*/
val gbpToGbpWithBaseGbp = fxWithBaseGBP.rate.to("GBP").now

val gbpMoneyWithBaseGbp = gbpToGbpWithBaseGbp.right.get

"Do not throw JodaTime exception on converting identical currencies [%s]".format(gbpMoneyWithBaseGbp) should {
"be equal 1 GBP" in {
gbpMoneyWithBaseGbp.isEqual(Money.of(CurrencyUnit.getInstance("GBP"), 1))
}
}
}

0 comments on commit b505abe

Please sign in to comment.