Skip to content

Commit

Permalink
Merge branch 'master' into feat/integrate-with-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
agusaldasoro authored Jan 21, 2025
2 parents 616f22b + aa77530 commit 4871900
Show file tree
Hide file tree
Showing 15 changed files with 557 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,6 @@ class RestTestCaseWriter : HttpWsTestCaseWriter {

private fun getAllUsedExamples(ind: RestIndividual) : List<String>{
return ind.seeFullTreeGenes()
.filterIsInstance<EnumGene<*>>()
.filter { it.name == RestActionBuilderV3.EXAMPLES_NAME }
.filter { it.staticCheckIfImpactPhenotype() }
.map { it.getValueAsRawString() }
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.evomaster.core.problem.rest

import com.fasterxml.jackson.databind.ObjectMapper
import io.swagger.parser.OpenAPIParser
import org.evomaster.client.java.instrumentation.shared.ClassToSchemaUtils.OPENAPI_REF_PATH
import org.evomaster.core.EMConfig
Expand All @@ -17,7 +18,6 @@ import org.evomaster.core.search.gene.collection.FixedMapGene
import org.evomaster.core.search.gene.collection.TaintedMapGene
import org.evomaster.core.search.gene.datetime.DateGene
import org.evomaster.core.search.gene.datetime.DateTimeGene
import org.evomaster.core.search.gene.datetime.TimeGene
import org.evomaster.core.search.gene.numeric.DoubleGene
import org.evomaster.core.search.gene.numeric.IntegerGene
import org.evomaster.core.search.gene.optional.ChoiceGene
Expand Down Expand Up @@ -831,7 +831,10 @@ class RestActionBuilderV3Test{

val actions: MutableMap<String, Action> = mutableMapOf()

RestActionBuilderV3.addActionsFromSwagger(schema, actions, options=options)
val errors = RestActionBuilderV3.addActionsFromSwagger(schema, actions, options=options)
errors.forEach {
println(it)
}

assertEquals(expectedNumberOfActions, actions.size)

Expand Down Expand Up @@ -1438,6 +1441,98 @@ class RestActionBuilderV3Test{
}


@ParameterizedTest
@ValueSource(strings = [
"/swagger/artificial/defaultandexamples/examples_object_single_in.yaml",
"/swagger/artificial/defaultandexamples/examples_object_single_out.yaml",
"/swagger/artificial/defaultandexamples/default_object_single.yaml"
])
fun testExampleObjectSingle(path: String){
val a = loadAndAssertActions(path, 1, RestActionBuilderV3.Options(probUseExamples = 0.5, probUseDefault = 0.5))
.values.first()

val rand = Randomness()
a.doInitialize(rand)

var Bar42Pos = false
var Bar42Neg = false

data class ObjectSingleDto(
var id: Int?,
var name: String?,
var extra: Int?
){
constructor() : this(null,null,null)
}
val mapper = ObjectMapper()

for(i in 0..1000){
a.randomize(rand,false)
val s = a.seeTopGenes().first().getValueAsRawString()

val dto = mapper.readValue(s, ObjectSingleDto::class.java)

if(dto.id == 42 && dto.name=="Bar" && dto.extra != null){
if(dto.extra!! >= 0){
Bar42Pos = true
} else {
Bar42Neg = true
}
}

if(Bar42Pos && Bar42Neg){
break
}
}

assertTrue(Bar42Pos)
assertTrue(Bar42Neg)

}


@Test
fun testExampleObjectMulti(){
val a = loadAndAssertActions("/swagger/artificial/defaultandexamples/examples_object_multi.yaml", 1, RestActionBuilderV3.Options(probUseExamples = 0.5, probUseDefault = 0.5))
.values.first()

val rand = Randomness()
a.doInitialize(rand)

var Bar42 = false
var Foo123 = false

data class ObjectSingleDto(
var id: Int?,
var name: String?,
var extra: Int?
){
constructor() : this(null,null,null)
}
val mapper = ObjectMapper()

for(i in 0..1000){
a.randomize(rand,false)
val s = a.seeTopGenes().first().getValueAsRawString()

val dto = mapper.readValue(s, ObjectSingleDto::class.java)

if(dto.id == 42 && dto.name=="Bar"){
Bar42 = true
}
if(dto.id == 123 && dto.name == "Foo" && dto.extra == 77){
Foo123 = true
}

if(Bar42 && Foo123){
break
}
}

assertTrue(Bar42)
assertTrue(Foo123)
}

@ParameterizedTest
@ValueSource(strings = ["/swagger/artificial/defaultandexamples/examples_string_in.yml",
"/swagger/artificial/defaultandexamples/examples_string_out.yml"])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
openapi: 3.0.3
info:
title: default_object_single
description: default_object_single
version: 1.0.0
servers:
- url: "/v2"
paths:
"/foo":
post:
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
id:
type: integer
name:
type: string
extra:
type: integer
default:
id: 42
name: Bar
responses:
'200':
description: OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
openapi: 3.0.3
info:
title: example_object_multi
description: example_object_multi
version: 1.0.0
servers:
- url: "/v2"
paths:
"/foo":
post:
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
id:
type: integer
name:
type: string
extra:
type: integer
examples:
Bar:
value:
id: 42
name: Bar
Foo:
value:
id: 123
name: Foo
extra: 77
responses:
'200':
description: OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
openapi: 3.0.3
info:
title: example_object_single
description: example_object_single
version: 1.0.0
servers:
- url: "/v2"
paths:
"/foo":
post:
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
id:
type: integer
name:
type: string
extra:
type: integer
example:
id: 42
name: Bar
responses:
'200':
description: OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
openapi: 3.0.3
info:
title: example_object_single
description: example_object_single
version: 1.0.0
servers:
- url: "/v2"
paths:
"/foo":
post:
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
id:
type: integer
name:
type: string
extra:
type: integer
example:
id: 42
name: Bar
responses:
'200':
description: OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.foo.rest.examples.bb.exampleobject

import org.evomaster.e2etests.utils.CoveredTargets
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*


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

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

@PostMapping()
fun post(@RequestBody dto: ExampleObjectDto): ResponseEntity<String>{

if(dto.id == "Foo" && dto.b == true && dto.x == 42 && dto.y == 12.3
&& dto.other?.name == "Bar" && dto.other?.x == 88){
CoveredTargets.cover("A")
return ResponseEntity("OK", HttpStatus.OK)
}

return ResponseEntity("FAIL", HttpStatus.BAD_REQUEST)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.foo.rest.examples.bb.exampleobject

class ExampleObjectDto {

var id: String? = null

var x: Int? = null

var b: Boolean? = null

var y: Double? = null

var other: OtherDto? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.foo.rest.examples.bb.exampleobject

class OtherDto {

val name: String? = null

val x: Int? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,4 @@ open class BBExamplesApplication {

return ResponseEntity.status(400).build()
}

//TODO examples in objects?
}
Loading

0 comments on commit 4871900

Please sign in to comment.