Add addType to TypeService

This commit is contained in:
2024-11-19 12:43:39 +01:00
parent b5d1c04d6b
commit 2a98cdf38f
3 changed files with 63 additions and 0 deletions

View File

@@ -7,6 +7,8 @@ plugins {
} }
dependencies { dependencies {
implementation(hlaeja.com.fasterxml.jackson.module.kotlin)
implementation(hlaeja.kotlin.logging)
implementation(hlaeja.kotlin.reflect) implementation(hlaeja.kotlin.reflect)
implementation(hlaeja.kotlinx.coroutines) implementation(hlaeja.kotlinx.coroutines)
implementation(hlaeja.ltd.hlaeja.library.common.messages) implementation(hlaeja.ltd.hlaeja.library.common.messages)

View File

@@ -3,7 +3,13 @@ package ltd.hlaeja.service
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import ltd.hlaeja.entity.TypeEntity import ltd.hlaeja.entity.TypeEntity
import ltd.hlaeja.repository.TypeRepository 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.stereotype.Service
import org.springframework.web.server.ResponseStatusException
private val log = KotlinLogging.logger {}
@Service @Service
class TypeService( class TypeService(
@@ -11,4 +17,14 @@ class TypeService(
) { ) {
fun getTypes(): Flow<TypeEntity> = typeRepository.findAll() fun getTypes(): Flow<TypeEntity> = 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)
}
} }

View File

@@ -1,15 +1,29 @@
package ltd.hlaeja.service package ltd.hlaeja.service
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify 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.flow.flowOf
import kotlinx.coroutines.test.runTest
import ltd.hlaeja.entity.TypeEntity import ltd.hlaeja.entity.TypeEntity
import ltd.hlaeja.repository.TypeRepository import ltd.hlaeja.repository.TypeRepository
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.springframework.dao.DuplicateKeyException
import org.springframework.web.server.ResponseStatusException
class TypeServiceTest { 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() val repository: TypeRepository = mockk()
@@ -31,4 +45,35 @@ class TypeServiceTest {
// then // then
verify(exactly = 1) { repository.findAll() } 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<ResponseStatusException> {
service.addType(entity)
}
}
} }