@@ -11,18 +11,27 @@ package infrastructure.api
1111import application.presenter.api.model.EnvironmentalDataApiDto
1212import application.presenter.api.model.RoomApiDto
1313import application.presenter.api.model.RoomApiDtoType
14+ import application.presenter.api.model.RoomEntry
1415import application.presenter.api.model.ValueWithUnit
1516import infrastructure.api.KtorTestingUtility.apiTestApplication
17+ import infrastructure.api.util.ApiResponses
1618import io.kotest.assertions.ktor.client.shouldHaveStatus
1719import io.kotest.core.spec.style.StringSpec
20+ import io.kotest.matchers.shouldBe
21+ import io.ktor.client.request.delete
22+ import io.ktor.client.request.get
1823import io.ktor.client.request.header
1924import io.ktor.client.request.post
2025import io.ktor.client.request.setBody
26+ import io.ktor.client.statement.bodyAsText
2127import io.ktor.http.ContentType
2228import io.ktor.http.HttpHeaders
2329import io.ktor.http.HttpStatusCode
30+ import io.ktor.server.testing.ApplicationTestBuilder
31+ import kotlinx.serialization.decodeFromString
2432import kotlinx.serialization.encodeToString
2533import kotlinx.serialization.json.Json
34+ import java.time.Instant
2635
2736class RoomApiTest : StringSpec ({
2837 val roomApiDto = RoomApiDto (
@@ -38,13 +47,81 @@ class RoomApiTest : StringSpec({
3847 )
3948 )
4049
50+ suspend fun ApplicationTestBuilder .insertRoom(room: RoomApiDto ) =
51+ client.post("/api/v1/rooms") {
52+ header(HttpHeaders .ContentType , ContentType .Application .Json .toString())
53+ setBody(Json .encodeToString(room))
54+ }
55+
4156 " I should be able to request the creation of a room" {
4257 apiTestApplication {
43- val response = client.post("/api/v1/rooms") {
44- header(HttpHeaders .ContentType , ContentType .Application .Json .toString())
45- setBody(Json .encodeToString(roomApiDto))
46- }
58+ val response = insertRoom(roomApiDto)
4759 response shouldHaveStatus HttpStatusCode .Created
4860 }
4961 }
62+
63+ " When there is a request to add an existent room then the service should respond with a conflict status code" {
64+ apiTestApplication {
65+ insertRoom(roomApiDto)
66+ val response = insertRoom(roomApiDto)
67+ response shouldHaveStatus HttpStatusCode .Conflict
68+ }
69+ }
70+
71+ " It should be possible to obtain an existing room" {
72+ apiTestApplication {
73+ insertRoom(roomApiDto)
74+ val response = client.get("/api/v1/rooms/${roomApiDto.id}")
75+ response shouldHaveStatus HttpStatusCode .OK
76+ Json .decodeFromString<RoomApiDto >(response.bodyAsText()) shouldBe roomApiDto
77+ }
78+ }
79+
80+ " It should be possible to obtain historical data about a room" {
81+ apiTestApplication {
82+ insertRoom(roomApiDto)
83+ val response = client.get("/api/v1/rooms/${roomApiDto.id}? dateTime=${Instant .now()}")
84+ response shouldHaveStatus HttpStatusCode .OK
85+ }
86+ }
87+
88+ " It should handle the get request on a non existing room" {
89+ apiTestApplication {
90+ client.get("/api/v1/rooms/${roomApiDto.id}") shouldHaveStatus HttpStatusCode .NotFound
91+ }
92+ }
93+
94+ " It should be able to get all the room entries" {
95+ apiTestApplication {
96+ insertRoom(roomApiDto)
97+ val response = client.get("/api/v1/rooms")
98+ response shouldHaveStatus HttpStatusCode .OK
99+ Json .decodeFromString<ApiResponses .ResponseEntryList <ApiResponses .ResponseEntry <RoomEntry >>>(
100+ response.bodyAsText()
101+ ).total shouldBe 1
102+ }
103+ }
104+
105+ " It should be able to get all the room entries, but when there aren't anyone should return no content status" {
106+ apiTestApplication {
107+ val response = client.get("/api/v1/rooms")
108+ response shouldHaveStatus HttpStatusCode .NoContent
109+ Json .decodeFromString<ApiResponses .ResponseEntryList <ApiResponses .ResponseEntry <RoomEntry >>>(
110+ response.bodyAsText()
111+ ).total shouldBe 0
112+ }
113+ }
114+
115+ " It should be possible to delete an existing room" {
116+ apiTestApplication {
117+ insertRoom(roomApiDto)
118+ client.delete("/api/v1/rooms/${roomApiDto.id}") shouldHaveStatus HttpStatusCode .NoContent
119+ }
120+ }
121+
122+ " It should not be possible to delete a not-existent room" {
123+ apiTestApplication {
124+ client.delete("/api/v1/rooms/${roomApiDto.id}") shouldHaveStatus HttpStatusCode .NotFound
125+ }
126+ }
50127})
0 commit comments