Big refactoring #77

Merged
flecomte merged 166 commits from refactoring-component-and-immutable into master 2021-03-24 19:06:07 +01:00
4 changed files with 265 additions and 24 deletions
Showing only changes of commit d83ba2d54d - Show all commits

View File

@@ -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")
}
}
}
}

View File

@@ -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<CitizenBasic>.() -> Unit = {},
) {
createWorkgroup(id?.toUUID(), name, description, anonymous, createdByUsername)
val workgroup: Workgroup<CitizenBasic> = createWorkgroup(id?.toUUID(), name, description, anonymous, createdBy)
callback(workgroup)
}
fun Workgroup<CitizenBasic>.`With members`(
vararg member: CitizenI.Name
) {
addMemberToWorkgroup(this, *member)
}
fun addMemberToWorkgroup(workgroup: Workgroup<CitizenBasic>, 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<Member<CitizenI>> = membersNames.map { memberName ->
val member: Citizen = citizenRepository.findByName(memberName) ?: error("Citizen not exist")
Member(member, listOf(Member.Role.EDITOR))
}
workgroupRepository.updateMembers<CitizenI>(
workgroup,
workgroup.members as List<Member<CitizenI>> + newMembers
)
}
private fun createWorkgroup(
@@ -30,22 +52,21 @@ private fun createWorkgroup(
name: String? = null,
description: String? = null,
anonymous: Boolean? = null,
createdByUsername: String? = null
createdBy: CitizenI.Name? = null,
): Workgroup<CitizenBasic> {
val username = (createdByUsername ?: "username" + UUID.randomUUID().toString())
val citizenRepository: CitizenRepository by lazy { GlobalContext.get().koin.get() }
val workgroupRepository: WorkgroupRepository by lazy { GlobalContext.get().koin.get() }
val createdBy = createdBy ?: CitizenI.Name("Paul", "Langevin")
val creator = citizenRepository.findByName(createdBy) ?: run {
val username = ("username" + UUID.randomUUID().toString())
.toLowerCase().replace(' ', '-')
val citizenRepository: CitizenRepository by lazy<CitizenRepository> { GlobalContext.get().koin.get() }
val workgroupRepository: WorkgroupRepository by lazy<WorkgroupRepository> { 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"),
name = createdBy,
email = "$username@dc-project.fr",
birthday = DateTime.now(),
user = user

View File

@@ -48,11 +48,19 @@ infix fun Pair<JsonPath, Any>.`whish contains`(expected: Any): Pair<JsonPath, An
second `should be equal to` expected
}
fun <T> TestApplicationResponse.`And the response should contain`(path: String, valueExpected: T?): T {
inline fun <reified T> TestApplicationResponse.`And the response should contain`(path: String, valueExpected: T?): T {
return JsonPath.read<T?>(content, path).also {
it.let {
if (it is JSONArray && it.count() == 1 && it.first() is T?) {
it.first() as T?
} else {
it
}
}.let {
assertEquals<T?>(valueExpected, it ?: throw AssertionError("\"$path -> ${valueExpected}\" element not found on json response"))
}
}
}
fun TestApplicationResponse.`And the response should contain list`(path: String, min: Int? = null, max: Int? = null) {
JsonPath.read<JSONArray?>(content, path).also {

View File

@@ -1,4 +1,5 @@
@workgroup
@disable
Feature: Workgroup
Scenario: Can get one workgroup