Add addType to TypeController
This commit is contained in:
@@ -1,2 +1,11 @@
|
||||
### let all types
|
||||
GET {{hostname}}/types
|
||||
|
||||
|
||||
### add type by name
|
||||
POST {{hostname}}/type
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "Test C"
|
||||
}
|
||||
|
||||
@@ -4,8 +4,11 @@ import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import ltd.hlaeja.library.deviceRegistry.Type
|
||||
import ltd.hlaeja.service.TypeService
|
||||
import ltd.hlaeja.util.toTypeEntity
|
||||
import ltd.hlaeja.util.toTypeResponse
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@@ -15,4 +18,9 @@ class TypeController(
|
||||
|
||||
@GetMapping("/types")
|
||||
fun getTypes(): Flow<Type.Response> = service.getTypes().map { it.toTypeResponse() }
|
||||
|
||||
@PostMapping("/type")
|
||||
suspend fun addType(
|
||||
@RequestBody register: Type.Request,
|
||||
): Type.Response = service.addType(register.toTypeEntity()).toTypeResponse()
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ltd.hlaeja.controller
|
||||
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
@@ -11,6 +13,7 @@ import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.single
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import ltd.hlaeja.entity.TypeEntity
|
||||
import ltd.hlaeja.library.deviceRegistry.Type
|
||||
import ltd.hlaeja.service.TypeService
|
||||
import ltd.hlaeja.assertj.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
@@ -46,4 +49,20 @@ class TypeControllerTest {
|
||||
assertThat(response.id).isUUID("00000000-0000-0000-0000-000000000000")
|
||||
assertThat(response.name).isEqualTo("name")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `add types`() = runTest {
|
||||
// given
|
||||
val request = Type.Request("name")
|
||||
coEvery { service.addType(any()) } returns TypeEntity(id, timestamp, "name")
|
||||
|
||||
// when
|
||||
val response = controller.addType(request)
|
||||
|
||||
// then
|
||||
coVerify(exactly = 1) { service.addType(any()) }
|
||||
|
||||
assertThat(response.id).isUUID("00000000-0000-0000-0000-000000000000")
|
||||
assertThat(response.name).isEqualTo("name")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user