Continue to implement opinion
improve target reference Improve Tests for Opinion fix SQL:upsert_opinion
This commit is contained in:
@@ -7,6 +7,7 @@ import fr.dcproject.module
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.server.testing.withTestApplication
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
import org.junit.jupiter.api.Tag
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import org.koin.test.AutoCloseKoinTest
|
||||
@@ -18,6 +19,7 @@ import org.koin.test.get
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class MailerTest : KoinTest, AutoCloseKoinTest() {
|
||||
@Test
|
||||
@Tag("online")
|
||||
fun `can be send an email`() {
|
||||
withTestApplication({ module(Env.TEST) }) {
|
||||
get<Mailer>().sendEmail {
|
||||
|
||||
@@ -6,8 +6,8 @@ import fr.dcproject.utils.LoggerDelegate
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.migration.Migrations
|
||||
import io.cucumber.core.api.Scenario
|
||||
import io.cucumber.java8.En
|
||||
import io.cucumber.java8.Scenario
|
||||
import io.cucumber.junit.Cucumber
|
||||
import io.cucumber.junit.CucumberOptions
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
|
||||
@@ -2,6 +2,8 @@ package feature
|
||||
|
||||
import fr.dcproject.entity.*
|
||||
import fr.dcproject.repository.CommentArticle
|
||||
import fr.dcproject.utils.toUUID
|
||||
import io.cucumber.datatable.DataTable
|
||||
import io.cucumber.java8.En
|
||||
import org.joda.time.DateTime
|
||||
import org.koin.test.KoinTest
|
||||
@@ -16,6 +18,9 @@ import fr.dcproject.repository.Citizen as CitizenRepository
|
||||
|
||||
class ArticleSteps : En, KoinTest {
|
||||
init {
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
Given("I have article with id {string}") { id: String ->
|
||||
var citizen = Citizen(
|
||||
name = CitizenI.Name("John", "Doe"),
|
||||
@@ -40,6 +45,23 @@ class ArticleSteps : En, KoinTest {
|
||||
get<ArticleRepository>().upsert(article)
|
||||
}
|
||||
|
||||
Given("I have article") { extraData: DataTable ->
|
||||
extraData.asMap<String, String>(String::class.java, String::class.java).let { params ->
|
||||
val username = params["createdBy"]?.toLowerCase()?.replace(' ', '-') ?: error("You must provide the 'createdBy' parameter")
|
||||
val citizen = get<CitizenRepository>().findByUsername(username) ?: error("Citizen not exist")
|
||||
val id = params["id"]?.toUUID() ?: UUID.randomUUID()
|
||||
val article = ArticleEntity(
|
||||
id = id,
|
||||
title = "hello",
|
||||
content = "bla bla bla",
|
||||
description = "A super article",
|
||||
createdBy = citizen
|
||||
)
|
||||
get<ArticleRepository>().upsert(article)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Given("I have article with id {string} created by {string}") { id: String, username: String ->
|
||||
val citizen = get<CitizenRepository>().findByUsername(username)!!
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import fr.dcproject.JwtConfig
|
||||
import fr.dcproject.entity.Citizen
|
||||
import fr.dcproject.entity.CitizenI
|
||||
import fr.dcproject.entity.User
|
||||
import fr.dcproject.utils.toUUID
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import io.cucumber.datatable.DataTable
|
||||
import io.cucumber.java8.En
|
||||
@@ -70,6 +71,38 @@ class KtorServerAuthSteps : En, KoinTest {
|
||||
}
|
||||
}
|
||||
|
||||
Given("I have citizen {word} {word}") { firstName: String, lastName: String, extraInfo: DataTable? ->
|
||||
val id: UUID = extraInfo?.asMap<String, String>(String::class.java, String::class.java)?.get("id")?.toUUID() ?: UUID.randomUUID()
|
||||
|
||||
val user = User(
|
||||
id = id,
|
||||
username = "$firstName-$lastName".toLowerCase(),
|
||||
plainPassword = "azerty"
|
||||
)
|
||||
val citizen = Citizen(
|
||||
id = id,
|
||||
name = CitizenI.Name(firstName, lastName),
|
||||
email = ("$firstName-$lastName".toLowerCase()) + "@dc-project.fr",
|
||||
birthday = DateTime.now(),
|
||||
user = user
|
||||
)
|
||||
|
||||
get<CitizenRepository>().insertWithUser(citizen)
|
||||
}
|
||||
|
||||
Given("I am authenticated as {word} {word}") { firstName: String, lastName: String ->
|
||||
val username = "$firstName-$lastName".toLowerCase()
|
||||
val citizen = get<CitizenRepository>().findByUsername(username) ?: error("Cititzen not exist with username $username")
|
||||
val jwtAsString: String = JWT.create()
|
||||
.withIssuer("dc-project.fr")
|
||||
.withClaim("id", citizen.id.toString())
|
||||
.sign(JwtConfig.algorithm)
|
||||
|
||||
KtorServerContext.defaultServer.addPreRequestSetup {
|
||||
addHeader(HttpHeaders.Authorization, "Bearer $jwtAsString")
|
||||
}
|
||||
}
|
||||
|
||||
Given("I have citizen {word} {word} with id {string}") { firstName: String, lastName: String, id: String ->
|
||||
val user = User(
|
||||
id = UUID.randomUUID(),
|
||||
|
||||
@@ -41,20 +41,20 @@ class KtorServerRestSteps : En {
|
||||
}
|
||||
}
|
||||
|
||||
private fun findJsonElement(node: String): JsonElement {
|
||||
private fun findJsonElement(path: String): JsonElement {
|
||||
var jsonElement: JsonElement = responseJsonElement
|
||||
val elements = node.split(".")
|
||||
|
||||
elements.forEach {
|
||||
val asArrayIndex = """\d+""".toRegex().find(it)
|
||||
|
||||
jsonElement = if (asArrayIndex != null) {
|
||||
val index = asArrayIndex.groups.first()!!
|
||||
jsonElement.jsonArray.get(index.value.toInt())
|
||||
} else {
|
||||
jsonElement.jsonObject.get(it) ?: throw AssertionError("\"$node\" element not found on json response")
|
||||
path
|
||||
.split("].", "]", "[", ".")
|
||||
.filter { it.trim().isNotBlank() }
|
||||
.map { it.trim() }
|
||||
.forEach {
|
||||
jsonElement = if (jsonElement is JsonArray) {
|
||||
jsonElement.jsonArray[it.toInt()]
|
||||
} else {
|
||||
jsonElement.jsonObject[it]
|
||||
} ?: throw AssertionError("\"$path\" element not found on json response")
|
||||
}
|
||||
}
|
||||
|
||||
return jsonElement
|
||||
}
|
||||
|
||||
40
src/test/kotlin/feature/OpinionSteps.kt
Normal file
40
src/test/kotlin/feature/OpinionSteps.kt
Normal file
@@ -0,0 +1,40 @@
|
||||
package feature
|
||||
|
||||
import fr.dcproject.entity.OpinionArticle
|
||||
import fr.dcproject.utils.toUUID
|
||||
import io.cucumber.datatable.DataTable
|
||||
import io.cucumber.java8.En
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.get
|
||||
import fr.dcproject.repository.Article as ArticleRepository
|
||||
import fr.dcproject.repository.Citizen as CitizenRepository
|
||||
import fr.dcproject.repository.OpinionArticle as OpinionRepository
|
||||
import fr.dcproject.repository.OpinionChoice as OpinionChoiceRepository
|
||||
|
||||
|
||||
class OpinionSteps : En, KoinTest {
|
||||
init {
|
||||
Given("I have the opinion {string} on article {string} created by {string}:") { opinionChoice: String, article: String, citizen: String, extraInfo: DataTable ->
|
||||
extraInfo.asMap<String, String>(String::class.java, String::class.java).let {
|
||||
val opinion = OpinionArticle(
|
||||
choice = get<OpinionChoiceRepository>().findOpinionsChoiceByName(opinionChoice) ?: error("Opinion Choice not exist"),
|
||||
target = get<ArticleRepository>().findById(article.toUUID()) ?: error("Article not exist"),
|
||||
createdBy = get<CitizenRepository>().findById(citizen.toUUID()) ?: error("Citizen not exist")
|
||||
)
|
||||
get<OpinionRepository>().opinion(opinion)
|
||||
}
|
||||
}
|
||||
|
||||
Given("I have an opinion") { extraInfo: DataTable ->
|
||||
extraInfo.asMap<String, String>(String::class.java, String::class.java)?.let { params ->
|
||||
val username = params["createdBy"]?.toLowerCase()?.replace(' ', '-') ?: error("You must provide the 'createdBy' parameter")
|
||||
val opinion = OpinionArticle(
|
||||
choice = params["opinion"]?.let { get<OpinionChoiceRepository>().findOpinionsChoiceByName(it) ?: error("Opinion Choice not exist")} ?: error("You must provide the 'opinion' parameter"),
|
||||
target = params["article"]?.let { get<ArticleRepository>().findById(it.toUUID()) ?: error("Article not exist")} ?: error("You must provide the 'article' parameter"),
|
||||
createdBy = get<CitizenRepository>().findByUsername(username) ?: error("Citizen not exist")
|
||||
)
|
||||
get<OpinionRepository>().opinion(opinion)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user