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

ktor2 test migration #128

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
name: Build stack and run tests
shell: /bin/bash
command: |
cp src/main/resources/application.conf.test ./src/main/resources/application.conf
docker-compose -f src/test/resources/docker-compose.yml up -d
docker build --network=mms5-test-network -t mms5-test:latest -f Dockerfile-Test .
docker run --network=mms5-test-network --name mms5-test-container mms5-test:latest
Expand Down
7 changes: 1 addition & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
testImplementation("io.kotest:kotest-assertions-core:$kotestVersion")
testImplementation("io.kotest:kotest-assertions-json-jvm:$kotestVersion")
testImplementation("io.kotest:kotest-property:$kotestVersion")
testImplementation("io.kotest.extensions:kotest-assertions-ktor:2.0.0")

val commonsCliVersion = "1.5.0"
implementation("commons-cli:commons-cli:$commonsCliVersion")
Expand Down Expand Up @@ -93,7 +94,6 @@ dependencies {
tasks {
test {
useJUnitPlatform()
dependsOn("copy-test-fuseki-server")
this.testLogging {
this.showStandardStreams = true
}
Expand All @@ -104,11 +104,6 @@ tasks {
if (System.getenv("MMS5_LOAD_SERVICE_URL") != null)
environment("MMS5_LOAD_SERVICE_URL", System.getenv("MMS5_LOAD_SERVICE_URL"))
}
register<Copy>("copy-test-fuseki-server") {
// Copy fuseki-server jar to known location (build/test-fuseki-server)
from(testFuseki.resolvedConfiguration.files)
destinationDir = project.buildDir.resolve("test-fuseki-server")
}
}
tasks.test {
finalizedBy(tasks.jacocoTestReport) // report is always generated after tests run
Expand Down
5 changes: 2 additions & 3 deletions src/test/kotlin/org/openmbee/mms5/OrgAny.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.openmbee.mms5.util.*
import org.slf4j.LoggerFactory

fun TriplesAsserter.validateOrgTriples(
createResponse: TestApplicationResponse,
orgId: String,
orgName: String,
extraPatterns: List<PairPattern> = listOf()
Expand All @@ -35,7 +34,7 @@ fun TriplesAsserter.validateCreatedOrgTriples(
orgName: String,
extraPatterns: List<PairPattern> = listOf()
) {
validateOrgTriples(createResponse, orgId, orgName, extraPatterns)
validateOrgTriples(orgId, orgName, extraPatterns)

// auto policy
matchOneSubjectTerseByPrefix("m-policy:AutoOrgOwner") {
Expand Down Expand Up @@ -72,4 +71,4 @@ open class OrgAny: CommonSpec() {
val validOrgBody = """
<> dct:title "$orgName"@en .
""".trimIndent()
}
}
28 changes: 14 additions & 14 deletions src/test/kotlin/org/openmbee/mms5/OrgRead.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.openmbee.mms5

import io.kotest.assertions.ktor.client.shouldHaveETag
import io.kotest.assertions.ktor.client.shouldHaveStatus
import io.ktor.http.*
import io.ktor.server.testing.*
import org.openmbee.mms5.util.*
import java.util.*

Expand Down Expand Up @@ -34,16 +37,13 @@ class OrgRead : OrgAny() {
}

"get org" {
val create = createOrg(orgId, orgName)

withTest {
httpGet(orgPath) {}.apply {
response shouldHaveStatus HttpStatusCode.OK
response.shouldHaveHeader(HttpHeaders.ETag, create.response.headers[HttpHeaders.ETag]!!)

response includesTriples {
validateOrgTriples(response, orgId, orgName)
}
testApplication {
val created = createOrg(orgId, orgName)
val response = get(orgPath){}
response shouldHaveStatus HttpStatusCode.OK
response shouldHaveETag created.headers[HttpHeaders.ETag]!!
response includesTriples {
validateOrgTriples(orgId, orgName)
}
}
}
Expand Down Expand Up @@ -110,12 +110,12 @@ class OrgRead : OrgAny() {
response.includesTriples {
modelName = it

validateOrgTriples(createBase.response, orgId, orgName)
validateOrgTriples(createFoo.response, orgFooId, orgFooName)
validateOrgTriples(createBar.response, orgBarId, orgBarName)
validateOrgTriples(orgId, orgName)
validateOrgTriples(orgFooId, orgFooName)
validateOrgTriples(orgBarId, orgBarName)
}
}
}
}
}
}
}
106 changes: 0 additions & 106 deletions src/test/kotlin/org/openmbee/mms5/util/BlazegraphBackend.kt

This file was deleted.

66 changes: 0 additions & 66 deletions src/test/kotlin/org/openmbee/mms5/util/FusekiBackend.kt

This file was deleted.

13 changes: 12 additions & 1 deletion src/test/kotlin/org/openmbee/mms5/util/Helper.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package org.openmbee.mms5.util

import io.kotest.assertions.ktor.client.shouldHaveStatus
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
import org.openmbee.mms5.*


suspend fun ApplicationTestBuilder.createOrg(orgId: String, orgName: String): HttpResponse {
val response = put("/orgs/$orgId") {
setTurtleBody("""
<> dct:title "$orgName"@en .
""".trimIndent())
}
response shouldHaveStatus HttpStatusCode.OK
return response
}
fun createOrg(orgId: String, orgName: String): TestApplicationCall {
return withTest {
httpPut("/orgs/$orgId") {
Expand Down
18 changes: 18 additions & 0 deletions src/test/kotlin/org/openmbee/mms5/util/RdfAssertions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ package org.openmbee.mms5.util
import io.kotest.assertions.fail
import io.kotest.assertions.json.shouldBeJsonObject
import io.kotest.assertions.json.shouldEqualJson
import io.kotest.assertions.ktor.client.shouldHaveStatus
import io.kotest.assertions.withClue
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldStartWith
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.request.*
import io.ktor.server.testing.*
Expand Down Expand Up @@ -367,6 +369,22 @@ fun TestApplicationResponse.includesTriples(statusCode: HttpStatusCode, assertio
infix fun TestApplicationResponse.includesTriples(assertions: TriplesAsserter.() -> Unit): TriplesAsserter {
return includesTriples(HttpStatusCode.OK, assertions)
}
suspend fun HttpResponse.includesTriples(statusCode: HttpStatusCode, assertions: TriplesAsserter.() -> Unit): TriplesAsserter {
this.shouldHaveStatus(statusCode)

// assert content-type header (ignore charset if present)
this.headers[HttpHeaders.ContentType].shouldStartWith(RdfContentTypes.Turtle.contentType)

// parse turtle into model
val model = ModelFactory.createDefaultModel()
parseTurtle(this.bodyAsText(), model, this.call.request.url.toString())

// make triple assertions and then assert the model is empty
return TriplesAsserter(model).apply { assertions() }
}
suspend infix fun HttpResponse.includesTriples(assertions: TriplesAsserter.() -> Unit): TriplesAsserter {
return includesTriples(HttpStatusCode.OK, assertions)
}

fun TestApplicationResponse.exclusivelyHasTriples(statusCode: HttpStatusCode, assertions: TriplesAsserter.() -> Unit) {
includesTriples(statusCode, assertions).assertEmpty()
Expand Down
20 changes: 20 additions & 0 deletions src/test/kotlin/org/openmbee/mms5/util/Requests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.openmbee.mms5.util

import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
import org.openmbee.mms5.ROOT_CONTEXT
Expand Down Expand Up @@ -124,3 +126,21 @@ fun TestApplicationEngine.httpPatch(uri: String, setup: TestApplicationRequest.(
fun TestApplicationEngine.httpDelete(uri: String, setup: TestApplicationRequest.() -> Unit): TestApplicationCall {
return this.httpRequest(HttpMethod.Delete, uri, setup)
}

//ktor2
fun HttpRequestBuilder.setTurtleBody(body: String) {
header("Content-Type", "text/turtle")
setBody(body)
}
suspend fun ApplicationTestBuilder.put(uri: String, setup: HttpRequestBuilder.() -> Unit): HttpResponse {
return client.put(uri) {
header("Authorization", authorization(rootAuth))
setup()
}
}
suspend fun ApplicationTestBuilder.get(uri: String, setup: HttpRequestBuilder.() -> Unit): HttpResponse {
return client.get(uri) {
header("Authorization", authorization(rootAuth))
setup()
}
}