From 2a98cdf38fa3e8d389ef542ebd80fc266fdcd67f Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Tue, 19 Nov 2024 12:43:39 +0100 Subject: [PATCH] Add addType to TypeService --- build.gradle.kts | 2 + .../kotlin/ltd/hlaeja/service/TypeService.kt | 16 +++++++ .../ltd/hlaeja/service/TypeServiceTest.kt | 45 +++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index 1c7d3d0..6a86613 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,6 +7,8 @@ plugins { } dependencies { + implementation(hlaeja.com.fasterxml.jackson.module.kotlin) + implementation(hlaeja.kotlin.logging) implementation(hlaeja.kotlin.reflect) implementation(hlaeja.kotlinx.coroutines) implementation(hlaeja.ltd.hlaeja.library.common.messages) diff --git a/src/main/kotlin/ltd/hlaeja/service/TypeService.kt b/src/main/kotlin/ltd/hlaeja/service/TypeService.kt index e904a0c..b2f243a 100644 --- a/src/main/kotlin/ltd/hlaeja/service/TypeService.kt +++ b/src/main/kotlin/ltd/hlaeja/service/TypeService.kt @@ -3,7 +3,13 @@ package ltd.hlaeja.service import kotlinx.coroutines.flow.Flow import ltd.hlaeja.entity.TypeEntity import ltd.hlaeja.repository.TypeRepository +import mu.KotlinLogging +import org.springframework.dao.DuplicateKeyException +import org.springframework.http.HttpStatus import org.springframework.stereotype.Service +import org.springframework.web.server.ResponseStatusException + +private val log = KotlinLogging.logger {} @Service class TypeService( @@ -11,4 +17,14 @@ class TypeService( ) { fun getTypes(): Flow = typeRepository.findAll() + + suspend fun addType( + entity: TypeEntity, + ): TypeEntity = try { + typeRepository.save(entity) + .also { log.debug("Added new type: {}", it.id) } + } catch (e: DuplicateKeyException) { + log.warn(e.localizedMessage) + throw ResponseStatusException(HttpStatus.CONFLICT) + } } diff --git a/src/test/kotlin/ltd/hlaeja/service/TypeServiceTest.kt b/src/test/kotlin/ltd/hlaeja/service/TypeServiceTest.kt index d18a4d6..8884bcc 100644 --- a/src/test/kotlin/ltd/hlaeja/service/TypeServiceTest.kt +++ b/src/test/kotlin/ltd/hlaeja/service/TypeServiceTest.kt @@ -1,15 +1,29 @@ package ltd.hlaeja.service +import io.mockk.coEvery +import io.mockk.coVerify import io.mockk.every import io.mockk.mockk import io.mockk.verify +import java.time.Instant +import java.time.ZoneId +import java.time.ZonedDateTime +import java.util.UUID +import kotlin.test.assertFailsWith import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest import ltd.hlaeja.entity.TypeEntity import ltd.hlaeja.repository.TypeRepository import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import org.springframework.dao.DuplicateKeyException +import org.springframework.web.server.ResponseStatusException class TypeServiceTest { + companion object { + val timestamp = ZonedDateTime.ofInstant(Instant.parse("2000-01-01T00:00:00.001Z"), ZoneId.of("UTC")) + val uuid = UUID.fromString("00000000-0000-0000-0000-000000000000") + } val repository: TypeRepository = mockk() @@ -31,4 +45,35 @@ class TypeServiceTest { // then verify(exactly = 1) { repository.findAll() } } + + @Test + fun `add new type success`() = runTest { + // given + val entity = TypeEntity( + null, + timestamp, + "name", + ) + + coEvery { repository.save(any()) } answers { call -> (call.invocation.args[0] as TypeEntity).copy(id = uuid) } + + // when + service.addType(entity) + + // then + coVerify(exactly = 1) { repository.save(any()) } + } + + @Test + fun `add new type exception`() = runTest { + // given + val entity: TypeEntity = mockk() + + coEvery { repository.save(any()) } throws DuplicateKeyException("duplicate key") + + // then exception + assertFailsWith { + service.addType(entity) + } + } }