-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #936 from EMResearch/scaffolding-it
working on new integration tests for REST
- Loading branch information
Showing
31 changed files
with
427 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
core-it/src/main/kotlin/bar/examples/it/spring/pathstatus/PathStatusApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package bar.examples.it.spring.pathstatus | ||
|
||
import org.springframework.boot.SpringApplication | ||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@SpringBootApplication(exclude = [SecurityAutoConfiguration::class]) | ||
@RequestMapping(path = ["/api/pathstatus"]) | ||
@RestController | ||
open class PathStatusApplication { | ||
|
||
|
||
companion object { | ||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
SpringApplication.run(PathStatusApplication::class.java, *args) | ||
} | ||
} | ||
|
||
@GetMapping("/byStatus/{status}") | ||
open fun getByStatus(@PathVariable status: Int) : ResponseEntity<String> { | ||
|
||
return ResponseEntity.status(status).body("byStatus") | ||
} | ||
|
||
@GetMapping("/others/{x}") | ||
open fun getOthers(@PathVariable x: Int) : ResponseEntity<String> { | ||
|
||
return ResponseEntity.status(200).body("$x") | ||
} | ||
|
||
|
||
|
||
} |
67 changes: 67 additions & 0 deletions
67
core-it/src/test/kotlin/bar/examples/it/spring/SpringController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package bar.examples.it.spring | ||
|
||
import org.evomaster.client.java.controller.EmbeddedSutController | ||
import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto | ||
import org.evomaster.client.java.controller.api.dto.SutInfoDto | ||
import org.evomaster.client.java.sql.DbSpecification | ||
import org.evomaster.client.java.controller.problem.ProblemInfo | ||
import org.evomaster.client.java.controller.problem.RestProblem | ||
import org.springframework.boot.SpringApplication | ||
import org.springframework.context.ConfigurableApplicationContext | ||
|
||
|
||
abstract class SpringController(protected val applicationClass: Class<*>) : EmbeddedSutController() { | ||
|
||
init { | ||
super.setControllerPort(0) | ||
} | ||
|
||
|
||
protected var ctx: ConfigurableApplicationContext? = null | ||
|
||
override fun startSut(): String { | ||
ctx = SpringApplication.run(applicationClass, "--server.port=0") | ||
return "http://localhost:$sutPort" | ||
} | ||
|
||
protected val sutPort: Int | ||
get() = (ctx!!.environment | ||
.propertySources["server.ports"].source as Map<*, *>)["local.server.port"] as Int | ||
|
||
override fun isSutRunning(): Boolean { | ||
return ctx != null && ctx!!.isRunning | ||
} | ||
|
||
override fun stopSut() { | ||
ctx?.stop() | ||
ctx?.close() | ||
} | ||
|
||
override fun getPackagePrefixesToCover(): String { | ||
return "bar.foo." | ||
} | ||
|
||
override fun resetStateOfSUT() { //nothing to do | ||
} | ||
|
||
override fun getProblemInfo(): ProblemInfo { | ||
return RestProblem( | ||
"http://localhost:$sutPort/v3/api-docs", | ||
null | ||
) | ||
} | ||
|
||
override fun getInfoForAuthentication(): List<AuthenticationDto> { | ||
return listOf() | ||
} | ||
|
||
override fun getDbSpecifications(): MutableList<DbSpecification>? { | ||
return null | ||
} | ||
|
||
|
||
override fun getPreferredOutputFormat(): SutInfoDto.OutputFormat { | ||
return SutInfoDto.OutputFormat.KOTLIN_JUNIT_5 | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
core-it/src/test/kotlin/bar/examples/it/spring/pathstatus/PathStatusController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package bar.examples.it.spring.pathstatus | ||
|
||
import bar.examples.it.spring.SpringController | ||
|
||
class PathStatusController : SpringController(PathStatusApplication::class.java) |
50 changes: 50 additions & 0 deletions
50
core-it/src/test/kotlin/org/evomaster/core/problem/rest/IntegrationTestRestBase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.evomaster.core.problem.rest | ||
|
||
import com.google.inject.Injector | ||
import org.evomaster.core.problem.enterprise.SampleType | ||
import org.evomaster.core.problem.rest.service.AbstractRestFitness | ||
import org.evomaster.core.problem.rest.service.AbstractRestSampler | ||
import org.evomaster.core.search.EvaluatedIndividual | ||
import org.evomaster.core.search.service.SearchGlobalState | ||
import org.evomaster.core.seeding.service.rest.PirToRest | ||
import org.evomaster.e2etests.utils.RestTestBase | ||
import org.junit.jupiter.api.BeforeEach | ||
|
||
abstract class IntegrationTestRestBase : RestTestBase() { | ||
|
||
|
||
protected lateinit var injector: Injector | ||
|
||
@BeforeEach | ||
fun initInjector(){ | ||
val args = listOf( | ||
"--sutControllerPort", "" + controllerPort, | ||
"--createConfigPathIfMissing", "false" | ||
) | ||
injector = init(args) | ||
} | ||
|
||
fun getPirToRest() = injector.getInstance(PirToRest::class.java) | ||
|
||
|
||
fun createIndividual(actions: List<RestCallAction>): EvaluatedIndividual<RestIndividual> { | ||
|
||
// val searchGlobalState = injector.getInstance(SearchGlobalState::class.java) | ||
|
||
// val ind = RestIndividual(actions.toMutableList(), SampleType.SEEDED) | ||
// ind.doGlobalInitialize(searchGlobalState) | ||
|
||
val sampler = injector.getInstance(AbstractRestSampler::class.java) | ||
/* | ||
the method `createIndividual` can be overridden | ||
in this case, the sampler is an instance of ResourceSampler, | ||
then check its implementation in ResourceSampler.createIndividual(...) | ||
*/ | ||
val ind = sampler.createIndividual(SampleType.SEEDED, actions.toMutableList()) | ||
|
||
val ff = injector.getInstance(AbstractRestFitness::class.java) | ||
val ei = ff.calculateCoverage(ind)!! | ||
|
||
return ei | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...rg/evomaster/core/problem/rest/selectorutils/RestIndividualSelectorUtilsPathStatusTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.evomaster.core.problem.rest.selectorutils | ||
|
||
import bar.examples.it.spring.pathstatus.PathStatusController | ||
import org.evomaster.core.problem.rest.HttpVerb | ||
import org.evomaster.core.problem.rest.IntegrationTestRestBase | ||
import org.evomaster.core.problem.rest.RestIndividualSelectorUtils | ||
import org.evomaster.core.problem.rest.RestPath | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
|
||
class RestIndividualSelectorUtilsPathStatusTest : IntegrationTestRestBase(){ | ||
|
||
companion object { | ||
@BeforeAll | ||
@JvmStatic | ||
fun init() { | ||
initClass(PathStatusController()) | ||
} | ||
} | ||
|
||
|
||
@Test | ||
fun testPathStatus(){ | ||
|
||
val pirTest = getPirToRest() | ||
|
||
val byStatus = RestPath("/api/pathstatus/byStatus/{status}") | ||
val others = RestPath("/api/pathstatus/others/{x}") | ||
|
||
val s200 = pirTest.fromVerbPath("get", "/api/pathstatus/byStatus/200")!! | ||
val s400 = pirTest.fromVerbPath("get", "/api/pathstatus/byStatus/400")!! | ||
val o200 = pirTest.fromVerbPath("get", "/api/pathstatus/others/200")!! | ||
val o500 = pirTest.fromVerbPath("get", "/api/pathstatus/others/500")!! | ||
|
||
val x0 = createIndividual(listOf(s200)) | ||
val x1 = createIndividual(listOf(s400)) | ||
val x2 = createIndividual(listOf(o200)) | ||
val x3 = createIndividual(listOf(o500)) | ||
|
||
val individuals = listOf(x0,x1,x2,x3) | ||
|
||
val r0 = RestIndividualSelectorUtils.findIndividuals(individuals, HttpVerb.GET, byStatus, 200) | ||
assertEquals(1, r0.size) | ||
|
||
val r1 = RestIndividualSelectorUtils.findIndividuals(individuals, HttpVerb.GET, byStatus, 500) | ||
assertEquals(0, r1.size) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.