|
| 1 | +/* |
| 2 | + * Copyright (c) 2023. Smart Operating Block |
| 3 | + * |
| 4 | + * Use of this source code is governed by an MIT-style |
| 5 | + * license that can be found in the LICENSE file or at |
| 6 | + * https://opensource.org/licenses/MIT. |
| 7 | + */ |
| 8 | + |
| 9 | +package application.handler |
| 10 | + |
| 11 | +import application.controller.MedicalTechnologyController |
| 12 | +import application.controller.RoomController |
| 13 | +import application.presenter.event.model.Event |
| 14 | +import application.presenter.event.model.MedicalTechnologyEvent |
| 15 | +import application.presenter.event.model.MedicalTechnologyEventKey |
| 16 | +import application.presenter.event.model.MedicalTechnologyUsagePayload |
| 17 | +import application.presenter.event.model.RoomEvent |
| 18 | +import application.presenter.event.model.RoomEventKey |
| 19 | +import application.presenter.event.model.RoomEventPayloads |
| 20 | +import application.service.MedicalTechnologyService |
| 21 | +import application.service.RoomService |
| 22 | +import entity.medicaltechnology.MedicalTechnology |
| 23 | +import entity.medicaltechnology.MedicalTechnologyID |
| 24 | +import entity.medicaltechnology.MedicalTechnologyType |
| 25 | +import entity.zone.Room |
| 26 | +import entity.zone.RoomID |
| 27 | +import entity.zone.RoomType |
| 28 | +import entity.zone.ZoneID |
| 29 | +import infrastructure.database.DatabaseManager |
| 30 | +import infrastructure.database.withMongo |
| 31 | +import infrastructure.digitaltwins.DigitalTwinManagerTestDouble |
| 32 | +import io.kotest.assertions.throwables.shouldNotThrow |
| 33 | +import io.kotest.core.spec.style.StringSpec |
| 34 | +import io.kotest.matchers.shouldBe |
| 35 | +import java.time.Instant |
| 36 | + |
| 37 | +class EventHandlersTest : StringSpec({ |
| 38 | + val exampleRoom = Room( |
| 39 | + RoomID("r1"), |
| 40 | + RoomType.OPERATING_ROOM, |
| 41 | + ZoneID("z1") |
| 42 | + ) |
| 43 | + val exampleMedicalTechnology = MedicalTechnology( |
| 44 | + id = MedicalTechnologyID("mt-1"), |
| 45 | + name = "name", |
| 46 | + description = "description", |
| 47 | + type = MedicalTechnologyType.ENDOSCOPE |
| 48 | + ) |
| 49 | + val databaseManager by lazy { DatabaseManager("mongodb://localhost:27017") } |
| 50 | + fun medicalTechnologyController() = MedicalTechnologyController(DigitalTwinManagerTestDouble(), databaseManager) |
| 51 | + fun roomController() = RoomController(DigitalTwinManagerTestDouble(), databaseManager) |
| 52 | + |
| 53 | + listOf( |
| 54 | + EventHandlers.TemperatureEventHandler(roomController()), |
| 55 | + EventHandlers.HumidityEventHandler(roomController()), |
| 56 | + EventHandlers.LuminosityEventHandler(roomController()), |
| 57 | + EventHandlers.PresenceEventHandler(roomController()), |
| 58 | + EventHandlers.MedicalTechnologyEventHandler(medicalTechnologyController()) |
| 59 | + ).forEach { |
| 60 | + "Event handlers should safely handle events that can't be processed" { |
| 61 | + val nonexistentEvent = object : Event<Int> { |
| 62 | + override val key: String = "NONEXISTENT_EVENT_KEY" |
| 63 | + override val data: Int = 1 |
| 64 | + override val dateTime: String = "DATE_TIME" |
| 65 | + } |
| 66 | + it.canHandle(nonexistentEvent) shouldBe false |
| 67 | + shouldNotThrow<Exception> { |
| 68 | + it.consume(nonexistentEvent) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + "Temperature update event must be handled correctly" { |
| 74 | + withMongo { |
| 75 | + val roomController = roomController() |
| 76 | + val event = RoomEvent( |
| 77 | + RoomEventKey.TEMPERATURE_EVENT, |
| 78 | + exampleRoom.id.value, |
| 79 | + RoomEventPayloads.TemperaturePayload(34.0, RoomEventPayloads.TemperaturePayloadUnit.CELSIUS), |
| 80 | + Instant.now().toString() |
| 81 | + ) |
| 82 | + val eventHandler = EventHandlers.TemperatureEventHandler(roomController) |
| 83 | + RoomService.CreateRoom(exampleRoom, roomController).execute() |
| 84 | + eventHandler.canHandle(event) shouldBe true |
| 85 | + shouldNotThrow<Exception> { eventHandler.consume(event) } |
| 86 | + RoomService.GetRoom(exampleRoom.id, roomController, Instant.now()).execute() |
| 87 | + ?.environmentalData?.temperature?.value shouldBe 34.0 |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + "Humidity update event must be handled correctly" { |
| 92 | + withMongo { |
| 93 | + val roomController = roomController() |
| 94 | + val event = RoomEvent( |
| 95 | + RoomEventKey.HUMIDITY_EVENT, |
| 96 | + exampleRoom.id.value, |
| 97 | + RoomEventPayloads.HumidityPayload(55), |
| 98 | + Instant.now().toString() |
| 99 | + ) |
| 100 | + val eventHandler = EventHandlers.HumidityEventHandler(roomController) |
| 101 | + RoomService.CreateRoom(exampleRoom, roomController).execute() |
| 102 | + eventHandler.canHandle(event) shouldBe true |
| 103 | + shouldNotThrow<Exception> { eventHandler.consume(event) } |
| 104 | + RoomService.GetRoom(exampleRoom.id, roomController, Instant.now()).execute() |
| 105 | + ?.environmentalData?.humidity?.percentage shouldBe 55.0 |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + "Luminosity update event must be handled correctly" { |
| 110 | + withMongo { |
| 111 | + val roomController = roomController() |
| 112 | + val event = RoomEvent( |
| 113 | + RoomEventKey.LUMINOSITY_EVENT, |
| 114 | + exampleRoom.id.value, |
| 115 | + RoomEventPayloads.LuminosityPayload(150.0, RoomEventPayloads.LuminosityPayloadUnit.LUX), |
| 116 | + Instant.now().toString() |
| 117 | + ) |
| 118 | + val eventHandler = EventHandlers.LuminosityEventHandler(roomController) |
| 119 | + RoomService.CreateRoom(exampleRoom, roomController).execute() |
| 120 | + eventHandler.canHandle(event) shouldBe true |
| 121 | + shouldNotThrow<Exception> { eventHandler.consume(event) } |
| 122 | + RoomService.GetRoom(exampleRoom.id, roomController, Instant.now()).execute() |
| 123 | + ?.environmentalData?.luminosity?.value shouldBe 150.0 |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + "Presence update event must be handled correctly" { |
| 128 | + withMongo { |
| 129 | + val roomController = roomController() |
| 130 | + val event = RoomEvent( |
| 131 | + RoomEventKey.PRESENCE_EVENT, |
| 132 | + exampleRoom.id.value, |
| 133 | + RoomEventPayloads.PresencePayload(true), |
| 134 | + Instant.now().toString() |
| 135 | + ) |
| 136 | + val eventHandler = EventHandlers.PresenceEventHandler(roomController) |
| 137 | + RoomService.CreateRoom(exampleRoom, roomController).execute() |
| 138 | + eventHandler.canHandle(event) shouldBe true |
| 139 | + shouldNotThrow<Exception> { eventHandler.consume(event) } |
| 140 | + RoomService.GetRoom(exampleRoom.id, roomController, Instant.now()).execute() |
| 141 | + ?.environmentalData?.presence?.presenceDetected shouldBe true |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + "Medical Technology usage update event must be handled correctly" { |
| 146 | + withMongo { |
| 147 | + val medicalTechnologyController = medicalTechnologyController() |
| 148 | + val roomController = roomController() |
| 149 | + val event = MedicalTechnologyEvent( |
| 150 | + MedicalTechnologyEventKey.USAGE_EVENT, |
| 151 | + MedicalTechnologyUsagePayload(exampleMedicalTechnology.id.value, true), |
| 152 | + Instant.now().toString() |
| 153 | + ) |
| 154 | + val eventHandler = EventHandlers.MedicalTechnologyEventHandler(medicalTechnologyController) |
| 155 | + MedicalTechnologyService.CreateMedicalTechnology(exampleMedicalTechnology, medicalTechnologyController) |
| 156 | + .execute() |
| 157 | + RoomService.CreateRoom(exampleRoom, roomController).execute() |
| 158 | + MedicalTechnologyService.MapMedicalTechnologyToRoom( |
| 159 | + exampleMedicalTechnology.id, |
| 160 | + exampleRoom.id, |
| 161 | + roomController, |
| 162 | + medicalTechnologyController |
| 163 | + ).execute() |
| 164 | + eventHandler.canHandle(event) shouldBe true |
| 165 | + shouldNotThrow<Exception> { eventHandler.consume(event) } |
| 166 | + MedicalTechnologyService.GetMedicalTechnology( |
| 167 | + exampleMedicalTechnology.id, |
| 168 | + medicalTechnologyController, |
| 169 | + Instant.now() |
| 170 | + ).execute()?.isInUse shouldBe true |
| 171 | + } |
| 172 | + } |
| 173 | +}) |
0 commit comments