diff --git a/src/test/kotlin/RunCucumberTest.kt b/src/test/kotlin/RunCucumberTest.kt index ccb2f1e..521fe75 100644 --- a/src/test/kotlin/RunCucumberTest.kt +++ b/src/test/kotlin/RunCucumberTest.kt @@ -42,7 +42,6 @@ class RunCucumberTest : En, KoinTest { withTestApplication({ module(CUCUMBER) }) { migrations() - fixtures() } unitialized = true } diff --git a/src/test/kotlin/feature/ArticleSteps.kt b/src/test/kotlin/feature/ArticleSteps.kt index 3d086c3..f0b33e0 100644 --- a/src/test/kotlin/feature/ArticleSteps.kt +++ b/src/test/kotlin/feature/ArticleSteps.kt @@ -18,61 +18,14 @@ 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"), - email = "john.doe@gmail.com", - birthday = DateTime.now(), - user = UserEntity(username = "john-doe", plainPassword = "azerty") - ) - - try { - get().insertWithUser(citizen) - } catch (e: CompletionException) { - citizen = get().findByUsername("john-doe")!! - } - - val article = ArticleEntity( - id = UUID.fromString(id), - title = "hello", - content = "bla bla bla", - description = "A super article", - createdBy = citizen - ) - get().upsert(article) - } - - Given("I have article") { extraData: DataTable -> - extraData.asMap(String::class.java, String::class.java).let { params -> - val username = params["createdBy"]?.toLowerCase()?.replace(' ', '-') - ?: error("You must provide the 'createdBy' parameter") - val citizen = get().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().upsert(article) + Given("I have {int} article") { nb: Int -> + repeat(nb) { + createArticle() } } - Given("I have article with id {string} created by {string}") { id: String, username: String -> - val citizen = get().findByUsername(username)!! - - val article = ArticleEntity( - id = UUID.fromString(id), - title = "hello", - content = "bla bla bla", - description = "A super article", - createdBy = citizen - ) - get().upsert(article) + Given("I have article") { extraData: DataTable? -> + createArticle(extraData) } Given("I have comment {string} on article {string}") { commentId: String, articleId: String -> @@ -107,4 +60,38 @@ class ArticleSteps : En, KoinTest { get().comment(comment) } } + + private fun createArticle(extraData: DataTable? = null) { + val params = extraData?.asMap(String::class.java, String::class.java) + val createdByUsername = params?.get("createdBy") + val username = (createdByUsername ?: UUID.randomUUID().toString()) + .toLowerCase().replace(' ', '-') + + val createdBy = if (createdByUsername != null) { + get().findByUsername(username) ?: error("Citizen not exist") + } else { + val first = "firstName"+UUID.randomUUID().toString() + val last = "lastName"+UUID.randomUUID().toString() + Citizen( + birthday = DateTime.now(), + name = CitizenI.Name( + first, + last + ), + email = "$first@fakeemail.com", + user = UserEntity(username = username, plainPassword = "azerty") + ).also { + get().insertWithUser(it) + } + } + + val article = ArticleEntity( + id = params?.get("id")?.toUUID() ?: UUID.randomUUID(), + title = "hello", + content = "bla bla bla", + description = "A super article", + createdBy = createdBy + ) + get().upsert(article) + } } \ No newline at end of file diff --git a/src/test/resources/feature/articles.feature b/src/test/resources/feature/articles.feature index f18fcd0..56174b1 100644 --- a/src/test/resources/feature/articles.feature +++ b/src/test/resources/feature/articles.feature @@ -2,21 +2,27 @@ Feature: articles routes Scenario: The route for get articles must response a 200 + Given I have 3 article When I send a GET request to "/articles" Then the response status code should be 200 Scenario: Can get versions of article by the id - When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/versions" + Given I have article + | id | 13e6091c-8fed-4600-b079-a97a6b7a9800 | + When I send a GET request to "/articles/13e6091c-8fed-4600-b079-a97a6b7a9800/versions" Then the response status code should be 200 Scenario: The route for get one article must response a 200 and return article - When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" + Given I have article + | id | 65cda9f3-8991-4420-8d41-1da9da72c9bb | + When I send a GET request to "/articles/65cda9f3-8991-4420-8d41-1da9da72c9bb" Then the response status code should be 200 And the response should contain object: - | id | 9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b | + | id | 65cda9f3-8991-4420-8d41-1da9da72c9bb | Scenario: The route for create article must response a 200 and return object - Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" + Given I have citizen John Doe + And I am authenticated as John Doe When I send a POST request to "/articles" with body: """ { @@ -27,10 +33,7 @@ Feature: articles routes "description": "description2", "tags": [ "green" - ], - "created_by": { - "id": "64b7b379-2298-43ec-b428-ba134930cabd" - } + ] } """ Then the response status code should be 200