Skip to content

Commit

Permalink
Factor out WireMock DSL stub
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jun 21, 2024
1 parent 8029893 commit e943f11
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
61 changes: 58 additions & 3 deletions src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package com.vonage.client.kt

import com.fasterxml.jackson.databind.ObjectMapper
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.common.ConsoleNotifier
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.options
import com.marcinziolo.kotlin.wiremock.*
import com.vonage.client.common.HttpMethod
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach

abstract class AbstractTest {
val apiKey = "a1b2c3d4"
val applicationId = "00000000-0000-4000-8000-000000000000"
private val apiSecret = "1234567890abcdef"
private val apiKeySecretEncoded = "YTFiMmMzZDQ6MTIzNDU2Nzg5MGFiY2RlZg=="

val port = 8081
val wiremock: WireMockServer = WireMockServer(
options().port(port).notifier(ConsoleNotifier(false))
)

val vonageClient = Vonage {
apiKey("a1b2c3d4")
apiSecret("1234567890abcdef")
applicationId("00000000-0000-4000-8000-000000000000")
apiKey(apiKey); apiSecret(apiSecret); applicationId(applicationId)
privateKeyPath("src/test/resources/com/vonage/client/kt/application_key")
httpConfig {
baseUri("http://localhost:$port")
Expand All @@ -32,4 +39,52 @@ abstract class AbstractTest {
wiremock.resetAll()
wiremock.stop()
}

protected enum class ContentType(val mime: String) {
APPLICATION_JSON("application/json"),
FORM_URLENCODED("application/x-www-form-urlencoded");

@Override
override fun toString(): String {
return mime
}
}

protected enum class AuthType {
JWT, API_KEY_SECRET
}

protected fun baseMockRequest(
httpMethod: HttpMethod,
expectedUrl: String,
contentType: ContentType?,
accept: ContentType?,
authType: AuthType?,
expectedBodyParams: Map<String, Any>? = null) =
wiremock.requestServerBuilderStep({
url equalTo expectedUrl
headers contains "User-Agent" like "vonage-java-sdk.*"
if (authType != null) {
headers contains "Authorization" like when (authType) {
AuthType.JWT -> "Bearer eyJ.+"
AuthType.API_KEY_SECRET -> "Basic $apiKeySecretEncoded"
}
}
if (contentType != null) {
headers contains "Content-Type" equalTo contentType.mime
}
if (accept != null) {
headers contains "Accept" equalTo accept.mime
}
if (expectedBodyParams != null) {
body equalTo ObjectMapper().writeValueAsString(expectedBodyParams)
}
}, when (httpMethod) {
HttpMethod.GET -> WireMock::get
HttpMethod.POST -> WireMock::post
HttpMethod.PUT -> WireMock::put
HttpMethod.PATCH -> WireMock::patch
HttpMethod.DELETE -> WireMock::delete
else -> throw IllegalArgumentException("Unhandled HTTP method: $httpMethod")
})
}
15 changes: 5 additions & 10 deletions src/test/kotlin/com/vonage/client/kt/MessagesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.vonage.client.kt

import com.fasterxml.jackson.databind.ObjectMapper
import com.marcinziolo.kotlin.wiremock.*
import com.vonage.client.common.HttpMethod
import com.vonage.client.messages.Channel
import com.vonage.client.messages.MessageRequest
import com.vonage.client.messages.MessageResponseException
Expand Down Expand Up @@ -36,16 +37,10 @@ class MessagesTest : AbstractTest() {
private val captionMap = mapOf("caption" to caption)

private fun baseMockRequest(expectedBodyParams: Map<String, Any>? = null) =
wiremock.post {
url equalTo "/v1/messages"
headers contains "User-Agent" like "vonage-java-sdk.*"
headers contains "Authorization" like "Bearer eyJ.+"
headers contains "Content-Type" equalTo "application/json"
headers contains "Accept" equalTo "application/json"
if (expectedBodyParams != null) {
body equalTo ObjectMapper().writeValueAsString(expectedBodyParams)
}
}
baseMockRequest(HttpMethod.POST, "/v1/messages",
ContentType.APPLICATION_JSON, ContentType.APPLICATION_JSON,
AuthType.JWT, expectedBodyParams
)

private fun mock202Response(expectedBodyParams: Map<String, Any>) {
baseMockRequest(expectedBodyParams) returns {
Expand Down

0 comments on commit e943f11

Please sign in to comment.