add DeviceController
This commit is contained in:
24
src/main/kotlin/ltd/hlaeja/controller/DeviceController.kt
Normal file
24
src/main/kotlin/ltd/hlaeja/controller/DeviceController.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
package ltd.hlaeja.controller
|
||||
|
||||
import ltd.hlaeja.library.deviceRegistry.Device
|
||||
import ltd.hlaeja.service.DeviceService
|
||||
import ltd.hlaeja.service.JwtService
|
||||
import org.springframework.http.HttpStatus.EXPECTATION_FAILED
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
@RestController
|
||||
class DeviceController(
|
||||
private val deviceService: DeviceService,
|
||||
private val jwtService: JwtService,
|
||||
) {
|
||||
|
||||
@PostMapping("/device")
|
||||
suspend fun addDevice(
|
||||
@RequestBody request: Device.Request,
|
||||
): Device.Identity = deviceService.addDevice(request.type)
|
||||
.let { jwtService.makeIdentity(it.id ?: throw ResponseStatusException(EXPECTATION_FAILED)) }
|
||||
.let { Device.Identity(it) }
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package ltd.hlaeja.controller
|
||||
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.mockk
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.UUID
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import ltd.hlaeja.entity.DeviceEntity
|
||||
import ltd.hlaeja.library.deviceRegistry.Device
|
||||
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.Test
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
class DeviceControllerTest {
|
||||
companion object {
|
||||
const val PAYLOAD: String = "head.body.signature"
|
||||
|
||||
val uuid = UUID.fromString("00000000-0000-0000-0000-000000000000")
|
||||
val timestamp = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 1), ZoneId.of("UTC"))
|
||||
}
|
||||
|
||||
val deviceService: DeviceService = mockk()
|
||||
val jwtService: JwtService = mockk()
|
||||
|
||||
lateinit var controller: DeviceController
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
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
|
||||
|
||||
// when
|
||||
val response = controller.addDevice(request)
|
||||
|
||||
// then
|
||||
coVerify(exactly = 1) { deviceService.addDevice(any()) }
|
||||
coVerify(exactly = 1) { jwtService.makeIdentity(any()) }
|
||||
|
||||
assertThat(response.identity).isEqualTo(PAYLOAD)
|
||||
}
|
||||
|
||||
@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")
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import java.util.UUID
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import ltd.hlaeja.entity.DeviceEntity
|
||||
import ltd.hlaeja.repository.DeviceRepository
|
||||
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
|
||||
@@ -20,7 +21,6 @@ import org.junit.jupiter.api.Test
|
||||
class DeviceServiceTest {
|
||||
companion object {
|
||||
val timestamp = ZonedDateTime.ofInstant(Instant.parse("2000-01-01T00:00:00.001Z"), ZoneId.of("UTC"))
|
||||
|
||||
val device = UUID.fromString("00000000-0000-0000-0000-000000000001")
|
||||
val type = UUID.fromString("00000000-0000-0000-0000-000000000002")
|
||||
}
|
||||
@@ -53,8 +53,9 @@ class DeviceServiceTest {
|
||||
|
||||
// then
|
||||
coVerify(exactly = 1) { repository.save(any()) }
|
||||
kotlin.test.assertEquals(device, result.id)
|
||||
kotlin.test.assertEquals("2000-01-01T00:00:00.001Z[UTC]", result.timestamp.toString())
|
||||
kotlin.test.assertEquals(type, result.type)
|
||||
|
||||
assertThat(result.id).isEqualTo(device)
|
||||
assertThat(result.timestamp.toString()).isEqualTo("2000-01-01T00:00:00.001Z[UTC]")
|
||||
assertThat(result.type).isEqualTo(type)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user