Skip to content

Commit

Permalink
[Server] Convert GetOutcome API into a GET with query params rather t…
Browse files Browse the repository at this point in the history
…han a GET with body.
  • Loading branch information
gmarciani committed May 19, 2024
1 parent a4f69b3 commit 2b5667d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 42 deletions.
11 changes: 6 additions & 5 deletions server/src/main/kotlin/com/yawa/server/api/simple/GetOutcome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import mu.KotlinLogging
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.util.*

Expand All @@ -17,9 +18,9 @@ private val log = KotlinLogging.logger {}
class GetOutcome {

@GetMapping("/simple/outcome", produces = [MediaType.APPLICATION_JSON_VALUE])
fun getOutcome(@RequestBody request: GetOutcomeRequest): GetOutcomeResponse {
log.info("Processing request: $request")
val outcome = if (request.outcome == Outcome.RANDOM) {
fun getOutcome(@RequestParam outcome: Outcome): GetOutcomeResponse {
log.info("Processing request: outcome=$outcome")
val _outcome = if (outcome == Outcome.RANDOM) {
val dice = random()
if (dice <= 6.0/10) {
Outcome.SUCCESS
Expand All @@ -32,8 +33,8 @@ class GetOutcome {
} else {
Outcome.INTERNAL_ERROR
}
} else request.outcome
when (outcome) {
} else outcome
when (_outcome) {
Outcome.SUCCESS -> {
log.info("Will return success")
return GetOutcomeResponse("Success")
Expand Down
31 changes: 10 additions & 21 deletions server/src/main/resources/openapi/definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,19 @@
"operationId": "getOutcome",
"parameters": [
{
"name": "request",
"name": "outcome",
"in": "query",
"required": true,
"schema": {
"$ref": "#/components/schemas/GetOutcomeRequest"
"type": "string",
"enum": [
"SUCCESS",
"NOT_AUTHORIZED",
"NOT_FOUND",
"BAD_REQUEST",
"INTERNAL_ERROR",
"RANDOM"
]
}
}
],
Expand Down Expand Up @@ -1512,25 +1520,6 @@
}
}
},
"GetOutcomeRequest": {
"required": [
"outcome"
],
"type": "object",
"properties": {
"outcome": {
"type": "string",
"enum": [
"SUCCESS",
"NOT_AUTHORIZED",
"NOT_FOUND",
"BAD_REQUEST",
"INTERNAL_ERROR",
"RANDOM"
]
}
}
},
"GetOutcomeResponse": {
"required": [
"message"
Expand Down
32 changes: 16 additions & 16 deletions server/src/test/kotlin/com/yawa/server/api/simple/GetOutcomeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,54 @@ class GetOutcomeTest : BehaviorSpec({
`when`("GetDeterministicOutcome is called") {

and("the requested outcome is ${GetOutcome.Outcome.SUCCESS}") {
val request = GetOutcome.GetOutcomeRequest(outcome = GetOutcome.Outcome.SUCCESS)
val outcome = GetOutcome.Outcome.SUCCESS

then("it returns a successful response") {
val response = subject.getOutcome(request)
val response = subject.getOutcome(outcome = outcome)
response shouldBe GetOutcome.GetOutcomeResponse("Success")
}
}

and("the requested outcome is ${GetOutcome.Outcome.NOT_AUTHORIZED}") {
val request = GetOutcome.GetOutcomeRequest(outcome = GetOutcome.Outcome.NOT_AUTHORIZED)
val outcome = GetOutcome.Outcome.NOT_AUTHORIZED

then("it returns a NotAuthorizedException exception") {
shouldThrowExactly<NotAuthorizedException> { subject.getOutcome(request) }
shouldThrowExactly<NotAuthorizedException> { subject.getOutcome(outcome = outcome) }
}
}

and("the requested outcome is ${GetOutcome.Outcome.NOT_FOUND}") {
val request = GetOutcome.GetOutcomeRequest(outcome = GetOutcome.Outcome.NOT_FOUND)
val outcome = GetOutcome.Outcome.NOT_FOUND

then("it returns a ResourceNotFoundException exception") {
shouldThrowExactly<ResourceNotFoundException> { subject.getOutcome(request) }
shouldThrowExactly<ResourceNotFoundException> { subject.getOutcome(outcome = outcome) }
}
}

and("the requested outcome is ${GetOutcome.Outcome.BAD_REQUEST}") {
val request = GetOutcome.GetOutcomeRequest(outcome = GetOutcome.Outcome.BAD_REQUEST)
val outcome = GetOutcome.Outcome.BAD_REQUEST

then("it returns a YawaBadRequestException exception") {
shouldThrowExactly<YawaBadRequestException> { subject.getOutcome(request) }
shouldThrowExactly<YawaBadRequestException> { subject.getOutcome(outcome = outcome) }
}
}

and("the requested outcome is ${GetOutcome.Outcome.INTERNAL_ERROR}") {
val request = GetOutcome.GetOutcomeRequest(outcome = GetOutcome.Outcome.INTERNAL_ERROR)
val outcome = GetOutcome.Outcome.INTERNAL_ERROR

then("it returns a YawaInternalException exception") {
shouldThrowExactly<YawaInternalException> { subject.getOutcome(request) }
shouldThrowExactly<YawaInternalException> { subject.getOutcome(outcome = outcome) }
}
}

and("the requested outcome is ${GetOutcome.Outcome.RANDOM}") {
val request = GetOutcome.GetOutcomeRequest(outcome = GetOutcome.Outcome.RANDOM)
val outcome = GetOutcome.Outcome.RANDOM

and("the random number is <= ${6.0/10}") {
every { subject["random"]() } returns 6.0/10

then("it returns a successful response") {
val response = subject.getOutcome(request)
val response = subject.getOutcome(outcome = outcome)
response shouldBe GetOutcome.GetOutcomeResponse("Success")
}
}
Expand All @@ -73,31 +73,31 @@ class GetOutcomeTest : BehaviorSpec({
every { subject["random"]() } returns 7.5/10

then("it returns a NotAuthorizedException exception") {
shouldThrowExactly<NotAuthorizedException> { subject.getOutcome(request) }
shouldThrowExactly<NotAuthorizedException> { subject.getOutcome(outcome = outcome) }
}
}

and("the random number is <= ${9.0/10}") {
every { subject["random"]() } returns 9.0/10

then("it returns a ResourceNotFoundException exception") {
shouldThrowExactly<ResourceNotFoundException> { subject.getOutcome(request) }
shouldThrowExactly<ResourceNotFoundException> { subject.getOutcome(outcome = outcome) }
}
}

and("the random number is <= ${9.5/10}") {
every { subject["random"]() } returns 9.5/10

then("it returns a YawaBadRequestException exception") {
shouldThrowExactly<YawaBadRequestException> { subject.getOutcome(request) }
shouldThrowExactly<YawaBadRequestException> { subject.getOutcome(outcome = outcome) }
}
}

and("the random number is > ${9.5/10}") {
every { subject["random"]() } returns 9.6/10

then("it returns a YawaInternalException exception") {
shouldThrowExactly<YawaInternalException> { subject.getOutcome(request) }
shouldThrowExactly<YawaInternalException> { subject.getOutcome(outcome = outcome) }
}
}
}
Expand Down

0 comments on commit 2b5667d

Please sign in to comment.