Add TypeController
This commit is contained in:
2
http/type.http
Normal file
2
http/type.http
Normal file
@@ -0,0 +1,2 @@
|
||||
### let all types
|
||||
GET {{hostname}}/types
|
||||
18
src/main/kotlin/ltd/hlaeja/controller/TypeController.kt
Normal file
18
src/main/kotlin/ltd/hlaeja/controller/TypeController.kt
Normal 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() }
|
||||
}
|
||||
49
src/test/kotlin/ltd/hlaeja/controller/TypeControllerTest.kt
Normal file
49
src/test/kotlin/ltd/hlaeja/controller/TypeControllerTest.kt
Normal 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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user