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

@@ -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")
}
}