diff --git a/src/main/kotlin/io/cloudflight/pgk/proxy/federation/adapter/outbound/HapiFhirFederationEndpointAdapter.kt b/src/main/kotlin/io/cloudflight/pgk/proxy/federation/adapter/outbound/HapiFhirFederationEndpointAdapter.kt index 1dcf4cf..dab70b7 100644 --- a/src/main/kotlin/io/cloudflight/pgk/proxy/federation/adapter/outbound/HapiFhirFederationEndpointAdapter.kt +++ b/src/main/kotlin/io/cloudflight/pgk/proxy/federation/adapter/outbound/HapiFhirFederationEndpointAdapter.kt @@ -21,6 +21,7 @@ import ca.uhn.fhir.rest.gclient.TokenClientParam import io.cloudflight.pgk.proxy.federation.domain.FederationEndpoint import io.cloudflight.pgk.proxy.federation.mapper.FederationEndpointMapper import io.cloudflight.pgk.proxy.federation.port.FederationListPort +import io.cloudflight.pgk.proxy.infrastructure.fhir.HapiBundlePager import org.hl7.fhir.r4.model.Bundle import org.hl7.fhir.r4.model.Endpoint import org.springframework.stereotype.Component @@ -32,14 +33,20 @@ class HapiFhirFederationEndpointAdapter( ) : FederationListPort { override fun fetchFederationEndpoints(): List { - val bundle = fhirClient.search() - .forResource(Endpoint::class.java) - .where(TokenClientParam(Endpoint.SP_CONNECTION_TYPE).exactly() - .systemAndIdentifier(CONNECTION_TYPE_SYSTEM, CONNECTION_TYPE_CODE)) - .returnBundle(Bundle::class.java) - .execute() + val endpoints = HapiBundlePager.allResources( + fhirClient, + fhirClient.search() + .forResource(Endpoint::class.java) + .where(TokenClientParam(Endpoint.SP_CONNECTION_TYPE).exactly() + .systemAndIdentifier(CONNECTION_TYPE_SYSTEM, CONNECTION_TYPE_CODE)) + .where(TokenClientParam(Endpoint.SP_STATUS).exactly() + .code(Endpoint.EndpointStatus.ACTIVE.toCode())) + .count(HapiBundlePager.PAGE_SIZE) + .returnBundle(Bundle::class.java) + .execute() + ) - return bundle.entry.mapNotNull { it.resource as? Endpoint } + return endpoints.filterIsInstance() .mapNotNull(endpointMapper::toDomain) .distinctBy { it.fqdn } } diff --git a/src/main/kotlin/io/cloudflight/pgk/proxy/federation/mapper/FederationEndpointMapper.kt b/src/main/kotlin/io/cloudflight/pgk/proxy/federation/mapper/FederationEndpointMapper.kt index b418c05..62dace0 100644 --- a/src/main/kotlin/io/cloudflight/pgk/proxy/federation/mapper/FederationEndpointMapper.kt +++ b/src/main/kotlin/io/cloudflight/pgk/proxy/federation/mapper/FederationEndpointMapper.kt @@ -21,7 +21,6 @@ import io.cloudflight.pgk.proxy.shared.Fqdn import io.cloudflight.pgk.proxy.shared.TrustLevel import org.hl7.fhir.r4.model.Endpoint import org.springframework.stereotype.Component -import java.net.URI @Component class FederationEndpointMapper { @@ -37,14 +36,19 @@ class FederationEndpointMapper { private fun extractFqdn(address: String?): Fqdn? { if (address.isNullOrBlank()) return null - val host = if (address.contains("://")) { - runCatching { URI(address).host }.getOrNull() - } else { - address.substringBefore("/").substringBefore(":") - } - - if (host.isNullOrBlank()) return null + val host = extractHost(address) ?: return null return runCatching { Fqdn(host) }.getOrNull() + } + private fun extractHost(address: String): String? { + val authority = address.substringAfter("://").substringBefore("/") + val host = if (authority.startsWith("[")) { + val closingBracket = authority.indexOf(']') + if (closingBracket == -1) return null + authority.substring(1, closingBracket) + } else { + authority.substringBefore(":") + } + return host.ifBlank { null } } } diff --git a/src/main/kotlin/io/cloudflight/pgk/proxy/shared/Fqdn.kt b/src/main/kotlin/io/cloudflight/pgk/proxy/shared/Fqdn.kt index 2eeb215..7d1520b 100644 --- a/src/main/kotlin/io/cloudflight/pgk/proxy/shared/Fqdn.kt +++ b/src/main/kotlin/io/cloudflight/pgk/proxy/shared/Fqdn.kt @@ -20,6 +20,6 @@ data class Fqdn(val value: String) { init { require(value.isNotBlank()) { "FQDN must not be blank" } - require(value.contains(".")) { "FQDN must contain at least one dot" } + require(value.contains(".") || value.contains(":")) { "FQDN must be a domain name or IP literal" } } } diff --git a/src/test/kotlin/io/cloudflight/pgk/proxy/federation/adapter/outbound/HapiFhirFederationEndpointAdapterTest.kt b/src/test/kotlin/io/cloudflight/pgk/proxy/federation/adapter/outbound/HapiFhirFederationEndpointAdapterTest.kt index edf0fe3..61cfc3a 100644 --- a/src/test/kotlin/io/cloudflight/pgk/proxy/federation/adapter/outbound/HapiFhirFederationEndpointAdapterTest.kt +++ b/src/test/kotlin/io/cloudflight/pgk/proxy/federation/adapter/outbound/HapiFhirFederationEndpointAdapterTest.kt @@ -59,6 +59,7 @@ class HapiFhirFederationEndpointAdapterTest { capturedParams += arg>(0) as ICriterionInternal query } + every { query.count(any()) } returns query every { query.returnBundle(Bundle::class.java) } returns query every { query.execute() } returns bundle @@ -152,11 +153,25 @@ class HapiFhirFederationEndpointAdapterTest { adapter.fetchFederationEndpoints() - val criterion = capturedParams.single() - assertEquals("connection-type", criterion.parameterName) + val criterion = capturedParams.single { it.parameterName == "connection-type" } assertEquals( "https://pgk.elga.gv.at/CodeSystem/endpoint-connection-type|matrix-homeserver", criterion.getParameterValue(FhirContext.forR4()) ) } + + @Test + @DisplayName("filters the search request by active endpoint status") + fun testFiltersSearchRequestByActiveStatus() { + val capturedParams = mutableListOf() + val adapter = HapiFhirFederationEndpointAdapter( + clientReturning(bundleWith(), capturedParams), + mapper + ) + + adapter.fetchFederationEndpoints() + + val criterion = capturedParams.single { it.parameterName == "status" } + assertEquals("active", criterion.getParameterValue(FhirContext.forR4())) + } } diff --git a/src/test/kotlin/io/cloudflight/pgk/proxy/federation/mapper/FederationEndpointMapperTest.kt b/src/test/kotlin/io/cloudflight/pgk/proxy/federation/mapper/FederationEndpointMapperTest.kt index 88b4748..e960274 100644 --- a/src/test/kotlin/io/cloudflight/pgk/proxy/federation/mapper/FederationEndpointMapperTest.kt +++ b/src/test/kotlin/io/cloudflight/pgk/proxy/federation/mapper/FederationEndpointMapperTest.kt @@ -137,6 +137,54 @@ class FederationEndpointMapperTest { assertNull(mapper.toDomain(Endpoint().apply { meta.addSecurity(TrustLevel.TL_4.toCoding()) })) } + @Test + @DisplayName("extracts the FQDN from a bare host with a TLD") + fun testExtractsFqdnFromBareHostWithTld() { + val result = mapper.toDomain(endpoint("matrix.org", TrustLevel.TL_4)) + + assertEquals(Fqdn("matrix.org"), result?.fqdn) + } + + @Test + @DisplayName("extracts the FQDN from a bare host with a TLD and an explicit port") + fun testExtractsFqdnFromBareHostWithTldAndPort() { + val result = mapper.toDomain(endpoint("matrix.org:8888", TrustLevel.TL_4)) + + assertEquals(Fqdn("matrix.org"), result?.fqdn) + } + + @Test + @DisplayName("extracts the FQDN from an IPv4 literal") + fun testExtractsFqdnFromIpv4Literal() { + val result = mapper.toDomain(endpoint("1.2.3.4", TrustLevel.TL_4)) + + assertEquals(Fqdn("1.2.3.4"), result?.fqdn) + } + + @Test + @DisplayName("extracts the FQDN from an IPv4 literal with an explicit port") + fun testExtractsFqdnFromIpv4LiteralWithPort() { + val result = mapper.toDomain(endpoint("1.2.3.4:1234", TrustLevel.TL_4)) + + assertEquals(Fqdn("1.2.3.4"), result?.fqdn) + } + + @Test + @DisplayName("extracts the FQDN from an IPv6 literal") + fun testExtractsFqdnFromIpv6Literal() { + val result = mapper.toDomain(endpoint("[1234:5678::abcd]", TrustLevel.TL_4)) + + assertEquals(Fqdn("1234:5678::abcd"), result?.fqdn) + } + + @Test + @DisplayName("extracts the FQDN from an IPv6 literal with an explicit port") + fun testExtractsFqdnFromIpv6LiteralWithPort() { + val result = mapper.toDomain(endpoint("[1234:5678::abcd]:5678", TrustLevel.TL_4)) + + assertEquals(Fqdn("1234:5678::abcd"), result?.fqdn) + } + @Test @DisplayName("returns null for an address without a valid FQDN") fun testReturnsNullForInvalidAddress() {