Skip to content

Commit

Permalink
Refactored tests, added custom exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil.jedrzejuk committed Oct 3, 2024
1 parent a7b5429 commit 87bf273
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/main/kotlin/camilyed/github/io/common/Money.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ data class Money(val amount: BigDecimal, val currency: String) {

init {
require(amount >= BigDecimal.ZERO) { "Money amount must be greater than or equal to zero" }
require(currency.isNotEmpty()) { "Currency cannot be empty" }
require(currency == "PLN" || currency == "USD") {
throw UnsupportedCurrencyException("Unsupported currency: $currency")
}
amount.setScale(2, RoundingMode.HALF_EVEN)
}

Expand Down Expand Up @@ -36,3 +38,4 @@ data class Money(val amount: BigDecimal, val currency: String) {
fun usd(amount: BigDecimal) = Money(amount.setScale(2, RoundingMode.HALF_EVEN), "USD")
}
}
class UnsupportedCurrencyException(message: String) : RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ class Account(
}

fun exchangePlnToUsd(amountPln: Money, exchangeRate: ExchangeRate) {
require(amountPln.currency == "PLN") { "Amount must be in PLN" }
require(!amountPln.isZero()) { "Amount must be greater than 0" }
require(amountPln <= balancePln) { "Insufficient PLN balance" }
require(!amountPln.isZero()) { throw InvalidAmountException("Amount must be greater than 0") }
require(amountPln <= balancePln) { throw InsufficientFundsException("Insufficient PLN balance") }

val amountUsd = Money(exchangeRate.convertFromPln(amountPln.amount), "USD")
balancePln -= amountPln
balanceUsd += amountUsd
}

fun exchangeUsdToPln(amountUsd: Money, exchangeRate: ExchangeRate) {
require(amountUsd.currency == "USD") { "Amount must be in USD" }
require(!amountUsd.isZero()) { "Amount must be greater than 0" }
require(amountUsd <= balanceUsd) { "Insufficient USD balance" }
require(!amountUsd.isZero()) { throw InvalidAmountException("Amount must be greater than 0") }
require(amountUsd <= balanceUsd) { throw InsufficientFundsException("Insufficient USD balance") }

val amountPln = Money(exchangeRate.convertToPln(amountUsd.amount), "PLN")
balanceUsd -= amountUsd
balancePln += amountPln
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package camilyed.github.io.currencyexchangeapi.domain

class InsufficientFundsException(message: String) : RuntimeException(message)

class InvalidAmountException(message: String) : RuntimeException(message)

class InvalidExchangeRateException(message: String) : RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import java.math.RoundingMode
data class ExchangeRate(val rate: BigDecimal) {

init {
require(rate > BigDecimal.ZERO) { "Exchange rate must be greater than 0" }
if (rate <= BigDecimal.ZERO) {
throw InvalidExchangeRateException("Exchange rate must be greater than 0")
}
}

fun convertFromPln(amountPln: BigDecimal): BigDecimal {
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/camilyed/github/io/common/MoneyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class MoneyTest {
expectCatching {
Money(BigDecimal("100.00"), "")
}.isFailure()
.isA<IllegalArgumentException>()
.message.isEqualTo("Currency cannot be empty")
.isA<UnsupportedCurrencyException>()
.message.isEqualTo("Unsupported currency: ")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package camilyed.github.io.currencyexchangeapi.application

import camilyed.github.io.currencyexchangeapi.domain.AccountSnapshot
import camilyed.github.io.currencyexchangeapi.domain.InsufficientFundsException
import camilyed.github.io.currencyexchangeapi.domain.InvalidAmountException
import camilyed.github.io.currencyexchangeapi.domain.InvalidExchangeRateException
import camilyed.github.io.currencyexchangeapi.testing.ability.CreateAccountAbility
import camilyed.github.io.currencyexchangeapi.testing.ability.SetNextAccountIdAbility
import camilyed.github.io.currencyexchangeapi.testing.assertions.hasBalanceInPln
Expand All @@ -22,7 +25,6 @@ import strikt.assertions.isA
import strikt.assertions.isEqualTo
import strikt.assertions.isFailure
import strikt.assertions.message
import java.math.BigDecimal

class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {

Expand Down Expand Up @@ -105,7 +107,7 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {
.withAmount("0.00")
)
}.isFailure()
.isA<IllegalArgumentException>()
.isA<InvalidAmountException>()
.message.isEqualTo("Amount must be greater than 0")
}

Expand Down Expand Up @@ -162,7 +164,7 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {
.withAmount("0.00")
)
}.isFailure()
.isA<IllegalArgumentException>()
.isA<InvalidAmountException>()
.message.isEqualTo("Amount must be greater than 0")
}

Expand Down Expand Up @@ -196,7 +198,7 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {
.withAmount("200.00")
)
}.isFailure()
.isA<IllegalArgumentException>()
.isA<InsufficientFundsException>()
.message.isEqualTo("Insufficient PLN balance")
}

Expand All @@ -213,7 +215,7 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {
.withAmount("100.00")
)
}.isFailure()
.isA<IllegalArgumentException>()
.isA<InsufficientFundsException>()
.message.isEqualTo("Insufficient USD balance")
}

Expand Down Expand Up @@ -268,7 +270,7 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {
.withExchangeRate("0.00")
)
}.isFailure()
.isA<IllegalArgumentException>()
.isA<InvalidExchangeRateException>()
.message.isEqualTo("Exchange rate must be greater than 0")
}

Expand Down

0 comments on commit 87bf273

Please sign in to comment.