Skip to content

Commit

Permalink
Add web test for partner GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissearle committed Nov 16, 2023
1 parent 4d71feb commit c8390e7
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dependencies {
testImplementation(libs.testcontainers.core)
testImplementation(libs.testcontainers.postgresql)
testImplementation(libs.mockk.jvm)
testImplementation(libs.ktor.client.content.negotiation)
}

tasks {
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ ktor-server-netty = { group = "io.ktor", name = "ktor-server-netty-jvm", version
ktor-server-status-pages = { group = "io.ktor", name = "ktor-server-status-pages-jvm", version.ref = "ktor_version" }
ktor-server-tests = { group = "io.ktor", name = "ktor-server-tests-jvm", version.ref = "ktor_version" }

ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor_version" }

logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logback_version" }

mockk-jvm = { group = "io.mockk", name = "mockk-jvm", version.ref = "mockk_version" }
Expand Down
26 changes: 26 additions & 0 deletions src/test/kotlin/no/java/partner/TestApplicationBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package no.java.partner

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.serialization.jackson.jackson
import io.ktor.server.application.Application
import io.ktor.server.testing.ApplicationTestBuilder
import no.java.partner.plugins.configureRouting
import no.java.partner.plugins.configureSerialization

fun ApplicationTestBuilder.testClient() = createClient {
install(ContentNegotiation) {
jackson {
registerModule(JavaTimeModule())
}
}
}

fun ApplicationTestBuilder.app(config: Application.() -> Unit) {
application {
configureSerialization()
configureRouting()

config()
}
}
131 changes: 131 additions & 0 deletions src/test/kotlin/no/java/partner/plugins/PartnerRoutingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package no.java.partner.plugins

import arrow.core.right
import com.typesafe.config.ConfigFactory
import io.kotest.matchers.shouldBe
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.accept
import io.ktor.client.request.get
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.config.HoconApplicationConfig
import io.ktor.server.testing.ApplicationTestBuilder
import io.ktor.server.testing.testApplication
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import no.java.partner.app
import no.java.partner.model.Contact
import no.java.partner.model.InfoList
import no.java.partner.model.Partner
import no.java.partner.model.web.BasicPartner
import no.java.partner.model.web.PartnerWithContacts
import no.java.partner.service.PartnerService
import no.java.partner.testClient
import org.junit.jupiter.api.Test

class PartnerRoutingTest {
@Test
fun `GET partner returns partner list`() {
val partnerService = mockk<PartnerService>()

every { partnerService.allPartners() } returns listOf(
Partner(id = 1, name = "Test Partner 1", domainName = "test.domain.tld", contacts = emptyList()),
)

testApplication {
val client = setup(partnerService)

val response = client.get("/partner") {
accept(ContentType.Application.Json)
}

response.status shouldBe HttpStatusCode.OK

val partners = response.body<List<BasicPartner>>()

partners.size shouldBe 1
partners.first().let {
it.id shouldBe 1L
it.name shouldBe "Test Partner 1"
it.domainName shouldBe "test.domain.tld"
}
}
}

@Test
fun `GET partner 1 returns partner`() {
val partnerService = mockk<PartnerService>()

val slot = slot<Long>()

every { partnerService.partnerById(capture(slot)) } returns Partner(
id = 1,
name = "Test Partner 1",
domainName = "test.domain.tld",
contacts = listOf(
Contact(
id = 1L,
name = "Test Contact 1",
email = "[email protected]",
telephone = "Test Telephone",
source = "Test Source",
lists = listOf(
InfoList(
id = 1L,
name = "Test List 1",
contacts = emptyList(),
unsubscribed = emptyList(),
),
),
),
),
).right()

testApplication {
val client = setup(partnerService)

val response = client.get("/partner/1") {
accept(ContentType.Application.Json)
}

response.status shouldBe HttpStatusCode.OK

slot.captured shouldBe 1L

val partner = response.body<PartnerWithContacts>()

partner.id shouldBe 1L
partner.name shouldBe "Test Partner 1"
partner.domainName shouldBe "test.domain.tld"

partner.contacts.size shouldBe 1
partner.contacts.first().let { contact ->
contact.id shouldBe 1L
contact.name shouldBe "Test Contact 1"
contact.email shouldBe "[email protected]"
contact.telephone shouldBe "Test Telephone"
contact.source shouldBe "Test Source"

contact.lists.size shouldBe 1
contact.lists.first().let { infoList ->
infoList.id shouldBe 1L
infoList.name shouldBe "Test List 1"
}
}
}
}

private fun ApplicationTestBuilder.setup(partnerService: PartnerService): HttpClient {
environment {
config = HoconApplicationConfig(ConfigFactory.parseMap(emptyMap()))
}

app {
configurePartnerRouting(partnerService)
}

return testClient()
}
}

0 comments on commit c8390e7

Please sign in to comment.