Skip to content

Commit

Permalink
fix: PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
andacata committed Jan 10, 2024
1 parent a570a09 commit b29ef05
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -38,7 +37,7 @@ class CredentialsClientService(
private val clientCredentialsRoleRepository: CredentialsRoleRepository,
private val serverVersionsEndpointUrl: String,
private val transportClientBuilder: TransportClientBuilder,
private val requiredOtherPartEndpointsProvider: suspend () -> Map<InterfaceRole, List<ModuleID>>
private val requiredEndpoints: RequiredEndpoints?
) {
suspend fun get(): Credentials = clientPartnerRepository
.getCredentialsClientToken(partnerUrl = serverVersionsEndpointUrl)
Expand Down Expand Up @@ -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(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<ModuleID> = listOf(),
val sender: List<ModuleID> = listOf()
)

fun checkRequiredEndpoints(
requiredEndpoints: RequiredEndpoints?,
actualEndpoints: List<Endpoint>
) {
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<Endpoint>,
requiredEndpoints: List<ModuleID>,
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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CredentialsServerService(
private val credentialsRoleRepository: CredentialsRoleRepository,
private val transportClientBuilder: TransportClientBuilder,
private val serverVersionsUrlProvider: suspend () -> String,
private val requiredOtherPartEndpointsProvider: suspend () -> Map<InterfaceRole, List<ModuleID>>
private val requiredEndpoints: RequiredEndpoints?
) : CredentialsInterface {

override suspend fun get(
Expand Down Expand Up @@ -201,7 +201,7 @@ class CredentialsServerService(
)
}

CredentialsCommon.checkRequiredEndpoints(requiredOtherPartEndpointsProvider(), versionDetail.endpoints)
checkRequiredEndpoints(requiredEndpoints, versionDetail.endpoints)

partnerRepository.saveEndpoints(partnerUrl = credentials.url, endpoints = versionDetail.endpoints)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -50,7 +50,7 @@ fun main() {
},
transportClientBuilder = Http4kTransportClientBuilder(),
serverVersionsUrlProvider = { receiverVersionsUrl },
requiredOtherPartEndpointsProvider = { requiredOtherPartEndpoints }
requiredEndpoints = requiredOtherPartEndpoints
)
).registerOn(receiverServer)
VersionsServer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,7 +45,7 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() {

private var database: MongoDatabase? = null

private fun setupReceiver(requiredEndpoints: Map<InterfaceRole, List<ModuleID>> = mapOf()): ServerSetupResult {
private fun setupReceiver(requiredEndpoints: RequiredEndpoints? = null): ServerSetupResult {
if (database == null) database = buildDBClient().getDatabase("ocpi-2-2-1-tests")
val receiverPartnerCollection = database!!
.getCollection<Partner>("receiver-server-${UUID.randomUUID()}")
Expand Down Expand Up @@ -76,7 +76,7 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() {
},
transportClientBuilder = Http4kTransportClientBuilder(),
serverVersionsUrlProvider = { receiverServerVersionsUrl },
requiredOtherPartEndpointsProvider = { requiredEndpoints }
requiredEndpoints = requiredEndpoints
)
).registerOn(receiverServer)
VersionsServer(
Expand Down Expand Up @@ -134,7 +134,7 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() {
private fun setupCredentialsSenderClient(
senderServerSetupResult: ServerSetupResult,
receiverServerSetupResult: ServerSetupResult,
requiredEndpoints: Map<InterfaceRole, List<ModuleID>> = mapOf()
requiredEndpoints: RequiredEndpoints? = null
): CredentialsClientService {
// Setup sender (client)
return CredentialsClientService(
Expand All @@ -153,7 +153,7 @@ class CredentialsIntegrationTests : BaseServerIntegrationTest() {
},
serverVersionsEndpointUrl = receiverServerSetupResult.versionsEndpoint,
transportClientBuilder = Http4kTransportClientBuilder(),
requiredOtherPartEndpointsProvider = { requiredEndpoints }
requiredEndpoints = requiredEndpoints
)
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b29ef05

Please sign in to comment.