add IdentityController
This commit is contained in:
2
http/identity.http
Normal file
2
http/identity.http
Normal file
@@ -0,0 +1,2 @@
|
||||
### get identity
|
||||
GET {{hostname}}/identity/device-00000000-0000-0000-0000-000000000001
|
||||
22
src/main/kotlin/ltd/hlaeja/controller/IdentityController.kt
Normal file
22
src/main/kotlin/ltd/hlaeja/controller/IdentityController.kt
Normal file
@@ -0,0 +1,22 @@
|
||||
package ltd.hlaeja.controller
|
||||
|
||||
import java.util.UUID
|
||||
import ltd.hlaeja.library.deviceRegistry.Identity
|
||||
import ltd.hlaeja.service.NodeService
|
||||
import ltd.hlaeja.util.toIdentityResponse
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/identity")
|
||||
class IdentityController(
|
||||
private val nodeService: NodeService,
|
||||
) {
|
||||
|
||||
@GetMapping("/device-{device}")
|
||||
suspend fun getIdentityFromDevice(
|
||||
@PathVariable device: UUID,
|
||||
): Identity.Response = nodeService.getNodeFromDevice(device).toIdentityResponse()
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
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.assertj.assertThat
|
||||
import ltd.hlaeja.entity.NodeEntity
|
||||
import ltd.hlaeja.service.NodeService
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class IdentityControllerTest {
|
||||
companion object {
|
||||
val timestamp: ZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 1), ZoneId.of("UTC"))
|
||||
}
|
||||
|
||||
val service: NodeService = mockk()
|
||||
|
||||
lateinit var controller: IdentityController
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
controller = IdentityController(service)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get node by device - success`() = runTest {
|
||||
// given
|
||||
val device = UUID.fromString("00000000-0000-0000-0000-000000000003")
|
||||
val entity = NodeEntity(
|
||||
UUID.fromString("00000000-0000-0000-0000-000000000001"),
|
||||
timestamp,
|
||||
UUID.fromString("00000000-0000-0000-0000-000000000002"),
|
||||
device,
|
||||
"test",
|
||||
)
|
||||
|
||||
coEvery { service.getNodeFromDevice(any()) } returns entity
|
||||
|
||||
// when
|
||||
val response = controller.getIdentityFromDevice(device)
|
||||
|
||||
// then
|
||||
coVerify(exactly = 1) { service.getNodeFromDevice(any()) }
|
||||
|
||||
assertThat(response.node).isUUID("00000000-0000-0000-0000-000000000001")
|
||||
assertThat(response.client).isUUID("00000000-0000-0000-0000-000000000002")
|
||||
assertThat(response.device).isUUID("00000000-0000-0000-0000-000000000003")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user