Add addType to TypeService
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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<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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ResponseStatusException> {
|
||||
service.addType(entity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user