diff --git a/core/src/main/kotlin/org/evomaster/core/output/service/ApiTestCaseWriter.kt b/core/src/main/kotlin/org/evomaster/core/output/service/ApiTestCaseWriter.kt index 310c3d89d1..bb451719c1 100644 --- a/core/src/main/kotlin/org/evomaster/core/output/service/ApiTestCaseWriter.kt +++ b/core/src/main/kotlin/org/evomaster/core/output/service/ApiTestCaseWriter.kt @@ -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() { @@ -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)}" + "\")" @@ -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) { diff --git a/core/src/main/kotlin/org/evomaster/core/output/service/HttpWsTestCaseWriter.kt b/core/src/main/kotlin/org/evomaster/core/output/service/HttpWsTestCaseWriter.kt index 260ed3e894..12920a6f2e 100644 --- a/core/src/main/kotlin/org/evomaster/core/output/service/HttpWsTestCaseWriter.kt +++ b/core/src/main/kotlin/org/evomaster/core/output/service/HttpWsTestCaseWriter.kt @@ -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(\"${"""\"\""""}\")") } } diff --git a/core/src/main/kotlin/org/evomaster/core/output/service/TestSuiteWriter.kt b/core/src/main/kotlin/org/evomaster/core/output/service/TestSuiteWriter.kt index a4b1804849..7219c50e7b 100644 --- a/core/src/main/kotlin/org/evomaster/core/output/service/TestSuiteWriter.kt +++ b/core/src/main/kotlin/org/evomaster/core/output/service/TestSuiteWriter.kt @@ -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) diff --git a/core/src/test/kotlin/org/evomaster/core/output/TestCaseWriterTest.kt b/core/src/test/kotlin/org/evomaster/core/output/TestCaseWriterTest.kt index 3fc5d2c93e..6fabb3f1fc 100644 --- a/core/src/test/kotlin/org/evomaster/core/output/TestCaseWriterTest.kt +++ b/core/src/test/kotlin/org/evomaster/core/output/TestCaseWriterTest.kt @@ -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.* @@ -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 @@ -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() 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()")) + + } }