Skip to content

Commit 7d2ec1d

Browse files
refactor: use only medical technology entry fields to create a new medical technology via api
1 parent 82de213 commit 7d2ec1d

File tree

5 files changed

+59
-33
lines changed

5 files changed

+59
-33
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
package application.presenter.api.deserializer
1010

11-
import application.presenter.api.model.MedicalTechnologyApiDto
1211
import application.presenter.api.model.MedicalTechnologyApiDtoType
12+
import application.presenter.api.model.MedicalTechnologyEntry
1313
import application.presenter.api.model.RoomApiDtoType
1414
import application.presenter.api.model.RoomEntry
1515
import entity.medicaltechnology.MedicalTechnology
@@ -25,7 +25,7 @@ import entity.zone.ZoneID
2525
*/
2626
object ApiDeserializer {
2727
/**
28-
* Extension method to convert Room API DTO to [entity.zone.Room] class.
28+
* Extension method to convert Room Entry to [entity.zone.Room] class.
2929
*/
3030
fun RoomEntry.toRoom() = Room(
3131
id = RoomID(this.id),
@@ -40,15 +40,13 @@ object ApiDeserializer {
4040
}
4141

4242
/**
43-
* Extension method to convert Medical Technology API DTO to [entity.medicaltechnology.MedicalTechnology] class.
43+
* Extension method to convert Medical Technology Entry to [entity.medicaltechnology.MedicalTechnology] class.
4444
*/
45-
fun MedicalTechnologyApiDto.toMedicalTechnology() = MedicalTechnology(
45+
fun MedicalTechnologyEntry.toMedicalTechnology() = MedicalTechnology(
4646
id = MedicalTechnologyID(this.id),
4747
name = this.name,
4848
description = this.description,
49-
type = this.type.toMedicalTechnologyType(),
50-
isInUse = this.inUse,
51-
roomId = this.roomId?.let { RoomID(it) }
49+
type = this.type.toMedicalTechnologyType()
5250
)
5351

5452
private fun MedicalTechnologyApiDtoType.toMedicalTechnologyType() = when (this) {

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

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

1313
/**
14-
* Presenter class to be able to handle data that comes from API.
14+
* Presenter class to be able to handle data that is returned to the API.
1515
* It handles [id], [name], [description], [type], [inUse] and the [roomId].
1616
*/
1717
@Serializable
@@ -25,7 +25,19 @@ data class MedicalTechnologyApiDto(
2525
)
2626

2727
/**
28-
* Presenter enum to deserialize medical technology type that comes from API.
28+
* It represents the presentation of a single [entity.medicaltechnology.MedicalTechnology] entry.
29+
* The necessary information are [id], [name], [description] and the [type].
30+
*/
31+
@Serializable
32+
data class MedicalTechnologyEntry(
33+
val id: String,
34+
val name: String,
35+
val description: String,
36+
val type: MedicalTechnologyApiDtoType
37+
)
38+
39+
/**
40+
* Presenter enum to handle medical technology type.
2941
*/
3042
@Serializable
3143
enum class MedicalTechnologyApiDtoType {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package infrastructure.api
1111
import application.controller.MedicalTechnologyController
1212
import application.controller.RoomController
1313
import application.presenter.api.deserializer.ApiDeserializer.toMedicalTechnology
14-
import application.presenter.api.model.MedicalTechnologyApiDto
14+
import application.presenter.api.model.MedicalTechnologyEntry
1515
import application.presenter.api.model.MedicalTechnologyPatch
1616
import application.presenter.api.serializer.ApiSerializer.toMedicalTechnologyApiDto
1717
import application.service.MedicalTechnologyService
@@ -41,7 +41,7 @@ import java.time.Instant
4141
fun Application.medicalTechnologyAPI(apiPath: String, port: Int, provider: ManagerProvider) {
4242
routing {
4343
post("$apiPath/medical-technologies") {
44-
val medicalTechnology = call.receive<MedicalTechnologyApiDto>().toMedicalTechnology()
44+
val medicalTechnology = call.receive<MedicalTechnologyEntry>().toMedicalTechnology()
4545
MedicalTechnologyService.CreateMedicalTechnology(
4646
medicalTechnology,
4747
MedicalTechnologyController(

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import application.presenter.api.deserializer.ApiDeserializer.toRoom
1313
import application.presenter.api.model.EnvironmentalDataApiDto
1414
import application.presenter.api.model.MedicalTechnologyApiDto
1515
import application.presenter.api.model.MedicalTechnologyApiDtoType
16+
import application.presenter.api.model.MedicalTechnologyEntry
1617
import application.presenter.api.model.RoomApiDto
1718
import application.presenter.api.model.RoomApiDtoType
1819
import application.presenter.api.model.RoomEntry
@@ -89,6 +90,13 @@ class ApiSerializationTest : StringSpec({
8990
roomId = "r-1"
9091
)
9192

93+
val medicalTechnologyEntry = MedicalTechnologyEntry(
94+
id = "mt-1",
95+
name = "name",
96+
description = "description",
97+
type = MedicalTechnologyApiDtoType.ENDOSCOPE
98+
)
99+
92100
"It should be possible to obtain the corresponding room from the data get from the API" {
93101
roomEntry.toRoom() shouldBe room
94102
}
@@ -102,7 +110,7 @@ class ApiSerializationTest : StringSpec({
102110
}
103111

104112
"It should be possible to obtain the corresponding medical technology from the data get from the API" {
105-
medicalTechnologyApiDto.toMedicalTechnology() shouldBe medicalTechnology
113+
medicalTechnologyEntry.toMedicalTechnology() shouldBe medicalTechnology
106114
}
107115

108116
"It should be possible to serialize a medical technology in order to send it through the API" {

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

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

1111
import application.presenter.api.model.MedicalTechnologyApiDto
1212
import application.presenter.api.model.MedicalTechnologyApiDtoType
13+
import application.presenter.api.model.MedicalTechnologyEntry
1314
import application.presenter.api.model.MedicalTechnologyPatch
14-
import application.presenter.api.model.RoomApiDto
1515
import application.presenter.api.model.RoomApiDtoType
16+
import application.presenter.api.model.RoomEntry
1617
import infrastructure.api.KtorTestingUtility.apiTestApplication
1718
import io.kotest.assertions.ktor.client.shouldHaveStatus
1819
import io.kotest.core.spec.style.StringSpec
@@ -35,73 +36,80 @@ import kotlinx.serialization.json.Json
3536
import java.time.Instant
3637

3738
class MedicalTechnologyApiTest : StringSpec({
39+
val medicalTechnologyEntry = MedicalTechnologyEntry(
40+
id = "mt-1",
41+
name = "name",
42+
description = "description",
43+
type = MedicalTechnologyApiDtoType.ENDOSCOPE
44+
)
45+
3846
val medicalTechnologyApiDto = MedicalTechnologyApiDto(
3947
id = "mt-1",
4048
name = "name",
4149
description = "description",
4250
type = MedicalTechnologyApiDtoType.ENDOSCOPE,
43-
inUse = true,
44-
roomId = "r-1"
51+
inUse = false,
52+
roomId = null
4553
)
4654

47-
suspend fun ApplicationTestBuilder.insertMedicalTechnology(medicalTechnology: MedicalTechnologyApiDto) =
55+
suspend fun ApplicationTestBuilder.insertMedicalTechnology(medicalTechnology: MedicalTechnologyEntry) =
4856
client.post("/api/v1/medical-technologies") {
4957
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
5058
setBody(Json.encodeToString(medicalTechnology))
5159
}
5260

5361
"I should be able to request the creation of a medical technology" {
5462
apiTestApplication {
55-
val response = insertMedicalTechnology(medicalTechnologyApiDto)
63+
val response = insertMedicalTechnology(medicalTechnologyEntry)
5664
response shouldHaveStatus HttpStatusCode.Created
5765
response.headers[HttpHeaders.Location] shouldNotBe null
5866
}
5967
}
6068

6169
"Add an existent medical technology then the service should result in a conflict status code" {
6270
apiTestApplication {
63-
insertMedicalTechnology(medicalTechnologyApiDto)
64-
val response = insertMedicalTechnology(medicalTechnologyApiDto)
71+
insertMedicalTechnology(medicalTechnologyEntry)
72+
val response = insertMedicalTechnology(medicalTechnologyEntry)
6573
response shouldHaveStatus HttpStatusCode.Conflict
6674
}
6775
}
6876

6977
"It should be possible to obtain an existing medical technology" {
7078
apiTestApplication {
71-
insertMedicalTechnology(medicalTechnologyApiDto)
72-
val response = client.get("/api/v1/medical-technologies/${medicalTechnologyApiDto.id}")
79+
insertMedicalTechnology(medicalTechnologyEntry)
80+
val response = client.get("/api/v1/medical-technologies/${medicalTechnologyEntry.id}")
7381
response shouldHaveStatus HttpStatusCode.OK
7482
Json.decodeFromString<MedicalTechnologyApiDto>(response.bodyAsText()) shouldBe medicalTechnologyApiDto
7583
}
7684
}
7785

7886
"It should be possible to obtain historical data about a medical technology" {
7987
apiTestApplication {
80-
insertMedicalTechnology(medicalTechnologyApiDto)
88+
insertMedicalTechnology(medicalTechnologyEntry)
8189
val response = client
82-
.get("/api/v1/medical-technologies/${medicalTechnologyApiDto.id}?dateTime=${Instant.now()}")
90+
.get("/api/v1/medical-technologies/${medicalTechnologyEntry.id}?dateTime=${Instant.now()}")
8391
response shouldHaveStatus HttpStatusCode.OK
8492
}
8593
}
8694

8795
"It should handle the get request on a non existing medical technology" {
8896
apiTestApplication {
89-
val response = client.get("/api/v1/medical-technologies/${medicalTechnologyApiDto.id}")
97+
val response = client.get("/api/v1/medical-technologies/${medicalTechnologyEntry.id}")
9098
response shouldHaveStatus HttpStatusCode.NotFound
9199
}
92100
}
93101

94102
"It should be possible to delete an existing medical technology" {
95103
apiTestApplication {
96-
insertMedicalTechnology(medicalTechnologyApiDto)
97-
val response = client.delete("/api/v1/medical-technologies/${medicalTechnologyApiDto.id}")
104+
insertMedicalTechnology(medicalTechnologyEntry)
105+
val response = client.delete("/api/v1/medical-technologies/${medicalTechnologyEntry.id}")
98106
response shouldHaveStatus HttpStatusCode.NoContent
99107
}
100108
}
101109

102110
"It should not be possible to delete a not-existent medical technology" {
103111
apiTestApplication {
104-
val response = client.delete("/api/v1/medical-technologies/${medicalTechnologyApiDto.id}")
112+
val response = client.delete("/api/v1/medical-technologies/${medicalTechnologyEntry.id}")
105113
response shouldHaveStatus HttpStatusCode.NotFound
106114
}
107115
}
@@ -113,7 +121,7 @@ class MedicalTechnologyApiTest : StringSpec({
113121
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
114122
setBody(
115123
Json.encodeToString(
116-
RoomApiDto(
124+
RoomEntry(
117125
"r1",
118126
"name",
119127
"z1",
@@ -123,8 +131,8 @@ class MedicalTechnologyApiTest : StringSpec({
123131
)
124132
}
125133
// create medical technology
126-
insertMedicalTechnology(medicalTechnologyApiDto)
127-
val response = client.patch("/api/v1/medical-technologies/${medicalTechnologyApiDto.id}") {
134+
insertMedicalTechnology(medicalTechnologyEntry)
135+
val response = client.patch("/api/v1/medical-technologies/${medicalTechnologyEntry.id}") {
128136
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
129137
setBody(Json.encodeToString(MedicalTechnologyPatch("r1")))
130138
}
@@ -134,7 +142,7 @@ class MedicalTechnologyApiTest : StringSpec({
134142

135143
"It should not be possible to map a non existent medical technology to a non existent room" {
136144
apiTestApplication {
137-
val response = client.patch("/api/v1/medical-technologies/${medicalTechnologyApiDto.id}") {
145+
val response = client.patch("/api/v1/medical-technologies/${medicalTechnologyEntry.id}") {
138146
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
139147
setBody(Json.encodeToString(MedicalTechnologyPatch("r1")))
140148
}
@@ -144,8 +152,8 @@ class MedicalTechnologyApiTest : StringSpec({
144152

145153
"It should not be possible to map an existent medical technology to a non existent room" {
146154
apiTestApplication {
147-
insertMedicalTechnology(medicalTechnologyApiDto)
148-
val response = client.patch("/api/v1/medical-technologies/${medicalTechnologyApiDto.id}") {
155+
insertMedicalTechnology(medicalTechnologyEntry)
156+
val response = client.patch("/api/v1/medical-technologies/${medicalTechnologyEntry.id}") {
149157
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
150158
setBody(Json.encodeToString(MedicalTechnologyPatch("r1")))
151159
}

0 commit comments

Comments
 (0)