From d83ba2d54dc5bf26174597b4535056274fb0f34e Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 24 Feb 2021 23:18:42 +0100 Subject: [PATCH] Add Integration test for workgroup routes --- .../kotlin/integration/Workgroup routes.kt | 211 ++++++++++++++++++ .../integration/steps/given/Workgroup.kt | 65 ++++-- .../kotlin/integration/steps/then/request.kt | 12 +- src/test/resources/feature/workgroup.feature | 1 + 4 files changed, 265 insertions(+), 24 deletions(-) create mode 100644 src/test/kotlin/integration/Workgroup routes.kt diff --git a/src/test/kotlin/integration/Workgroup routes.kt b/src/test/kotlin/integration/Workgroup routes.kt new file mode 100644 index 0000000..f288057 --- /dev/null +++ b/src/test/kotlin/integration/Workgroup routes.kt @@ -0,0 +1,211 @@ +package integration + +import fr.dcproject.component.citizen.CitizenI.Name +import integration.steps.`And the response should be null` +import integration.steps.`And the response should contain list` +import integration.steps.`And the response should contain` +import integration.steps.`Then the response should be` +import integration.steps.`when`.`When I send a DELETE request` +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 citizen` +import integration.steps.given.`Given I have workgroup` +import integration.steps.given.`With members` +import integration.steps.given.`authenticated as` +import integration.steps.given.`with no content` +import io.ktor.http.HttpStatusCode.Companion.Created +import io.ktor.http.HttpStatusCode.Companion.NoContent +import io.ktor.http.HttpStatusCode.Companion.NotFound +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("vote")) +class `Workgroup routes` : BaseTest() { + @Test + fun `I can get one workgroup`() { + withIntegrationApplication { + `Given I have citizen`("Stephen", "Hawking") + `Given I have citizen`("Sadi", "Carnot", id = "be3b0926-8628-4426-804a-75188a6eb315") + `Given I have citizen`("Joseph", "Fourier", id = "d9671eca-abaf-4b67-9230-3ece700c1ddb") + `Given I have workgroup`( + id = "ab469134-bf14-4856-b093-ae1aa990f977", + name = "Les Mousquets", + createdBy = Name("Stephen", "Hawking") + ) { + `With members`( + Name("Sadi", "Carnot"), + Name("Joseph", "Fourier"), + ) + } + `When I send a GET request`("/workgroups/ab469134-bf14-4856-b093-ae1aa990f977") { + `authenticated as`("Stephen", "Hawking") + } `Then the response should be` OK and { + `And the response should contain`("$.id", "ab469134-bf14-4856-b093-ae1aa990f977") + `And the response should contain`("$.name", "Les Mousquets") + `And the response should contain`( + "$.members[*].citizen.name[?(@.first_name=='Stephen')].first_name", + "Stephen" + ) + `And the response should contain`( + "$.members[*].citizen.name[?(@.first_name=='Sadi')].first_name", + "Sadi" + ) + } + } + } + + @Test + fun `I can create a workgroup`() { + withIntegrationApplication { + `Given I have citizen`("Werner", "Heisenberg") + `When I send a POST request`("/workgroups") { + `authenticated as`("Werner", "Heisenberg") + """ + { + "id":"f496d86d-6654-4068-91ff-90e1dbcc5f38", + "name":"Les Bouffons", + "description":"La vie est belle", + "anonymous":false + } + """ + } `Then the response should be` Created and { + `And the response should contain`("$.id", "f496d86d-6654-4068-91ff-90e1dbcc5f38") + `And the response should contain`("$.name", "Les Bouffons") + `And the response should contain`("$.description", "La vie est belle") + `And the response should contain`("$.anonymous", false) + } + + `When I send a GET request`("/workgroups/f496d86d-6654-4068-91ff-90e1dbcc5f38") { + `authenticated as`("Werner", "Heisenberg") + } `Then the response should be` OK + } + } + + @Test + fun `I can delete a workgroup`() { + withIntegrationApplication { + `Given I have citizen`("Louis", "Néel") + `Given I have workgroup`("ab469134-bf14-4856-b093-ae1aa990f977", createdBy = Name("Louis", "Néel")) + `When I send a DELETE request`("/workgroups/ab469134-bf14-4856-b093-ae1aa990f977") { + `authenticated as`("Louis", "Néel") + `with no content`() + } `Then the response should be` NoContent and { + `And the response should be null`() + } + + `When I send a GET request`("/workgroups/ab469134-bf14-4856-b093-ae1aa990f977") { + `authenticated as`("Louis", "Néel") + } `Then the response should be` NotFound + } + } + + @Test + fun `I can get workgroups list`() { + withIntegrationApplication { + `Given I have citizen`("Max", "Planck") + `Given I have workgroup`("3fd8edb6-c4b4-4c94-bc75-ddd9b290d32c") + `When I send a GET request`("/workgroups") { + `authenticated as`("Max", "Planck") + `with no content`() + } `Then the response should be` OK and { + `And the response should contain`("$.result[0].id", "3fd8edb6-c4b4-4c94-bc75-ddd9b290d32c") + } + } + } + + @Test + fun `I can add member to workgroup`() { + withIntegrationApplication { + `Given I have citizen`("Blaise", "Pascal") + `Given I have citizen`("Roger", "Penrose", id = "6d883fe7-5fc0-4a50-8858-72230673eba4") + `Given I have citizen`("Alessandro", "Volta", id = "b5bac515-45d4-4aeb-9b6d-2627a0bbc419") + `Given I have workgroup`("b0ea1922-3bc6-44e2-aa7c-40158998cfbb", createdBy = Name("Blaise", "Pascal")) + `When I send a POST request`("/workgroups/b0ea1922-3bc6-44e2-aa7c-40158998cfbb/members") { + `authenticated as`("Blaise", "Pascal") + """ + [ + { + "citizen": {"id":"6d883fe7-5fc0-4a50-8858-72230673eba4"}, + "roles": ["MASTER"] + }, + { + "citizen": {"id":"b5bac515-45d4-4aeb-9b6d-2627a0bbc419"}, + "roles": ["MASTER"] + } + ] + """ + } `Then the response should be` Created + } + } + + @Test + fun `I can remove member to workgroup`() { + withIntegrationApplication { + `Given I have citizen`("Heinrich", "Hertz", id = "94f92424-c257-4582-907c-98564a8c4ac9") + `Given I have citizen`("William", "Thomson", id = "87909ba3-2069-431c-9924-219fd8411cf2") + `Given I have citizen`("Paul", "Dirac", id = "1baf48bb-02bc-4d8f-ac86-33335354f5e7") + `Given I have workgroup`("b6c975df-dd44-4e99-adc1-f605746b0e11", createdBy = Name("Heinrich", "Hertz")) { + `With members`( + Name("William", "Thomson"), + Name("Paul", "Dirac"), + ) + } + `When I send a DELETE request`("/workgroups/b6c975df-dd44-4e99-adc1-f605746b0e11/members") { + `authenticated as`("Heinrich", "Hertz") + """ + [ + { + "citizen": {"id":"87909ba3-2069-431c-9924-219fd8411cf2"}, + "roles": ["MASTER"] + } + ] + """ + } `Then the response should be` OK and { + `And the response should contain list`("$", 2, 2) + `And the response should contain`("$.[0]citizen.id", "94f92424-c257-4582-907c-98564a8c4ac9") + `And the response should contain`("$.[1]citizen.id", "1baf48bb-02bc-4d8f-ac86-33335354f5e7") + } + } + } + + @Test + fun `I can update members on workgroup`() { + withIntegrationApplication { + `Given I have citizen`("Leon", "Foucault") + `Given I have citizen`("Sadi", "Carnot", id = "be3b0926-8628-4426-804a-75188a6eb315") + `Given I have citizen`("Joseph", "Fourier", id = "b49e20c1-8393-45d6-a6a0-3fa5c71cbdc1") + `Given I have citizen`("Georg", "Ohm") + `Given I have workgroup`("784fe6bc-7635-4ae2-b080-3a4743b998bf", createdBy = Name("Leon", "Foucault")) { + `With members`( + Name("Sadi", "Carnot"), + Name("Joseph", "Fourier"), + ) + } + `When I send a PUT request`("/workgroups/784fe6bc-7635-4ae2-b080-3a4743b998bf/members") { + `authenticated as`("Leon", "Foucault") + """ + [ + { + "citizen": {"id":"be3b0926-8628-4426-804a-75188a6eb315"}, + "roles": ["MASTER"] + }, + { + "citizen": {"id":"b49e20c1-8393-45d6-a6a0-3fa5c71cbdc1"}, + "roles": ["MASTER"] + } + ] + """ + } `Then the response should be` OK and { + `And the response should contain list`("$", 2, 2) + `And the response should contain`("$.[0]citizen.id", "be3b0926-8628-4426-804a-75188a6eb315") + `And the response should contain`("$.[1]citizen.id", "b49e20c1-8393-45d6-a6a0-3fa5c71cbdc1") + } + } + } +} diff --git a/src/test/kotlin/integration/steps/given/Workgroup.kt b/src/test/kotlin/integration/steps/given/Workgroup.kt index 758b514..77c4f56 100644 --- a/src/test/kotlin/integration/steps/given/Workgroup.kt +++ b/src/test/kotlin/integration/steps/given/Workgroup.kt @@ -2,6 +2,7 @@ package integration.steps.given import fr.dcproject.common.utils.toUUID import fr.dcproject.component.auth.UserForCreate +import fr.dcproject.component.citizen.Citizen import fr.dcproject.component.citizen.CitizenBasic import fr.dcproject.component.citizen.CitizenForCreate import fr.dcproject.component.citizen.CitizenI @@ -20,9 +21,30 @@ fun TestApplicationEngine.`Given I have workgroup`( name: String? = null, description: String? = null, anonymous: Boolean? = null, - createdByUsername: String? = null + createdBy: CitizenI.Name? = null, + callback: Workgroup.() -> Unit = {}, ) { - createWorkgroup(id?.toUUID(), name, description, anonymous, createdByUsername) + val workgroup: Workgroup = createWorkgroup(id?.toUUID(), name, description, anonymous, createdBy) + callback(workgroup) +} +fun Workgroup.`With members`( + vararg member: CitizenI.Name +) { + addMemberToWorkgroup(this, *member) +} + +fun addMemberToWorkgroup(workgroup: Workgroup, vararg membersNames: CitizenI.Name) { + val citizenRepository: CitizenRepository by lazy { GlobalContext.get().koin.get() } + val workgroupRepository: WorkgroupRepository by lazy { GlobalContext.get().koin.get() } + + val newMembers: List> = membersNames.map { memberName -> + val member: Citizen = citizenRepository.findByName(memberName) ?: error("Citizen not exist") + Member(member, listOf(Member.Role.EDITOR)) + } + workgroupRepository.updateMembers( + workgroup, + workgroup.members as List> + newMembers + ) } private fun createWorkgroup( @@ -30,29 +52,28 @@ private fun createWorkgroup( name: String? = null, description: String? = null, anonymous: Boolean? = null, - createdByUsername: String? = null + createdBy: CitizenI.Name? = null, ): Workgroup { - val username = (createdByUsername ?: "username" + UUID.randomUUID().toString()) - .toLowerCase().replace(' ', '-') + val citizenRepository: CitizenRepository by lazy { GlobalContext.get().koin.get() } + val workgroupRepository: WorkgroupRepository by lazy { GlobalContext.get().koin.get() } - val citizenRepository: CitizenRepository by lazy { GlobalContext.get().koin.get() } - val workgroupRepository: WorkgroupRepository by lazy { GlobalContext.get().koin.get() } - - val creator = citizenRepository.findByUsername(username.toLowerCase().replace(' ', '-')) - ?: run { - val user = UserForCreate( - username = username, - password = "azerty", - ) - CitizenForCreate( - name = CitizenI.Name("Paul", "Langevin"), - email = "$username@dc-project.fr", - birthday = DateTime.now(), - user = user - ).let { - citizenRepository.insertWithUser(it) ?: error("Unable to create User") - } + val createdBy = createdBy ?: CitizenI.Name("Paul", "Langevin") + val creator = citizenRepository.findByName(createdBy) ?: run { + val username = ("username" + UUID.randomUUID().toString()) + .toLowerCase().replace(' ', '-') + val user = UserForCreate( + username = username, + password = "azerty", + ) + CitizenForCreate( + name = createdBy, + email = "$username@dc-project.fr", + birthday = DateTime.now(), + user = user + ).let { + citizenRepository.insertWithUser(it) ?: error("Unable to create User") } + } val workgroup = Workgroup( id = id ?: UUID.randomUUID(), diff --git a/src/test/kotlin/integration/steps/then/request.kt b/src/test/kotlin/integration/steps/then/request.kt index c649b5e..f1a49ea 100644 --- a/src/test/kotlin/integration/steps/then/request.kt +++ b/src/test/kotlin/integration/steps/then/request.kt @@ -48,9 +48,17 @@ infix fun Pair.`whish contains`(expected: Any): Pair TestApplicationResponse.`And the response should contain`(path: String, valueExpected: T?): T { +inline fun TestApplicationResponse.`And the response should contain`(path: String, valueExpected: T?): T { return JsonPath.read(content, path).also { - assertEquals(valueExpected, it ?: throw AssertionError("\"$path -> ${valueExpected}\" element not found on json response")) + it.let { + if (it is JSONArray && it.count() == 1 && it.first() is T?) { + it.first() as T? + } else { + it + } + }.let { + assertEquals(valueExpected, it ?: throw AssertionError("\"$path -> ${valueExpected}\" element not found on json response")) + } } } diff --git a/src/test/resources/feature/workgroup.feature b/src/test/resources/feature/workgroup.feature index 2025e5d..965fea4 100644 --- a/src/test/resources/feature/workgroup.feature +++ b/src/test/resources/feature/workgroup.feature @@ -1,4 +1,5 @@ @workgroup +@disable Feature: Workgroup Scenario: Can get one workgroup