update for getDevice

- add get device in DeviceController
- add get device in DeviceService
- update logging
- update and cleanup in README.md
This commit is contained in:
2024-12-11 00:05:34 +01:00
parent e7dbbc7a78
commit df8e1a7b48
9 changed files with 134 additions and 62 deletions

View File

@@ -1,9 +1,12 @@
package ltd.hlaeja.controller
import java.util.UUID
import ltd.hlaeja.library.deviceRegistry.Device
import ltd.hlaeja.service.DeviceService
import ltd.hlaeja.service.JwtService
import ltd.hlaeja.util.toDeviceResponse
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
@@ -19,4 +22,10 @@ class DeviceController(
@RequestBody request: Device.Request,
): Device.Response = deviceService.addDevice(request.type)
.toDeviceResponse(jwtService)
@GetMapping("/device-{device}")
suspend fun getDevice(
@PathVariable device: UUID,
): Device.Response = deviceService.getDevice(device)
.toDeviceResponse(jwtService)
}

View File

@@ -1,11 +1,13 @@
package ltd.hlaeja.service
import io.github.oshai.kotlinlogging.KotlinLogging
import java.time.ZonedDateTime
import java.util.UUID
import ltd.hlaeja.entity.DeviceEntity
import ltd.hlaeja.repository.DeviceRepository
import mu.KotlinLogging
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException
private val log = KotlinLogging.logger {}
@@ -18,4 +20,8 @@ class DeviceService(
type: UUID,
): DeviceEntity = deviceRepository.save(DeviceEntity(null, ZonedDateTime.now(), type))
.also { log.debug { "Added device ${it.id}" } }
suspend fun getDevice(device: UUID): DeviceEntity = deviceRepository.findById(device)
?.also { log.debug { "Get device ${it.id}" } }
?: throw ResponseStatusException(NOT_FOUND)
}

View File

@@ -1,9 +1,9 @@
package ltd.hlaeja.service
import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.UUID
import ltd.hlaeja.entity.NodeEntity
import ltd.hlaeja.repository.NodeRepository
import mu.KotlinLogging
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException

View File

@@ -1,9 +1,9 @@
package ltd.hlaeja.service
import io.github.oshai.kotlinlogging.KotlinLogging
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
@@ -22,9 +22,9 @@ class TypeService(
entity: TypeEntity,
): TypeEntity = try {
typeRepository.save(entity)
.also { log.debug("Added new type: {}", it.id) }
.also { log.debug { "Added new type: $it.id" } }
} catch (e: DuplicateKeyException) {
log.warn(e.localizedMessage)
log.warn { e.localizedMessage }
throw ResponseStatusException(HttpStatus.CONFLICT)
}
}

View File

@@ -14,6 +14,7 @@ import ltd.hlaeja.service.DeviceService
import ltd.hlaeja.service.JwtService
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.web.server.ResponseStatusException
@@ -36,35 +37,59 @@ class DeviceControllerTest {
controller = DeviceController(deviceService, jwtService)
}
@Test
fun `add device - success`() = runTest {
// given
val request = Device.Request(uuid)
coEvery { deviceService.addDevice(any()) } returns DeviceEntity(uuid, timestamp, uuid)
coEvery { jwtService.makeIdentity(any()) } returns PAYLOAD
@Nested
inner class AddDeviceTest {
// when
val response = controller.addDevice(request)
@Test
fun `add device - success`() = runTest {
// given
val request = Device.Request(uuid)
coEvery { deviceService.addDevice(any()) } returns DeviceEntity(uuid, timestamp, uuid)
coEvery { jwtService.makeIdentity(any()) } returns PAYLOAD
// then
coVerify(exactly = 1) { deviceService.addDevice(any()) }
coVerify(exactly = 1) { jwtService.makeIdentity(any()) }
// when
val response = controller.addDevice(request)
assertThat(response.identity).isEqualTo(PAYLOAD)
}
// then
coVerify(exactly = 1) { deviceService.addDevice(any()) }
coVerify(exactly = 1) { jwtService.makeIdentity(any()) }
@Test
fun `add device - device service fail`() = runTest {
// given
val request = Device.Request(uuid)
coEvery { deviceService.addDevice(any()) } returns DeviceEntity(null, timestamp, uuid)
// when exception
val exception = assertThrows<ResponseStatusException> {
controller.addDevice(request)
assertThat(response.identity).isEqualTo(PAYLOAD)
}
// then
assertThat(exception.message).isEqualTo("417 EXPECTATION_FAILED")
@Test
fun `add device - device service fail`() = runTest {
// given
val request = Device.Request(uuid)
coEvery { deviceService.addDevice(any()) } returns DeviceEntity(null, timestamp, uuid)
// when exception
val exception = assertThrows<ResponseStatusException> {
controller.addDevice(request)
}
// then
assertThat(exception.message).isEqualTo("417 EXPECTATION_FAILED")
}
}
@Nested
inner class GetDeviceTest {
@Test
fun `get device - success`() = runTest {
// given
coEvery { deviceService.getDevice(any()) } returns DeviceEntity(uuid, timestamp, uuid)
coEvery { jwtService.makeIdentity(any()) } returns PAYLOAD
// when
val response = controller.getDevice(uuid)
// then
coVerify(exactly = 1) { deviceService.getDevice(any()) }
coVerify(exactly = 1) { jwtService.makeIdentity(any()) }
assertThat(response.identity).isEqualTo(PAYLOAD)
}
}
}

View File

@@ -17,6 +17,8 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.web.server.ResponseStatusException
class DeviceServiceTest {
companion object {
@@ -42,7 +44,7 @@ class DeviceServiceTest {
}
@Test
fun `add new type success`() = runTest {
fun `add new device success`() = runTest {
// given
coEvery { repository.save(any()) } answers { call ->
(call.invocation.args[0] as DeviceEntity).copy(id = device)
@@ -58,4 +60,37 @@ class DeviceServiceTest {
assertThat(result.timestamp.toString()).isEqualTo("2000-01-01T00:00:00.001Z[UTC]")
assertThat(result.type).isEqualTo(type)
}
@Test
fun `get device - success`() = runTest {
// given
val device = UUID.fromString("00000000-0000-0000-0000-000000000000")
val entity: DeviceEntity = mockk()
coEvery { repository.findById(any()) } returns entity
coEvery { entity.id } returns device
// when
val result = service.getDevice(type)
// then
coVerify(exactly = 1) { repository.findById(any()) }
assertThat(result.id).isEqualTo(device)
}
@Test
fun `get device - fail not found`() = runTest {
// given
val device = UUID.fromString("00000000-0000-0000-0000-000000000000")
coEvery { repository.findById(any()) } returns null
// when
val exception = assertThrows<ResponseStatusException> {
service.getDevice(device)
}
// then
assertThat(exception.message).isEqualTo("404 NOT_FOUND")
}
}