Skip to content

Commit

Permalink
Implement test for exchange USD to PLN (pass)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil.jedrzejuk committed Oct 2, 2024
1 parent 0905437 commit fab9b24
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import camilyed.github.io.currencyexchangeapi.domain.Account
import camilyed.github.io.currencyexchangeapi.domain.AccountRepository
import camilyed.github.io.currencyexchangeapi.domain.AccountSnapshot
import java.math.BigDecimal
import java.util.*

class AccountService(
private val repository: AccountRepository
Expand All @@ -27,4 +26,10 @@ class AccountService(
account.exchangePlnToUsd(amountPln = command.amount, exchangeRate = command.exchangeRate)
return account.toSnapshot()
}

fun exchangeUsdToPln(command: ExchangeUsdToPlnCommand): AccountSnapshot {
val account = repository.find(command.accountId)!!
account.exchangeUsdToPln(amountUsd = command.amount, exchangeRate = command.exchangeRate)
return account.toSnapshot()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package camilyed.github.io.currencyexchangeapi.application

import java.math.BigDecimal
import java.util.UUID

data class ExchangeUsdToPlnCommand(
val accountId: UUID,
val amount: BigDecimal,
val exchangeRate: BigDecimal
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class Account(
balanceUsd = balanceUsd.add(amountUsd)
}

fun exchangeUsdToPln(amountUsd: BigDecimal, exchangeRate: BigDecimal) {
require(amountUsd > BigDecimal.ZERO) { "Amount must be greater than 0" }
val amountPln = amountUsd.multiply(exchangeRate).setScale(2, RoundingMode.HALF_EVEN)
balanceUsd = balanceUsd.subtract(amountUsd)
balancePln = balancePln.add(amountPln)
}

fun toSnapshot(): AccountSnapshot {
return AccountSnapshot(
id = id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import camilyed.github.io.currencyexchangeapi.testing.assertions.hasOwner
import camilyed.github.io.currencyexchangeapi.testing.builders.CreateAccountCommandBuilder
import camilyed.github.io.currencyexchangeapi.testing.builders.CreateAccountCommandBuilder.Companion.anAccount
import camilyed.github.io.currencyexchangeapi.testing.builders.ExchangePlnToUsdCommandBuilder
import camilyed.github.io.currencyexchangeapi.testing.builders.ExchangePlnToUsdCommandBuilder.Companion.anExchange
import camilyed.github.io.currencyexchangeapi.testing.builders.ExchangePlnToUsdCommandBuilder.Companion.anExchangeToUsd
import camilyed.github.io.currencyexchangeapi.testing.builders.ExchangeUsdToPlnCommandBuilder
import camilyed.github.io.currencyexchangeapi.testing.builders.ExchangeUsdToPlnCommandBuilder.Companion.anExchangeToPln
import camilyed.github.io.currencyexchangeapi.testing.fakes.TestingAccountRepository
import org.junit.jupiter.api.Test
import strikt.api.expectCatching
Expand Down Expand Up @@ -77,7 +79,7 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {

// when
account = exchangePlnToUsd(
anExchange()
anExchangeToUsd()
.withAccountId(account.id)
.withAmount("400.00")
.withExchangeRate("4.0")
Expand All @@ -97,7 +99,7 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {
// then
expectCatching {
exchangePlnToUsd(
anExchange()
anExchangeToUsd()
.withAccountId(account.id)
.withAmount("0.00")
)
Expand All @@ -113,24 +115,24 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {

// and
account = exchangePlnToUsd(
anExchange()
anExchangeToUsd()
.withAccountId(account.id)
.withAmount("400.00")
.withExchangeRate("4.0")
)

// when
account = exchangeUsdToPln(
anExchange()
anExchangeToPln()
.withAccountId(account.id)
.withAmount("100.00")
.withExchangeRate("4.0")
)

// then
expectThat(account)
.hasBalanceInPln("1000.00") // Powrót do stanu początkowego
.hasBalanceInUsd("0.00") // Saldo USD powinno być teraz zerowe
.hasBalanceInPln("1000.00")
.hasBalanceInUsd("0.00")
}

private fun create(command: CreateAccountCommandBuilder): AccountSnapshot {
Expand All @@ -140,4 +142,8 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {
private fun exchangePlnToUsd(command: ExchangePlnToUsdCommandBuilder): AccountSnapshot {
return accountService.exchangePlnToUsd(command.build())
}

private fun exchangeUsdToPln(command: ExchangeUsdToPlnCommandBuilder): AccountSnapshot {
return accountService.exchangeUsdToPln(command.build())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ExchangePlnToUsdCommandBuilder private constructor() {
}

companion object {
fun anExchange(): ExchangePlnToUsdCommandBuilder {
fun anExchangeToUsd(): ExchangePlnToUsdCommandBuilder {
return ExchangePlnToUsdCommandBuilder()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package camilyed.github.io.currencyexchangeapi.testing.builders

import camilyed.github.io.currencyexchangeapi.application.ExchangeUsdToPlnCommand
import java.math.BigDecimal
import java.util.*

class ExchangeUsdToPlnCommandBuilder private constructor() {
private var accountId: UUID = UUID.randomUUID()
private var amount: BigDecimal = BigDecimal("100.00")
private var exchangeRate: BigDecimal = BigDecimal("4.0")

fun withAccountId(accountId: UUID) = apply { this.accountId = accountId }
fun withAmount(amount: String) = apply { this.amount = BigDecimal(amount) }
fun withExchangeRate(exchangeRate: String) = apply { this.exchangeRate = BigDecimal(exchangeRate) }

fun build(): ExchangeUsdToPlnCommand {
return ExchangeUsdToPlnCommand(
accountId = accountId,
amount = amount,
exchangeRate = exchangeRate
)
}

companion object {
fun anExchangeToPln(): ExchangeUsdToPlnCommandBuilder {
return ExchangeUsdToPlnCommandBuilder()
}
}
}

0 comments on commit fab9b24

Please sign in to comment.