generated from aura-ascend/template-service
add AccountService
This commit is contained in:
19
src/main/kotlin/ltd/lulz/service/AccountService.kt
Normal file
19
src/main/kotlin/ltd/lulz/service/AccountService.kt
Normal file
@@ -0,0 +1,19 @@
|
||||
package ltd.lulz.service
|
||||
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import ltd.lulz.model.AccountEntity
|
||||
import ltd.lulz.repository.AccountRepository
|
||||
import org.springframework.stereotype.Service
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
@Service
|
||||
class AccountService(
|
||||
private val accountRepository: AccountRepository,
|
||||
) {
|
||||
|
||||
fun create(entity: AccountEntity): Mono<AccountEntity> = accountRepository
|
||||
.save(entity)
|
||||
.doOnNext { log.debug { "account created with id: ${it.id}" } }
|
||||
}
|
||||
53
src/test/kotlin/ltd/lulz/service/AccountServiceTest.kt
Normal file
53
src/test/kotlin/ltd/lulz/service/AccountServiceTest.kt
Normal file
@@ -0,0 +1,53 @@
|
||||
package ltd.lulz.service
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.slot
|
||||
import io.mockk.verify
|
||||
import java.math.BigDecimal
|
||||
import java.util.UUID
|
||||
import ltd.lulz.model.AccountEntity
|
||||
import ltd.lulz.repository.AccountRepository
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import reactor.core.publisher.Mono
|
||||
import reactor.test.StepVerifier
|
||||
|
||||
@Suppress("ReactiveStreamsUnusedPublisher", "MayBeConstant")
|
||||
class AccountServiceTest {
|
||||
|
||||
companion object {
|
||||
val name: String = "some name"
|
||||
val amount: BigDecimal = BigDecimal.valueOf(0.01)
|
||||
val uuid: UUID = UUID.fromString("00000000-0000-0000-0000-000000000000")
|
||||
}
|
||||
|
||||
private var repository: AccountRepository = mockk()
|
||||
private lateinit var service: AccountService
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
service = AccountService(repository)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `create account`() {
|
||||
// given
|
||||
val entity = AccountEntity(name = name, amount = amount)
|
||||
|
||||
val capture = slot<AccountEntity>()
|
||||
every { repository.save(capture(capture)) } answers { Mono.just(capture.captured.copy(id = uuid)) }
|
||||
|
||||
// when stepped
|
||||
StepVerifier.create(service.create(entity))
|
||||
.assertNext { result ->
|
||||
assertThat(result.id).isEqualTo(uuid)
|
||||
assertThat(result.name).isEqualTo(name)
|
||||
assertThat(result.amount).isEqualTo(amount)
|
||||
}
|
||||
.verifyComplete()
|
||||
|
||||
verify { repository.save(any()) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user