Add Integration test for vote routes
This commit is contained in:
@@ -5,6 +5,7 @@ import fr.dcproject.common.utils.toUUID
|
||||
import fr.dcproject.component.article.ArticleForUpdate
|
||||
import fr.dcproject.component.article.ArticleForView
|
||||
import fr.dcproject.component.article.ArticleRepository
|
||||
import fr.dcproject.component.citizen.CitizenI.Name
|
||||
import fr.dcproject.component.workgroup.WorkgroupRef
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import org.koin.core.context.GlobalContext
|
||||
@@ -13,9 +14,9 @@ import java.util.UUID
|
||||
fun TestApplicationEngine.`Given I have article`(
|
||||
id: String? = null,
|
||||
workgroup: WorkgroupRef? = null,
|
||||
createdByUsername: String? = null
|
||||
createdBy: Name? = null
|
||||
) {
|
||||
createArticle(id?.toUUID(), workgroup, createdByUsername)
|
||||
createArticle(id?.toUUID(), workgroup, createdBy)
|
||||
}
|
||||
|
||||
fun TestApplicationEngine.`Given I have articles`(
|
||||
@@ -34,18 +35,18 @@ fun TestApplicationEngine.`Given I have article created by workgroup`(
|
||||
fun createArticle(
|
||||
id: UUID? = null,
|
||||
workgroup: WorkgroupRef? = null,
|
||||
createdByUsername: String? = null
|
||||
createdBy: Name? = null
|
||||
): ArticleForView {
|
||||
val articleRepository: ArticleRepository by lazy { GlobalContext.get().koin.get() }
|
||||
|
||||
val createdBy = createCitizen(createdByUsername)
|
||||
val citizen = createCitizen(createdBy)
|
||||
|
||||
val article = ArticleForUpdate(
|
||||
id = id ?: UUID.randomUUID(),
|
||||
title = LoremIpsum().getTitle(3),
|
||||
content = LoremIpsum().getParagraphs(1, 2),
|
||||
description = LoremIpsum().getParagraphs(1, 2),
|
||||
createdBy = createdBy,
|
||||
createdBy = citizen,
|
||||
workgroup = workgroup,
|
||||
versionId = UUID.randomUUID()
|
||||
)
|
||||
|
||||
@@ -36,17 +36,15 @@ fun TestApplicationEngine.`Given I have citizen`(
|
||||
return repo.insertWithUser(citizen)?.also { callback(it) }
|
||||
}
|
||||
|
||||
fun createCitizen(createdByUsername: String? = null): Citizen {
|
||||
fun createCitizen(createdBy: CitizenI.Name? = null): Citizen {
|
||||
val citizenRepository: CitizenRepository by lazy { GlobalContext.get().koin.get() }
|
||||
|
||||
val username = (createdByUsername ?: "username" + UUID.randomUUID().toString())
|
||||
.toLowerCase().replace(' ', '-')
|
||||
|
||||
return if (createdByUsername != null) {
|
||||
citizenRepository.findByUsername(createdByUsername) ?: error("Citizen not exist")
|
||||
return if (createdBy != null) {
|
||||
citizenRepository.findByName(createdBy) ?: error("Citizen not exist")
|
||||
} else {
|
||||
val first = "firstName" + UUID.randomUUID().toString()
|
||||
val last = "lastName" + UUID.randomUUID().toString()
|
||||
val username = ("username" + UUID.randomUUID().toString())
|
||||
CitizenForCreate(
|
||||
birthday = DateTime.now(),
|
||||
name = CitizenI.Name(
|
||||
|
||||
@@ -5,6 +5,7 @@ import fr.dcproject.common.entity.TargetI
|
||||
import fr.dcproject.common.utils.toUUID
|
||||
import fr.dcproject.component.article.ArticleRef
|
||||
import fr.dcproject.component.article.ArticleRepository
|
||||
import fr.dcproject.component.citizen.CitizenI.Name
|
||||
import fr.dcproject.component.comment.generic.CommentForUpdate
|
||||
import fr.dcproject.component.comment.generic.CommentRepository
|
||||
import fr.dcproject.component.constitution.ConstitutionRef
|
||||
@@ -16,10 +17,10 @@ import java.util.UUID
|
||||
fun TestApplicationEngine.`Given I have comment on article`(
|
||||
id: String? = null,
|
||||
article: String? = null,
|
||||
createdByUsername: String? = null,
|
||||
createdBy: Name? = null,
|
||||
content: String? = null,
|
||||
) {
|
||||
createComment(id?.toUUID(), ArticleRef(article?.toUUID()), createdByUsername, content)
|
||||
createComment(id?.toUUID(), ArticleRef(article?.toUUID()), createdBy, content)
|
||||
}
|
||||
|
||||
fun TestApplicationEngine.`Given I have comments on article`(
|
||||
@@ -34,14 +35,14 @@ fun TestApplicationEngine.`Given I have comments on article`(
|
||||
fun createComment(
|
||||
id: UUID? = null,
|
||||
article: ArticleRef? = null,
|
||||
createdByUsername: String? = null,
|
||||
createdBy: Name? = null,
|
||||
content: String? = null
|
||||
) {
|
||||
val articleRepository: ArticleRepository by lazy { GlobalContext.get().koin.get() }
|
||||
createCommentOnTarget(
|
||||
id,
|
||||
article?.id?.let { articleRepository.findById(article.id) } ?: createArticle(article?.id),
|
||||
createdByUsername,
|
||||
createdBy,
|
||||
content
|
||||
)
|
||||
}
|
||||
@@ -49,23 +50,23 @@ fun createComment(
|
||||
fun TestApplicationEngine.`Given I have comment on constitution`(
|
||||
id: String? = null,
|
||||
constitution: String? = null,
|
||||
createdByUsername: String? = null,
|
||||
createdBy: Name? = null,
|
||||
content: String? = null,
|
||||
) {
|
||||
createComment(id?.toUUID(), ConstitutionRef(constitution?.toUUID()), createdByUsername, content)
|
||||
createComment(id?.toUUID(), ConstitutionRef(constitution?.toUUID()), createdBy, content)
|
||||
}
|
||||
|
||||
fun createComment(
|
||||
id: UUID? = null,
|
||||
constitution: ConstitutionRef? = null,
|
||||
createdByUsername: String? = null,
|
||||
createdBy: Name? = null,
|
||||
content: String? = null
|
||||
) {
|
||||
val constitutionRepository: ConstitutionRepository by lazy { GlobalContext.get().koin.get() }
|
||||
createCommentOnTarget(
|
||||
id,
|
||||
constitution?.id?.let { constitutionRepository.findById(constitution.id) } ?: createConstitution(constitution?.id),
|
||||
createdByUsername,
|
||||
createdBy,
|
||||
content
|
||||
)
|
||||
}
|
||||
@@ -73,11 +74,11 @@ fun createComment(
|
||||
fun createCommentOnTarget(
|
||||
id: UUID? = null,
|
||||
target: TargetI,
|
||||
createdByUsername: String? = null,
|
||||
createdBy: Name? = null,
|
||||
content: String? = null
|
||||
) {
|
||||
val commentRepository: CommentRepository by lazy { GlobalContext.get().koin.get() }
|
||||
val createdBy = createCitizen(createdByUsername)
|
||||
val createdBy = createCitizen(createdBy)
|
||||
val comment = CommentForUpdate(
|
||||
id = id ?: UUID.randomUUID(),
|
||||
createdBy = createdBy,
|
||||
|
||||
@@ -3,6 +3,7 @@ package integration.steps.given
|
||||
import com.thedeanda.lorem.LoremIpsum
|
||||
import fr.dcproject.common.utils.toUUID
|
||||
import fr.dcproject.component.article.ArticleRef
|
||||
import fr.dcproject.component.citizen.CitizenI.Name
|
||||
import fr.dcproject.component.citizen.CitizenWithUserI
|
||||
import fr.dcproject.component.constitution.Constitution
|
||||
import fr.dcproject.component.constitution.ConstitutionRepository
|
||||
@@ -15,9 +16,9 @@ import java.util.UUID
|
||||
fun TestApplicationEngine.`Given I have constitution`(
|
||||
id: String? = null,
|
||||
titles: List<TitleSimple<ArticleRef>>? = null,
|
||||
createdByUsername: String? = null
|
||||
createdBy: Name? = null
|
||||
) {
|
||||
createConstitution(id?.toUUID(), titles, createdByUsername)
|
||||
createConstitution(id?.toUUID(), titles, createdBy)
|
||||
}
|
||||
|
||||
fun TestApplicationEngine.`Given I have constitutions`(
|
||||
@@ -41,11 +42,11 @@ fun createTitle(): TitleSimple<ArticleRef> {
|
||||
fun createConstitution(
|
||||
id: UUID? = null,
|
||||
titles: List<TitleSimple<ArticleRef>>? = null,
|
||||
createdByUsername: String? = null
|
||||
createdBy: Name? = null
|
||||
): Constitution {
|
||||
val constitutionRepository: ConstitutionRepository by lazy { GlobalContext.get().koin.get() }
|
||||
|
||||
val createdBy: CitizenWithUserI = createCitizen(createdByUsername)
|
||||
val createdBy: CitizenWithUserI = createCitizen(createdBy)
|
||||
|
||||
val constitution = ConstitutionSimple(
|
||||
id = id ?: UUID.randomUUID(),
|
||||
|
||||
46
src/test/kotlin/integration/steps/given/Vote.kt
Normal file
46
src/test/kotlin/integration/steps/given/Vote.kt
Normal file
@@ -0,0 +1,46 @@
|
||||
package integration.steps.given
|
||||
|
||||
import fr.dcproject.common.utils.toUUID
|
||||
import fr.dcproject.component.article.ArticleRepository
|
||||
import fr.dcproject.component.citizen.CitizenI.Name
|
||||
import fr.dcproject.component.citizen.CitizenRepository
|
||||
import fr.dcproject.component.vote.VoteArticleRepository
|
||||
import fr.dcproject.component.vote.entity.VoteAggregation
|
||||
import fr.dcproject.component.vote.entity.VoteForUpdate
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import org.koin.core.context.GlobalContext
|
||||
import java.util.UUID
|
||||
|
||||
fun TestApplicationEngine.`Given I have vote +1 on article`(
|
||||
article: String,
|
||||
name: Name,
|
||||
id: String? = null,
|
||||
) {
|
||||
createVote(article, name, 1, id?.toUUID())
|
||||
}
|
||||
|
||||
fun TestApplicationEngine.`Given I have vote -1 on article`(
|
||||
article: String,
|
||||
name: Name,
|
||||
id: String? = null,
|
||||
) {
|
||||
createVote(article, name, -1, id?.toUUID())
|
||||
}
|
||||
|
||||
fun createVote(
|
||||
article: String,
|
||||
name: Name,
|
||||
note: Int,
|
||||
id: UUID? = null,
|
||||
): VoteAggregation {
|
||||
val voteArticleRepository: VoteArticleRepository by lazy { GlobalContext.get().koin.get() }
|
||||
val articleRepository: ArticleRepository by lazy { GlobalContext.get().koin.get() }
|
||||
val citizenRepository: CitizenRepository by lazy { GlobalContext.get().koin.get() }
|
||||
val vote = VoteForUpdate(
|
||||
id = id ?: UUID.randomUUID(),
|
||||
note = note,
|
||||
target = articleRepository.findById(article.toUUID()) ?: error("Article not exist"),
|
||||
createdBy = citizenRepository.findByName(name) ?: error("Citizen not exist")
|
||||
)
|
||||
return voteArticleRepository.vote(vote)
|
||||
}
|
||||
Reference in New Issue
Block a user