Skip to content

Commit

Permalink
Modified test for creating a new account to check id (pass)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil.jedrzejuk committed Oct 2, 2024
1 parent d796882 commit 964832f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package camilyed.github.io.currencyexchangeapi.application

import camilyed.github.io.currencyexchangeapi.domain.Account
import java.util.*
import camilyed.github.io.currencyexchangeapi.domain.AccountRepository

class AccountService {
class AccountService(
private val repository: AccountRepository
) {

fun create(command: CreateAccountCommand): Account {
return Account(id = UUID.randomUUID(), owner = command.owner, balancePln = command.initialBalance)
val id = repository.nextAccountId()
return Account(
id = id,
owner = command.owner,
balancePln = command.initialBalance
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package camilyed.github.io.currencyexchangeapi.domain

import java.util.UUID

interface AccountRepository {

fun nextAccountId(): UUID
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package camilyed.github.io.currencyexchangeapi.application

import camilyed.github.io.currencyexchangeapi.domain.Account
import camilyed.github.io.currencyexchangeapi.testing.ability.SetNextAccountIdAbility
import camilyed.github.io.currencyexchangeapi.testing.ability.SetNextAccountIdAbility.Companion.accountRepository
import camilyed.github.io.currencyexchangeapi.testing.assertions.hasId
import camilyed.github.io.currencyexchangeapi.testing.assertions.hasInitialBalance
import camilyed.github.io.currencyexchangeapi.testing.assertions.hasOwner
Expand All @@ -12,13 +14,16 @@ import org.junit.jupiter.api.assertThrows
import strikt.api.expectThat
import java.math.BigDecimal

class AccountServiceTest {
class AccountServiceTest: SetNextAccountIdAbility {

private val accountService = AccountService()
private val accountService = AccountService(accountRepository)

@Test
fun `should create a new account with valid details`() {
// given
theNextAccountIdWillBe("db59d3ba-5044-4ea9-85e2-aa1e67ec713c")

// and
val command = anAccount()
.withOwner("Jan Kowalski")
.withInitialBalance(BigDecimal(1000.0))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package camilyed.github.io.currencyexchangeapi.testing.ability

import camilyed.github.io.currencyexchangeapi.testing.fakes.TestingAccountRepository
import java.util.*

interface SetNextAccountIdAbility {

fun theNextAccountIdWillBe(id: String) {
accountRepository.setNextId(UUID.fromString(id))
}

companion object {
val accountRepository = TestingAccountRepository()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package camilyed.github.io.currencyexchangeapi.testing.fakes

import camilyed.github.io.currencyexchangeapi.domain.AccountRepository
import java.util.*

class TestingAccountRepository : AccountRepository {

private var nextID: UUID? = null

override fun nextAccountId(): UUID {
return nextID ?: UUID.randomUUID()
}

fun setNextId(id: UUID) {
this.nextID = id
}
}

0 comments on commit 964832f

Please sign in to comment.