From ade0276d575c5ee29ec4df6598d6b8a7be6adf78 Mon Sep 17 00:00:00 2001 From: Johan Blomgren Date: Fri, 15 Dec 2023 04:55:51 +0100 Subject: [PATCH 1/2] Skal kunne kalle endepunkter som returnerer 201 uten body --- .../libs/http/client/AbstractRestClient.kt | 20 +++++++++++++++++-- .../http/client/AbstractRestClientTest.kt | 15 ++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt b/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt index 519cb93..944ec9a 100644 --- a/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt +++ b/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt @@ -34,6 +34,13 @@ abstract class AbstractRestClient(val restTemplate: RestTemplate) { uriVariables: Map = emptyMap(), ): T = execute(uri, HttpMethod.POST, HttpEntity(payload, httpHeaders), uriVariables) + inline fun postForEntityNullable( + uri: String, + payload: Any, + httpHeaders: HttpHeaders? = null, + uriVariables: Map = emptyMap(), + ): T? = executeNullable(uri, HttpMethod.POST, HttpEntity(payload, httpHeaders), uriVariables) + inline fun putForEntity( uri: String, payload: Any, @@ -55,15 +62,24 @@ abstract class AbstractRestClient(val restTemplate: RestTemplate) { uriVariables: Map = emptyMap(), ): T = execute(uri, HttpMethod.DELETE, HttpEntity(payload, httpHeaders), uriVariables) - inline fun execute( + inline fun execute( urlTemplate: String, method: HttpMethod, entity: HttpEntity<*>, uriVariables: Map = emptyMap(), ): T { + return executeNullable(urlTemplate, method, entity, uriVariables) + ?: error("Mangler body") + } + + inline fun executeNullable( + urlTemplate: String, + method: HttpMethod, + entity: HttpEntity<*>, + uriVariables: Map + ): T? { try { return restTemplate.exchange(urlTemplate, method, entity, uriVariables).body - ?: error("Mangler body") } catch (e: RestClientResponseException) { val url = expand(urlTemplate, uriVariables) secureLogger.warn("Feil ved kall method=$method mot url=$url", e) diff --git a/http-client/test/no/nav/tilleggsstonader/libs/http/client/AbstractRestClientTest.kt b/http-client/test/no/nav/tilleggsstonader/libs/http/client/AbstractRestClientTest.kt index a515bf2..e983c49 100644 --- a/http-client/test/no/nav/tilleggsstonader/libs/http/client/AbstractRestClientTest.kt +++ b/http-client/test/no/nav/tilleggsstonader/libs/http/client/AbstractRestClientTest.kt @@ -4,6 +4,7 @@ import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.client.WireMock.aResponse import com.github.tomakehurst.wiremock.client.WireMock.anyUrl +import com.github.tomakehurst.wiremock.client.WireMock.created import com.github.tomakehurst.wiremock.client.WireMock.okJson import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo import com.github.tomakehurst.wiremock.common.ConsoleNotifier @@ -46,6 +47,10 @@ internal class AbstractRestClientTest { .toUriString() getForEntity(uri, uriVariables = mapOf("id" to "123", "userId" to "id")) } + + fun postUtenResponseBody(): String? { + return postForEntityNullable(uri.toString(), emptyMap()) + } } companion object { @@ -102,6 +107,16 @@ internal class AbstractRestClientTest { } } + @Test + fun `skal kunne kalle på endepunkt og forvente svar uten body`() { + wireMockServer.stubFor( + WireMock.post(anyUrl()) + .willReturn(created()), + ) + + assertThat(client.postUtenResponseBody()).isNull() + } + @Test fun `query param request skal feile hvis query params ikke mer med`() { wireMockServer.stubFor( From 4d5c502fb6832f877b8e453a4b1f5150d01c6d2e Mon Sep 17 00:00:00 2001 From: Johan Blomgren Date: Fri, 15 Dec 2023 08:44:55 +0100 Subject: [PATCH 2/2] lint --- .../nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt b/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt index 944ec9a..2b31c0e 100644 --- a/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt +++ b/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt @@ -76,7 +76,7 @@ abstract class AbstractRestClient(val restTemplate: RestTemplate) { urlTemplate: String, method: HttpMethod, entity: HttpEntity<*>, - uriVariables: Map + uriVariables: Map, ): T? { try { return restTemplate.exchange(urlTemplate, method, entity, uriVariables).body