diff --git a/core/src/main/kotlin/org/evomaster/core/problem/rest/service/AbstractRestFitness.kt b/core/src/main/kotlin/org/evomaster/core/problem/rest/service/AbstractRestFitness.kt index 943639d86e..24cc8bdf9b 100755 --- a/core/src/main/kotlin/org/evomaster/core/problem/rest/service/AbstractRestFitness.kt +++ b/core/src/main/kotlin/org/evomaster/core/problem/rest/service/AbstractRestFitness.kt @@ -37,6 +37,7 @@ import org.evomaster.core.search.action.ActionFilter import org.evomaster.core.search.gene.* import org.evomaster.core.search.gene.collection.EnumGene import org.evomaster.core.search.gene.numeric.NumberGene +import org.evomaster.core.search.gene.optional.ChoiceGene import org.evomaster.core.search.gene.optional.OptionalGene import org.evomaster.core.search.gene.string.StringGene import org.evomaster.core.search.gene.utils.GeneUtils @@ -460,6 +461,30 @@ abstract class AbstractRestFitness : HttpWsFitness() { //body payload type in response fv.coverTarget(idMapper.handleLocalTarget("RESPONSE_BODY_PAYLOAD_${call.id}_${result.getBodyType()}")) + + /* + explicit targets for examples + */ + val examples = call.seeTopGenes() + .flatMap { it.flatView() } + .filter { it.staticCheckIfImpactPhenotype() } + .filter { it.name == RestActionBuilderV3.EXAMPLES_NAME } + + examples.forEach { + val name = (it.parent as Gene).name + val label = when(it){ + is EnumGene<*> -> it.getValueAsRawString() + is ChoiceGene<*> -> ""+it.activeGeneIndex + else ->{ + log.warn("Unhandled example gene type: ${it.javaClass}") + assert(false) + "undefined" + } + } + + val target = "EXAMPLE_${call.id}_${name}_$label" + fv.coverTarget(idMapper.handleLocalTarget(target)) + } } private fun handleAdditionalStatusTargetDescription( diff --git a/e2e-tests/spring-rest-bb/src/main/resources/static/openapi-bbexampleobject.json b/e2e-tests/spring-rest-bb/src/main/resources/static/openapi-bbexampleobject.json index 57fd666a0c..3c654180a7 100644 --- a/e2e-tests/spring-rest-bb/src/main/resources/static/openapi-bbexampleobject.json +++ b/e2e-tests/spring-rest-bb/src/main/resources/static/openapi-bbexampleobject.json @@ -23,14 +23,43 @@ "schema": { "$ref": "#/components/schemas/ExampleObjectDto" }, - "example": { - "id": "Foo", - "b": true, - "x": 42, - "y": 12.3, - "other": { - "name": "Bar", - "x": 88 + "examples": { + "X": { + "value": { + "id": "Foo", + "b": true, + "x": 42, + "y": 12.3, + "other": { + "name": "Bar", + "x": 88 + } + } + }, + "Y0": { + "value": { + "id": "Y0" + } + }, + "Y1": { + "value": { + "id": "Y1" + } + }, + "Y2": { + "value": { + "id": "Y2" + } + }, + "Y3": { + "value": { + "id": "Y3" + } + }, + "Y4": { + "value": { + "id": "Y4" + } } } } diff --git a/e2e-tests/spring-rest-bb/src/test/kotlin/org/evomaster/e2etests/spring/rest/bb/exampleobject/BBExampleObjectEMTest.kt b/e2e-tests/spring-rest-bb/src/test/kotlin/org/evomaster/e2etests/spring/rest/bb/exampleobject/BBExampleObjectEMTest.kt index b38c746ed5..6895be5a91 100644 --- a/e2e-tests/spring-rest-bb/src/test/kotlin/org/evomaster/e2etests/spring/rest/bb/exampleobject/BBExampleObjectEMTest.kt +++ b/e2e-tests/spring-rest-bb/src/test/kotlin/org/evomaster/e2etests/spring/rest/bb/exampleobject/BBExampleObjectEMTest.kt @@ -4,6 +4,7 @@ import com.foo.rest.examples.bb.exampleobject.BBExampleObjectController import com.foo.rest.examples.bb.examplevalues.BBExamplesController import org.evomaster.core.output.OutputFormat import org.evomaster.core.problem.rest.HttpVerb +import org.evomaster.core.problem.rest.param.BodyParam import org.evomaster.e2etests.spring.rest.bb.SpringTestBase import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.assertTrue @@ -40,6 +41,22 @@ class BBExampleObjectEMTest : SpringTestBase() { assertTrue(solution.individuals.size >= 1) assertHasAtLeastOne(solution, HttpVerb.POST, 200, "/api/bbexampleobject", "OK") + + val bodies = solution.individuals + .asSequence() + .map { it.individual } + .flatMap { it.seeMainExecutableActions() } + .flatMap { it.parameters } + .filterIsInstance() + .map { it.primaryGene().getValueAsRawString() } + .toList() + + assertTrue(bodies.any { it.contains("Foo") }, "Missing Foo") + assertTrue(bodies.any { it.contains("Y0") }, "Missing Y0") + assertTrue(bodies.any { it.contains("Y1") }, "Missing Y1") + assertTrue(bodies.any { it.contains("Y2") }, "Missing Y2") + assertTrue(bodies.any { it.contains("Y3") }, "Missing Y3") + assertTrue(bodies.any { it.contains("Y4") }, "Missing Y4") } } }