Add TypeController

This commit is contained in:
2024-11-18 15:57:08 +01:00
parent 96f09bc749
commit b5d1c04d6b
3 changed files with 69 additions and 0 deletions

2
http/type.http Normal file
View File

@@ -0,0 +1,2 @@
### let all types
GET {{hostname}}/types

View File

@@ -0,0 +1,18 @@
package ltd.hlaeja.controller
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.toTypeResponse
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class TypeController(
private val service: TypeService,
) {
@GetMapping("/types")
fun getTypes(): Flow<Type.Response> = service.getTypes().map { it.toTypeResponse() }
}

View File

@@ -0,0 +1,49 @@
package ltd.hlaeja.controller
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.util.UUID
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.single
import kotlinx.coroutines.test.runTest
import ltd.hlaeja.entity.TypeEntity
import ltd.hlaeja.service.TypeService
import ltd.hlaeja.assertj.assertThat
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
class TypeControllerTest {
companion object {
val id = UUID.fromString("00000000-0000-0000-0000-000000000000")
val timestamp = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 1), ZoneId.of("UTC"))
}
val service: TypeService = mockk()
lateinit var controller: TypeController
@BeforeEach
fun setUp() {
controller = TypeController(service)
}
@Test
fun `get all types`() = runTest {
// given
every { service.getTypes() } returns flowOf(TypeEntity(id, timestamp, "name"))
// when
val response = controller.getTypes().single()
// then
verify(exactly = 1) { service.getTypes() }
assertThat(response.id).isUUID("00000000-0000-0000-0000-000000000000")
assertThat(response.name).isEqualTo("name")
}
}