Refactoring Comment Tests

This commit is contained in:
2020-03-16 15:02:43 +01:00
parent 561905f7ab
commit 74503c9d3a
4 changed files with 76 additions and 34 deletions

View File

@@ -9,7 +9,6 @@ import org.joda.time.DateTime
import org.koin.test.KoinTest
import org.koin.test.get
import java.util.*
import java.util.concurrent.CompletionException
import fr.dcproject.entity.Article as ArticleEntity
import fr.dcproject.entity.Comment as CommentEntity
import fr.dcproject.entity.User as UserEntity
@@ -28,36 +27,11 @@ class ArticleSteps : En, KoinTest {
createArticle(extraData)
}
Given("I have comment {string} on article {string}") { commentId: String, articleId: String ->
var citizen = Citizen(
name = CitizenI.Name("John", "Doe"),
email = "john.doe@gmail.com",
birthday = DateTime.now(),
user = UserEntity(username = "john-doe", plainPassword = "azerty")
)
try {
get<CitizenRepository>().insertWithUser(citizen)
} catch (e: CompletionException) {
citizen = get<CitizenRepository>().findByUsername("john-doe")!!
}
val article = ArticleEntity(
id = UUID.fromString(articleId),
title = "hello",
content = "bla bla bla",
description = "A super article",
createdBy = citizen
)
get<ArticleRepository>().upsert(article)
val comment: CommentEntity<ArticleRef> = CommentEntity(
id = UUID.fromString(commentId),
createdBy = citizen,
target = article,
content = "hello"
)
get<CommentArticle>().comment(comment)
Given("I have comment created by {word} {word} on article {string}:") { firstName: String, lastName: String, articleId: String, extraData: DataTable? ->
commentArticle(articleId, firstName, lastName, extraData)
}
Given("I have comment created by {word} {word} on article {string}") { firstName: String, lastName: String, articleId: String ->
commentArticle(articleId, firstName, lastName)
}
}
@@ -94,4 +68,20 @@ class ArticleSteps : En, KoinTest {
)
get<ArticleRepository>().upsert(article)
}
private fun commentArticle(articleId: String, firstName: String, lastName: String, extraData: DataTable? = null) {
val params = extraData?.asMap<String, String>(String::class.java, String::class.java)
val article = get<ArticleRepository>().findById(UUID.fromString(articleId)) ?: error("Article not exist")
val citizen = get<CitizenRepository>().findByUsername(("$firstName-$lastName".toLowerCase()).toLowerCase().replace(' ', '-')) ?: error("Citizen not exist")
val comment: CommentEntity<ArticleRef> = CommentEntity(
id = params?.get("commentId")?.let { UUID.fromString(it) } ?: UUID.randomUUID(),
createdBy = citizen,
target = article,
content = params?.get("content") ?: "hello"
)
get<CommentArticle>().comment(comment)
}
}

View File

@@ -1,7 +1,11 @@
@comment
Feature: comment
Scenario: Can comment childrens
Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd"
And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b"
When I send a GET request to "/comments/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/children"
Given I have citizen John Dalton
And I am authenticated as John Dalton
And I have article
| id | 4c948e8f-eada-4e10-8d7d-7192affe1313 |
And I have comment created by John Dalton on article "4c948e8f-eada-4e10-8d7d-7192affe1313"
When I send a GET request to "/comments/4c948e8f-eada-4e10-8d7d-7192affe1313/children"
Then the response status code should be 200