fixs and move files
This commit is contained in:
96
src/test/kotlin/ArticleRouteTest.kt
Normal file
96
src/test/kotlin/ArticleRouteTest.kt
Normal file
@@ -0,0 +1,96 @@
|
||||
package fr.dcproject
|
||||
|
||||
import fr.postgresjson.migration.Migrations
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpMethod
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.server.testing.handleRequest
|
||||
import io.ktor.server.testing.setBody
|
||||
import io.ktor.server.testing.withTestApplication
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.core.context.stopKoin
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.inject
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@KtorExperimentalAPI
|
||||
@TestInstance(PER_CLASS)
|
||||
class ArticleRouteTest: KoinTest {
|
||||
private val migrations: Migrations by inject()
|
||||
|
||||
@BeforeEach
|
||||
fun beforeAll() {
|
||||
startKoin {
|
||||
modules(Module)
|
||||
config.database = "test"
|
||||
config.username = "test"
|
||||
config.password = "test"
|
||||
|
||||
}
|
||||
migrations.run()
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun afterAll() {
|
||||
migrations.forceAllDown()
|
||||
stopKoin()
|
||||
}
|
||||
|
||||
private val article: String = """{
|
||||
"id" : "8e8dd0aa-2b2b-41e1-bff5-ea613c988774",
|
||||
"version_id" : "e3ec9ea8-87ac-46ac-8321-8f2bc8c687bc",
|
||||
"version_number" : 1,
|
||||
"title" : "title13",
|
||||
"annonymous" : false,
|
||||
"content" : "content13",
|
||||
"description" : "description13",
|
||||
"tags" : [ "sky", "nuclear" ],
|
||||
"created_at" : "2019-07-30T14:08:51.420Z",
|
||||
"created_by" : {
|
||||
"id" : "d821a211-10d6-4d65-b0db-e0bd33d21761",
|
||||
"name" : {
|
||||
"civility" : "m",
|
||||
"last_name" : "LAST NAME13",
|
||||
"first_name" : "first name13"
|
||||
},
|
||||
"birthday" : "1994-07-30",
|
||||
"user_id" : "127b9979-1474-4da1-8453-1e10462ae593",
|
||||
"vote_annonymous" : false,
|
||||
"follow_annonymous" : false,
|
||||
"user" : null,
|
||||
"created_at" : "2019-07-30T14:08:49.742Z"
|
||||
}
|
||||
}"""
|
||||
|
||||
@Test
|
||||
fun testRoute() {
|
||||
withTestApplication({ module() }) {
|
||||
handleRequest(HttpMethod.Get, "/articles").apply {
|
||||
assertEquals(HttpStatusCode.OK, response.status())
|
||||
}
|
||||
|
||||
handleRequest(HttpMethod.Post, "/articles") {
|
||||
this.setBody(article)
|
||||
this.addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
|
||||
}.apply {
|
||||
assertEquals(HttpStatusCode.OK, response.status())
|
||||
}
|
||||
|
||||
handleRequest(HttpMethod.Get, "/articles/8e8dd0aa-2b2b-41e1-bff5-ea613c988774").apply {
|
||||
assertEquals(HttpStatusCode.OK, response.status())
|
||||
assertTrue(response.content!!.contains("8e8dd0aa-2b2b-41e1-bff5-ea613c988774"))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
35
src/test/kotlin/RunCucumberTest.kt
Normal file
35
src/test/kotlin/RunCucumberTest.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
import cucumber.api.CucumberOptions
|
||||
import cucumber.api.Scenario
|
||||
import cucumber.api.java8.En
|
||||
import cucumber.api.junit.Cucumber
|
||||
import feature.Context
|
||||
import fr.dcproject.config
|
||||
import fr.postgresjson.migration.Migrations
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import io.ktor.server.testing.createTestEnvironment
|
||||
import org.junit.runner.RunWith
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.inject
|
||||
import java.util.concurrent.TimeUnit
|
||||
import feature.Context.Companion.current as contextCurrent
|
||||
|
||||
@RunWith(Cucumber::class)
|
||||
@CucumberOptions(plugin = ["pretty"])
|
||||
class RunCucumberTest: En, KoinTest {
|
||||
private val migrations: Migrations by inject()
|
||||
init {
|
||||
Before(-1) { scenario: Scenario ->
|
||||
config.database = "test"
|
||||
config.username = "test"
|
||||
config.password = "test"
|
||||
contextCurrent = Context(TestApplicationEngine(createTestEnvironment()) {}, scenario)
|
||||
|
||||
migrations.run()
|
||||
}
|
||||
|
||||
After { scenario: Scenario ->
|
||||
migrations.forceAllDown()
|
||||
contextCurrent.engine.stop(0L, 0L, TimeUnit.MILLISECONDS)
|
||||
}
|
||||
}
|
||||
}
|
||||
44
src/test/kotlin/feature/Context.kt
Normal file
44
src/test/kotlin/feature/Context.kt
Normal file
@@ -0,0 +1,44 @@
|
||||
package feature
|
||||
|
||||
import cucumber.api.Scenario
|
||||
import fr.dcproject.module
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.server.testing.TestApplicationCall
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import io.ktor.server.testing.TestApplicationRequest
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@KtorExperimentalAPI
|
||||
class Context(
|
||||
val engine: TestApplicationEngine,
|
||||
val scenario: Scenario
|
||||
) {
|
||||
companion object {
|
||||
lateinit var current: Context
|
||||
}
|
||||
|
||||
init {
|
||||
engine.start()
|
||||
val moduleFunction: Application.() -> Unit = { module() }
|
||||
val test: TestApplicationEngine.() -> Unit = {
|
||||
moduleFunction(application)
|
||||
}
|
||||
engine.test()
|
||||
}
|
||||
|
||||
var call: TestApplicationCall? = null
|
||||
|
||||
private val requestContextConfigurations: MutableList<TestApplicationRequest.() -> Unit> = mutableListOf()
|
||||
fun setupRequest(testApplicationRequest: TestApplicationRequest) {
|
||||
requestContextConfigurations.forEach {
|
||||
it(testApplicationRequest)
|
||||
}
|
||||
}
|
||||
fun setupNextRequests(requestContextConfiguration: TestApplicationRequest.() -> Unit) = requestContextConfigurations.add(requestContextConfiguration)
|
||||
}
|
||||
|
||||
fun TestApplicationRequest.applyConfigurations() {
|
||||
Context.current.setupRequest(this)
|
||||
}
|
||||
123
src/test/kotlin/feature/Request.kt
Normal file
123
src/test/kotlin/feature/Request.kt
Normal file
@@ -0,0 +1,123 @@
|
||||
package feature
|
||||
|
||||
import com.google.gson.Gson
|
||||
import cucumber.api.java8.En
|
||||
import fr.dcproject.entity.Citizen
|
||||
import fr.dcproject.entity.User
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.migration.Migrations
|
||||
import io.cucumber.datatable.DataTable
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpMethod
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.testing.TestApplicationCall
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import io.ktor.server.testing.setBody
|
||||
import org.joda.time.DateTime
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.inject
|
||||
import org.opentest4j.AssertionFailedError
|
||||
import java.util.*
|
||||
import kotlin.test.asserter
|
||||
import feature.Context.Companion.current as currentContext
|
||||
|
||||
class Request: En, KoinTest {
|
||||
private val migrations: Migrations by inject()
|
||||
private val requester: Requester by inject()
|
||||
init {
|
||||
// Before { scenario: Scenario ->
|
||||
// migrations.run()
|
||||
// }
|
||||
//
|
||||
// After { scenario: Scenario ->
|
||||
// migrations.forceAllDown()
|
||||
// }
|
||||
|
||||
When("I have citizen:") { body: DataTable ->
|
||||
val user = User(username = "jaque", plainPassword = "azerty")
|
||||
val data = body.asMap<String, String>(String::class.java, String::class.java)
|
||||
val citizen = Citizen(
|
||||
id = UUID.fromString(data["id"]),
|
||||
name = Citizen.Name(data["firstName"], data["lastName"]),
|
||||
birthday = DateTime.now(),
|
||||
user = user
|
||||
)
|
||||
val test: TestApplicationEngine.() -> Unit = {
|
||||
requester
|
||||
.getFunction("insert_user")
|
||||
.selectOne(user)
|
||||
requester
|
||||
.getFunction("upsert_citizen")
|
||||
.selectOne(citizen)
|
||||
}
|
||||
|
||||
currentContext.engine.test()
|
||||
}
|
||||
|
||||
When("I send a {string} request to {string} with body:") { method: String, uri: String, body: String ->
|
||||
val test: TestApplicationEngine.() -> Unit = {
|
||||
currentContext.call = handleRequest {
|
||||
applyConfigurations()
|
||||
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
|
||||
this.method = HttpMethod.parse(method)
|
||||
this.uri = uri
|
||||
setBody(body)
|
||||
}
|
||||
}
|
||||
|
||||
currentContext.engine.test()
|
||||
}
|
||||
|
||||
When("I send a {string} request to {string}") { method: String, uri: String ->
|
||||
val test: TestApplicationEngine.() -> Unit = {
|
||||
currentContext.call = handleRequest {
|
||||
applyConfigurations()
|
||||
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
|
||||
this.method = HttpMethod.parse(method.toUpperCase())
|
||||
this.uri = uri
|
||||
}
|
||||
}
|
||||
|
||||
currentContext.engine.test()
|
||||
}
|
||||
|
||||
Then("the response status code should be {int}") { statusCode: Int ->
|
||||
val call: TestApplicationCall = currentContext.call ?: throw AssertionFailedError("No call", statusCode, null)
|
||||
with(call) {
|
||||
assertEquals(HttpStatusCode.fromValue(statusCode), response.status(), response.content)
|
||||
}
|
||||
}
|
||||
|
||||
And("the response should contain:") { expected: DataTable ->
|
||||
val call: TestApplicationCall = currentContext.call ?: throw AssertionFailedError("No call")
|
||||
val p = call.response
|
||||
val response = Gson().fromJson<List<Map<String, String>>>(p.content, List::class.java)
|
||||
|
||||
expected.asMap<String, String>(String::class.java, String::class.java).forEach { (key, value) ->
|
||||
response.forEach {
|
||||
if (it.containsKey(key)) {
|
||||
assertEquals(it[key], value)
|
||||
return@And
|
||||
}
|
||||
}
|
||||
asserter.fail("The response not contain $key field")
|
||||
}
|
||||
}
|
||||
|
||||
And("the response should contain object:") { expected: DataTable ->
|
||||
val call: TestApplicationCall = currentContext.call ?: throw AssertionFailedError("No call")
|
||||
val p = call.response
|
||||
val response = Gson().fromJson<Map<String, String>>(p.content, Map::class.java)
|
||||
|
||||
expected.asMap<String, String>(String::class.java, String::class.java).forEach { (key, value) ->
|
||||
if (response.containsKey(key)) {
|
||||
assertEquals(value, response[key])
|
||||
return@And
|
||||
}
|
||||
asserter.fail("The response not contain $key field")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user