From 319816e5ab2c1a7fc4d8825bc739378560b0226c Mon Sep 17 00:00:00 2001 From: andacata <1506402+andacata@users.noreply.github.com> Date: Wed, 10 Jan 2024 13:27:35 +0100 Subject: [PATCH] fix: PR comments --- .../services/CredentialsClientService.kt | 7 ++- .../credentials/services/CredentialsCommon.kt | 52 ------------------- .../services/CredentialsRequiredEndpoints.kt | 42 +++++++++++++++ .../services/CredentialsServerService.kt | 4 +- .../credentials/CredentialsReceiver.kt | 6 +-- .../samples/credentials/CredentialsSender.kt | 4 +- .../CredentialsIntegrationTests.kt | 28 ++++------ 7 files changed, 62 insertions(+), 81 deletions(-) delete mode 100644 ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsCommon.kt create mode 100644 ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsRequiredEndpoints.kt diff --git a/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsClientService.kt b/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsClientService.kt index ba85a8f9..fc89e947 100644 --- a/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsClientService.kt +++ b/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsClientService.kt @@ -8,7 +8,6 @@ import com.izivia.ocpi.toolkit.modules.credentials.repositories.PartnerRepositor import com.izivia.ocpi.toolkit.modules.versions.VersionDetailsClient import com.izivia.ocpi.toolkit.modules.versions.VersionsClient import com.izivia.ocpi.toolkit.modules.versions.domain.Endpoint -import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID import com.izivia.ocpi.toolkit.modules.versions.domain.parseVersionNumber import com.izivia.ocpi.toolkit.modules.versions.repositories.VersionsRepository @@ -29,7 +28,7 @@ import com.izivia.ocpi.toolkit.transport.TransportClientBuilder * @property clientCredentialsRoleRepository client's repository to retrieve its role * @property serverVersionsEndpointUrl the versions endpoint url of the server (for the client to retrieve endpoints) * @property transportClientBuilder used to build a transport (will be used to create CredentialClient to make calls) - * @property requiredOtherPartEndpointsProvider the endpoints this client expects from the other part to provide + * @property requiredEndpoints the endpoints this client expects from the other part to provide */ class CredentialsClientService( private val clientVersionsEndpointUrl: String, @@ -38,7 +37,7 @@ class CredentialsClientService( private val clientCredentialsRoleRepository: CredentialsRoleRepository, private val serverVersionsEndpointUrl: String, private val transportClientBuilder: TransportClientBuilder, - private val requiredOtherPartEndpointsProvider: suspend () -> Map> + private val requiredEndpoints: RequiredEndpoints? ) { suspend fun get(): Credentials = clientPartnerRepository .getCredentialsClientToken(partnerUrl = serverVersionsEndpointUrl) @@ -229,7 +228,7 @@ class CredentialsClientService( it.data ?: throw OcpiResponseException(it.status_code, it.status_message ?: "unknown") } - CredentialsCommon.checkRequiredEndpoints(requiredOtherPartEndpointsProvider(), versionDetails.endpoints) + checkRequiredEndpoints(requiredEndpoints, versionDetails.endpoints) // Store version & endpoint return clientPartnerRepository.saveEndpoints( diff --git a/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsCommon.kt b/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsCommon.kt deleted file mode 100644 index 6b3338cf..00000000 --- a/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsCommon.kt +++ /dev/null @@ -1,52 +0,0 @@ -package com.izivia.ocpi.toolkit.modules.credentials.services - -import com.izivia.ocpi.toolkit.common.OcpiServerNoMatchingEndpointsException -import com.izivia.ocpi.toolkit.modules.versions.domain.Endpoint -import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole -import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID - -class CredentialsCommon { - companion object { - fun checkRequiredEndpoints( - requiredEndpoints: Map>, - actualEndpoints: List - ) { - if (requiredEndpoints.isEmpty()) { - return - } - - val requiredOtherPartAsReceiverEndpoints = requiredEndpoints[InterfaceRole.RECEIVER] ?: listOf() - val requiredOtherPartAsSenderEndpoints = requiredEndpoints[InterfaceRole.SENDER] ?: listOf() - - actualEndpoints - .find { it.identifier == ModuleID.credentials } - .let { - it ?: throw OcpiServerNoMatchingEndpointsException( - "${ModuleID.credentials} other part endpoint missing" - ) - } - - checkRequiredRoleEndpoints(actualEndpoints, requiredOtherPartAsReceiverEndpoints, InterfaceRole.RECEIVER) - checkRequiredRoleEndpoints(actualEndpoints, requiredOtherPartAsSenderEndpoints, InterfaceRole.SENDER) - } - - private fun checkRequiredRoleEndpoints( - endpoints: List, - requiredEndpoints: List, - role: InterfaceRole - ) { - for (requiredEndpoint in requiredEndpoints) { - endpoints - .find { - requiredEndpoint == ModuleID.credentials || - (it.role == role && it.identifier == requiredEndpoint) - } - .let { - it ?: throw OcpiServerNoMatchingEndpointsException( - "${requiredEndpoint.name} as ${role.name} other part endpoint missing" - ) - } - } - } - } -} diff --git a/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsRequiredEndpoints.kt b/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsRequiredEndpoints.kt new file mode 100644 index 00000000..328e4e25 --- /dev/null +++ b/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsRequiredEndpoints.kt @@ -0,0 +1,42 @@ +package com.izivia.ocpi.toolkit.modules.credentials.services + +import com.izivia.ocpi.toolkit.common.OcpiServerNoMatchingEndpointsException +import com.izivia.ocpi.toolkit.modules.versions.domain.Endpoint +import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole +import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID + +data class RequiredEndpoints( + val receiver: List = listOf(), + val sender: List = listOf() +) + +fun checkRequiredEndpoints( + requiredEndpoints: RequiredEndpoints?, + actualEndpoints: List +) { + if (requiredEndpoints == null) { + return + } + + actualEndpoints + .find { it.identifier == ModuleID.credentials } + ?: throw OcpiServerNoMatchingEndpointsException("${ModuleID.credentials} endpoint missing") + + checkRequiredRoleEndpoints(actualEndpoints, requiredEndpoints.receiver, InterfaceRole.RECEIVER) + checkRequiredRoleEndpoints(actualEndpoints, requiredEndpoints.sender, InterfaceRole.SENDER) +} + +private fun checkRequiredRoleEndpoints( + endpoints: List, + requiredEndpoints: List, + role: InterfaceRole +) { + for (requiredEndpoint in requiredEndpoints) { + endpoints + .find { + requiredEndpoint == ModuleID.credentials || + (it.role == role && it.identifier == requiredEndpoint) + } + ?: throw OcpiServerNoMatchingEndpointsException("${requiredEndpoint.name} as ${role.name} endpoint missing") + } +} diff --git a/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsServerService.kt b/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsServerService.kt index c5b7218c..24fd116a 100644 --- a/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsServerService.kt +++ b/ocpi-toolkit-2.2.1/src/main/kotlin/com/izivia/ocpi/toolkit/modules/credentials/services/CredentialsServerService.kt @@ -16,7 +16,7 @@ class CredentialsServerService( private val credentialsRoleRepository: CredentialsRoleRepository, private val transportClientBuilder: TransportClientBuilder, private val serverVersionsUrlProvider: suspend () -> String, - private val requiredOtherPartEndpointsProvider: suspend () -> Map> + private val requiredEndpoints: RequiredEndpoints? ) : CredentialsInterface { override suspend fun get( @@ -201,7 +201,7 @@ class CredentialsServerService( ) } - CredentialsCommon.checkRequiredEndpoints(requiredOtherPartEndpointsProvider(), versionDetail.endpoints) + checkRequiredEndpoints(requiredEndpoints, versionDetail.endpoints) partnerRepository.saveEndpoints(partnerUrl = credentials.url, endpoints = versionDetail.endpoints) } diff --git a/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/samples/credentials/CredentialsReceiver.kt b/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/samples/credentials/CredentialsReceiver.kt index 973684cc..ca512643 100644 --- a/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/samples/credentials/CredentialsReceiver.kt +++ b/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/samples/credentials/CredentialsReceiver.kt @@ -6,10 +6,10 @@ import com.izivia.ocpi.toolkit.modules.credentials.domain.CredentialRole import com.izivia.ocpi.toolkit.modules.credentials.domain.Role import com.izivia.ocpi.toolkit.modules.credentials.repositories.CredentialsRoleRepository import com.izivia.ocpi.toolkit.modules.credentials.services.CredentialsServerService +import com.izivia.ocpi.toolkit.modules.credentials.services.RequiredEndpoints import com.izivia.ocpi.toolkit.modules.locations.domain.BusinessDetails import com.izivia.ocpi.toolkit.modules.versions.VersionDetailsServer import com.izivia.ocpi.toolkit.modules.versions.VersionsServer -import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID import com.izivia.ocpi.toolkit.modules.versions.services.VersionDetailsService import com.izivia.ocpi.toolkit.modules.versions.services.VersionsService @@ -32,7 +32,7 @@ fun main() { secureFilter = receiverPlatformRepository::checkToken ) - val requiredOtherPartEndpoints = mapOf(InterfaceRole.RECEIVER to listOf(ModuleID.credentials)) + val requiredOtherPartEndpoints = RequiredEndpoints(receiver = listOf(ModuleID.credentials)) runBlocking { CredentialsServer( @@ -50,7 +50,7 @@ fun main() { }, transportClientBuilder = Http4kTransportClientBuilder(), serverVersionsUrlProvider = { receiverVersionsUrl }, - requiredOtherPartEndpointsProvider = { requiredOtherPartEndpoints } + requiredEndpoints = requiredOtherPartEndpoints ) ).registerOn(receiverServer) VersionsServer( diff --git a/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/samples/credentials/CredentialsSender.kt b/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/samples/credentials/CredentialsSender.kt index aaefb81f..99e75135 100644 --- a/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/samples/credentials/CredentialsSender.kt +++ b/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/samples/credentials/CredentialsSender.kt @@ -5,10 +5,10 @@ import com.izivia.ocpi.toolkit.modules.credentials.domain.CredentialRole import com.izivia.ocpi.toolkit.modules.credentials.domain.Role import com.izivia.ocpi.toolkit.modules.credentials.repositories.CredentialsRoleRepository import com.izivia.ocpi.toolkit.modules.credentials.services.CredentialsClientService +import com.izivia.ocpi.toolkit.modules.credentials.services.RequiredEndpoints import com.izivia.ocpi.toolkit.modules.locations.domain.BusinessDetails import com.izivia.ocpi.toolkit.modules.versions.VersionDetailsServer import com.izivia.ocpi.toolkit.modules.versions.VersionsServer -import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID import com.izivia.ocpi.toolkit.modules.versions.services.VersionDetailsService import com.izivia.ocpi.toolkit.modules.versions.services.VersionsService @@ -64,7 +64,7 @@ fun main() { }, serverVersionsEndpointUrl = receiverVersionsUrl, transportClientBuilder = Http4kTransportClientBuilder(), - requiredOtherPartEndpointsProvider = { mapOf(InterfaceRole.RECEIVER to listOf(ModuleID.credentials)) } + requiredEndpoints = RequiredEndpoints(receiver = listOf(ModuleID.credentials)) ) runBlocking { diff --git a/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/tests/integration/CredentialsIntegrationTests.kt b/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/tests/integration/CredentialsIntegrationTests.kt index c48d81bc..78ffe37f 100644 --- a/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/tests/integration/CredentialsIntegrationTests.kt +++ b/ocpi-toolkit-2.2.1/src/test/kotlin/com/izivia/ocpi/toolkit/tests/integration/CredentialsIntegrationTests.kt @@ -7,11 +7,11 @@ import com.izivia.ocpi.toolkit.modules.credentials.domain.Role import com.izivia.ocpi.toolkit.modules.credentials.repositories.CredentialsRoleRepository import com.izivia.ocpi.toolkit.modules.credentials.services.CredentialsClientService import com.izivia.ocpi.toolkit.modules.credentials.services.CredentialsServerService +import com.izivia.ocpi.toolkit.modules.credentials.services.RequiredEndpoints import com.izivia.ocpi.toolkit.modules.locations.domain.BusinessDetails import com.izivia.ocpi.toolkit.modules.versions.VersionDetailsServer import com.izivia.ocpi.toolkit.modules.versions.VersionsClient import com.izivia.ocpi.toolkit.modules.versions.VersionsServer -import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID import com.izivia.ocpi.toolkit.modules.versions.services.VersionDetailsService import com.izivia.ocpi.toolkit.modules.versions.services.VersionsService @@ -45,7 +45,7 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() { private var database: MongoDatabase? = null - private fun setupReceiver(requiredEndpoints: Map> = mapOf()): ServerSetupResult { + private fun setupReceiver(requiredEndpoints: RequiredEndpoints? = null): ServerSetupResult { if (database == null) database = buildDBClient().getDatabase("ocpi-2-2-1-tests") val receiverPartnerCollection = database!! .getCollection("receiver-server-${UUID.randomUUID()}") @@ -76,7 +76,7 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() { }, transportClientBuilder = Http4kTransportClientBuilder(), serverVersionsUrlProvider = { receiverServerVersionsUrl }, - requiredOtherPartEndpointsProvider = { requiredEndpoints } + requiredEndpoints = requiredEndpoints ) ).registerOn(receiverServer) VersionsServer( @@ -134,7 +134,7 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() { private fun setupCredentialsSenderClient( senderServerSetupResult: ServerSetupResult, receiverServerSetupResult: ServerSetupResult, - requiredEndpoints: Map> = mapOf() + requiredEndpoints: RequiredEndpoints? = null ): CredentialsClientService { // Setup sender (client) return CredentialsClientService( @@ -153,7 +153,7 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() { }, serverVersionsEndpointUrl = receiverServerSetupResult.versionsEndpoint, transportClientBuilder = Http4kTransportClientBuilder(), - requiredOtherPartEndpointsProvider = { requiredEndpoints } + requiredEndpoints = requiredEndpoints ) } @@ -257,14 +257,9 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() { @Test fun `should not properly run registration process because required endpoints are missing`() { val receiverServer = setupReceiver( - mapOf( - InterfaceRole.RECEIVER to listOf( - ModuleID.credentials, - ModuleID.locations - ), - InterfaceRole.SENDER to listOf( - ModuleID.chargingprofiles - ) + RequiredEndpoints( + receiver = listOf(ModuleID.credentials, ModuleID.locations), + sender = listOf(ModuleID.chargingprofiles) ) ) val senderServer = setupSender() @@ -299,11 +294,8 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() { @Test fun `should properly run registration process then correct get credentials from receiver`() { val receiverServer = setupReceiver( - mapOf( - InterfaceRole.RECEIVER to listOf( - ModuleID.credentials, - ModuleID.locations - ) + RequiredEndpoints( + receiver = listOf(ModuleID.credentials, ModuleID.locations) ) ) val senderServer = setupSender()