Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skal kunne kalle endepunkter som returnerer 201 uten body #35

Merged
merged 2 commits into from
Dec 15, 2023
Merged
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
Expand Up @@ -34,6 +34,13 @@ abstract class AbstractRestClient(val restTemplate: RestTemplate) {
uriVariables: Map<String, *> = emptyMap<String, String>(),
): T = execute(uri, HttpMethod.POST, HttpEntity(payload, httpHeaders), uriVariables)

inline fun <reified T : Any> postForEntityNullable(
uri: String,
payload: Any,
httpHeaders: HttpHeaders? = null,
uriVariables: Map<String, *> = emptyMap<String, String>(),
): T? = executeNullable(uri, HttpMethod.POST, HttpEntity(payload, httpHeaders), uriVariables)

inline fun <reified T : Any> putForEntity(
uri: String,
payload: Any,
Expand All @@ -55,15 +62,24 @@ abstract class AbstractRestClient(val restTemplate: RestTemplate) {
uriVariables: Map<String, *> = emptyMap<String, String>(),
): T = execute(uri, HttpMethod.DELETE, HttpEntity(payload, httpHeaders), uriVariables)

inline fun <reified T> execute(
inline fun <reified T : Any> execute(
urlTemplate: String,
method: HttpMethod,
entity: HttpEntity<*>,
uriVariables: Map<String, *> = emptyMap<String, String>(),
): T {
return executeNullable<T>(urlTemplate, method, entity, uriVariables)
?: error("Mangler body")
}

inline fun <reified T : Any> executeNullable(
urlTemplate: String,
method: HttpMethod,
entity: HttpEntity<*>,
uriVariables: Map<String, *>,
): T? {
try {
return restTemplate.exchange<T>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,6 +47,10 @@ internal class AbstractRestClientTest {
.toUriString()
getForEntity<Any>(uri, uriVariables = mapOf("id" to "123", "userId" to "id"))
}

fun postUtenResponseBody(): String? {
return postForEntityNullable<String>(uri.toString(), emptyMap<String, String>())
}
}

companion object {
Expand Down Expand Up @@ -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(
Expand Down