From 10b28446203f280deb1bbbc8812c412d087f05e0 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 16 Mar 2020 20:39:16 +0100 Subject: [PATCH] Refactoring CommentArticle Tests --- src/test/kotlin/feature/ConstitutionSteps.kt | 113 +++++++++++------- .../feature/commentConstitution.feature | 40 +++---- 2 files changed, 89 insertions(+), 64 deletions(-) diff --git a/src/test/kotlin/feature/ConstitutionSteps.kt b/src/test/kotlin/feature/ConstitutionSteps.kt index 6d4ea23..463e671 100644 --- a/src/test/kotlin/feature/ConstitutionSteps.kt +++ b/src/test/kotlin/feature/ConstitutionSteps.kt @@ -1,62 +1,91 @@ package feature import fr.dcproject.entity.* -import fr.dcproject.entity.ConstitutionSimple.TitleSimple -import fr.dcproject.entity.request.Constitution +import fr.dcproject.repository.CommentConstitution +import fr.dcproject.utils.toUUID +import io.cucumber.datatable.DataTable import io.cucumber.java8.En 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.Constitution as ConstitutionEntity +import fr.dcproject.entity.Comment as CommentEntity import fr.dcproject.entity.User as UserEntity -import fr.dcproject.repository.Citizen as CitizenRepository import fr.dcproject.repository.Constitution as ConstitutionRepository +import fr.dcproject.repository.Citizen as CitizenRepository class ConstitutionSteps : En, KoinTest { init { - Given("I have constitution with id {string}") { id: String -> - var citizen = Citizen( - id = UUID.fromString(id), - 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")!! + Given("I have {int} constitution") { nb: Int -> + repeat(nb) { + createConstitution() } - - val title1 = Constitution.Title( - name = "My Title" - ) - - val constitution = Constitution( - title = "hello", - titles = mutableListOf(title1), - anonymous = false - ) - get().upsert(constitution.create(citizen)) } - Given("I have constitution with id {string} created by {string}") { id: String, username: String -> - val citizen = get().findByUsername(username)!! + Given("I have constitution") { extraData: DataTable? -> + createConstitution(extraData) + } - val title1 = TitleSimple( - name = "My Title" - ) - - val constitution = ConstitutionSimple>( - id = UUID.fromString(id), - title = "hello", - titles = mutableListOf(title1), - anonymous = false, - createdBy = citizen - ) - get().upsert(constitution) + Given("I have comment created by {word} {word} on constitution {string}:") { firstName: String, lastName: String, constitutionId: String, extraData: DataTable? -> + commentConstitution(constitutionId, firstName, lastName, extraData) + } + Given("I have comment created by {word} {word} on constitution {string}") { firstName: String, lastName: String, constitutionId: String -> + commentConstitution(constitutionId, firstName, lastName) } } + + private fun createConstitution(extraData: DataTable? = null) { + val params = extraData?.asMap(String::class.java, String::class.java) + val createdByUsername = params?.get("createdBy") + val username = (createdByUsername ?: "username"+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 title1 = ConstitutionSimple.TitleSimple( + name = "My Title" + ) + + val constitution = ConstitutionSimple>( + id = params?.get("id")?.toUUID() ?: UUID.randomUUID(), + title = "hello", + titles = mutableListOf(title1), + anonymous = false, + createdBy = createdBy + ) + get().upsert(constitution) + } + + private fun commentConstitution(constitutionId: String, firstName: String, lastName: String, extraData: DataTable? = null) { + val params = extraData?.asMap(String::class.java, String::class.java) + + val constitution = get().findById(UUID.fromString(constitutionId)) ?: error("Constitution not exist") + + val citizen = get().findByUsername(("$firstName-$lastName".toLowerCase()).toLowerCase().replace(' ', '-')) ?: error("Citizen not exist") + + val comment: CommentEntity = CommentEntity( + id = params?.get("id")?.let { UUID.fromString(it) } ?: UUID.randomUUID(), + createdBy = citizen, + target = constitution, + content = params?.get("content") ?: "hello" + ) + get().comment(comment) + } } \ No newline at end of file diff --git a/src/test/resources/feature/commentConstitution.feature b/src/test/resources/feature/commentConstitution.feature index 8cfb9ce..6a1001c 100644 --- a/src/test/resources/feature/commentConstitution.feature +++ b/src/test/resources/feature/commentConstitution.feature @@ -1,35 +1,31 @@ +@comment Feature: comment Constitution Scenario: Can comment an constitution - Given I am authenticated as John Toe with id "a6eb1f5a-8c02-42f4-8e8e-a722f26841ef" - And I have constitution with id "d7e20f0b-3fdd-4638-817a-bbd87054eb82" created by "john-toe" - When I send a POST request to "/constitutions/d7e20f0b-3fdd-4638-817a-bbd87054eb82/comments" with body: + Given I have citizen Nicolas Copernic + And I am authenticated as Nicolas Copernic + And I have constitution + | id | 1707c287-a472-4a62-89f2-9e85030e915c | + When I send a POST request to "/constitutions/1707c287-a472-4a62-89f2-9e85030e915c/comments" with body: """ - Hello mister + { + "content": "Hello mister" + } """ Then the response status code should be 201 - Scenario: Can get comments on constitution of the current citizen - Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" - And I have constitution with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" - When I send a GET request to "/citizens/64b7b379-2298-43ec-b428-ba134930cabd/comments/constitutions" + Scenario: Can get comments on constitutions of the current citizen + Given I have citizen Charles Darwin with ID "46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5" + And I have constitution + | id | 34ddd50a-da00-4a90-a869-08baa2a121be | + | createdBy | Charles Darwin | + And I have comment created by Charles Darwin on constitution "34ddd50a-da00-4a90-a869-08baa2a121be" + When I send a GET request to "/citizens/46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5/comments/constitutions" Then the response status code should be 200 And the response should contain object: | current_page | 1 | | limit | 50 | - - Scenario: Can edit a comment - Given I am authenticated as username 3 with id "92877af7-0a45-fd6a-2ed7-fe81e1236b78" - When I send a PUT request to "/comments/b0422e48-687f-bea7-b45f-b6b301246e97" with body: + And the Response should contain: """ - Hello boy + 34ddd50a-da00-4a90-a869-08baa2a121be """ - Then the response status code should be 200 - And the JSON should contain: - | content | Hello boy | - - Scenario: Can get comment by its ID - When I send a GET request to "/comments/b0422e48-687f-bea7-b45f-b6b301246e97" - Then the response status code should be 200 - And the JSON should contain: - | content | Hello boy |