diff --git a/http/device.http b/http/device.http new file mode 100644 index 0000000..156ce3b --- /dev/null +++ b/http/device.http @@ -0,0 +1,7 @@ +### register device for a type +POST {{hostname}}/device +Content-Type: application/json + +{ + "type": "00000000-0000-0000-0000-000000000000" +} diff --git a/src/main/kotlin/ltd/hlaeja/controller/DeviceController.kt b/src/main/kotlin/ltd/hlaeja/controller/DeviceController.kt new file mode 100644 index 0000000..e50f699 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/controller/DeviceController.kt @@ -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) } +} diff --git a/src/test/kotlin/ltd/hlaeja/controller/DeviceControllerTest.kt b/src/test/kotlin/ltd/hlaeja/controller/DeviceControllerTest.kt new file mode 100644 index 0000000..c4ce89f --- /dev/null +++ b/src/test/kotlin/ltd/hlaeja/controller/DeviceControllerTest.kt @@ -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 { + controller.addDevice(request) + } + + // then + assertThat(exception.message).isEqualTo("417 EXPECTATION_FAILED") + } +} diff --git a/src/test/kotlin/ltd/hlaeja/service/DeviceServiceTest.kt b/src/test/kotlin/ltd/hlaeja/service/DeviceServiceTest.kt index b9ac546..3c81dbe 100644 --- a/src/test/kotlin/ltd/hlaeja/service/DeviceServiceTest.kt +++ b/src/test/kotlin/ltd/hlaeja/service/DeviceServiceTest.kt @@ -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) } }