Add Integration test for comment

This commit is contained in:
2021-02-17 23:02:38 +01:00
parent 55aa512aa5
commit 0ecf0c205f
17 changed files with 426 additions and 18 deletions

View File

@@ -1,7 +1,9 @@
package integration.steps.given
import com.thedeanda.lorem.LoremIpsum
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.workgroup.WorkgroupRef
import io.ktor.server.testing.TestApplicationEngine
@@ -33,19 +35,19 @@ fun createArticle(
id: UUID? = null,
workgroup: WorkgroupRef? = null,
createdByUsername: String? = null
) {
val articleRepository: ArticleRepository by lazy<ArticleRepository> { GlobalContext.get().koin.get() }
): ArticleForView {
val articleRepository: ArticleRepository by lazy { GlobalContext.get().koin.get() }
val createdBy = createCitizen(createdByUsername)
val article = ArticleForUpdate(
id = id ?: UUID.randomUUID(),
title = "hello",
content = "bla bla bla",
description = "A super article",
title = LoremIpsum().getTitle(3),
content = LoremIpsum().getParagraphs(1, 2),
description = LoremIpsum().getParagraphs(1, 2),
createdBy = createdBy,
workgroup = workgroup,
versionId = UUID.randomUUID()
)
articleRepository.upsert(article)
return articleRepository.upsert(article) ?: error("Cannot create article")
}

View File

@@ -14,7 +14,7 @@ fun TestApplicationRequest.`authenticated as`(
): Citizen {
val username = "$firstName-$lastName".toLowerCase()
val repo: CitizenRepository by lazy<CitizenRepository> { GlobalContext.get().koin.get() }
val citizen = repo.findByUsername(username) ?: error("Cititzen not exist with username $username")
val citizen = repo.findByUsername(username) ?: error("Citizen not exist with username $username")
val jwtAsString: String = JWT.create()
.withIssuer("dc-project.fr")
.withClaim("id", citizen.user.id.toString())

View File

@@ -17,7 +17,7 @@ fun TestApplicationEngine.`Given I have citizen`(
email: String = ("$firstName-$lastName".toLowerCase()) + "@dc-project.fr",
id: String = UUID.randomUUID().toString()
): Citizen? {
val repo: CitizenRepository by lazy<CitizenRepository> { GlobalContext.get().koin.get() }
val repo: CitizenRepository by lazy { GlobalContext.get().koin.get() }
val user = UserForCreate(
id = id.toUUID(),
@@ -36,7 +36,7 @@ fun TestApplicationEngine.`Given I have citizen`(
}
fun createCitizen(createdByUsername: String? = null): Citizen {
val citizenRepository: CitizenRepository by lazy<CitizenRepository> { GlobalContext.get().koin.get() }
val citizenRepository: CitizenRepository by lazy { GlobalContext.get().koin.get() }
val username = (createdByUsername ?: "username" + UUID.randomUUID().toString())
.toLowerCase().replace(' ', '-')

View File

@@ -0,0 +1,88 @@
package integration.steps.given
import com.thedeanda.lorem.LoremIpsum
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.comment.generic.CommentForUpdate
import fr.dcproject.component.comment.generic.CommentRepository
import fr.dcproject.component.constitution.ConstitutionRef
import fr.dcproject.component.constitution.ConstitutionRepository
import io.ktor.server.testing.TestApplicationEngine
import org.koin.core.context.GlobalContext
import java.util.UUID
fun TestApplicationEngine.`Given I have comment on article`(
id: String? = null,
article: String? = null,
createdByUsername: String? = null,
content: String? = null,
) {
createComment(id?.toUUID(), ArticleRef(article?.toUUID()), createdByUsername, content)
}
fun TestApplicationEngine.`Given I have comments on article`(
numbers: Int,
article: String? = null,
) {
repeat(numbers) {
createComment(article = ArticleRef(article?.toUUID()))
}
}
fun createComment(
id: UUID? = null,
article: ArticleRef? = null,
createdByUsername: String? = 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,
content
)
}
fun TestApplicationEngine.`Given I have comment on constitution`(
id: String? = null,
constitution: String? = null,
createdByUsername: String? = null,
content: String? = null,
) {
createComment(id?.toUUID(), ConstitutionRef(constitution?.toUUID()), createdByUsername, content)
}
fun createComment(
id: UUID? = null,
constitution: ConstitutionRef? = null,
createdByUsername: String? = 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,
content
)
}
fun createCommentOnTarget(
id: UUID? = null,
target: TargetI,
createdByUsername: String? = null,
content: String? = null
) {
val commentRepository: CommentRepository by lazy { GlobalContext.get().koin.get() }
val createdBy = createCitizen(createdByUsername)
val comment = CommentForUpdate(
id = id ?: UUID.randomUUID(),
createdBy = createdBy,
target = target,
content = content ?: LoremIpsum().getParagraphs(1, 3)
)
commentRepository.comment(comment)
}

View File

@@ -0,0 +1,58 @@
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.CitizenWithUserI
import fr.dcproject.component.constitution.Constitution
import fr.dcproject.component.constitution.ConstitutionRepository
import fr.dcproject.component.constitution.ConstitutionSimple
import fr.dcproject.component.constitution.ConstitutionSimple.TitleSimple
import io.ktor.server.testing.TestApplicationEngine
import org.koin.core.context.GlobalContext
import java.util.UUID
fun TestApplicationEngine.`Given I have constitution`(
id: String? = null,
titles: List<TitleSimple<ArticleRef>>? = null,
createdByUsername: String? = null
) {
createConstitution(id?.toUUID(), titles, createdByUsername)
}
fun TestApplicationEngine.`Given I have constitutions`(
numbers: Int,
) {
repeat(numbers) {
createConstitution()
}
}
fun createTitles(nbr: Int): List<TitleSimple<ArticleRef>> = sequence {
repeat(nbr) {
yield(createTitle())
}
}.toList()
fun createTitle(): TitleSimple<ArticleRef> {
return TitleSimple(name = LoremIpsum().getTitle(3))
}
fun createConstitution(
id: UUID? = null,
titles: List<TitleSimple<ArticleRef>>? = null,
createdByUsername: String? = null
): Constitution {
val constitutionRepository: ConstitutionRepository by lazy { GlobalContext.get().koin.get() }
val createdBy: CitizenWithUserI = createCitizen(createdByUsername)
val constitution = ConstitutionSimple(
id = id ?: UUID.randomUUID(),
title = LoremIpsum().getTitle(3),
titles = titles ?: createTitles(5),
createdBy = createdBy,
versionId = UUID.randomUUID()
)
return constitutionRepository.upsert(constitution) ?: error("Cannot create constitution")
}