Skip to content

Commit 52b87a0

Browse files
committed
completed E2E for named examples
1 parent 28e9e9a commit 52b87a0

12 files changed

Lines changed: 48 additions & 20 deletions

File tree

core-tests/e2e-tests/spring/spring-rest-openapi-v3/src/main/kotlin/com/foo/rest/examples/spring/openapi/v3/namedexample/NamedExampleApplication.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ open class NamedExampleApplication {
3838

3939
//on purpose do nothing with input... should still be selected in test suites due to BB coverage
4040

41-
return ResponseEntity.ok("Hello World!!!")
41+
return ResponseEntity.ok("OK")
4242
}
43-
}
43+
}

core-tests/e2e-tests/spring/spring-rest-openapi-v3/src/test/kotlin/org/evomaster/e2etests/spring/openapi/v3/namedexample/NamedExampleEMTest.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.evomaster.e2etests.spring.openapi.v3.namedexample
22

33
import com.foo.rest.examples.spring.openapi.v3.namedexample.NamedExampleController
4+
import junit.framework.TestCase.assertTrue
45
import org.evomaster.core.problem.rest.data.HttpVerb
6+
import org.evomaster.core.search.service.IdMapper
57
import org.evomaster.e2etests.spring.openapi.v3.SpringTestBase
68
import org.junit.jupiter.api.Assertions
79
import org.junit.jupiter.api.BeforeAll
@@ -24,12 +26,16 @@ class NamedExampleEMTest : SpringTestBase(){
2426
200,
2527
) { args: MutableList<String> ->
2628

27-
val solution = initAndRun(args)
29+
val (injector, solution) = initAndDebug(args)
2830

2931
Assertions.assertTrue(solution.individuals.size >= 1)
30-
assertHasAtLeastOne(solution, HttpVerb.POST, 200, "/object", "OK")
32+
assertHasAtLeastOne(solution, HttpVerb.POST, 200, "/api/namedexample", "OK")
3133

32-
TODO add checks on named example target
34+
val fv = solution.overall
35+
36+
val idMapper = injector.getInstance(IdMapper::class.java)
37+
val covered = fv.coveredTargets().any{ IdMapper.isNamedExample(idMapper.getDescriptiveId(it))}
38+
Assertions.assertTrue(covered)
3339
}
3440
}
3541

