From 73047eec386a7e6af2b00c57f76fcb7642e14c19 Mon Sep 17 00:00:00 2001 From: Pawel Pasterz Date: Thu, 29 Apr 2021 19:45:25 +0200 Subject: [PATCH 1/2] Refactor DescribeNetworkProfiles logging --- .../ftl/domain/DescribeNetworkProfiles.kt | 6 +-- .../environment/NetworkProfileDescription.kt | 44 ++++--------------- .../main/kotlin/ftl/presentation/Output.kt | 19 ++++++++ .../NetworkProfilesDescribeCommand.kt | 12 +++++ 4 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 test_runner/src/main/kotlin/ftl/presentation/Output.kt 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() + } + } } From af885e9671e55dc37766ec9fba4a715fc7f8fe70 Mon Sep 17 00:00:00 2001 From: Pawel Pasterz Date: Fri, 30 Apr 2021 08:10:42 +0200 Subject: [PATCH 2/2] Review changes --- .../src/main/kotlin/ftl/api/NetworkProfile.kt | 4 ++++ .../ftl/domain/DescribeNetworkProfiles.kt | 6 +++++- .../environment/NetworkProfileDescription.kt | 19 +++++++++---------- .../NetworkProfilesDescribeCommand.kt | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) 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 0ab9e0906e..037e6ee3e2 100644 --- a/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt +++ b/test_runner/src/main/kotlin/ftl/domain/DescribeNetworkProfiles.kt @@ -1,5 +1,6 @@ package ftl.domain +import ftl.api.NetworkProfile import ftl.environment.networkProfileDescription import ftl.presentation.Output import ftl.run.exception.FlankConfigurationError @@ -10,5 +11,8 @@ interface DescribeNetworkProfiles : Output { operator fun DescribeNetworkProfiles.invoke() { if (profileId.isBlank()) throw FlankConfigurationError("Argument PROFILE_ID must be specified.") - networkProfileDescription(profileId).out() + 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 a50e6061ed..59640da7f6 100644 --- a/test_runner/src/main/kotlin/ftl/environment/NetworkProfileDescription.kt +++ b/test_runner/src/main/kotlin/ftl/environment/NetworkProfileDescription.kt @@ -3,21 +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) } - ?: "Unable to fetch profile [$profile] description" -fun NetworkProfile.prepareDescription() = +fun NetworkProfile.prepareDescription(): String = """ downRule: - bandwidth: ${downRule.bandwidth.orUnable()} - delay: ${downRule.delay.orUnable()} - packetLossRatio: ${downRule.packetLossRatio.orUnable()} + bandwidth: ${downRule.bandwidth ?: UNABLE_TO_FETCH} + delay: ${downRule.delay ?: UNABLE_TO_FETCH} + packetLossRatio: ${downRule.packetLossRatio ?: UNABLE_TO_FETCH} id: $id upRule: - bandwidth: ${upRule.bandwidth.orUnable()} - delay: ${upRule.delay.orUnable()} - packetLossRatio: ${upRule.packetLossRatio.orUnable()} + bandwidth: ${upRule.bandwidth ?: UNABLE_TO_FETCH} + delay: ${upRule.delay ?: UNABLE_TO_FETCH} + packetLossRatio: ${upRule.packetLossRatio ?: UNABLE_TO_FETCH} """.trimIndent() -private fun Any?.orUnable() = this ?: "[Unable to fetch]" +private const val UNABLE_TO_FETCH = "[Unable to fetch]" 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 5aad50746e..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 @@ -40,7 +40,7 @@ class NetworkProfilesDescribeCommand : override val out = outputLogger { when (this) { is NetworkProfile -> prepareDescription() - is String -> this + is NetworkProfile.ErrorMessage -> message else -> throwUnknownType() } }