Skip to content

Commit c2dc95c

Browse files
committed
e2e for Json Merge Patch
1 parent 7c0f687 commit c2dc95c

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.foo.rest.examples.bb.jsonmergepatch
2+
3+
import com.fasterxml.jackson.databind.JsonNode
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import io.swagger.v3.oas.annotations.media.Schema
6+
import org.springframework.boot.SpringApplication
7+
import org.springframework.boot.autoconfigure.SpringBootApplication
8+
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
9+
import org.evomaster.e2etests.utils.CoveredTargets
10+
import org.springframework.http.ResponseEntity
11+
import org.springframework.web.bind.annotation.*
12+
import java.util.Optional
13+
14+
@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
15+
@RestController
16+
@RequestMapping("/api/jsonmergepatch")
17+
open class BBJsonMergePatchApplication {
18+
19+
companion object {
20+
@JvmStatic
21+
fun main(args: Array<String>) {
22+
SpringApplication.run(BBJsonMergePatchApplication::class.java, *args)
23+
}
24+
}
25+
26+
27+
28+
@PatchMapping("/", consumes = ["application/merge-patch+json"])
29+
fun patchPet(@RequestBody dto: BBJsonMergePatchDto): ResponseEntity<String> {
30+
31+
if(dto.name == null && dto.x == null){
32+
CoveredTargets.cover("BOTH_UNDEFINED")
33+
return ResponseEntity.status(400).build()
34+
}
35+
if(dto.name == null || dto.x == null){
36+
CoveredTargets.cover("ONE_UNDEFINED")
37+
return ResponseEntity.status(401).build()
38+
}
39+
//we use different status codes just to make sure tests end up in final test suite
40+
if(dto.name.isEmpty && dto.x.isEmpty){
41+
CoveredTargets.cover("BOTH_NULL")
42+
return ResponseEntity.status(403).build()
43+
}
44+
if(!dto.name.isEmpty && !dto.x.isEmpty){
45+
CoveredTargets.cover("BOTH_PRESENT")
46+
return ResponseEntity.status(200).build()
47+
}
48+
if(dto.name.isEmpty || dto.x.isEmpty){
49+
CoveredTargets.cover("ONE_PRESENT")
50+
return ResponseEntity.status(404).build()
51+
}
52+
53+
//should never be reached
54+
return ResponseEntity.ok("patched")
55+
}
56+
}
57+
58+
data class BBJsonMergePatchDto(
59+
@field:Schema(nullable = true)
60+
val name: Optional<String>? = null,
61+
62+
@field:Schema(nullable = true)
63+
val x: Optional<Int>? = null
64+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.foo.rest.examples.bb.jsonmergepatch
2+
3+
import com.foo.rest.examples.bb.SpringController
4+
import com.foo.rest.examples.bb.jsonpatch.BBJsonPatchApplication
5+
6+
class BBJsonMergePatchController : SpringController(BBJsonMergePatchApplication::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.evomaster.e2etests.spring.rest.bb.jsonmergepatch
2+
3+
import com.foo.rest.examples.bb.jsonmergepatch.BBJsonMergePatchController
4+
import org.evomaster.core.output.OutputFormat
5+
import org.evomaster.core.problem.rest.data.HttpVerb
6+
import org.evomaster.e2etests.spring.rest.bb.SpringTestBase
7+
import org.junit.jupiter.api.Assertions.assertTrue
8+
import org.junit.jupiter.api.BeforeAll
9+
import org.junit.jupiter.params.ParameterizedTest
10+
import org.junit.jupiter.params.provider.EnumSource
11+
12+
class BBJsonMergePatchEMTest : SpringTestBase() {
13+
14+
companion object {
15+
init {
16+
shouldApplyInstrumentation = false
17+
}
18+
19+
@BeforeAll
20+
@JvmStatic
21+
fun init() {
22+
initClass(BBJsonMergePatchController())
23+
}
24+
}
25+
26+
27+
28+
@ParameterizedTest
29+
@EnumSource
30+
fun testBlackBoxOutput(outputFormat: OutputFormat) {
31+
32+
executeAndEvaluateBBTest(
33+
outputFormat,
34+
"jsonmergepatch",
35+
100,
36+
3,
37+
listOf("BOTH_UNDEFINED", "ONE_UNDEFINED", "BOTH_NULL","BOTH_PRESENT","ONE_PRESENT")
38+
){ args: MutableList<String> ->
39+
40+
41+
val solution = initAndRun(args)
42+
43+
assertTrue(solution.individuals.size >= 1)
44+
assertHasAtLeastOne(solution, HttpVerb.PATCH, 400, "/api/jsonmergepatch/", null)
45+
assertHasAtLeastOne(solution, HttpVerb.PATCH, 401, "/api/jsonmergepatch/", null)
46+
assertHasAtLeastOne(solution, HttpVerb.PATCH, 403, "/api/jsonmergepatch/", null)
47+
assertHasAtLeastOne(solution, HttpVerb.PATCH, 404, "/api/jsonmergepatch/", null)
48+
assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/api/jsonmergepatch/", null)
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)