From 2b5667df5adf700bc9bdc79aade445be152a80a9 Mon Sep 17 00:00:00 2001 From: Giacomo Marciani Date: Sun, 19 May 2024 19:30:09 +0200 Subject: [PATCH] [Server] Convert GetOutcome API into a GET with query params rather than a GET with body. --- .../com/yawa/server/api/simple/GetOutcome.kt | 11 ++++--- .../main/resources/openapi/definition.json | 31 ++++++------------ .../yawa/server/api/simple/GetOutcomeTest.kt | 32 +++++++++---------- 3 files changed, 32 insertions(+), 42 deletions(-) diff --git a/server/src/main/kotlin/com/yawa/server/api/simple/GetOutcome.kt b/server/src/main/kotlin/com/yawa/server/api/simple/GetOutcome.kt index 79c3eef3..1ea72963 100644 --- a/server/src/main/kotlin/com/yawa/server/api/simple/GetOutcome.kt +++ b/server/src/main/kotlin/com/yawa/server/api/simple/GetOutcome.kt @@ -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.* @@ -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 @@ -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") diff --git a/server/src/main/resources/openapi/definition.json b/server/src/main/resources/openapi/definition.json index 86e2a282..44b7626b 100644 --- a/server/src/main/resources/openapi/definition.json +++ b/server/src/main/resources/openapi/definition.json @@ -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" + ] } } ], @@ -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" diff --git a/server/src/test/kotlin/com/yawa/server/api/simple/GetOutcomeTest.kt b/server/src/test/kotlin/com/yawa/server/api/simple/GetOutcomeTest.kt index f881d48e..955533b8 100644 --- a/server/src/test/kotlin/com/yawa/server/api/simple/GetOutcomeTest.kt +++ b/server/src/test/kotlin/com/yawa/server/api/simple/GetOutcomeTest.kt @@ -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 { subject.getOutcome(request) } + shouldThrowExactly { 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 { subject.getOutcome(request) } + shouldThrowExactly { 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 { subject.getOutcome(request) } + shouldThrowExactly { 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 { subject.getOutcome(request) } + shouldThrowExactly { 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") } } @@ -73,7 +73,7 @@ class GetOutcomeTest : BehaviorSpec({ every { subject["random"]() } returns 7.5/10 then("it returns a NotAuthorizedException exception") { - shouldThrowExactly { subject.getOutcome(request) } + shouldThrowExactly { subject.getOutcome(outcome = outcome) } } } @@ -81,7 +81,7 @@ class GetOutcomeTest : BehaviorSpec({ every { subject["random"]() } returns 9.0/10 then("it returns a ResourceNotFoundException exception") { - shouldThrowExactly { subject.getOutcome(request) } + shouldThrowExactly { subject.getOutcome(outcome = outcome) } } } @@ -89,7 +89,7 @@ class GetOutcomeTest : BehaviorSpec({ every { subject["random"]() } returns 9.5/10 then("it returns a YawaBadRequestException exception") { - shouldThrowExactly { subject.getOutcome(request) } + shouldThrowExactly { subject.getOutcome(outcome = outcome) } } } @@ -97,7 +97,7 @@ class GetOutcomeTest : BehaviorSpec({ every { subject["random"]() } returns 9.6/10 then("it returns a YawaInternalException exception") { - shouldThrowExactly { subject.getOutcome(request) } + shouldThrowExactly { subject.getOutcome(outcome = outcome) } } } }