Skip to content

Commit

Permalink
E2E for default
Browse files Browse the repository at this point in the history
  • Loading branch information
arcuri82 committed Nov 9, 2023
1 parent 6d74938 commit 268e079
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ protected void assertNone(Solution<RestIndividual> solution,
boolean ok = solution.getIndividuals().stream().noneMatch(
ind -> hasAtLeastOne(ind, verb, expectedStatusCode));

assertOK(solution, ok);
}

private static void assertOK(Solution<RestIndividual> solution, boolean ok) {
StringBuffer msg = new StringBuffer("REST calls:\n");
if (!ok) {
solution.getIndividuals().stream().flatMap(ind -> ind.evaluatedMainActions().stream())
Expand All @@ -342,5 +346,16 @@ protected void assertNone(Solution<RestIndividual> solution,
assertTrue(ok, msg.toString());
}

protected void assertNone(Solution<RestIndividual> solution,
HttpVerb verb,
int expectedStatusCode,
String path,
String inResponse){

boolean ok = solution.getIndividuals().stream().noneMatch(
ind -> hasAtLeastOne(ind, verb, expectedStatusCode, path, inResponse));

assertOK(solution, ok);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.foo.rest.examples.spring.openapi.v3.bbdefault

import com.foo.rest.examples.spring.openapi.v3.base.BaseApplication
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.RequestHeader
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController


@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
@RequestMapping(path = ["/api/bbdefault"])
@RestController
open class BBDefaultApplication {

companion object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(BBDefaultApplication::class.java, *args)
}
}

@GetMapping
open fun get(@RequestParam data: String?) : ResponseEntity<String> {

if(data == "foo")
return ResponseEntity.ok("OK")

return ResponseEntity.status(400).build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost:8080",
"description": "Generated server url"
}
],
"paths": {
"/api/bbdefault": {
"get": {
"tags": [
"bb-default-application"
],
"operationId": "get",
"parameters": [
{
"name": "data",
"in": "query",
"required": false,
"schema": {
"type": "string",
"default": "foo"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.foo.rest.examples.spring.openapi.v3.bbdefault

import com.foo.rest.examples.spring.openapi.v3.SpringController
import org.evomaster.client.java.controller.problem.ProblemInfo
import org.evomaster.client.java.controller.problem.RestProblem

class BBDefaultController : SpringController(BBDefaultApplication::class.java){


override fun getProblemInfo(): ProblemInfo {
return RestProblem(
"http://localhost:$sutPort/openapi-bbdefault.json",
null
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.evomaster.e2etests.spring.openapi.v3.bbdefault

import com.foo.rest.examples.spring.openapi.v3.bbauth.BBAuthController
import com.foo.rest.examples.spring.openapi.v3.bbdefault.BBDefaultController
import org.evomaster.core.problem.rest.HttpVerb
import org.evomaster.e2etests.spring.openapi.v3.SpringTestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test

class BBDefaultEMTest : SpringTestBase() {

companion object {
@BeforeAll
@JvmStatic
fun init() {
initClass(BBDefaultController())
}
}


@Test
fun testRunEMOk() {
runTestHandlingFlakyAndCompilation(
"BBDefaultEMOk",
"org.foo.BBDefaultEMOk",
20
) { args: MutableList<String> ->

args.add("--blackBox")
args.add("true")
args.add("--bbTargetUrl")
args.add(baseUrlOfSut)
args.add("--bbSwaggerUrl")
args.add("$baseUrlOfSut/openapi-bbdefault.json")
args.add("--bbExperiments")
args.add("false")

args.add("--probRestDefault")
args.add("0.5")

//make sure we do not solve it via taint analysis
args.add("--baseTaintAnalysisProbability")
args.add("0")

val solution = initAndRun(args)

Assertions.assertTrue(solution.individuals.size >= 1)
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/api/bbdefault", "OK")
}
}

@Test
fun testRunEMFail() {
runTestHandlingFlakyAndCompilation(
"BBDefaultEMFail",
"org.foo.BBDefaultEMFail",
20
) { args: MutableList<String> ->

args.add("--blackBox")
args.add("true")
args.add("--bbTargetUrl")
args.add(baseUrlOfSut)
args.add("--bbSwaggerUrl")
args.add("$baseUrlOfSut/openapi-bbdefault.json")
args.add("--bbExperiments")
args.add("false")

args.add("--probRestDefault")
args.add("0.0") // no way in BB should be able to get the right string with no further info

//make sure we do not solve it via taint analysis
args.add("--baseTaintAnalysisProbability")
args.add("0")

val solution = initAndRun(args)

Assertions.assertTrue(solution.individuals.size >= 1)
assertHasAtLeastOne(solution, HttpVerb.GET, 400, "/api/bbdefault", null)
assertNone(solution,HttpVerb.GET,200,"/api/bbdefault", "OK")
}
}

}

0 comments on commit 268e079

Please sign in to comment.