Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,14 +33,20 @@ class HapiFhirFederationEndpointAdapter(
) : FederationListPort {

override fun fetchFederationEndpoints(): List<FederationEndpoint> {
val bundle = fhirClient.search<Bundle>()
.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<Bundle>()
.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<Endpoint>()
.mapNotNull(endpointMapper::toDomain)
.distinctBy { it.fqdn }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/io/cloudflight/pgk/proxy/shared/Fqdn.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class HapiFhirFederationEndpointAdapterTest {
capturedParams += arg<ICriterion<*>>(0) as ICriterionInternal
query
}
every { query.count(any()) } returns query
every { query.returnBundle(Bundle::class.java) } returns query
every { query.execute() } returns bundle

Expand Down Expand Up @@ -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<ICriterionInternal>()
val adapter = HapiFhirFederationEndpointAdapter(
clientReturning(bundleWith(), capturedParams),
mapper
)

adapter.fetchFederationEndpoints()

val criterion = capturedParams.single { it.parameterName == "status" }
assertEquals("active", criterion.getParameterValue(FhirContext.forR4()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading