Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.foo.rest.examples.bb.jsonpatch

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.evomaster.e2etests.utils.CoveredTargets
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
@RestController
@RequestMapping("/pets")
open class BBJsonPatchApplication {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why blackbox and whitebox, I think we need only one of them for testing this functionality.


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

private val store: MutableMap<Long, BBJsonPatchDto> = mutableMapOf(
1L to BBJsonPatchDto("Dog", 3),
2L to BBJsonPatchDto("Cat", 5)
)

private val mapper = ObjectMapper()

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Pet found"),
ApiResponse(responseCode = "400", description = "Invalid pet id")
])
@GetMapping("/{id}")
fun getPet(@PathVariable id: Long): ResponseEntity<BBJsonPatchDto> {
val pet = store[id] ?: return ResponseEntity.badRequest().build()
return ResponseEntity.ok(pet)
}

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Patch document processed successfully"),
ApiResponse(responseCode = "400", description = "Patch document is not a JSON array")
])
@PatchMapping("/{id}", consumes = ["application/json-patch+json"])
fun patchPet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> {
if (parsePatchDocument(body) == null)
return ResponseEntity.badRequest().body("Patch document must be a JSON array")

CoveredTargets.cover("PATCHED")
return ResponseEntity.ok("patched")
}

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Add operation processed successfully"),
ApiResponse(responseCode = "400", description = "Patch document does not contain an add operation")
])
@PatchMapping("/{id}/add", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun addPet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(body, "add", "JSON_PATCH_ADD")

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Remove operation processed successfully"),
ApiResponse(responseCode = "400", description = "Patch document does not contain a remove operation")
])
@PatchMapping("/{id}/remove", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun removePet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(body, "remove", "JSON_PATCH_REMOVE")

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Replace operation processed successfully"),
ApiResponse(responseCode = "400", description = "Patch document does not contain a replace operation")
])
@PatchMapping("/{id}/replace", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun replacePet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(body, "replace", "JSON_PATCH_REPLACE")

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Move operation processed successfully"),
ApiResponse(responseCode = "400", description = "Patch document does not contain a move operation")
])
@PatchMapping("/{id}/move", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun movePet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(body, "move", "JSON_PATCH_MOVE")

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Copy operation processed successfully"),
ApiResponse(responseCode = "400", description = "Patch document does not contain a copy operation")
])
@PatchMapping("/{id}/copy", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun copyPet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(body, "copy", "JSON_PATCH_COPY")

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Test operation processed successfully"),
ApiResponse(responseCode = "400", description = "Patch document does not contain a test operation")
])
@PatchMapping("/{id}/test", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun testPet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(body, "test", "JSON_PATCH_TEST")

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Patch document has multiple operations"),
ApiResponse(responseCode = "400", description = "Patch document has fewer than two operations")
])
@PatchMapping("/{id}/sequence", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun sequencePet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> {
if (!hasMultipleOperations(body))
return ResponseEntity.badRequest().body("Patch document must contain at least two operations")

CoveredTargets.cover("JSON_PATCH_SEQUENCE")
return ResponseEntity.ok("sequence patched")
}

private fun patchOperation(body: String, operation: String, target: String): ResponseEntity<String> {
if (!hasOperation(body, operation))
return ResponseEntity.badRequest().body("Patch document must contain a $operation operation")

CoveredTargets.cover(target)
return ResponseEntity.ok("$operation patched")
}

private fun hasOperation(body: String, operation: String): Boolean =
parsePatchDocument(body)?.any { it.path("op").asText() == operation } ?: false

private fun hasMultipleOperations(body: String): Boolean =
parsePatchDocument(body)
?.takeIf { it.size() >= 2 }
?.all { it.hasNonNull("op") }
?: false

private fun parsePatchDocument(body: String): JsonNode? =
try {
mapper.readTree(body).takeIf { it.isArray }
} catch (e: Exception) {
null
}
}

data class BBJsonPatchDto(val name: String = "", val age: Int = 0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.foo.rest.examples.bb.jsonpatch

import com.foo.rest.examples.bb.SpringController

class BBJsonPatchController : SpringController(BBJsonPatchApplication::class.java)
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.evomaster.e2etests.spring.rest.bb.jsonpatch

import com.foo.rest.examples.bb.jsonpatch.BBJsonPatchController
import org.evomaster.core.EMConfig
import org.evomaster.core.output.OutputFormat
import org.evomaster.core.problem.rest.data.HttpVerb
import org.evomaster.e2etests.spring.rest.bb.SpringTestBase
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.EnumSource

class BBJsonPatchTest : SpringTestBase() {

companion object {
@BeforeAll
@JvmStatic
fun init() {
val config = EMConfig()
initClass(BBJsonPatchController(), config)
}
}

@ParameterizedTest
@EnumSource
fun testBlackBoxOutput(outputFormat: OutputFormat) {
executeAndEvaluateBBTest(
outputFormat,
"BBJsonPatchEM",
1000,
3,
listOf(
"PATCHED",
"JSON_PATCH_ADD",
"JSON_PATCH_REMOVE",
"JSON_PATCH_REPLACE",
"JSON_PATCH_MOVE",
"JSON_PATCH_COPY",
"JSON_PATCH_TEST",
"JSON_PATCH_SEQUENCE"
)
) { args: MutableList<String> ->

val solution = initAndRun(args)

assertTrue(solution.individuals.size >= 1)

assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/pets/{id}", "patched")

assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/pets/{id}/add", "add patched")
assertHasAtLeastOne(solution, HttpVerb.PATCH, 400, "/pets/{id}/add", null)

assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/pets/{id}/remove", "remove patched")
assertHasAtLeastOne(solution, HttpVerb.PATCH, 400, "/pets/{id}/remove", null)

assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/pets/{id}/replace", "replace patched")
assertHasAtLeastOne(solution, HttpVerb.PATCH, 400, "/pets/{id}/replace", null)

assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/pets/{id}/move", "move patched")
assertHasAtLeastOne(solution, HttpVerb.PATCH, 400, "/pets/{id}/move", null)

assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/pets/{id}/copy", "copy patched")
assertHasAtLeastOne(solution, HttpVerb.PATCH, 400, "/pets/{id}/copy", null)

assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/pets/{id}/test", "test patched")
assertHasAtLeastOne(solution, HttpVerb.PATCH, 400, "/pets/{id}/test", null)

assertHasAtLeastOne(solution, HttpVerb.PATCH, 200, "/pets/{id}/sequence", "sequence patched")
assertHasAtLeastOne(solution, HttpVerb.PATCH, 400, "/pets/{id}/sequence", null)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package com.foo.rest.examples.spring.openapi.v3.jsonpatch

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
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.*

@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
@RestController
@RequestMapping("/pets")
open class JsonPatchApplication {

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

private val store: MutableMap<Long, JsonPatchDto> = mutableMapOf(
1L to JsonPatchDto("Dog", 3),
2L to JsonPatchDto("Cat", 5)
)

private val mapper = ObjectMapper()

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Pet found"),
ApiResponse(responseCode = "400", description = "Invalid pet id")
])
@GetMapping("/{id}")
fun getPet(@PathVariable id: Long): ResponseEntity<JsonPatchDto> {
val pet = store[id] ?: return ResponseEntity.badRequest().build()
return ResponseEntity.ok(pet)
}

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Patch document processed successfully"),
ApiResponse(responseCode = "400", description = "Pet id is invalid or patch document is not a JSON array")
])
@PatchMapping("/{id}", consumes = ["application/json-patch+json"])
fun patchPet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> {
if (!store.containsKey(id)) return ResponseEntity.badRequest().body("Invalid pet id")

if (parsePatchDocument(body) == null)
return ResponseEntity.badRequest().body("Patch document must be a JSON array")

return ResponseEntity.ok("patched")
}

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Add operation processed successfully"),
ApiResponse(responseCode = "400", description = "Pet id is not positive or patch document does not contain an add operation for /a")
])
@PatchMapping("/{id}/add", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun addPet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(id, body, "add") { it.path("path").asText() == "/a" }

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Remove operation processed successfully"),
ApiResponse(responseCode = "400", description = "Pet id is not positive or patch document does not contain a remove operation for /b")
])
@PatchMapping("/{id}/remove", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun removePet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(id, body, "remove") { it.path("path").asText() == "/b" }

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Replace operation processed successfully"),
ApiResponse(responseCode = "400", description = "Pet id is not positive or patch document does not contain a replace operation for /c")
])
@PatchMapping("/{id}/replace", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun replacePet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(id, body, "replace") { it.path("path").asText() == "/c" }

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Move operation processed successfully"),
ApiResponse(responseCode = "400", description = "Pet id is not positive or patch document does not contain a move operation from /a to /d")
])
@PatchMapping("/{id}/move", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun movePet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(id, body, "move") {
it.path("from").asText() == "/a" && it.path("path").asText() == "/d"
}

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Copy operation processed successfully"),
ApiResponse(responseCode = "400", description = "Pet id is not positive or patch document does not contain a copy operation from /a to /d")
])
@PatchMapping("/{id}/copy", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun copyPet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(id, body, "copy") {
it.path("from").asText() == "/a" && it.path("path").asText() == "/d"
}

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Test operation processed successfully"),
ApiResponse(responseCode = "400", description = "Pet id is not positive or patch document does not contain a test operation for /b")
])
@PatchMapping("/{id}/test", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun testPet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> =
patchOperation(id, body, "test") { it.path("path").asText() == "/b" }

@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "Patch document has multiple operations"),
ApiResponse(responseCode = "400", description = "Pet id is not positive or patch document does not add /a before replacing /c")
])
@PatchMapping("/{id}/sequence", consumes = ["application/json-patch+json"], produces = ["text/plain"])
fun sequencePet(@PathVariable id: Long, @RequestBody body: String): ResponseEntity<String> {
if (id <= 0)
return ResponseEntity.badRequest().body("Pet id must be positive")

if (!hasAddThenReplaceSequence(body))
return ResponseEntity.badRequest().body("Patch document must contain add followed by replace")

return ResponseEntity.ok("sequence patched")
}

private fun patchOperation(
id: Long,
body: String,
operation: String,
extraCheck: (JsonNode) -> Boolean
): ResponseEntity<String> {
if (id <= 0)
return ResponseEntity.badRequest().body("Pet id must be positive")

if (!hasOperation(body, operation, extraCheck))
return ResponseEntity.badRequest().body("Patch document must contain a $operation operation")

return ResponseEntity.ok("$operation patched")
}

private fun hasOperation(body: String, operation: String, extraCheck: (JsonNode) -> Boolean): Boolean =
parsePatchDocument(body)?.any { it.path("op").asText() == operation && extraCheck(it) } ?: false

private fun hasAddThenReplaceSequence(body: String): Boolean {
val operations = parsePatchDocument(body) ?: return false
if (operations.size() < 2)
return false

var hasAdd = false
var hasReplace = false

for (operation in operations) {
if (operation.path("op").asText() == "add" && operation.path("path").asText() == "/a")
hasAdd = true
if (operation.path("op").asText() == "replace" && operation.path("path").asText() == "/c")
hasReplace = true
}

return hasAdd && hasReplace
}

private fun parsePatchDocument(body: String): JsonNode? =
try {
mapper.readTree(body).takeIf { it.isArray }
} catch (e: Exception) {
null
}
}

data class JsonPatchDto(val name: String = "", val age: Int = 0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.foo.rest.examples.spring.openapi.v3.jsonpatch

import com.foo.rest.examples.spring.openapi.v3.SpringController

class JsonPatchController : SpringController(JsonPatchApplication::class.java)
Loading
Loading