add NodeController
This commit is contained in:
9
http/node.http
Normal file
9
http/node.http
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
### register node for a client and device with name
|
||||||
|
POST {{hostname}}/node
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"client": "00000000-0000-0000-0000-000000000000",
|
||||||
|
"device": "00000000-0000-0000-0000-000000000000",
|
||||||
|
"name": "my test device"
|
||||||
|
}
|
||||||
20
src/main/kotlin/ltd/hlaeja/controller/NodeController.kt
Normal file
20
src/main/kotlin/ltd/hlaeja/controller/NodeController.kt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package ltd.hlaeja.controller
|
||||||
|
|
||||||
|
import ltd.hlaeja.library.deviceRegistry.Node
|
||||||
|
import ltd.hlaeja.service.NodeService
|
||||||
|
import ltd.hlaeja.util.toEntity
|
||||||
|
import ltd.hlaeja.util.toNodeResponse
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody
|
||||||
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
class NodeController(
|
||||||
|
private val nodeService: NodeService,
|
||||||
|
) {
|
||||||
|
|
||||||
|
@PostMapping("/node")
|
||||||
|
suspend fun addNode(
|
||||||
|
@RequestBody request: Node.Request,
|
||||||
|
): Node.Response = nodeService.addNode(request.toEntity()).toNodeResponse()
|
||||||
|
}
|
||||||
50
src/test/kotlin/ltd/hlaeja/controller/NodeControllerTest.kt
Normal file
50
src/test/kotlin/ltd/hlaeja/controller/NodeControllerTest.kt
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package ltd.hlaeja.controller
|
||||||
|
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.mockk
|
||||||
|
import java.util.UUID
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import ltd.hlaeja.assertj.assertThat
|
||||||
|
import ltd.hlaeja.entity.NodeEntity
|
||||||
|
import ltd.hlaeja.library.deviceRegistry.Node
|
||||||
|
import ltd.hlaeja.service.NodeService
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
class NodeControllerTest {
|
||||||
|
|
||||||
|
val service: NodeService = mockk()
|
||||||
|
|
||||||
|
lateinit var controller: NodeController
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun setUp() {
|
||||||
|
controller = NodeController(service)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `add device - success`() = runTest {
|
||||||
|
// given
|
||||||
|
val request: Node.Request = Node.Request(
|
||||||
|
UUID.fromString("00000000-0000-0000-0000-000000000001"),
|
||||||
|
UUID.fromString("00000000-0000-0000-0000-000000000002"),
|
||||||
|
"test",
|
||||||
|
)
|
||||||
|
coEvery { service.addNode(any()) } answers { call ->
|
||||||
|
(call.invocation.args[0] as NodeEntity).copy(id = UUID.fromString("00000000-0000-0000-0000-000000000003"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// when
|
||||||
|
val response = controller.addNode(request)
|
||||||
|
|
||||||
|
// then
|
||||||
|
coVerify(exactly = 1) { service.addNode(any()) }
|
||||||
|
|
||||||
|
assertThat(response.id).isUUID("00000000-0000-0000-0000-000000000003")
|
||||||
|
assertThat(response.client).isUUID("00000000-0000-0000-0000-000000000001")
|
||||||
|
assertThat(response.device).isUUID("00000000-0000-0000-0000-000000000002")
|
||||||
|
assertThat(response.name).isEqualTo("test")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user