From 87570c19d9cf7d0f67d4e1e657697580b9692030 Mon Sep 17 00:00:00 2001 From: Christophe Loiseau Date: Thu, 5 Dec 2024 17:27:17 +0100 Subject: [PATCH] Partial fake service implementation --- .../de/sovity/chatapp/api/ApiResource.kt | 19 ++++-- .../de/sovity/chatapp/model/MessageSendDto.kt | 3 +- .../de/sovity/chatapp/service/EdcService.kt | 15 +++++ .../sovity/chatapp/service/FakeEdcService.kt | 64 +++++++++++++++++++ 4 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 backend/src/main/kotlin/de/sovity/chatapp/service/EdcService.kt create mode 100644 backend/src/main/kotlin/de/sovity/chatapp/service/FakeEdcService.kt diff --git a/backend/src/main/kotlin/de/sovity/chatapp/api/ApiResource.kt b/backend/src/main/kotlin/de/sovity/chatapp/api/ApiResource.kt index c2f5757..0589edf 100644 --- a/backend/src/main/kotlin/de/sovity/chatapp/api/ApiResource.kt +++ b/backend/src/main/kotlin/de/sovity/chatapp/api/ApiResource.kt @@ -4,6 +4,8 @@ import de.sovity.chatapp.model.ConnectorCreateDto import de.sovity.chatapp.model.ConnectorDto import de.sovity.chatapp.model.MessageDto import de.sovity.chatapp.model.MessageNotificationDto +import de.sovity.chatapp.model.MessageSendDto +import de.sovity.chatapp.service.EdcService import jakarta.ws.rs.GET import jakarta.ws.rs.HeaderParam import jakarta.ws.rs.POST @@ -11,34 +13,37 @@ import jakarta.ws.rs.Path import jakarta.ws.rs.QueryParam @Path("/") -class ApiResource { +class ApiResource( + val edcService: EdcService +) { + @GET @Path("ui/connectors") fun getConnectors(): List { - TODO("Implementation") + return edcService.getConnectors() } @POST @Path("ui/connectors") fun createConnector(request: ConnectorCreateDto): ConnectorDto { - TODO("Implementation") + return edcService.createConnector(request) } @GET @Path("ui/connectors/{connectorId}/messages") fun getAllMessages(@QueryParam("connectorId") connectorId: String): List { - TODO("Implementation") + return edcService.getMessages(connectorId) } @POST @Path("ui/connectors/{connectorId}/messages") - fun sendMessage(@QueryParam("connectorId") connectorId: String): MessageDto { - TODO("Implementation") + fun sendMessage(@QueryParam("connectorId") connectorId: String, message: MessageSendDto): MessageDto { + return edcService.sendMessage(connectorId, message) } @POST @Path("edc/notifications/receive") fun receiveNotification(@HeaderParam("Edc-Bpn") edcBpn: String, notification: MessageNotificationDto) { - TODO("Implementation") + edcService.receiveNotification(edcBpn, notification) } } diff --git a/backend/src/main/kotlin/de/sovity/chatapp/model/MessageSendDto.kt b/backend/src/main/kotlin/de/sovity/chatapp/model/MessageSendDto.kt index f96748d..12943fb 100644 --- a/backend/src/main/kotlin/de/sovity/chatapp/model/MessageSendDto.kt +++ b/backend/src/main/kotlin/de/sovity/chatapp/model/MessageSendDto.kt @@ -1,6 +1,5 @@ package de.sovity.chatapp.model data class MessageSendDto( - val message: String, - val username: String + val message: String ) diff --git a/backend/src/main/kotlin/de/sovity/chatapp/service/EdcService.kt b/backend/src/main/kotlin/de/sovity/chatapp/service/EdcService.kt new file mode 100644 index 0000000..0b44b63 --- /dev/null +++ b/backend/src/main/kotlin/de/sovity/chatapp/service/EdcService.kt @@ -0,0 +1,15 @@ +package de.sovity.chatapp.service + +import de.sovity.chatapp.model.ConnectorCreateDto +import de.sovity.chatapp.model.ConnectorDto +import de.sovity.chatapp.model.MessageDto +import de.sovity.chatapp.model.MessageNotificationDto +import de.sovity.chatapp.model.MessageSendDto + +interface EdcService { + fun getConnectors(): List + fun createConnector(dto: ConnectorCreateDto): ConnectorDto + fun getMessages(connectorId: String): List + fun sendMessage(string: String, dto: MessageSendDto): MessageDto + fun receiveNotification(string: String, dto: MessageNotificationDto) +} diff --git a/backend/src/main/kotlin/de/sovity/chatapp/service/FakeEdcService.kt b/backend/src/main/kotlin/de/sovity/chatapp/service/FakeEdcService.kt new file mode 100644 index 0000000..7a1dd84 --- /dev/null +++ b/backend/src/main/kotlin/de/sovity/chatapp/service/FakeEdcService.kt @@ -0,0 +1,64 @@ +package de.sovity.chatapp.service + +import de.sovity.chatapp.model.ConnectorCreateDto +import de.sovity.chatapp.model.ConnectorDto +import de.sovity.chatapp.model.ConnectorStatusDto +import de.sovity.chatapp.model.MessageDirectionDto +import de.sovity.chatapp.model.MessageDto +import de.sovity.chatapp.model.MessageNotificationDto +import de.sovity.chatapp.model.MessageSendDto +import de.sovity.chatapp.model.MessageStatusDto +import jakarta.enterprise.context.ApplicationScoped +import java.time.ZoneOffset +import java.time.ZonedDateTime +import java.util.UUID + +@ApplicationScoped +class FakeEdcService : EdcService { + + val connectors = mutableSetOf() + val messages = mutableMapOf>() + + override fun getConnectors(): List { + return connectors.toList() + } + + override fun createConnector(connectorCreateDto: ConnectorCreateDto): ConnectorDto { + val connector = ConnectorDto( + UUID.randomUUID().toString(), + connectorCreateDto.participantId, + connectorCreateDto.connectorEndpoint, + ConnectorStatusDto.ONLINE + ) + + connectors.add(connector) + + return connector + } + + override fun getMessages(connectorId: String): List { + return messages[connectorId] ?: listOf() + } + + override fun sendMessage( + recievingConnectorId: String, + messageSendDto: MessageSendDto + ): MessageDto { + val message = MessageDto( + UUID.randomUUID().toString(), + ZonedDateTime.now(ZoneOffset.UTC).toString(), + messageSendDto.message, + MessageDirectionDto.INCOMING, + MessageStatusDto.OK + ) + + val messages = messages.computeIfAbsent(recievingConnectorId) { mutableListOf() } + messages.add(message) + + return message + } + + override fun receiveNotification(edcBpn: String, notification: MessageNotificationDto) { + TODO("Not yet implemented") + } +}