diff --git a/test_runner/src/main/kotlin/ftl/api/NetworkProfile.kt b/test_runner/src/main/kotlin/ftl/api/NetworkProfile.kt index 0608d19d5b..b30799230d 100644 --- a/test_runner/src/main/kotlin/ftl/api/NetworkProfile.kt +++ b/test_runner/src/main/kotlin/ftl/api/NetworkProfile.kt @@ -17,5 +17,9 @@ data class NetworkProfile( val burst: Float? ) + data class ErrorMessage( + val message: String + ) + interface Fetch : () -> List } diff --git a/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt b/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt index d4656ac868..037e6ee3e2 100644 --- a/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt +++ b/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt @@ -1,14 +1,18 @@ package ftl.domain -import flank.common.logLn +import ftl.api.NetworkProfile 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)) + val description = networkProfileDescription(profileId) + ?: NetworkProfile.ErrorMessage("Unable to fetch profile [$profileId] description") + + description.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..59640da7f6 100644 --- a/test_runner/src/main/kotlin/ftl/environment/NetworkProfileDescription.kt +++ b/test_runner/src/main/kotlin/ftl/environment/NetworkProfileDescription.kt @@ -3,49 +3,20 @@ package ftl.environment import ftl.api.NetworkProfile import ftl.api.fetchNetworkProfiles -fun networkProfileDescription(profile: String) = fetchNetworkProfiles() +fun networkProfileDescription(profile: String): NetworkProfile? = 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(): String = """ downRule: - bandwidth: ${downRule.bandwidth} - delay: ${downRule.delay} - packetLossRatio: ${downRule.packetLossRatio} + bandwidth: ${downRule.bandwidth ?: UNABLE_TO_FETCH} + delay: ${downRule.delay ?: UNABLE_TO_FETCH} + packetLossRatio: ${downRule.packetLossRatio ?: UNABLE_TO_FETCH} id: $id upRule: - bandwidth: ${upRule.bandwidth} - delay: ${upRule.delay} - packetLossRatio: ${upRule.packetLossRatio} + bandwidth: ${upRule.bandwidth ?: UNABLE_TO_FETCH} + delay: ${upRule.delay ?: UNABLE_TO_FETCH} + packetLossRatio: ${upRule.packetLossRatio ?: UNABLE_TO_FETCH} """.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 const val UNABLE_TO_FETCH = "[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..34da80cd73 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 NetworkProfile.ErrorMessage -> message + else -> throwUnknownType() + } + } }