Skip to content

Commit

Permalink
refactor: Refactor os versions commands (#1833)
Browse files Browse the repository at this point in the history
Fixes #1750 

## Test Plan
> How do we know the code works?

* Code is refactored according to the description in #1750 
* os version commands (list and describe) works as before

## Checklist

- [X] Unit tested
  • Loading branch information
adamfilipow92 authored Apr 23, 2021
1 parent 7ce2e3e commit ad3a74b
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 45 deletions.
11 changes: 11 additions & 0 deletions test_runner/src/main/kotlin/ftl/adapter/AndroidOsVersionsFetch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ftl.adapter

import ftl.adapter.google.toApiModel
import ftl.api.OsVersion
import ftl.client.google.androidOsVersions

object AndroidOsVersionsFetch :
OsVersion.Android.Fetch,
(String) -> List<OsVersion.Android> by {
androidOsVersions(it).toApiModel()
}
11 changes: 11 additions & 0 deletions test_runner/src/main/kotlin/ftl/adapter/IosVersionsFetch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ftl.adapter

import ftl.adapter.google.toApiModel
import ftl.api.OsVersion
import ftl.client.google.iosOsVersions

object IosVersionsFetch :
OsVersion.Ios.Fetch,
(String) -> List<OsVersion.Ios> by {
iosOsVersions(it).toApiModel()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ftl.adapter.google

import com.google.testing.model.AndroidVersion
import com.google.testing.model.Date
import com.google.testing.model.Distribution
import ftl.api.OsVersion

fun List<AndroidVersion>.toApiModel() = map {
it.toApiModel()
}

private fun AndroidVersion.toApiModel() = OsVersion.Android(
apiLevel,
codeName,
distribution?.toApiModel(),
id,
releaseDate?.toApiModel(),
tags,
versionString
)

private fun Distribution.toApiModel() = OsVersion.Distribution(marketShare, measurementTime)

private fun Date.toApiModel() = OsVersion.Date(day, month, year)
10 changes: 10 additions & 0 deletions test_runner/src/main/kotlin/ftl/adapter/google/IosOsVersions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ftl.adapter.google

import com.google.testing.model.IosVersion
import ftl.api.OsVersion

fun List<IosVersion>.toApiModel() = map {
it.toApiModel()
}

private fun IosVersion.toApiModel() = OsVersion.Ios(id, majorVersion, minorVersion, supportedXcodeVersionIds, tags)
39 changes: 39 additions & 0 deletions test_runner/src/main/kotlin/ftl/api/OsVersion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ftl.api

import ftl.adapter.AndroidOsVersionsFetch
import ftl.adapter.IosVersionsFetch

val fetchAndroidOsVersion: OsVersion.Android.Fetch get() = AndroidOsVersionsFetch
val fetchIosOsVersion: OsVersion.Ios.Fetch get() = IosVersionsFetch

object OsVersion {

data class Android(
val apiLevel: Int?,
val codeName: String?,
val distribution: Distribution?,
val id: String?,
val releaseDate: Date?,
val tags: List<String>?,
val versionString: String?,
) {
interface Fetch : (String) -> List<Android>
}

data class Ios(
val id: String,
val majorVersion: Int,
val minorVersion: Int,
val supportedXcodeVersionIds: List<String>,
val tags: List<String>,
) {
interface Fetch : (String) -> List<Ios>
}

data class Distribution(
val marketShare: Double,
val measurementTime: String,
)

data class Date(val day: Int?, val month: Int?, val year: Int?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import com.google.testing.model.AndroidDeviceCatalog
import com.google.testing.model.AndroidModel
import com.google.testing.model.Orientation
import flank.common.logLn
import ftl.api.fetchAndroidOsVersion
import ftl.environment.android.getDescription
import ftl.environment.android.toCliTable
import ftl.environment.asPrintableTable
import ftl.environment.common.toCliTable
import ftl.environment.getLocaleDescription
import ftl.gc.GcTesting
import ftl.http.executeWithRetry
Expand All @@ -32,9 +32,9 @@ object AndroidCatalog {

fun getModels(projectId: String): List<AndroidModel> = deviceCatalog(projectId).models.orEmpty()

fun supportedVersionsAsTable(projectId: String) = getVersionsList(projectId).toCliTable()
fun supportedVersionsAsTable(projectId: String) = fetchAndroidOsVersion(projectId).toCliTable()

fun describeSoftwareVersion(projectId: String, versionId: String) = getVersionsList(projectId).getDescription(versionId)
fun describeSoftwareVersion(projectId: String, versionId: String) = fetchAndroidOsVersion(projectId).getDescription(versionId)

private fun getVersionsList(projectId: String) = deviceCatalog(projectId).versions

Expand Down
24 changes: 24 additions & 0 deletions test_runner/src/main/kotlin/ftl/client/google/OsVersion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ftl.client.google

import com.google.testing.model.AndroidVersion
import com.google.testing.model.IosVersion
import ftl.gc.GcTesting
import ftl.http.executeWithRetry

fun androidOsVersions(projectId: String): List<AndroidVersion> =
GcTesting.get.testEnvironmentCatalog()
.get("android")
.setProjectId(projectId)
.executeWithRetry()
.androidDeviceCatalog
.versions
.orEmpty()

fun iosOsVersions(projectId: String): List<IosVersion> =
GcTesting.get.testEnvironmentCatalog()
.get("ios")
.setProjectId(projectId)
.executeWithRetry()
.iosDeviceCatalog
.versions
.orEmpty()
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ftl.domain

import flank.common.logLn
import ftl.api.Platform
import ftl.api.fetchAndroidOsVersion
import ftl.api.fetchDeviceModelAndroid
import ftl.api.fetchIpBlocks
import ftl.api.fetchNetworkProfiles
Expand All @@ -20,8 +21,9 @@ interface DescribeAndroidTestEnvironment {

fun DescribeAndroidTestEnvironment.invoke() {
val projectId = AndroidArgs.loadOrDefault(Paths.get(configPath)).project
// TODO move toCliTable() to presentation layer during refactor of presentation after #1728
logLn(fetchAndroidOsVersion(projectId).toCliTable())
logLn(fetchDeviceModelAndroid(projectId).toCliTable()) // TODO move toCliTable() to presentation layer during refactor of presentation after #1728
logLn(AndroidCatalog.supportedVersionsAsTable(projectId))
logLn(AndroidCatalog.localesAsTable(projectId))
logLn(fetchSoftwareCatalog().toCliTable())
logLn(fetchNetworkProfiles().toCliTable())
Expand Down
6 changes: 4 additions & 2 deletions test_runner/src/main/kotlin/ftl/domain/ListAndroidVersions.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package ftl.domain

import flank.common.logLn
import ftl.api.fetchAndroidOsVersion
import ftl.args.AndroidArgs
import ftl.client.google.AndroidCatalog
import ftl.environment.android.toCliTable
import java.nio.file.Paths

interface ListAndroidVersions {
val configPath: String
}

operator fun ListAndroidVersions.invoke() {
logLn(AndroidCatalog.supportedVersionsAsTable(AndroidArgs.loadOrDefault(Paths.get(configPath)).project))
// TODO move toCliTable() to presentation layer during refactor of presentation after #1728
logLn(fetchAndroidOsVersion(AndroidArgs.loadOrDefault(Paths.get(configPath)).project).toCliTable())
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package ftl.environment.android

import com.google.testing.model.AndroidVersion
import ftl.api.OsVersion
import ftl.environment.orUnknown
import ftl.run.exception.FlankGeneralError

fun List<AndroidVersion>.getDescription(versionId: String) = findVersion(versionId)?.prepareDescription().orErrorMessage(versionId)
fun List<OsVersion.Android>.getDescription(versionId: String) = findVersion(versionId)?.prepareDescription().orErrorMessage(versionId)

private fun List<AndroidVersion>.findVersion(versionId: String) = firstOrNull { it.id == versionId }
private fun List<OsVersion.Android>.findVersion(versionId: String) = firstOrNull { it.id == versionId }

private fun AndroidVersion.prepareDescription() = """
private fun OsVersion.Android.prepareDescription() = """
apiLevel: $apiLevel
codeName: $codeName
id: '$id'
releaseDate:
day: ${releaseDate.day}
month: ${releaseDate.month}
year: ${releaseDate.year}
""".trimIndent().addDataIfExists(tags).addVersion(versionString).trim()
day: ${releaseDate?.day}
month: ${releaseDate?.month}
year: ${releaseDate?.year}
""".trimIndent().addDataIfExists(tags).addVersion(versionString.orUnknown()).trim()

private fun String.addVersion(versionString: String) = StringBuilder(this).appendLine("\nversionString: $versionString")
.toString()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ftl.environment.android

import com.google.testing.model.AndroidVersion
import com.google.testing.model.Date
import ftl.api.OsVersion
import ftl.environment.OS_VERSION_ID
import ftl.environment.TAGS
import ftl.environment.TestEnvironmentInfo
Expand All @@ -13,9 +12,9 @@ import ftl.reports.api.twoDigitString
import ftl.util.applyColorsUsing
import ftl.util.buildTable

fun List<AndroidVersion>.toCliTable() = createTestEnvironmentInfo().createAndroidSoftwareVersionsTable()
fun List<OsVersion.Android>.toCliTable() = createTestEnvironmentInfo().createAndroidSoftwareVersionsTable()

private fun List<AndroidVersion>.createTestEnvironmentInfo() =
private fun List<OsVersion.Android>.createTestEnvironmentInfo() =
fold(mutableMapOf<String, MutableList<String>>()) { softwareInfo, softwareVersion ->
softwareInfo.apply {
getOrCreateList(OS_VERSION_ID).add(softwareVersion.id.orUnknown())
Expand All @@ -27,7 +26,7 @@ private fun List<AndroidVersion>.createTestEnvironmentInfo() =
}
}

private fun Date?.printableReleaseDate() =
private fun OsVersion.Date?.printableReleaseDate() =
if (this == null || year == null || month == null || day == null) "UNKNOWN"
else "$year-${month.twoDigitString()}-${day.twoDigitString()}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ftl.android

import com.google.common.truth.Truth.assertThat
import com.google.testing.model.AndroidDevice
import ftl.api.fetchAndroidOsVersion
import ftl.api.fetchDeviceModelAndroid
import ftl.client.google.AndroidCatalog
import ftl.environment.android.toCliTable
Expand Down Expand Up @@ -89,7 +90,7 @@ class AndroidCatalogTest {
val expectedSeparatorCount = expectedHeaders.size + 1

// when
val devicesTable = AndroidCatalog.supportedVersionsAsTable(projectId)
val devicesTable = fetchAndroidOsVersion(projectId).toCliTable()
val headers = devicesTable.lines()[1]

// then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ftl.environment.android

import com.google.testing.model.AndroidVersion
import com.google.testing.model.Date
import ftl.api.OsVersion
import ftl.test.util.TestHelper.getThrowable
import org.junit.Assert
import org.junit.Test
Expand All @@ -10,18 +9,19 @@ class AndroidSoftwareVersionDescriptionTest {
@Test
fun `should return software version with tag if any tag exists`() {
val versions = listOf(
AndroidVersion().apply {
id = "26"
apiLevel = 26
codeName = "Oreo"
versionString = "8.0.x"
releaseDate = Date().apply {
day = 21
month = 8
OsVersion.Android(
id = "26",
apiLevel = 26,
codeName = "Oreo",
versionString = "8.0.x",
releaseDate = OsVersion.Date(
day = 21,
month = 8,
year = 2017
}
tags = listOf("default")
}
),
tags = listOf("default"),
distribution = null
)
)

val localesDescription = versions.getDescription("26")
Expand All @@ -43,17 +43,19 @@ class AndroidSoftwareVersionDescriptionTest {
@Test
fun `should return software version without tag if no tags`() {
val versions = listOf(
AndroidVersion().apply {
id = "23"
apiLevel = 23
codeName = "Marshmallow"
versionString = "6.0.x"
releaseDate = Date().apply {
day = 5
month = 10
year = 2015
}
}
OsVersion.Android(
id = "23",
apiLevel = 23,
codeName = "Marshmallow",
versionString = "6.0.x",
releaseDate = OsVersion.Date(
day = 5,
month = 10,
year = 2015,
),
distribution = null,
tags = null
)
)

val localesDescription = versions.getDescription("23")
Expand All @@ -72,7 +74,7 @@ class AndroidSoftwareVersionDescriptionTest {

@Test
fun `should return error message if version not found`() {
val versions = listOf<AndroidVersion>()
val versions = listOf<OsVersion.Android>()
val versionName = "test"
val localesDescription = getThrowable { versions.getDescription(versionName) }
val expected = "ERROR: '$versionName' is not a valid OS version"
Expand Down

0 comments on commit ad3a74b

Please sign in to comment.