From d25a43c2199879c1dd4c0a8818c374ac5f8eedd3 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Thu, 11 Sep 2025 11:56:33 +0200 Subject: [PATCH] add AccountService --- build.gradle.kts | 1 + .../kotlin/ltd/lulz/service/AccountService.kt | 19 +++++++ .../ltd/lulz/service/AccountServiceTest.kt | 53 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/main/kotlin/ltd/lulz/service/AccountService.kt create mode 100644 src/test/kotlin/ltd/lulz/service/AccountServiceTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 93f835f..439b04b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -21,6 +21,7 @@ dependencies { testImplementation(aa.kotlin.junit5) testImplementation(aa.kotlinx.coroutines.test) testImplementation(aa.mockk) + testImplementation(aa.reactor.test) testImplementation(aa.springboot.starter.test) testRuntimeOnly(aa.junit.platform.launcher) diff --git a/src/main/kotlin/ltd/lulz/service/AccountService.kt b/src/main/kotlin/ltd/lulz/service/AccountService.kt new file mode 100644 index 0000000..ecccaa8 --- /dev/null +++ b/src/main/kotlin/ltd/lulz/service/AccountService.kt @@ -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 = accountRepository + .save(entity) + .doOnNext { log.debug { "account created with id: ${it.id}" } } +} diff --git a/src/test/kotlin/ltd/lulz/service/AccountServiceTest.kt b/src/test/kotlin/ltd/lulz/service/AccountServiceTest.kt new file mode 100644 index 0000000..2817562 --- /dev/null +++ b/src/test/kotlin/ltd/lulz/service/AccountServiceTest.kt @@ -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() + 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()) } + } +}