Skip to content

Commit

Permalink
More tests, some clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfilipow92 committed May 13, 2020
1 parent c572001 commit 5f0158c
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package ftl.args.yml

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
import ftl.args.yml.errors.ConfigErrorMessageBuilder
import ftl.args.yml.errors.ErrorParser
import ftl.args.yml.errors.ConfigurationErrorMessageBuilder
import ftl.util.FlankConfigurationException

fun convertMissingParameterException(missingParameterError: MissingKotlinParameterException, yaml: JsonNode): Throwable {
val errorMessageBuilder = ConfigErrorMessageBuilder()
fun convertConfigurationErrorExceptions(missingParameterError: Exception, yaml: JsonNode): Throwable {
val errorMessageBuilder = ConfigurationErrorMessageBuilder()
val errorMessage = missingParameterError.message
return if (errorMessage != null) {
FlankConfigurationException(errorMessageBuilder(errorMessage, yaml))
Expand Down
7 changes: 4 additions & 3 deletions test_runner/src/main/kotlin/ftl/args/yml/YamlObjecMapper.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ftl.args.yml

import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException

Expand All @@ -11,7 +10,9 @@ class YamlObjecMapper() : ObjectMapper(YAMLFactory()) {
try {
return readValue(content, _typeFactory.constructType(valueType))
} catch (missingParameterError: MissingKotlinParameterException) {
throw convertMissingParameterException(missingParameterError, readTree(content))
throw convertConfigurationErrorExceptions(missingParameterError, readTree(content))
} catch (mismatchedInputException: MismatchedInputException) {
throw convertConfigurationErrorExceptions(mismatchedInputException, readTree(content))
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package ftl.args.yml.errors

import com.fasterxml.jackson.databind.JsonNode
import java.lang.Exception
import javax.swing.tree.TreeNode

class ConfigErrorMessageBuilder {
private val parseMessage = ErrorParser()
class ConfigurationErrorMessageBuilder {

private val parseMessage = ConfigurationErrorParser()
private val resolveErrorNode = ErrorNodeResolver()

//region error message elements
private val messageHeader = "Error on parse config: "
private val missingElementMessage = "Missing element or value for: '%s'"
Expand All @@ -17,23 +18,19 @@ class ConfigErrorMessageBuilder {

private val exceptionTemplate = "Parse message error: %s"

operator fun invoke(errorMessage: String) =
try {
val errorModel = parseMessage(errorMessage)
StringBuilder(messageHeader).appendln(createReferenceChain(errorModel.referenceChain)).appendln(
missingElementMessage.format(errorModel.propertyName)
).appendln(atMessage.format(errorModel.line, errorModel.column)).toString().trim()
} catch (error: Exception) {
exceptionTemplate.format(errorMessage)
}

operator fun invoke(errorMessage: String, yamlTreeNode: JsonNode) =
operator fun invoke(errorMessage: String, yamlTreeNode: JsonNode? = null) =
try {
val errorModel = parseMessage(errorMessage)
StringBuilder(messageHeader).appendln(createReferenceChain(errorModel.referenceChain)).appendln(
missingElementMessage.format(errorModel.propertyName)
).appendln(atMessage.format(errorModel.line, errorModel.column))
.appendln(errorNodeMessage.format(resolveErrorNode(yamlTreeNode, errorModel))).toString().trim()
val errorMessageBuilder = StringBuilder(messageHeader)
errorMessageBuilder.appendln(createReferenceChain(errorModel.referenceChain))
if (errorModel.propertyName != "") {
errorMessageBuilder.appendln(missingElementMessage.format(errorModel.propertyName))
}
errorMessageBuilder.appendln(atMessage.format(errorModel.line, errorModel.column))
yamlTreeNode?.let {
errorMessageBuilder.appendln(errorNodeMessage.format(resolveErrorNode(yamlTreeNode, errorModel)))
}
errorMessageBuilder.toString().trim()
} catch (error: Exception) {
exceptionTemplate.format(errorMessage)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package ftl.args.yml.errors

internal data class ConfigurationErrorModel(val propertyName: String, val line: Int, val column: Int, val referenceChain: List<String>)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ftl.args.yml.errors

internal class ErrorParser {
internal class ConfigurationErrorParser {

//region regex patterns
private val propertyNameRegex = "(?<=property\\s)[a-z]*".toRegex()
Expand All @@ -9,18 +9,17 @@ internal class ErrorParser {
private val lineAndColumnRegex = "((?<=line:\\s)\\d*), column:\\s(\\d*)".toRegex()
//endregion


operator fun invoke(errorMessage: String): ConfigErrorModel {
operator fun invoke(errorMessage: String): ConfigurationErrorModel {
val (line, column) = parseErrorPositionLine(errorMessage)
return ConfigErrorModel(
return ConfigurationErrorModel(
parsePropertyName(errorMessage),
line.toInt(),
column.toInt(),
parseReferenceChain(errorMessage)
)
}

private fun parsePropertyName(errorMessage: String) = propertyNameRegex.find(errorMessage)!!.value
private fun parsePropertyName(errorMessage: String) = propertyNameRegex.find(errorMessage)?.value ?: ""
private fun parseErrorPositionLine(errorMessage: String) = lineAndColumnRegex.find(errorMessage)!!.destructured

private fun parseReferenceChain(errorMessage: String) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ftl.args.yml.errors

import com.fasterxml.jackson.databind.JsonNode
import javax.swing.tree.TreeNode

class ErrorNodeResolver {
operator fun invoke(treeNode: JsonNode, errorModel: ConfigErrorModel): String {
internal class ErrorNodeResolver {
operator fun invoke(treeNode: JsonNode, errorModel: ConfigurationErrorModel): String {
var currentNode: JsonNode = treeNode

val lastNode = errorModel.referenceChain.last()
Expand Down
44 changes: 39 additions & 5 deletions test_runner/src/test/kotlin/ftl/args/yml/ErrorParserTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ftl.args.yml

import ftl.args.AndroidArgs
import ftl.args.yml.errors.ConfigErrorMessageBuilder
import ftl.args.yml.errors.ConfigurationErrorMessageBuilder
import ftl.test.util.TestHelper
import ftl.test.util.TestHelper.getThrowable
import ftl.util.FlankConfigurationException
import org.junit.Assert
import org.junit.Test
Expand All @@ -13,7 +14,7 @@ class ErrorParserTest {
private val yamlNoModelName =
TestHelper.getPath("src/test/kotlin/ftl/args/yml/test_error_yaml_cases/flank-no-model-name.yml")
private val yamlNoModelNode =
TestHelper.getPath("src/test/kotlin/ftl/args/yml/test_error_yaml_cases/flank-no-model-name.yml")
TestHelper.getPath("src/test/kotlin/ftl/args/yml/test_error_yaml_cases/flank-no-model-node.yml")

@Test
fun `parse json mapping error`() {
Expand All @@ -26,17 +27,16 @@ Error on parse config: gcloud->device[4]->version
Missing element or value for: 'version'
At line: 23, column: 3
""".trimIndent()
val buildErrorMessage = ConfigErrorMessageBuilder()
val buildErrorMessage = ConfigurationErrorMessageBuilder()
Assert.assertEquals(expected, buildErrorMessage(instantionError))
}


@Test
fun `return exception with inner message on parse error`() {
val instantionError =
"Instantiation oflParams[\"device\"]->java.util.A"
val expected = "Parse message error: Instantiation oflParams[\"device\"]->java.util.A".trimIndent()
val buildErrorMessage = ConfigErrorMessageBuilder()
val buildErrorMessage = ConfigurationErrorMessageBuilder()

Assert.assertEquals(expected, buildErrorMessage(instantionError))
}
Expand All @@ -46,6 +46,38 @@ At line: 23, column: 3
AndroidArgs.load(yamlWithoutDeviceVersion)
}

@Test
fun `without model name should have message`() {
val actualMessage = getThrowable { AndroidArgs.load(yamlNoModelName) }.message
val exceptedMessage = """
Error on parse config: gcloud->device[0]->model
Missing element or value for: 'model'
At line: 8, column: 1
Error node: {
"model" : null,
"version" : "test"
}
""".trimIndent()
Assert.assertEquals(exceptedMessage, actualMessage)
}

@Test
fun `without model node should have message`() {
val actualMessage = getThrowable { AndroidArgs.load(yamlNoModelNode) }.message
val exceptedMessage = """
Error on parse config: gcloud->device
At line: 6, column: 5
Error node: {
"app" : "./src/test/kotlin/ftl/fixtures/tmp/apk/app-debug.apk",
"test" : "./src/test/kotlin/ftl/fixtures/tmp/apk/app-single-success-debug-androidTest.apk",
"device" : {
"version" : "test"
}
}
""".trimIndent()
Assert.assertEquals(exceptedMessage, actualMessage)
}

@Test(expected = FlankConfigurationException::class)
fun `should throw FlankConfigException without model name`() {
AndroidArgs.load(yamlNoModelName)
Expand All @@ -55,4 +87,6 @@ At line: 23, column: 3
fun `should throw FlankConfigException without model node`() {
AndroidArgs.load(yamlNoModelNode)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ gcloud:
test: ./src/test/kotlin/ftl/fixtures/tmp/apk/app-single-success-debug-androidTest.apk
device:
- model:
version: X
version: test

flank:
disable-sharding: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ gcloud:
app: ./src/test/kotlin/ftl/fixtures/tmp/apk/app-debug.apk
test: ./src/test/kotlin/ftl/fixtures/tmp/apk/app-single-success-debug-androidTest.apk
device:
version: X
version: test

flank:
disable-sharding: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ gcloud:
test: ./src/test/kotlin/ftl/fixtures/tmp/apk/app-single-success-debug-androidTest.apk
device:
- model: NexusLowRes
version: 23
orientation: landscape
- model: NexusLowRes
version: 23
Expand All @@ -19,7 +18,7 @@ gcloud:
orientation: portrait
# Samsung Galaxy S9 SM-G9600
- model: starqltechn
versxion: 28
version: 28
locale: en
orientation: portrait
# LG Nexus 5
Expand Down
12 changes: 12 additions & 0 deletions test_runner/src/test/kotlin/ftl/test/util/TestHelper.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ftl.test.util

import ftl.util.FlankConfigurationException
import io.ktor.features.NotFoundException
import org.junit.Assert
import java.nio.file.Path
import java.nio.file.Paths
Expand All @@ -21,4 +23,14 @@ object TestHelper {
// required for tests to pass on Windows
return this.replace("\r\n", "\n")
}

fun getThrowable(action: () -> Unit): Throwable = try {
action()
throw FlankTestNotFoundException("Action not throwing exception")
} catch (exception: Throwable) {
exception
}

}

class FlankTestNotFoundException(message: String) : Exception(message)

0 comments on commit 5f0158c

Please sign in to comment.