Skip to content

Commit 82de213

Browse files
refactor: use only room entry fields to create a new room via api
1 parent b460c3d commit 82de213

File tree

7 files changed

+54
-78
lines changed

7 files changed

+54
-78
lines changed

src/main/kotlin/application/presenter/api/deserializer/ApiDeserializer.kt

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@
88

99
package application.presenter.api.deserializer
1010

11-
import application.presenter.api.model.EnvironmentalDataApiDto
1211
import application.presenter.api.model.MedicalTechnologyApiDto
1312
import application.presenter.api.model.MedicalTechnologyApiDtoType
14-
import application.presenter.api.model.RoomApiDto
1513
import application.presenter.api.model.RoomApiDtoType
16-
import entity.environment.Humidity
17-
import entity.environment.LightUnit
18-
import entity.environment.Luminosity
19-
import entity.environment.Presence
20-
import entity.environment.Temperature
21-
import entity.environment.TemperatureUnit
14+
import application.presenter.api.model.RoomEntry
2215
import entity.medicaltechnology.MedicalTechnology
2316
import entity.medicaltechnology.MedicalTechnologyID
2417
import entity.medicaltechnology.MedicalTechnologyType
2518
import entity.zone.Room
26-
import entity.zone.RoomEnvironmentalData
2719
import entity.zone.RoomID
2820
import entity.zone.RoomType
2921
import entity.zone.ZoneID
@@ -35,19 +27,11 @@ object ApiDeserializer {
3527
/**
3628
* Extension method to convert Room API DTO to [entity.zone.Room] class.
3729
*/
38-
fun RoomApiDto.toRoom() = Room(
30+
fun RoomEntry.toRoom() = Room(
3931
id = RoomID(this.id),
4032
type = this.type.toRoomType(),
4133
zoneId = ZoneID(this.zoneId),
42-
name = this.name,
43-
environmentalData = this.environmentalData.toRoomEnvironmentalData()
44-
)
45-
46-
private fun EnvironmentalDataApiDto.toRoomEnvironmentalData() = RoomEnvironmentalData(
47-
temperature = this.temperature?.let { Temperature(it.value, TemperatureUnit.valueOf(it.unit)) },
48-
humidity = this.humidity?.let { Humidity(it) },
49-
luminosity = this.luminosity?.let { Luminosity(it.value, LightUnit.valueOf(it.unit)) },
50-
presence = this.presence?.let { Presence(it) }
34+
name = this.name
5135
)
5236

5337
private fun RoomApiDtoType.toRoomType() = when (this) {

src/main/kotlin/application/presenter/api/model/RoomApiDto.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ package application.presenter.api.model
1111
import kotlinx.serialization.Serializable
1212

1313
/**
14-
* Presenter class to be able to deserialize data that comes from API.
15-
* It deserializes [id], [name], [zoneId], the [type] of the room and [environmentalData].
14+
* Presenter class to be able to serialize data returned by the API.
15+
* It is composed by [id], [name], [zoneId], the [type] of the room and [environmentalData].
1616
*/
1717
@Serializable
1818
data class RoomApiDto(
@@ -24,7 +24,19 @@ data class RoomApiDto(
2424
)
2525

2626
/**
27-
* Presenter enum class to deserialize room type that comes from API.
27+
* It represents the presentation of a single [entity.zone.Room] entry.
28+
* The necessary information are [id], [name], [zoneId] and the [type].
29+
*/
30+
@Serializable
31+
data class RoomEntry(
32+
val id: String,
33+
val name: String,
34+
val zoneId: String,
35+
val type: RoomApiDtoType
36+
)
37+
38+
/**
39+
* Presenter enum class to represent the room type.
2840
*/
2941
@Serializable
3042
enum class RoomApiDtoType {

src/main/kotlin/application/presenter/api/model/RoomEntry.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/kotlin/application/presenter/api/serializer/ApiSerializer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object ApiSerializer {
3333
id = this.id.value,
3434
name = this.name.orEmpty(),
3535
zoneId = this.zoneId.value,
36-
type = this.type.toString()
36+
type = this.type.toRoomApiDtoType()
3737
)
3838

3939
/**

src/main/kotlin/infrastructure/api/RoomApi.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package infrastructure.api
1010

1111
import application.controller.RoomController
1212
import application.presenter.api.deserializer.ApiDeserializer.toRoom
13-
import application.presenter.api.model.RoomApiDto
13+
import application.presenter.api.model.RoomEntry
1414
import application.presenter.api.serializer.ApiSerializer.toRoomApiDto
1515
import application.service.RoomService
1616
import entity.zone.RoomID
@@ -38,7 +38,7 @@ import java.time.Instant
3838
fun Application.roomAPI(apiPath: String, port: Int, provider: ManagerProvider) {
3939
routing {
4040
post("$apiPath/rooms") {
41-
val room = call.receive<RoomApiDto>().toRoom()
41+
val room = call.receive<RoomEntry>().toRoom()
4242
RoomService.CreateRoom(
4343
room,
4444
RoomController(provider.roomDigitalTwinManager, provider.roomDatabaseManager)

src/test/kotlin/application/presenter/api/ApiSerializationTest.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ApiSerializationTest : StringSpec({
5050
Presence(true)
5151
)
5252
)
53+
5354
val roomApiDto = RoomApiDto(
5455
"r1",
5556
"name",
@@ -63,6 +64,13 @@ class ApiSerializationTest : StringSpec({
6364
)
6465
)
6566

67+
val roomEntry = RoomEntry(
68+
id = "r1",
69+
name = "name",
70+
zoneId = "z1",
71+
type = RoomApiDtoType.OPERATING_ROOM
72+
)
73+
6674
val medicalTechnology = MedicalTechnology(
6775
id = MedicalTechnologyID("mt-1"),
6876
name = "name",
@@ -82,20 +90,15 @@ class ApiSerializationTest : StringSpec({
8290
)
8391

8492
"It should be possible to obtain the corresponding room from the data get from the API" {
85-
roomApiDto.toRoom() shouldBe room
93+
roomEntry.toRoom() shouldBe room
8694
}
8795

88-
"It should be possible to serialize a room in order to send it through the API" {
89-
room.toRoomApiDto() shouldBe roomApiDto
96+
"It should be possible to serialize a room in a room entry" {
97+
room.toRoomEntry() shouldBe roomEntry
9098
}
9199

92-
"It should be possible to serialize a room in a room entry" {
93-
room.toRoomEntry() shouldBe RoomEntry(
94-
id = "r1",
95-
name = "name",
96-
zoneId = "z1",
97-
type = "OPERATING_ROOM"
98-
)
100+
"It should be possible to serialize a room in order to send it through the API" {
101+
room.toRoomApiDto() shouldBe roomApiDto
99102
}
100103

101104
"It should be possible to obtain the corresponding medical technology from the data get from the API" {

src/test/kotlin/infrastructure/api/RoomApiTest.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import application.presenter.api.model.EnvironmentalDataApiDto
1212
import application.presenter.api.model.RoomApiDto
1313
import application.presenter.api.model.RoomApiDtoType
1414
import application.presenter.api.model.RoomEntry
15-
import application.presenter.api.model.ValueWithUnit
1615
import infrastructure.api.KtorTestingUtility.apiTestApplication
1716
import infrastructure.api.util.ApiResponses
1817
import io.kotest.assertions.ktor.client.shouldHaveStatus
@@ -35,67 +34,68 @@ import kotlinx.serialization.json.Json
3534
import java.time.Instant
3635

3736
class RoomApiTest : StringSpec({
37+
val roomEntry = RoomEntry(
38+
"r1",
39+
"name",
40+
"z1",
41+
RoomApiDtoType.OPERATING_ROOM
42+
)
3843
val roomApiDto = RoomApiDto(
3944
"r1",
4045
"name",
4146
"z1",
4247
RoomApiDtoType.OPERATING_ROOM,
43-
EnvironmentalDataApiDto(
44-
ValueWithUnit(33.0, "CELSIUS"),
45-
55.0,
46-
ValueWithUnit(150.0, "LUX"),
47-
true
48-
)
48+
EnvironmentalDataApiDto()
4949
)
5050

51-
suspend fun ApplicationTestBuilder.insertRoom(room: RoomApiDto) =
51+
suspend fun ApplicationTestBuilder.insertRoom(room: RoomEntry) =
5252
client.post("/api/v1/rooms") {
5353
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
5454
setBody(Json.encodeToString(room))
5555
}
5656

5757
"I should be able to request the creation of a room" {
5858
apiTestApplication {
59-
val response = insertRoom(roomApiDto)
59+
val response = insertRoom(roomEntry)
6060
response shouldHaveStatus HttpStatusCode.Created
6161
response.headers[HttpHeaders.Location] shouldNotBe null
6262
}
6363
}
6464

6565
"When there is a request to add an existent room then the service should respond with a conflict status code" {
6666
apiTestApplication {
67-
insertRoom(roomApiDto)
68-
val response = insertRoom(roomApiDto)
67+
insertRoom(roomEntry)
68+
val response = insertRoom(roomEntry)
6969
response shouldHaveStatus HttpStatusCode.Conflict
7070
}
7171
}
7272

7373
"It should be possible to obtain an existing room" {
7474
apiTestApplication {
75-
insertRoom(roomApiDto)
76-
val response = client.get("/api/v1/rooms/${roomApiDto.id}")
75+
insertRoom(roomEntry)
76+
val response = client.get("/api/v1/rooms/${roomEntry.id}")
7777
response shouldHaveStatus HttpStatusCode.OK
7878
Json.decodeFromString<RoomApiDto>(response.bodyAsText()) shouldBe roomApiDto
7979
}
8080
}
8181

8282
"It should be possible to obtain historical data about a room" {
8383
apiTestApplication {
84-
insertRoom(roomApiDto)
85-
val response = client.get("/api/v1/rooms/${roomApiDto.id}?dateTime=${Instant.now()}")
84+
insertRoom(roomEntry)
85+
val response = client.get("/api/v1/rooms/${roomEntry.id}?dateTime=${Instant.now()}")
8686
response shouldHaveStatus HttpStatusCode.OK
8787
}
8888
}
8989

9090
"It should handle the get request on a non existing room" {
9191
apiTestApplication {
92-
client.get("/api/v1/rooms/${roomApiDto.id}") shouldHaveStatus HttpStatusCode.NotFound
92+
client.get("/api/v1/rooms/${roomEntry.id}") shouldHaveStatus HttpStatusCode.NotFound
9393
}
9494
}
9595

9696
"It should be able to get all the room entries" {
9797
apiTestApplication {
98-
insertRoom(roomApiDto)
98+
insertRoom(roomEntry)
9999
val response = client.get("/api/v1/rooms")
100100
response shouldHaveStatus HttpStatusCode.OK
101101
Json.decodeFromString<ApiResponses.ResponseEntryList<ApiResponses.ResponseEntry<RoomEntry>>>(
@@ -116,14 +116,14 @@ class RoomApiTest : StringSpec({
116116

117117
"It should be possible to delete an existing room" {
118118
apiTestApplication {
119-
insertRoom(roomApiDto)
120-
client.delete("/api/v1/rooms/${roomApiDto.id}") shouldHaveStatus HttpStatusCode.NoContent
119+
insertRoom(roomEntry)
120+
client.delete("/api/v1/rooms/${roomEntry.id}") shouldHaveStatus HttpStatusCode.NoContent
121121
}
122122
}
123123

124124
"It should not be possible to delete a not-existent room" {
125125
apiTestApplication {
126-
client.delete("/api/v1/rooms/${roomApiDto.id}") shouldHaveStatus HttpStatusCode.NotFound
126+
client.delete("/api/v1/rooms/${roomEntry.id}") shouldHaveStatus HttpStatusCode.NotFound
127127
}
128128
}
129129
})

0 commit comments

Comments
 (0)