Add addType to TypeController

This commit is contained in:
2024-11-19 14:11:19 +01:00
parent ba6f38a585
commit 16784e4aff
3 changed files with 36 additions and 0 deletions

View File

@@ -1,2 +1,11 @@
### let all types ### let all types
GET {{hostname}}/types GET {{hostname}}/types
### add type by name
POST {{hostname}}/type
Content-Type: application/json
{
"name": "Test C"
}

View File

@@ -4,8 +4,11 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import ltd.hlaeja.library.deviceRegistry.Type import ltd.hlaeja.library.deviceRegistry.Type
import ltd.hlaeja.service.TypeService import ltd.hlaeja.service.TypeService
import ltd.hlaeja.util.toTypeEntity
import ltd.hlaeja.util.toTypeResponse import ltd.hlaeja.util.toTypeResponse
import org.springframework.web.bind.annotation.GetMapping 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 import org.springframework.web.bind.annotation.RestController
@RestController @RestController
@@ -15,4 +18,9 @@ class TypeController(
@GetMapping("/types") @GetMapping("/types")
fun getTypes(): Flow<Type.Response> = service.getTypes().map { it.toTypeResponse() } 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()
} }

View File

@@ -1,5 +1,7 @@
package ltd.hlaeja.controller package ltd.hlaeja.controller
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify import io.mockk.verify
@@ -11,6 +13,7 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.single import kotlinx.coroutines.flow.single
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import ltd.hlaeja.entity.TypeEntity import ltd.hlaeja.entity.TypeEntity
import ltd.hlaeja.library.deviceRegistry.Type
import ltd.hlaeja.service.TypeService import ltd.hlaeja.service.TypeService
import ltd.hlaeja.assertj.assertThat import ltd.hlaeja.assertj.assertThat
import org.assertj.core.api.Assertions.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.id).isUUID("00000000-0000-0000-0000-000000000000")
assertThat(response.name).isEqualTo("name") 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")
}
} }