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

@@ -38,6 +38,7 @@ class Citizen(
deletedAt: DateTime? = null
) : CitizenFull,
CitizenBasicI,
CitizenWithUserI,
CitizenRef(id),
CitizenCartI,
EntityCreatedAt by EntityCreatedAtImp(),

View File

@@ -48,7 +48,7 @@ open class ConstitutionSimple<Cr : CitizenWithUserI, T : TitleSimple<*>>(
id: UUID = UUID.randomUUID(),
val title: String,
val anonymous: Boolean = true,
val titles: MutableList<T> = mutableListOf(),
val titles: List<T> = listOf(),
val draft: Boolean = false,
val lastVersion: Boolean = false,
override val createdBy: Cr,
@@ -73,7 +73,7 @@ open class ConstitutionSimple<Cr : CitizenWithUserI, T : TitleSimple<*>>(
) : TitleRef(id)
}
open class ConstitutionRef(id: UUID = UUID.randomUUID()) : ConstitutionS(id) {
open class ConstitutionRef(id: UUID? = null) : ConstitutionS(id ?: UUID.randomUUID()) {
open class TitleRef(
id: UUID = UUID.randomUUID()
) : UuidEntity(id)

View File

@@ -0,0 +1,16 @@
package assert
import kotlin.test.assertTrue
fun <T : Number> T.assertGreaterThan(expected: T, message: String) {
assertTrue(expected.toDouble() > this.toDouble(), message)
}
infix fun <T : Number> T.assertGreaterThan(expected: T) {
assertTrue(expected.toDouble() >= this.toDouble(), "Expected $this greater than $expected")
}
fun <T : Number> T.assertLessThan(expected: T, message: String) {
assertTrue(expected.toDouble() <= this.toDouble(), message)
}
infix fun <T : Number> T.assertLessThan(expected: T) {
assertTrue(expected.toDouble() <= this.toDouble(), "Expected $this less than $expected")
}

View File

@@ -0,0 +1,128 @@
package integration
import integration.steps.`And the response should contain`
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.`when`.`When I send a POST request`
import integration.steps.`when`.`When I send a PUT request`
import integration.steps.and
import integration.steps.given.`Given I have article`
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have comment on article`
import integration.steps.given.`authenticated as`
import io.ktor.http.HttpStatusCode.Companion.Created
import io.ktor.http.HttpStatusCode.Companion.OK
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Tags
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tags(Tag("integration"), Tag("citizen"))
class `Comment articles routes` : BaseTest() {
@Test
fun `I can comment article`() {
withIntegrationApplication {
`Given I have citizen`("Michael", "Faraday")
`Given I have article`(id = "aa16c635-28da-46f0-9a89-934eef88c7ca")
`When I send a POST request`("/articles/aa16c635-28da-46f0-9a89-934eef88c7ca/comments") {
`authenticated as`("Michael", "Faraday")
"""
{
"content": "Hello mister"
}
"""
} `Then the response should be` Created and {
`And the response should not be null`
`And the response should contain`("$.target.id", "aa16c635-28da-46f0-9a89-934eef88c7ca")
`And the response should contain`("$.content", "Hello mister")
}
}
}
/* TODO add more comment on article */
@Test
fun `I can get all comment on article`() {
withIntegrationApplication {
`Given I have citizen`("Enrico", "Fermi")
`Given I have article`(id = "6166c078-ca97-4366-b0aa-2a5cd558c78a")
`Given I have comment on article`(article = "6166c078-ca97-4366-b0aa-2a5cd558c78a", createdByUsername = "enrico-fermi")
`When I send a GET request`("/articles/6166c078-ca97-4366-b0aa-2a5cd558c78a/comments") {
`authenticated as`("Enrico", "Fermi")
} `Then the response should be` OK and {
`And the response should not be null`
`And the response should contain`("$.result[0].target.id", "6166c078-ca97-4366-b0aa-2a5cd558c78a")
}
}
}
/* TODO add votes */
@Test
fun `I can get all comment on article sorted by votes`() {
withIntegrationApplication {
`Given I have citizen`("Pierre", "Curie")
`Given I have article`(id = "5e209f63-57ce-43ca-922a-273b0d62f567")
`Given I have comment on article`(article = "5e209f63-57ce-43ca-922a-273b0d62f567", createdByUsername = "pierre-curie")
`When I send a GET request`("/articles/5e209f63-57ce-43ca-922a-273b0d62f567/comments?sort=votes") {
`authenticated as`("Pierre", "Curie")
} `Then the response should be` OK and {
`And the response should not be null`
`And the response should contain`("$.result[0].votes.up", 0)
}
}
}
@Test
fun `I can get comments on articles of the current citizen`() {
withIntegrationApplication {
`Given I have citizen`("Erwin", "Schrodinger", id = "292a20cc-4a60-489e-9866-a95d38ffaf47")
`Given I have article`(id = "17df7fb9-b388-4e20-ab19-29c29972da01", createdByUsername = "erwin-schrodinger")
`Given I have comment on article`(article = "17df7fb9-b388-4e20-ab19-29c29972da01", createdByUsername = "erwin-schrodinger")
`When I send a GET request`("/citizens/292a20cc-4a60-489e-9866-a95d38ffaf47/comments/articles") {
} `Then the response should be` OK and {
`And the response should not be null`
`And the response should contain`("$.current_page", 1)
`And the response should contain`("$.limit", 50)
`And the response should contain`("$.result[0]created_by.id", "292a20cc-4a60-489e-9866-a95d38ffaf47")
}
}
}
@Test
fun `I can edit comment`() {
withIntegrationApplication {
`Given I have citizen`("Hubert", "Reeves")
`Given I have article`(id = "bb05e4a3-55a1-4088-85e7-8d8c23be29b1")
`Given I have comment on article`(article = "bb05e4a3-55a1-4088-85e7-8d8c23be29b1", createdByUsername = "hubert-reeves", id = "fd30d20f-656c-42c6-8955-f61c04537464")
`When I send a PUT request`("/comments/fd30d20f-656c-42c6-8955-f61c04537464") {
`authenticated as`("Hubert", "Reeves")
"""
Hello boy
"""
} `Then the response should be` OK and {
`And the response should not be null`
`And the response should contain`("$.content", "Hello boy")
}
}
}
@Test
fun `I can get comment by its ID`() {
withIntegrationApplication {
`Given I have citizen`("Alfred", "Kastler")
`Given I have article`(id = "3897465b-19d2-43a0-86ea-1e29dbb11ec9")
`Given I have comment on article`(
article = "3897465b-19d2-43a0-86ea-1e29dbb11ec9",
createdByUsername = "alfred-kastler",
id = "edd296a8-fc7a-4717-a2bb-9f035ceca3c2",
content = "Hello boy"
)
`When I send a GET request`("/comments/edd296a8-fc7a-4717-a2bb-9f035ceca3c2") {
} `Then the response should be` OK and {
`And the response should not be null`
`And the response should contain`("$.content", "Hello boy")
}
}
}
}

View File

@@ -0,0 +1,59 @@
package integration
import integration.steps.`And the response should contain list`
import integration.steps.`And the response should contain`
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.`when`.`When I send a POST request`
import integration.steps.and
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have comment on constitution`
import integration.steps.given.`Given I have constitution`
import integration.steps.given.`authenticated as`
import io.ktor.http.HttpStatusCode.Companion.Created
import io.ktor.http.HttpStatusCode.Companion.OK
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Tags
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tags(Tag("integration"), Tag("citizen"))
class `Comment constitutions routes` : BaseTest() {
@Test
fun `I can comment constitution`() {
withIntegrationApplication {
`Given I have citizen`("Nicolas", "Copernic")
`Given I have constitution`(id = "1707c287-a472-4a62-89f2-9e85030e915c")
`When I send a POST request`("/constitutions/1707c287-a472-4a62-89f2-9e85030e915c/comments") {
`authenticated as`("Nicolas", "Copernic")
"""
{
"content": "Hello mister"
}
"""
} `Then the response should be` Created and {
`And the response should not be null`
}
}
}
@Test
fun `I can get comments on constitutions of the current citizen`() {
withIntegrationApplication {
`Given I have citizen`("Charles", "Darwin", id = "46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5")
`Given I have constitution`(id = "34ddd50a-da00-4a90-a869-08baa2a121be", createdByUsername = "charles-darwin")
`Given I have comment on constitution`(constitution = "34ddd50a-da00-4a90-a869-08baa2a121be", createdByUsername = "charles-darwin")
`When I send a GET request`("/citizens/46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5/comments/constitutions") {
} `Then the response should be` OK and {
`And the response should not be null`
`And the response should contain`("$.current_page", 1)
`And the response should contain`("$.limit", 50)
`And the response should contain`("$.result[0].created_by.id", "46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5")
`And the response should contain`("$.result[0].target.id", "34ddd50a-da00-4a90-a869-08baa2a121be")
`And the response should contain list`("$.result[*]", 1, 1)
}
}
}
}

View File

@@ -0,0 +1,33 @@
package integration
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.and
import integration.steps.given.`Given I have article`
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have comment on article`
import integration.steps.given.`authenticated as`
import io.ktor.http.HttpStatusCode.Companion.OK
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Tags
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tags(Tag("integration"), Tag("citizen"))
class `Comment routes` : BaseTest() {
@Test
fun `I can get comments children`() {
withIntegrationApplication {
`Given I have citizen`("John", "Dalton")
`Given I have article`(id = "4c948e8f-eada-4e10-8d7d-7192affe1313")
`Given I have comment on article`(id = "da22fc55-b0fd-42a5-a317-9583b1bb93c5", article = "4c948e8f-eada-4e10-8d7d-7192affe1313")
`When I send a GET request`("/comments/da22fc55-b0fd-42a5-a317-9583b1bb93c5/children") {
`authenticated as`("John", "Dalton")
} `Then the response should be` OK and {
`And the response should not be null`
}
}
}
}

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")
}

View File

@@ -1,14 +1,19 @@
package integration.steps
import assert.assertGreaterThan
import assert.assertLessThan
import com.jayway.jsonpath.JsonPath
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.TestApplicationCall
import io.ktor.server.testing.TestApplicationResponse
import net.minidev.json.JSONArray
import org.amshove.kluent.`should be equal to`
import org.amshove.kluent.`should be null`
import org.amshove.kluent.`should be`
import org.amshove.kluent.`should not be null`
import org.amshove.kluent.shouldContain
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
fun TestApplicationCall.`Then the response should be`(status: HttpStatusCode? = null, block: TestApplicationResponse.() -> Unit): TestApplicationCall = this.apply {
if (status != null) {
@@ -43,11 +48,26 @@ infix fun Pair<JsonPath, Any>.`whish contains`(expected: Any): Pair<JsonPath, An
second `should be equal to` expected
}
fun TestApplicationResponse.`And the response should contain`(path: String, valueExpected: String) {
assertEquals(valueExpected, JsonPath.read<Any>(content, path)?.toString() ?: throw AssertionError("\"$path -> ${valueExpected}\" element not found on json response"))
fun <T> TestApplicationResponse.`And the response should contain`(path: String, valueExpected: T?): T {
return JsonPath.read<T?>(content, path).also {
assertEquals<T?>(valueExpected, it ?: throw AssertionError("\"$path -> ${valueExpected}\" element not found on json response"))
}
}
fun TestApplicationResponse.`And the response should contain list`(path: String, min: Int? = null, max: Int? = null) {
JsonPath.read<JSONArray?>(content, path).also {
assertNotNull(it)
if (min != null) {
it.size assertGreaterThan min
}
if (max != null) {
it.size assertLessThan max
}
}
}
val TestApplicationResponse.`And the response should not be null` get() = content.`should not be null`()
fun TestApplicationResponse.`And the response should be null`() = content.`should be null`()
infix fun String.`and should contains`(expected: String) = this
.`should not be null`()
.shouldContain(expected)

View File

@@ -8,8 +8,8 @@ import io.ktor.server.testing.TestApplicationEngine
import io.ktor.server.testing.TestApplicationRequest
import io.ktor.server.testing.setBody
public fun TestApplicationEngine.`When I send a GET request`(uri: String? = null, setup: (TestApplicationRequest.() -> Unit)? = null): TestApplicationCall {
val setupOveride: TestApplicationRequest.() -> Unit = {
fun TestApplicationEngine.`When I send a GET request`(uri: String? = null, setup: (TestApplicationRequest.() -> Unit)? = null): TestApplicationCall {
return handleRequest(true) {
method = HttpMethod.Get
if (uri != null) {
this.uri = uri
@@ -17,10 +17,9 @@ public fun TestApplicationEngine.`When I send a GET request`(uri: String? = null
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setup?.let { it() }
}
return handleRequest(true, setupOveride)
}
public fun TestApplicationEngine.`When I send a POST request`(uri: String? = null, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
fun TestApplicationEngine.`When I send a POST request`(uri: String? = null, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
val setupOveride: TestApplicationRequest.() -> Unit = {
method = HttpMethod.Post
if (uri != null) {
@@ -34,7 +33,7 @@ public fun TestApplicationEngine.`When I send a POST request`(uri: String? = nul
return handleRequest(true, setupOveride)
}
public fun TestApplicationEngine.`When I send a PUT request`(uri: String? = null, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
fun TestApplicationEngine.`When I send a PUT request`(uri: String? = null, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
val setupOveride: TestApplicationRequest.() -> Unit = {
method = HttpMethod.Put
if (uri != null) {

View File

@@ -1,4 +1,5 @@
@comment
@disable
Feature: comment
Scenario: Can comment childrens

View File

@@ -1,4 +1,5 @@
@comment
@disable
Feature: comment Article
Scenario: Can comment an article

View File

@@ -1,4 +1,5 @@
@comment
@disable
Feature: comment Constitution
Scenario: Can comment an constitution