core-tests/integration-tests/core-it/src/test/kotlin/org/evomaster/core/mutatortest/OneMaxMutator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class OneMaxMutator {
105105
m.close()
106106

107107
val tp = reportFP(config.mutatedGeneFile, improve)
108-
val covered = solution.overall.coveredTargets() * 1.0 /n
108+
val covered = solution.overall.numberOfCoveredTargets() * 1.0 /n
109109

110110
val result = Pair(tp, covered)
111111
map.getOrPut(expId){ mutableListOf()}.add(result)
@@ -125,4 +125,4 @@ class OneMaxMutator {
125125

126126
fun main(array: Array<String>){
127127
OneMaxMutator().mutatorExp()
128-
}
128+
}

core/src/main/kotlin/org/evomaster/core/output/TestSuiteOrganizer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class SortingHelper {
191191
* The purpose is to give an example of sorting based on fitness information.
192192
*/
193193
private val coveredTargets: Comparator<EvaluatedIndividual<*>> = compareBy {
194-
it.fitness.coveredTargets()
194+
it.fitness.numberOfCoveredTargets()
195195
}
196196

197197
/**

core/src/main/kotlin/org/evomaster/core/output/service/TestSuiteWriter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class TestSuiteWriter {
338338

339339
lines.addBlockCommentLine(" The generated test suite contains ${solution.individuals.size} tests")
340340
classDescriptionEmptyLine(lines)
341-
lines.addBlockCommentLine(" Covered targets: ${solution.overall.coveredTargets()}")
341+
lines.addBlockCommentLine(" Covered targets: ${solution.overall.numberOfCoveredTargets()}")
342342
classDescriptionEmptyLine(lines)
343343
lines.addBlockCommentLine(" Used time: ${searchTimeController.getElapsedTime()}")
344344
classDescriptionEmptyLine(lines)

core/src/main/kotlin/org/evomaster/core/problem/rest/service/fitness/AbstractRestFitness.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ abstract class AbstractRestFitness : HttpWsFitness<RestIndividual>() {
541541
continue
542542
}
543543

544-
val target = "NAMED_EXAMPLE_${call.id}_${e.key}"
544+
val target = idMapper.getNamedExampleId(call.id, e.key, status ?: 0)
545545
fv.coverTarget(idMapper.handleLocalTarget(target))
546546
}
547547
}

core/src/main/kotlin/org/evomaster/core/search/FitnessValue.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.evomaster.core.search
22

33
import com.webfuzzing.commons.faults.DefinedFaultCategory
4-
import com.webfuzzing.commons.faults.FaultCategory
54
import org.evomaster.client.java.controller.api.dto.BootTimeInfoDto
65
import org.evomaster.client.java.controller.api.dto.database.execution.MongoFailedQuery
76
import org.evomaster.core.EMConfig
@@ -200,11 +199,18 @@ class FitnessValue(
200199
return targets.filterKeys { targetIds.contains(it)}.values.map { h -> h.score }.sum()
201200
}
202201

203-
fun coveredTargets(): Int {
202+
fun coveredTargets() : Set<Int> {
203+
return targets.entries
204+
.filter { it.value.score == MAX_VALUE }
205+
.map { it.key }
206+
.toSet()
207+
}
208+
209+
fun numberOfCoveredTargets(): Int {
204210
return targets.values.count { t -> t.score == MAX_VALUE }
205211
}
206212

207-
fun coveredTargets(prefix: String, idMapper: IdMapper) : Int{
213+
fun numberOfCoveredTargets(prefix: String, idMapper: IdMapper) : Int{
208214

209215
return targets.entries
210216
.filter { it.value.score == MAX_VALUE }
@@ -253,7 +259,7 @@ class FitnessValue(
253259
*/
254260
fun unionWithBootTimeCoveredTargets(prefix: String?, idMapper: IdMapper, bootTimeInfoDto: BootTimeInfoDto?): TargetStatistic{
255261
if (bootTimeInfoDto?.targets == null){
256-
return (if (prefix == null) coveredTargets() else coveredTargets(prefix, idMapper)).run {
262+
return (if (prefix == null) numberOfCoveredTargets() else numberOfCoveredTargets(prefix, idMapper)).run {
257263
TargetStatistic(
258264
bootTime = BOOT_TIME_INFO_UNAVAILABLE,
259265
searchTime = this - coveredTargetsDuringSeeding(),

core/src/main/kotlin/org/evomaster/core/search/gene/collection/EnumGene.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ class EnumGene<T : Comparable<T>>(
260260

261261
override fun containsSameValueAs(other: Gene): Boolean {
262262
if (other !is EnumGene<*>) {
263-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
263+
return false // FIXME
264+
//throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
264265
}
265266
//FIXME what if compared to another enum with different values???
266267
return this.index == other.index

core/src/main/kotlin/org/evomaster/core/search/service/IdMapper.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ class IdMapper {
2020

2121
private const val FAULT_OBJECTIVE_PREFIX = "PotentialFault"
2222

23+
private const val NAMED_EXAMPLE = "NamedExample"
24+
2325
/**
2426
* local objective prefix might depend on problems, eg, HTTP_SUCCESS and HTTP_FAULT for REST
2527
* it can be identified with its numeric id, ie, less than 0
2628
* however we need this key to specify whether to consider such objectives in impact collections
29+
*
30+
* TODO is this verified anywhere? eg, i think we created many new local targets not using this...
31+
* TODO if this is still relevant, should be enforced in an invariant somewhere
2732
*/
2833
const val LOCAL_OBJECTIVE_KEY = "Local"
2934

@@ -43,6 +48,8 @@ class IdMapper {
4348
private const val GQL_NO_ERRORS = "GQL_NO_ERRORS"
4449

4550

51+
fun isNamedExample(descriptiveId: String) = descriptiveId.startsWith(NAMED_EXAMPLE)
52+
4653
fun isFault(descriptiveId: String) = descriptiveId.startsWith(FAULT_OBJECTIVE_PREFIX)
4754

4855
fun isSpecifiedFault(descriptiveId: String, category: FaultCategory) =
@@ -101,6 +108,10 @@ class IdMapper {
101108
return "$FAULT_OBJECTIVE_PREFIX ${category.label} $postfix"
102109
}
103110

111+
fun getNamedExampleId(actionId: String, exampleName: String, statusCode: Int) : String {
112+
return "$NAMED_EXAMPLE $actionId - $exampleName - $statusCode"
113+
}
114+
104115
fun getGQLNoErrors(method: String) = "$GQL_NO_ERRORS:$method"
105116

106117
fun getHandledRPC(postfix: String): String {
@@ -133,4 +144,4 @@ class IdMapper {
133144
fun isRPCHandledAndSuccess(id: Int) = mapping[id]?.let { isRPCHandledAndSuccess(it) } == true
134145

135146

136-
}
147+
}

core/src/main/kotlin/org/evomaster/core/search/service/Sampler.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ abstract class Sampler<T> : TrackOperator where T : Individual {
163163

164164
//we look at something already selected, with more than 1 occurrence, and not fully used yet
165165
val options = inUse.filter { candidates.contains(it.key) }
166-
assert(options.isNotEmpty())
166+
if(options.isEmpty()){
167+
//this can happen when there is a selected named example with a single occurrence in the schema.
168+
//so, nothing to do
169+
return
170+
}
167171

168172
val choice = randomness.choose(options.keys)
169173

0 commit comments

Comments
 (0)