Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Runnner Improvement #23

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
implementation("com.squareup.okhttp3:okhttp:4.11.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.yaml:snakeyaml:2.2")
implementation("com.google.code.gson:gson:2.10.1")
}

// Apply a specific Java toolchain to ease working on different environments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ private fun String.runCommand(workingDir: File): String? =
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
process.waitFor(60, TimeUnit.MINUTES)
process.inputStream.bufferedReader().readText()
} catch (e: IOException) {
printMessageInRedColor("Exception while executing command -> ${e.message}")
Expand Down
26 changes: 14 additions & 12 deletions src/main/kotlin/com/featurevisor/testRunner/Matrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ fun generateCombinations(
keys: List<String>,
matrix: AssertionMatrix,
idx: Int,
prev: MutableMap<String, Any>,
combinations: MutableList<MutableMap<String, Any>>
prev: MutableMap<String, AttributeValue>,
combinations: MutableList<MutableMap<String, AttributeValue>>
) {
val key = keys[idx]
val values = matrix[key] ?: emptyList()
Expand All @@ -23,19 +23,19 @@ fun generateCombinations(
}
}

fun getMatrixCombinations(matrix: AssertionMatrix): List<MutableMap<String, Any>> {
fun getMatrixCombinations(matrix: AssertionMatrix): List<MutableMap<String, AttributeValue>> {
val keys = matrix.keys.toList()

if (keys.isEmpty()) {
return emptyList()
}

val combinations = mutableListOf<MutableMap<String, Any>>()
val combinations = mutableListOf<MutableMap<String, AttributeValue>>()
generateCombinations(keys, matrix, 0, mutableMapOf(), combinations)
return combinations
}

fun applyCombinationToValue(value: Any?, combination: Map<String, Any>): Any? {
fun applyCombinationToValue(value: Any?, combination: Map<String, AttributeValue>): Any? {
if (value is String) {
val variableKeysInValue = Regex("""\$\{\{\s*([^\s}]+)\s*}}""").findAll(value)

Expand All @@ -46,14 +46,14 @@ fun applyCombinationToValue(value: Any?, combination: Map<String, Any>): Any? {
return variableKeysInValue.fold(value) { acc, result ->
val key = result.groupValues[1].trim()
val regex = Regex("""\$\{\{\s*([^\s}]+)\s*}}""")
acc.replace(regex, combination[key].toString())
acc.replace(regex, getContextValues(combination[key]).toString())
}
}
return value
}

fun applyCombinationToFeatureAssertion(
combination: Map<String, Any>,
combination: Map<String, AttributeValue>,
assertion: FeatureAssertion
): FeatureAssertion {
val flattenedAssertion = assertion.copy()
Expand Down Expand Up @@ -92,7 +92,7 @@ fun getFeatureAssertionsFromMatrix(
): List<FeatureAssertion> {
if (assertionWithMatrix.matrix == null) {
val assertion = assertionWithMatrix.copy()
assertion.description = "Assertion #${aIndex + 1}: (${assertion.environment}) ${
assertion.description = "Assertion #${ nextCount()}: (${assertion.environment}) ${
assertion.description ?: "at ${getAtValue(assertion.at)}%"
}"
return listOf(assertion)
Expand All @@ -103,7 +103,7 @@ fun getFeatureAssertionsFromMatrix(

for (combination in combinations) {
val assertion = applyCombinationToFeatureAssertion(combination, assertionWithMatrix)
assertion.description = "Assertion #${aIndex + 1}: (${assertion.environment}) ${
assertion.description = "Assertion #${ nextCount()}: (${assertion.environment}) ${
assertion.description ?: "at ${getAtValue(assertion.at)}%"
}"
assertions.add(assertion)
Expand All @@ -128,7 +128,7 @@ fun getAtValue(at: WeightType) = when (at) {
}

fun applyCombinationToSegmentAssertion(
combination: Map<String, Any>,
combination: Map<String, AttributeValue>,
assertion: SegmentAssertion
): SegmentAssertion {
val flattenedAssertion = assertion.copy()
Expand All @@ -151,16 +151,18 @@ fun getSegmentAssertionsFromMatrix(
): List<SegmentAssertion> {
if (assertionWithMatrix.matrix == null) {
val assertion = assertionWithMatrix.copy()
assertion.description = "Assertion #${aIndex + 1}: ${assertion.description ?: "#${aIndex + 1}"}"
val assertionCount = nextCount()
assertion.description = "Assertion #${assertionCount}: ${assertion.description ?: "#${assertionCount}"}"
return listOf(assertion)
}

val assertions = mutableListOf<SegmentAssertion>()
val combinations = getMatrixCombinations(assertionWithMatrix.matrix)

for (combination in combinations) {
val assertionCount = nextCount()
val assertion = applyCombinationToSegmentAssertion(combination, assertionWithMatrix)
assertion.description = "Assertion #${aIndex + 1}: ${assertion.description ?: "#${aIndex + 1}"}"
assertion.description = "Assertion #${assertionCount}: ${assertion.description ?: "#${assertionCount}"}"
assertions.add(assertion)
}

Expand Down
67 changes: 56 additions & 11 deletions src/main/kotlin/com/featurevisor/testRunner/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package com.featurevisor.testRunner
import com.featurevisor.sdk.serializers.isValidJson
import com.featurevisor.sdk.serializers.mapOperator
import com.featurevisor.types.*
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.Json
import com.google.gson.Gson
import org.yaml.snakeyaml.Yaml
import java.io.File
import java.util.*
Expand All @@ -26,7 +24,11 @@ internal fun parseTestFeatureAssertions(yamlFilePath: String) =
description = assertionMap["description"] as? String,
context = (assertionMap["context"] as Map<AttributeKey, Any?>).mapValues { parseAttributeValue(it.value) },
expectedToMatch = assertionMap["expectedToMatch"] as Boolean,
matrix = assertionMap["matrix"] as? AssertionMatrix
matrix = (assertionMap["matrix"] as? Map<String, List<Any>>)?.mapValues {
it.value.map { item ->
mapMatrixValues(item)
}
}
)
}
val testSegment = TestSegment(key = segment, assertions = segmentAssertion)
Expand All @@ -45,7 +47,11 @@ internal fun parseTestFeatureAssertions(yamlFilePath: String) =
)
},
expectedVariation = assertionMap["expectedVariation"] as? String,
matrix = assertionMap["matrix"] as? AssertionMatrix
matrix = (assertionMap["matrix"] as? Map<String, List<Any>>)?.mapValues {
it.value.map { item ->
mapMatrixValues(item)
}
}
)
}

Expand All @@ -55,10 +61,35 @@ internal fun parseTestFeatureAssertions(yamlFilePath: String) =
null
}
} catch (e: Exception) {
printMessageInRedColor("Exception while parsing Yaml Assertion File --> ${e.message}")
printMessageInRedColor("Exception while parsing Yaml Assertion File -- $yamlFilePath --> ${e.message}")
null
}

private fun mapMatrixValues(value: Any) =
when(value){
is Boolean -> {
if (value){
AttributeValue.StringValue("yes")
}else{
AttributeValue.StringValue("no")
}
}
is Int -> {
AttributeValue.IntValue(value)
}
is Double -> {
AttributeValue.DoubleValue(value)
}
is String -> {
AttributeValue.StringValue(value)
}
is Date -> {
AttributeValue.DateValue(value)
}

else -> { AttributeValue.StringValue("")}
}

private fun parseWeightValue(value: Any): WeightType {
return when (value) {
is Int -> WeightType.IntType(value)
Expand Down Expand Up @@ -87,9 +118,8 @@ private fun parseVariableValue(value: Any?): VariableValue {
}

is Map<*, *> -> {
val mapData = value as Map<String, String>
val json1 = Json.encodeToString(MapSerializer(String.serializer(), String.serializer()), mapData)
VariableValue.JsonValue(json1)
val json = Gson().toJson(value)
VariableValue.JsonValue(json)
}

else -> throw IllegalArgumentException("Unsupported variable value type")
Expand Down Expand Up @@ -121,8 +151,17 @@ private fun parseAttributeValue(value: Any?): AttributeValue {
AttributeValue.DateValue(value)
}

is List<*> -> {
AttributeValue.StringValue(value.toString())
}

is Map<*, *> -> {
val json = Gson().toJson(value)
AttributeValue.StringValue(json)
}

else -> {
throw IllegalArgumentException("Unsupported attribute value type")
throw IllegalArgumentException("Unsupported attribute value type $value")
}
}
}
Expand Down Expand Up @@ -199,7 +238,13 @@ private fun parseConditionValue(value: Any?): ConditionValue {
}

return when (value) {
is String -> ConditionValue.StringValue(value)
is String -> {
value.toIntOrNull()?.let {
ConditionValue.IntValue(value.toInt())
} ?: value.toDoubleOrNull()?.let {
ConditionValue.DoubleValue(value.toDouble())
} ?: ConditionValue.StringValue(value)
}
is Int -> ConditionValue.IntValue(value)
is Double -> ConditionValue.DoubleValue(value)
is Boolean -> ConditionValue.BooleanValue(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fun startTest(option: TestProjectOption) {

private fun executeTest(filePath: String, dataFile: DataFile, option: TestProjectOption): ExecutionResult {
val test = parseTestFeatureAssertions(filePath)
setCount(0)

val executionResult = ExecutionResult(
passed = true,
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/com/featurevisor/testRunner/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ internal val json = Json {
isLenient = true
}

private var count = 0
Tan108 marked this conversation as resolved.
Show resolved Hide resolved

internal fun nextCount(): Int {
count++
return count
}

internal fun setCount(counter:Int){
count = counter
}

internal fun printMessageInGreenColor(message: String) =
println("$ANSI_GREEN$message$ANSI_RESET")

Expand Down Expand Up @@ -134,13 +145,14 @@ fun getContextValue(contextValue: Any?) =
else -> throw Exception("Unsupported context value")
}

fun getContextValues(contextValue: AttributeValue) =
fun getContextValues(contextValue: AttributeValue?) =
when (contextValue) {
is AttributeValue.IntValue -> contextValue.value
is AttributeValue.DoubleValue -> contextValue.value
is AttributeValue.StringValue -> contextValue.value
is AttributeValue.BooleanValue -> contextValue.value
is AttributeValue.DateValue -> contextValue.value
null -> null
}

fun checkIfArraysAreEqual(a: Array<Any>, b: Array<Any>): Boolean {
Expand Down
Loading