diff --git a/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt b/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt index d4656ac868..0ab9e0906e 100644 --- a/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt +++ b/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt @@ -1,14 +1,14 @@ package ftl.domain -import flank.common.logLn import ftl.environment.networkProfileDescription +import ftl.presentation.Output import ftl.run.exception.FlankConfigurationError -interface DescribeNetworkProfiles { +interface DescribeNetworkProfiles : Output { val profileId: String } operator fun DescribeNetworkProfiles.invoke() { if (profileId.isBlank()) throw FlankConfigurationError("Argument PROFILE_ID must be specified.") - logLn(networkProfileDescription(profileId)) + networkProfileDescription(profileId).out() } diff --git a/test_runner/src/main/kotlin/ftl/environment/NetworkProfileDescription.kt b/test_runner/src/main/kotlin/ftl/environment/NetworkProfileDescription.kt index 9dfbf4b2bd..a50e6061ed 100644 --- a/test_runner/src/main/kotlin/ftl/environment/NetworkProfileDescription.kt +++ b/test_runner/src/main/kotlin/ftl/environment/NetworkProfileDescription.kt @@ -5,47 +5,19 @@ import ftl.api.fetchNetworkProfiles fun networkProfileDescription(profile: String) = fetchNetworkProfiles() .find { it.id.equals(profile, ignoreCase = true) } - .toNullProof() - .prepareDescription() ?: "Unable to fetch profile [$profile] description" -private fun NetworkProfile?.toNullProof() = this?.run { - NetworkConfigurationWrapper( - downRule = wrappedOrEmpty(downRule), - upRule = wrappedOrEmpty(upRule), - id = id.toStringOrUnableToFetch() - ) -} - -private fun wrappedOrEmpty(rule: NetworkProfile.Rule?) = rule?.let { - Rule( - bandwidth = it.bandwidth.toStringOrUnableToFetch(), - delay = it.delay.toStringOrUnableToFetch(), - packetLossRatio = it.packetLossRatio.toStringOrUnableToFetch() - ) -} ?: emptyRule - -private const val UNABLE = "[Unable to fetch]" - -private fun Any?.toStringOrUnableToFetch() = this?.toString() ?: UNABLE - -private val emptyRule: Rule - get() = Rule(UNABLE, UNABLE, UNABLE) - -private fun NetworkConfigurationWrapper?.prepareDescription() = this?.run { +fun NetworkProfile.prepareDescription() = """ downRule: - bandwidth: ${downRule.bandwidth} - delay: ${downRule.delay} - packetLossRatio: ${downRule.packetLossRatio} + bandwidth: ${downRule.bandwidth.orUnable()} + delay: ${downRule.delay.orUnable()} + packetLossRatio: ${downRule.packetLossRatio.orUnable()} id: $id upRule: - bandwidth: ${upRule.bandwidth} - delay: ${upRule.delay} - packetLossRatio: ${upRule.packetLossRatio} + bandwidth: ${upRule.bandwidth.orUnable()} + delay: ${upRule.delay.orUnable()} + packetLossRatio: ${upRule.packetLossRatio.orUnable()} """.trimIndent() -} - -private data class NetworkConfigurationWrapper(val downRule: Rule, val upRule: Rule, val id: String) -private data class Rule(val bandwidth: String, val delay: String, val packetLossRatio: String) +private fun Any?.orUnable() = this ?: "[Unable to fetch]" diff --git a/test_runner/src/main/kotlin/ftl/presentation/Output.kt b/test_runner/src/main/kotlin/ftl/presentation/Output.kt new file mode 100644 index 0000000000..c22cd75003 --- /dev/null +++ b/test_runner/src/main/kotlin/ftl/presentation/Output.kt @@ -0,0 +1,19 @@ +package ftl.presentation + +import flank.common.logLn + +/** + * The abstraction which allows passing output from the domain to presentation. + * Implement in domain top-level interfaces for getting access to outputting result structures. + * @property out The reference to outputting result function. + */ +interface Output { + val out: Any.() -> Unit +} + +fun outputLogger(map: Any.() -> String): Any.() -> Unit = { + logLn(map()) +} + +fun Any.throwUnknownType(): Nothing = + throw IllegalArgumentException(javaClass.toGenericString()) diff --git a/test_runner/src/main/kotlin/ftl/presentation/cli/firebase/test/networkprofiles/NetworkProfilesDescribeCommand.kt b/test_runner/src/main/kotlin/ftl/presentation/cli/firebase/test/networkprofiles/NetworkProfilesDescribeCommand.kt index 76d0380606..5aad50746e 100644 --- a/test_runner/src/main/kotlin/ftl/presentation/cli/firebase/test/networkprofiles/NetworkProfilesDescribeCommand.kt +++ b/test_runner/src/main/kotlin/ftl/presentation/cli/firebase/test/networkprofiles/NetworkProfilesDescribeCommand.kt @@ -1,7 +1,11 @@ package ftl.presentation.cli.firebase.test.networkprofiles +import ftl.api.NetworkProfile import ftl.domain.DescribeNetworkProfiles import ftl.domain.invoke +import ftl.environment.prepareDescription +import ftl.presentation.outputLogger +import ftl.presentation.throwUnknownType import picocli.CommandLine @CommandLine.Command( @@ -32,4 +36,12 @@ class NetworkProfilesDescribeCommand : override var profileId: String = "" override fun run() = invoke() + + override val out = outputLogger { + when (this) { + is NetworkProfile -> prepareDescription() + is String -> this + else -> throwUnknownType() + } + } }