From 41cc48cb83fd12da87369d3867ceac2cb4392eb3 Mon Sep 17 00:00:00 2001 From: Sina Madani Date: Mon, 10 Jun 2024 17:16:57 +0100 Subject: [PATCH] WireMock test sendSMS --- pom.xml | 31 +++++++++--- .../kotlin/com/vonage/client/kt/Vonage.kt | 10 ++-- .../com/vonage/client/kt/AbstractTest.kt | 35 +++++++++++++ .../com/vonage/client/kt/MessagesTest.kt | 50 +++++++++++++++++++ .../kotlin/com/vonage/client/kt/VonageTest.kt | 4 ++ .../com/vonage/client/kt/application_key | 28 +++++++++++ 6 files changed, 143 insertions(+), 15 deletions(-) create mode 100644 src/test/kotlin/com/vonage/client/kt/AbstractTest.kt create mode 100644 src/test/kotlin/com/vonage/client/kt/MessagesTest.kt create mode 100644 src/test/kotlin/com/vonage/client/kt/VonageTest.kt create mode 100644 src/test/resources/com/vonage/client/kt/application_key diff --git a/pom.xml b/pom.xml index 10c1abc..1dd9eb8 100644 --- a/pom.xml +++ b/pom.xml @@ -44,11 +44,9 @@ https://oss.sonatype.org scm:git@github.com:Vonage/vonage-kotlin-sdk UTF-8 - 1.8 2.0 2.0 - ${java.version} - ${java.version} + 1.8 @@ -64,10 +62,16 @@ org.jetbrains.kotlin - kotlin-test-junit + kotlin-test-junit5 2.0.0 test + + com.marcinziolo + kotlin-wiremock + 2.1.1 + test + @@ -101,7 +105,7 @@ 3.6.3 - ${java.version} + 1.8 @@ -122,7 +126,7 @@ maven-javadoc-plugin - 3.6.2 + 3.7.0 dokka-jar @@ -132,8 +136,8 @@ dokka - ${project.build.directory}/dokka - true + @@ -153,6 +157,16 @@ + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + 8 + 8 + 8 + + maven-assembly-plugin 3.7.1 @@ -186,6 +200,7 @@ kotlin-maven-plugin org.jetbrains.kotlin + 2.0.0 true diff --git a/src/main/kotlin/com/vonage/client/kt/Vonage.kt b/src/main/kotlin/com/vonage/client/kt/Vonage.kt index a682811..33a4f59 100644 --- a/src/main/kotlin/com/vonage/client/kt/Vonage.kt +++ b/src/main/kotlin/com/vonage/client/kt/Vonage.kt @@ -15,7 +15,7 @@ class Vonage constructor(init: VonageClient.Builder.() -> Unit) { } -fun VonageClient.Builder.authFromEnv() : VonageClient.Builder { +fun VonageClient.Builder.authFromEnv(): VonageClient.Builder { apiKey(env("VONAGE_API_KEY")) apiSecret(env("VONAGE_API_SECRET")) signatureSecret(env("VONAGE_SIGNATURE_SECRET")) @@ -24,14 +24,10 @@ fun VonageClient.Builder.authFromEnv() : VonageClient.Builder { return this } -fun httpConfig(init: HttpConfig.Builder.() -> Unit): HttpConfig { - return HttpConfig.builder().apply(init).build() +fun VonageClient.Builder.httpConfig(init: HttpConfig.Builder.() -> Unit): VonageClient.Builder { + return this.httpConfig(HttpConfig.builder().apply(init).build()) } private fun env(variable: String) : String? { return System.getenv(variable) -} - -fun main() { - val client = Vonage { authFromEnv() } } \ No newline at end of file diff --git a/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt b/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt new file mode 100644 index 0000000..aa3ed40 --- /dev/null +++ b/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt @@ -0,0 +1,35 @@ +package com.vonage.client.kt + +import com.github.tomakehurst.wiremock.WireMockServer +import com.github.tomakehurst.wiremock.common.ConsoleNotifier +import com.github.tomakehurst.wiremock.core.WireMockConfiguration.options +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach + +abstract class AbstractTest { + val port = 8081 + val wiremock: WireMockServer = WireMockServer( + options().port(port).notifier(ConsoleNotifier(true)) + ) + + val vonageClient = Vonage { + apiKey("a1b2c3d4") + apiSecret("1234567890abcdef") + applicationId("00000000-0000-4000-8000-000000000000") + privateKeyPath("src/test/resources/com/vonage/client/kt/application_key") + httpConfig { + baseUri("http://localhost:$port") + } + } + + @BeforeEach + fun setUp() { + wiremock.start() + } + + @AfterEach + fun afterEach() { + wiremock.resetAll() + wiremock.stop() + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt b/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt new file mode 100644 index 0000000..0498218 --- /dev/null +++ b/src/test/kotlin/com/vonage/client/kt/MessagesTest.kt @@ -0,0 +1,50 @@ +package com.vonage.client.kt + +import com.marcinziolo.kotlin.wiremock.* +import org.junit.jupiter.api.Test + +class MessagesTest : AbstractTest() { + val messagesClient = vonageClient.messages + val fromNumber = "447700900001" + val toNumber = "447712345689" + val text = "Hello, World!" + + fun expectedTextBody(channel: String): Map = mapOf( + "message_type" to "text", + "text" to text, + "to" to toNumber, + "from" to fromNumber, + "channel" to channel + ) + + fun mockResponse(expectedBodyParams: Map) { + 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" + for (entry in expectedBodyParams.entries) { + body contains entry.key equalTo entry.value + } + } returns { + header = "Content-Type" to "application/json" + statusCode = 202 + body = """ + { + "message_uuid": "aaaaaaaa-bbbb-4ccc-8ddd-0123456789ab" + } + """ + } + } + + @Test + fun `send SMS`() { + mockResponse(expectedTextBody("sms")) + messagesClient.send(smsText { + from(fromNumber) + to(toNumber) + text(text) + }) + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/vonage/client/kt/VonageTest.kt b/src/test/kotlin/com/vonage/client/kt/VonageTest.kt new file mode 100644 index 0000000..36578c3 --- /dev/null +++ b/src/test/kotlin/com/vonage/client/kt/VonageTest.kt @@ -0,0 +1,4 @@ +package com.vonage.client.kt + +class VonageTest { +} \ No newline at end of file diff --git a/src/test/resources/com/vonage/client/kt/application_key b/src/test/resources/com/vonage/client/kt/application_key new file mode 100644 index 0000000..4b654b2 --- /dev/null +++ b/src/test/resources/com/vonage/client/kt/application_key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSj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