Refactoring CommentArticle Tests

This commit is contained in:
2020-03-16 20:39:16 +01:00
parent bbe54c3115
commit 10b2844620
2 changed files with 89 additions and 64 deletions

View File

@@ -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",
Given("I have {int} constitution") { nb: Int ->
repeat(nb) {
createConstitution()
}
}
Given("I have constitution") { extraData: DataTable? ->
createConstitution(extraData)
}
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, String>(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<CitizenRepository>().findByUsername(username) ?: error("Citizen not exist")
} else {
val first = "firstName"+UUID.randomUUID().toString()
val last = "lastName"+UUID.randomUUID().toString()
Citizen(
birthday = DateTime.now(),
user = UserEntity(username = "john-doe", plainPassword = "azerty")
)
try {
get<CitizenRepository>().insertWithUser(citizen)
} catch (e: CompletionException) {
citizen = get<CitizenRepository>().findByUsername("john-doe")!!
name = CitizenI.Name(
first,
last
),
email = "$first@fakeemail.com",
user = UserEntity(username = username, plainPassword = "azerty")
).also {
get<CitizenRepository>().insertWithUser(it)
}
}
val title1 = Constitution.Title(
val title1 = ConstitutionSimple.TitleSimple<ArticleRef>(
name = "My Title"
)
val constitution = Constitution(
title = "hello",
titles = mutableListOf(title1),
anonymous = false
)
get<ConstitutionRepository>().upsert(constitution.create(citizen))
}
Given("I have constitution with id {string} created by {string}") { id: String, username: String ->
val citizen = get<CitizenRepository>().findByUsername(username)!!
val title1 = TitleSimple<ArticleRef>(
name = "My Title"
)
val constitution = ConstitutionSimple<CitizenSimple, TitleSimple<ArticleRef>>(
id = UUID.fromString(id),
val constitution = ConstitutionSimple<CitizenSimple, ConstitutionSimple.TitleSimple<ArticleRef>>(
id = params?.get("id")?.toUUID() ?: UUID.randomUUID(),
title = "hello",
titles = mutableListOf(title1),
anonymous = false,
createdBy = citizen
createdBy = createdBy
)
get<ConstitutionRepository>().upsert(constitution)
}
private fun commentConstitution(constitutionId: String, firstName: String, lastName: String, extraData: DataTable? = null) {
val params = extraData?.asMap<String, String>(String::class.java, String::class.java)
val constitution = get<ConstitutionRepository>().findById(UUID.fromString(constitutionId)) ?: error("Constitution not exist")
val citizen = get<CitizenRepository>().findByUsername(("$firstName-$lastName".toLowerCase()).toLowerCase().replace(' ', '-')) ?: error("Citizen not exist")
val comment: CommentEntity<ConstitutionRef> = CommentEntity(
id = params?.get("id")?.let { UUID.fromString(it) } ?: UUID.randomUUID(),
createdBy = citizen,
target = constitution,
content = params?.get("content") ?: "hello"
)
get<CommentConstitution>().comment(comment)
}
}

View File

@@ -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 |