Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import org.evomaster.core.search.gene.utils.GeneUtils
import org.evomaster.core.sql.SqlAction
import org.evomaster.core.sql.SqlActionResult
import org.evomaster.core.utils.StringUtils
import java.math.BigDecimal
import java.math.BigInteger

abstract class ApiTestCaseWriter : TestCaseWriter() {

Expand Down Expand Up @@ -347,7 +349,7 @@ abstract class ApiTestCaseWriter : TestCaseWriter() {
if (format.isJavaOrKotlin()) {
val left = when (value) {
is Boolean -> "equalTo($value)"
is Number -> "numberMatches($value)"
is Number -> "numberMatches(${handleNumberInJavaOrKotlinTest(value)})"
is String -> "containsString(" +
"\"${GeneUtils.applyEscapes(value as String, mode = GeneUtils.EscapeMode.ASSERTION, format = format)}" +
"\")"
Expand Down Expand Up @@ -394,6 +396,42 @@ abstract class ApiTestCaseWriter : TestCaseWriter() {
throw IllegalStateException("Not supported format $format")
}

private fun handleNumberInJavaOrKotlinTest(value: Any): String {
return when (value) {
is Byte, is Short, is Int -> value.toString()

is Long -> {
if (value > Int.MAX_VALUE || value < Int.MIN_VALUE) {
"${value}L"
} else {
value.toString()
}
}

is Float -> {
if (value.isNaN()) "Float.NaN"
else if (value == Float.POSITIVE_INFINITY) "Float.POSITIVE_INFINITY"
else if (value == Float.NEGATIVE_INFINITY) "Float.NEGATIVE_INFINITY"
else "${value}f"
}

is Double -> {
if (value.isNaN()) "Double.NaN"
else if (value == Double.POSITIVE_INFINITY) "Double.POSITIVE_INFINITY"
else if (value == Double.NEGATIVE_INFINITY) "Double.NEGATIVE_INFINITY"
else value.toString()
}

is BigInteger -> "BigInteger(\"$value\")"

is BigDecimal -> "BigDecimal(\"$value\")"

is Number -> value.toString()

else -> value.toString()
}
}

private fun valueToPrint(value: Any?, format: OutputFormat) : String{
assert(format.isJavaScript() || format.isCsharp() || format.isPython())
val toPrint = if (value is String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,29 +579,32 @@ abstract class HttpWsTestCaseWriter : ApiTestCaseWriter() {
} else if (bodyParam.isTextPlain()) {

val body = bodyParam.getValueAsPrintableString(mode = GeneUtils.EscapeMode.TEXT, targetFormat = format)
if (body != "\"\"") {
when {
format.isCsharp() -> {
lines.append("new StringContent(\"$body\", Encoding.UTF8, \"${bodyParam.contentType()}\")")
// handle body only if it is not black
if (body.isNotBlank()){
if (body != "\"\"") {
when {
format.isCsharp() -> {
lines.append("new StringContent(\"$body\", Encoding.UTF8, \"${bodyParam.contentType()}\")")
}
format.isPython() -> {
if (body.trim().isNullOrBlank()) {
lines.add("body = \"\"")
} else {
lines.add("body = $body")
}
}
else -> lines.add(".$send($body)")
}
format.isPython() -> {
if (body.trim().isNullOrBlank()) {
} else {
when {
format.isCsharp() -> {
lines.append("new StringContent(\"${"""\"\""""}\", Encoding.UTF8, \"${bodyParam.contentType()}\")")
}
format.isPython() -> {
lines.add("body = \"\"")
} else {
lines.add("body = $body")
}
else -> lines.add(".$send(\"${"""\"\""""}\")")
}
else -> lines.add(".$send($body)")
}
} else {
when {
format.isCsharp() -> {
lines.append("new StringContent(\"${"""\"\""""}\", Encoding.UTF8, \"${bodyParam.contentType()}\")")
}
format.isPython() -> {
lines.add("body = \"\"")
}
else -> lines.add(".$send(\"${"""\"\""""}\")")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ class TestSuiteWriter {
addImport(EMTestUtils::class.java.name +".*", lines, true)
addImport("org.evomaster.client.java.controller.SutHandler", lines)

// BigInteger and BigDecimal
addImport("java.math.BigDecimal", lines)
addImport("java.math.BigInteger", lines)

if (useRestAssured()) {
addImport("io.restassured.RestAssured", lines)
addImport("io.restassured.RestAssured.given", lines, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.evomaster.core.output.service.PartialOracles
import org.evomaster.core.output.service.RestTestCaseWriter
import org.evomaster.core.problem.enterprise.SampleType
import org.evomaster.core.problem.rest.data.*
import org.evomaster.core.problem.rest.param.BodyParam
import org.evomaster.core.search.EvaluatedIndividual
import org.evomaster.core.search.FitnessValue
import org.evomaster.core.search.gene.*
Expand All @@ -23,8 +24,11 @@ import org.evomaster.core.search.gene.sql.SqlAutoIncrementGene
import org.evomaster.core.search.gene.sql.SqlForeignKeyGene
import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
import org.evomaster.core.search.gene.UUIDGene
import org.evomaster.core.search.gene.collection.EnumGene
import org.evomaster.core.search.gene.numeric.IntegerGene
import org.evomaster.core.search.gene.string.StringGene
import org.evomaster.core.search.gene.utils.GeneUtils
import org.evomaster.core.search.gene.wrapper.OptionalGene
import org.evomaster.core.sql.schema.TableId
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -1521,4 +1525,80 @@ public void test() throws Exception {

assertEquals(3, getNumberOfFlakyComment(config,lines.toString()))
}

@Test
fun testNumberMatchesForLong(){
val fooAction = RestCallAction("1", HttpVerb.GET, RestPath("/foo"), mutableListOf())

val (format, baseUrlOfSut, ei) = buildResourceEvaluatedIndividual(
dbInitialization = mutableListOf(),
groups = mutableListOf(
(mutableListOf<SqlAction>() to mutableListOf(fooAction))
),
format = OutputFormat.JAVA_JUNIT_5
)

val fooResult = ei.seeResult(fooAction.getLocalId()) as RestCallResult
fooResult.setTimedout(false)
fooResult.setStatusCode(200)
fooResult.setBody(
"""
{
"p0": [3000000000, 3000000001, 3000000002]
}
""".trimIndent()
)
fooResult.setBodyType(MediaType.APPLICATION_JSON_TYPE)

val config = getConfig(format)

val test = TestCase(test = ei, name = "test")

val writer = RestTestCaseWriter(config, PartialOracles())
val lines = writer.convertToCompilableTestCode( test, baseUrlOfSut)
lines.toString().apply {
assertTrue(contains("numberMatches(3000000000L)"))
assertTrue(contains("numberMatches(3000000001L)"))
assertTrue(contains("numberMatches(3000000002L)"))
}
}

@Test
fun testInActiveBodyParamInTest(){
val stringGene = StringGene("stringGene")
val optionalGene = OptionalGene(stringGene.name, stringGene)
optionalGene.isActive = false
val enumGene = EnumGene("contentType", listOf("text/plain"))
stringGene.value = "EX_123"
enumGene.index = 0
val bodyParam = BodyParam(gene = optionalGene, typeGene = enumGene)
bodyParam.contentRemoveQuotesGene.gene.value = false

val format = OutputFormat.JAVA_JUNIT_5

val textBody = bodyParam.getValueAsPrintableString(mode = GeneUtils.EscapeMode.TEXT, targetFormat = format)
assertEquals("", textBody)

val baseUrlOfSut = "baseUrlOfSut"
val action = RestCallAction("1", HttpVerb.PUT, RestPath("/"), mutableListOf(bodyParam))
val restActions = listOf(action).toMutableList()
val individual = RestIndividual(restActions, SampleType.RANDOM)
TestUtils.doInitializeIndividualForTesting(individual)

val fitnessVal = FitnessValue(0.0)
val result = RestCallResult(action.getLocalId())
result.setTimedout(timedout = true)
val results = listOf(result)
val ei = EvaluatedIndividual(fitnessVal, individual, results)
val config = getConfig(format)

val test = TestCase(test = ei, name = "test")

val writer = RestTestCaseWriter(config, PartialOracles())

val lines = writer.convertToCompilableTestCode( test, baseUrlOfSut)

assertFalse(lines.toString().contains(".body()"))

}
}
Loading