-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract exception controller tests (!1201)
- Loading branch information
1 parent
8b72464
commit cb70c64
Showing
26 changed files
with
1,728 additions
and
1,312 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
140 changes: 140 additions & 0 deletions
140
common/spring-webmvc/src/test/kotlin/org/orkg/common/exceptions/JsonExceptionUnitTest.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,140 @@ | ||
package org.orkg.common.exceptions | ||
|
||
import org.hamcrest.Matchers.`is` | ||
import org.hamcrest.Matchers.notNullValue | ||
import org.junit.jupiter.api.Test | ||
import org.orkg.common.configuration.CommonSpringConfig | ||
import org.orkg.common.exceptions.JsonExceptionUnitTest.TestController | ||
import org.orkg.testing.configuration.FixedClockConfig | ||
import org.orkg.testing.spring.MockMvcBaseTest | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | ||
import org.springframework.boot.test.context.TestComponent | ||
import org.springframework.http.MediaType | ||
import org.springframework.test.context.ContextConfiguration | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@WebMvcTest | ||
@ContextConfiguration(classes = [ExceptionHandler::class, TestController::class, CommonSpringConfig::class, FixedClockConfig::class]) | ||
internal class JsonExceptionUnitTest : MockMvcBaseTest("errors") { | ||
|
||
@Test | ||
fun jsonMissingFieldException() { | ||
post("/errors/json") | ||
.content("""{"field": "abc", "nested": {}}""") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.perform() | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status", `is`(400))) | ||
.andExpect(jsonPath("$.error", `is`("Bad Request"))) | ||
.andExpect(jsonPath("$.message", `is`("""Field "$.nested.field" is either missing, "null", of invalid type, or contains "null" values."""))) | ||
.andExpect(jsonPath("$.path", `is`("/errors/json"))) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
} | ||
|
||
@Test | ||
fun jsonUnknownFieldException() { | ||
post("/errors/json") | ||
.content("""{"field": "abc", "nested": {"unknown": 1, "field": "def", "list": []}}""") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.perform() | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status", `is`(400))) | ||
.andExpect(jsonPath("$.error", `is`("Bad Request"))) | ||
.andExpect(jsonPath("$.message", `is`("""Unknown field "$.nested.unknown"."""))) | ||
.andExpect(jsonPath("$.path", `is`("/errors/json"))) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
} | ||
|
||
@Test | ||
fun jsonTypeMismatchException() { | ||
post("/errors/json") | ||
.content("""{"field": "abc", "nested": {"field": [], "list": []}}""") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.perform() | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status", `is`(400))) | ||
.andExpect(jsonPath("$.error", `is`("Bad Request"))) | ||
.andExpect(jsonPath("$.message", `is`("""Field "$.nested.field" is either missing, "null", of invalid type, or contains "null" values."""))) | ||
.andExpect(jsonPath("$.path", `is`("/errors/json"))) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
} | ||
|
||
@Test | ||
fun jsonTypeMismatchArrayException() { | ||
post("/errors/json") | ||
.content("""{"field": "abc", "array": [{"unknown": 1}] "nested": {"field": "def", "list": []}}""") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.perform() | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status", `is`(400))) | ||
.andExpect(jsonPath("$.error", `is`("Bad Request"))) | ||
.andExpect(jsonPath("$.message", `is`("""Field "$.array[0].field" is either missing, "null", of invalid type, or contains "null" values."""))) | ||
.andExpect(jsonPath("$.path", `is`("/errors/json"))) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
} | ||
|
||
@Test | ||
fun jsonNullValueInCollectionException() { | ||
post("/errors/json") | ||
.content("""{"field": "abc", "nested": {"field": "def", "list": [null]}}""") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.perform() | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status", `is`(400))) | ||
.andExpect(jsonPath("$.error", `is`("Bad Request"))) | ||
.andExpect(jsonPath("$.message", `is`("""Field "$.nested.list" is either missing, "null", of invalid type, or contains "null" values."""))) | ||
.andExpect(jsonPath("$.path", `is`("/errors/json"))) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
} | ||
|
||
@Test | ||
fun jsonNullValueException() { | ||
post("/errors/json") | ||
.content("""{"field": null, "nested": {"field": "def", "list": []}}""") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.perform() | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status", `is`(400))) | ||
.andExpect(jsonPath("$.error", `is`("Bad Request"))) | ||
.andExpect(jsonPath("$.message", `is`("""Field "$.field" is either missing, "null", of invalid type, or contains "null" values."""))) | ||
.andExpect(jsonPath("$.path", `is`("/errors/json"))) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
} | ||
|
||
@Test | ||
fun jsonMalformedException() { | ||
post("/errors/json") | ||
.content("""{"field": "abc" "nested": {"field": "def", "list": []}}""") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.perform() | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.status", `is`(400))) | ||
.andExpect(jsonPath("$.error", `is`("Bad Request"))) | ||
.andExpect(jsonPath("$.message", `is`("""Unexpected character ('"' (code 34)): was expecting comma to separate Object entries"""))) | ||
.andExpect(jsonPath("$.path", `is`("/errors/json"))) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
} | ||
|
||
@TestComponent | ||
@RestController | ||
internal class TestController { | ||
@PostMapping("/errors/json") | ||
fun json(@RequestBody request: JsonRequest): Nothing = throw NotImplementedError() | ||
|
||
@Suppress("ArrayInDataClass") | ||
data class JsonRequest( | ||
val field: String, | ||
val nested: Nested, | ||
val array: Array<Nested> | ||
) { | ||
data class Nested( | ||
val field: String, | ||
val list: List<String> | ||
) | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...bmvc/src/test/kotlin/org/orkg/common/exceptions/MediaTypeCapabilitiesExceptionUnitTest.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,86 @@ | ||
package org.orkg.common.exceptions | ||
|
||
import org.hamcrest.Matchers.`is` | ||
import org.hamcrest.Matchers.notNullValue | ||
import org.junit.jupiter.api.Test | ||
import org.orkg.common.configuration.CommonSpringConfig | ||
import org.orkg.common.exceptions.MediaTypeCapabilitiesExceptionUnitTest.TestController | ||
import org.orkg.testing.configuration.FixedClockConfig | ||
import org.orkg.testing.spring.MockMvcBaseTest | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | ||
import org.springframework.boot.test.context.TestComponent | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.test.context.ContextConfiguration | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.header | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestParam | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@WebMvcTest | ||
@ContextConfiguration(classes = [ExceptionHandler::class, TestController::class, CommonSpringConfig::class, FixedClockConfig::class]) | ||
internal class MediaTypeCapabilitiesExceptionUnitTest : MockMvcBaseTest("errors") { | ||
|
||
@Test | ||
fun malformedMediaTypeCapability() { | ||
val name = "formatted-label" | ||
val value = "true" | ||
|
||
get("/errors/malformed-media-type-capability") | ||
.param("name", name) | ||
.param("value", value) | ||
.perform() | ||
.andExpect(status().isNotAcceptable) | ||
.andExpect(jsonPath("$.status").value(HttpStatus.NOT_ACCEPTABLE.value())) | ||
.andExpect(jsonPath("$.error", `is`("Not Acceptable"))) | ||
.andExpect(jsonPath("$.path").value("/errors/malformed-media-type-capability")) | ||
.andExpect(jsonPath("$.message").value("""Malformed value "$value" for media type capability "$name".""")) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
} | ||
|
||
@Test | ||
fun handleHttpMediaTypeNotAcceptable() { | ||
get("/errors/handle-http-media-type-not-acceptable") | ||
.accept("application/unsupported+json") | ||
.perform() | ||
.andExpect(status().isNotAcceptable) | ||
.andExpect(jsonPath("$.status").value(HttpStatus.NOT_ACCEPTABLE.value())) | ||
.andExpect(jsonPath("$.error", `is`("Not Acceptable"))) | ||
.andExpect(jsonPath("$.path").value("/errors/handle-http-media-type-not-acceptable")) | ||
.andExpect(jsonPath("$.message").value("""Unsupported response media type. Please check the 'Accept' header for a list of supported media types.""")) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
.andExpect(header().string("Accept", "application/json, application/xml")) | ||
} | ||
|
||
@Test | ||
fun httpMediaTypeNotSupported() { | ||
post("/errors/http-media-type-not-supported") | ||
.content("request") | ||
.contentType("text/plain") | ||
.perform() | ||
.andExpect(status().isUnsupportedMediaType) | ||
.andExpect(jsonPath("$.status").value(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value())) | ||
.andExpect(jsonPath("$.error", `is`("Unsupported Media Type"))) | ||
.andExpect(jsonPath("$.path").value("/errors/http-media-type-not-supported")) | ||
.andExpect(jsonPath("$.message").value("""Unsupported request media type. Please check the 'Accept' header for a list of supported media types.""")) | ||
.andExpect(jsonPath("$.timestamp", `is`(notNullValue()))) | ||
.andExpect(header().string("Accept", "application/json, application/xml")) | ||
} | ||
|
||
@TestComponent | ||
@RestController | ||
internal class TestController { | ||
@GetMapping("/errors/malformed-media-type-capability") | ||
fun malformedMediaTypeCapability(@RequestParam name: String, @RequestParam value: String): Nothing = | ||
throw MalformedMediaTypeCapability(name, value) | ||
|
||
@GetMapping("/errors/handle-http-media-type-not-acceptable", produces = ["application/json", "application/xml"]) | ||
fun handleHttpMediaTypeNotAcceptable() = "response" | ||
|
||
@PostMapping("/errors/http-media-type-not-supported", consumes = ["application/json", "application/xml"]) | ||
fun httpMediaTypeNotSupported(@RequestBody body: String): Nothing = throw NotImplementedError() | ||
} | ||
} |
Oops, something went wrong.