Skip to content

Commit

Permalink
WireMock test sendSMS
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jun 10, 2024
1 parent 5747500 commit 41cc48c
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 15 deletions.
31 changes: 23 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@
<nexusUrl>https://oss.sonatype.org</nexusUrl>
<project.scm.connection>scm:git@github.com:Vonage/vonage-kotlin-sdk</project.scm.connection>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<kotlin.compiler.languageVersion>2.0</kotlin.compiler.languageVersion>
<kotlin.compiler.apiVersion>2.0</kotlin.compiler.apiVersion>
<kotlin.compiler.jvmTarget>${java.version}</kotlin.compiler.jvmTarget>
<maven.compiler.release>${java.version}</maven.compiler.release>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
</properties>

<dependencies>
Expand All @@ -64,10 +62,16 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<artifactId>kotlin-test-junit5</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.marcinziolo</groupId>
<artifactId>kotlin-wiremock</artifactId>
<version>2.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down Expand Up @@ -101,7 +105,7 @@
<version>3.6.3</version>
</requireMavenVersion>
<requireJavaVersion>
<version>${java.version}</version>
<version>1.8</version>
</requireJavaVersion>
</rules>
</configuration>
Expand All @@ -122,7 +126,7 @@
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.2</version>
<version>3.7.0</version>
<executions>
<execution>
<id>dokka-jar</id>
Expand All @@ -132,8 +136,8 @@
</goals>
<configuration>
<classifier>dokka</classifier>
<classesDirectory>${project.build.directory}/dokka</classesDirectory>
<skipIfEmpty>true</skipIfEmpty>
<!--classesDirectory>${project.build.directory}/dokka</classesDirectory>
<skipIfEmpty>true</skipIfEmpty-->
</configuration>
</execution>
</executions>
Expand All @@ -153,6 +157,16 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>8</source>
<target>8</target>
<release>8</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.7.1</version>
Expand Down Expand Up @@ -186,6 +200,7 @@
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>2.0.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
Expand Down
10 changes: 3 additions & 7 deletions src/main/kotlin/com/vonage/client/kt/Vonage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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() }
}
35 changes: 35 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
50 changes: 50 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/MessagesTest.kt
Original file line number Diff line number Diff line change
@@ -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<String, String> = mapOf(
"message_type" to "text",
"text" to text,
"to" to toNumber,
"from" to fromNumber,
"channel" to channel
)

fun mockResponse(expectedBodyParams: Map<String, String>) {
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)
})
}
}
4 changes: 4 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/VonageTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.vonage.client.kt

class VonageTest {
}
28 changes: 28 additions & 0 deletions src/test/resources/com/vonage/client/kt/application_key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDqSe/genQY2KUz
xc1iqj2pgeH1k2SKHpIK8L8JLwfEEskhG92Ch9euivu30roKHTVMI4lXRrOL0OyM
uX0EZQGSuDvB8cZpOL+044oV3XInx/LwkEgHIsB7anv6rJ/fyxHTGvlc1c3DmQhl
J5ToFtu2DL8dY/nmYvdpeo74+0iOyTODiGCNrB8Kqc7zrx/KQM732BBrLeRs6TwY
+7lQYIebFQN5C9JvGWAlStw9IJO+dlJojMru9bz//B+rWe350m5WiVSFHnSP5K6o
t1SBuraoJRsdORwVb9dp0gU3UDkGApYgmBFTt0MJoCZWNA3QNkJ8clpjh8JJtIcF
dWFL2mvxAgMBAAECggEAZZd9/rbalNOMfzCsaLYtWs1JL/WjyQiMh1XxYIgWM/15
XXP5z1ocOkFl+UXVCgG0VLmsGj48KMqFaFgeT8OEtRxSPT0brhC/gC6Sd/y4PWvE
em/167I0CNAZxo8IHECwD/xIWOsU+FXpgANz3FfdGcnZLWNmv3H2mrRcPrantdHQ
oMiS2wCF+6Zyo0TTVdddHjvQAutznMEoQLdTvl3MtGY5WWchDzFAmwqDv/+ne9H7
aerpq3tv1Ng0uOfzw39UuUTqrYXrqYDGYtK2D5yoEzN6ktK2uYIV0MIf9Eh5FRp9
YoYYI7t/3SHXwidViOE8wXVhZ0DUbyIbYv97elebyQKBgQD7KrXFmMBYcxVJsEx0
QQFRUu9iRkqMrfsINc/O79y81oD8ewc2snizyubdrRv0CCz3wFVlCBRwCURiczpC
il+EsrbIhdVyQZVRGCUNDDwMP7T/r7mBFqVyQfSgIFGKkexkCUnxW7pxZjkpBiWA
HnAvKrzTor5AEE/Wn3t7VRWAjwKBgQDuzBVApH/0Y52wQPU/t8drJQSDkFzhA3ov
CsIUvLXsS5ZfK2p4jBZzWG9IpJ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