#71 Use response object for route FindArticles

This commit is contained in:
2021-03-05 00:39:49 +01:00
parent 9fc21f5459
commit bb637dd96a
19 changed files with 175 additions and 58 deletions

View File

@@ -0,0 +1,13 @@
package fr.dcproject.common.dto
import fr.dcproject.component.citizen.database.CitizenI
import java.util.UUID
import fr.dcproject.common.entity.CreatedBy as EntityCreatedBy
interface CreatedBy {
val createdBy: UUID
class Imp(parent: EntityCreatedBy<CitizenI>) : CreatedBy {
override val createdBy: UUID = parent.createdBy.id
}
}

View File

@@ -0,0 +1,16 @@
package fr.dcproject.common.dto
import fr.dcproject.common.entity.EntityI
import fr.postgresjson.connexion.Paginated
fun <E : EntityI> Paginated<E>.toOutput(setup: (E) -> Any): Any {
return object {
val count = this@toOutput.count
val currentPage = this@toOutput.count
val limit = this@toOutput.limit
val offset = this@toOutput.offset
val total = this@toOutput.total
val totalPages = this@toOutput.totalPages
val result = this@toOutput.result.map { setup(it) }
}
}

View File

@@ -8,8 +8,8 @@ import fr.dcproject.common.entity.TargetI
import fr.dcproject.common.entity.TargetRef
import fr.dcproject.common.entity.Versionable
import fr.dcproject.common.entity.VersionableId
import fr.dcproject.component.citizen.database.CitizenCart
import fr.dcproject.component.citizen.database.CitizenCartI
import fr.dcproject.component.citizen.database.CitizenCreator
import fr.dcproject.component.citizen.database.CitizenI
import fr.dcproject.component.citizen.database.CitizenRef
import fr.dcproject.component.opinion.entity.Opinionable
@@ -78,7 +78,7 @@ class ArticleForUpdate(
class ArticleForListing(
id: UUID? = null,
override val title: String,
override val createdBy: CitizenCart,
override val createdBy: CitizenCreator,
override val workgroup: WorkgroupCart?,
override val deletedAt: DateTime?,
override val draft: Boolean

View File

@@ -1,5 +1,6 @@
package fr.dcproject.component.article.routes
import fr.dcproject.common.dto.toOutput
import fr.dcproject.common.security.assert
import fr.dcproject.component.article.ArticleAccessControl
import fr.dcproject.component.article.database.ArticleForListing
@@ -44,7 +45,33 @@ object FindArticles {
get<ArticlesRequest> {
repo.findArticles(it)
.apply { ac.assert { canView(result, citizenOrNull) } }
.let { call.respond(it) }
.let {
call.respond(
it.toOutput {
object {
val id = it.id
val title = it.title
val createdBy = object {
val id = it.createdBy.id
val name = it.createdBy.name.let {
object {
val firstName = it.firstName
val lastName = it.lastName
}
}
val email = it.createdBy.email
}
val workgroup = it.workgroup?.let {
object {
val id = it.id
val name = it.name
}
}
val draft = it.draft
}
}
)
}
}
}
}

View File

@@ -243,7 +243,37 @@ paths:
result:
type: array
items:
$ref: '#/components/schemas/ArticleResponse'
properties:
id:
type: string
format: uuid
title:
type: string
created_by:
type: object
properties:
id:
type: string
format: uuid
name:
type: object
properties:
first_name:
type: string
last_name:
type: string
email:
type: string
workgroup:
type: object
properties:
id:
type: string
format: uuid
name:
type: string
draft:
type: boolean
post:
security:
- JWTAuth: []

View File

@@ -1,18 +1,21 @@
package integration
import integration.steps.`And have property`
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.then.`And have property`
import integration.steps.then.`And the response should contain pattern`
import integration.steps.then.`And the response should not be null`
import integration.steps.then.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.`when`.`When I send a POST request`
import integration.steps.`whish contains`
import integration.steps.and
import integration.steps.then.`whish contains`
import integration.steps.then.and
import integration.steps.given.`Given I have article created by workgroup`
import integration.steps.given.`Given I have article`
import integration.steps.given.`Given I have articles`
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have workgroup`
import integration.steps.given.`authenticated as`
import integration.steps.then.`And the response should contain list`
import integration.steps.then.`And the response should not contain`
import io.ktor.http.HttpStatusCode.Companion.OK
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Tags
@@ -28,6 +31,11 @@ class `Article routes` : BaseTest() {
`Given I have articles`(3)
`When I send a GET request`("/articles") `Then the response should be` OK and {
`And the response should not be null`()
`And the response should contain pattern`("$.result[0].created_by.name.first_name", "firstName.+")
`And the response should contain pattern`("$.result[1].created_by.name.first_name", "firstName.+")
`And the response should contain pattern`("$.result[2].created_by.name.first_name", "firstName.+")
`And the response should not contain`("$.result[3]")
`And the response should contain list`("$.result", 3, 3)
}
}
}

View File

@@ -1,12 +1,12 @@
package integration
import integration.steps.`And have property`
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.then.`And have property`
import integration.steps.then.`And the response should not be null`
import integration.steps.then.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.`when`.`When I send a PUT request`
import integration.steps.`whish contains`
import integration.steps.and
import integration.steps.then.`whish contains`
import integration.steps.then.and
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`authenticated as`
import io.ktor.http.HttpStatusCode.Companion.BadRequest

View File

@@ -1,13 +1,13 @@
package integration
import fr.dcproject.component.citizen.database.CitizenI.Name
import integration.steps.`And the response should contain`
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.then.`And the response should contain`
import integration.steps.then.`And the response should not be null`
import integration.steps.then.`Then the response should be`
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.then.and
import integration.steps.given.`Given I have article`
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have comment on article`

View File

@@ -1,13 +1,13 @@
package integration
import fr.dcproject.component.citizen.database.CitizenI.Name
import integration.steps.`And the response should contain list`
import integration.steps.`And the response should contain`
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.then.`And the response should contain list`
import integration.steps.then.`And the response should contain`
import integration.steps.then.`And the response should not be null`
import integration.steps.then.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.`when`.`When I send a POST request`
import integration.steps.and
import integration.steps.then.and
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have comment on constitution`
import integration.steps.given.`Given I have constitution`

View File

@@ -1,9 +1,9 @@
package integration
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.then.`And the response should not be null`
import integration.steps.then.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.and
import integration.steps.then.and
import integration.steps.given.`Given I have article`
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have comment on article`

View File

@@ -1,12 +1,12 @@
package integration
import integration.steps.`And have property`
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.then.`And have property`
import integration.steps.then.`And the response should not be null`
import integration.steps.then.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.`when`.`When I send a POST request`
import integration.steps.`whish contains`
import integration.steps.and
import integration.steps.then.`whish contains`
import integration.steps.then.and
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have constitution`
import integration.steps.given.`Given I have constitutions`

View File

@@ -1,13 +1,13 @@
package integration
import integration.steps.`And the response should be null`
import integration.steps.`And the response should contain`
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.then.`And the response should be null`
import integration.steps.then.`And the response should contain`
import integration.steps.then.`And the response should not be null`
import integration.steps.then.`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.and
import integration.steps.then.and
import integration.steps.given.`And follow article`
import integration.steps.given.`Given I have article`
import integration.steps.given.`Given I have citizen`

View File

@@ -1,13 +1,13 @@
package integration
import integration.steps.`And the response should be null`
import integration.steps.`And the response should contain`
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.then.`And the response should be null`
import integration.steps.then.`And the response should contain`
import integration.steps.then.`And the response should not be null`
import integration.steps.then.`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.and
import integration.steps.then.and
import integration.steps.given.`And follow constitution`
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have constitution`

View File

@@ -1,8 +1,8 @@
package integration
import integration.steps.`And the response should not be null`
import integration.steps.`Then the response should be`
import integration.steps.`and should contains`
import integration.steps.then.`And the response should not be null`
import integration.steps.then.`Then the response should be`
import integration.steps.then.`and should contains`
import integration.steps.`when`.`When I send a POST request`
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`authenticated as`

View File

@@ -1,12 +1,12 @@
package integration
import fr.dcproject.component.citizen.database.CitizenI.Name
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.then.`And the response should contain list`
import integration.steps.then.`And the response should contain`
import integration.steps.then.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.`when`.`When I send a PUT request`
import integration.steps.and
import integration.steps.then.and
import integration.steps.given.`Given I have an opinion choice`
import integration.steps.given.`Given I have article`
import integration.steps.given.`Given I have citizen`

View File

@@ -1,6 +1,6 @@
package integration
import integration.steps.`Then the response should be`
import integration.steps.then.`Then the response should be`
import integration.steps.`when`.`When I send a POST request`
import io.ktor.http.HttpStatusCode
import org.amshove.kluent.`should be null`

View File

@@ -1,11 +1,11 @@
package integration
import fr.dcproject.component.citizen.database.CitizenI.Name
import integration.steps.`And the response should contain`
import integration.steps.`Then the response should be`
import integration.steps.then.`And the response should contain`
import integration.steps.then.`Then the response should be`
import integration.steps.`when`.`When I send a GET request`
import integration.steps.`when`.`When I send a PUT request`
import integration.steps.and
import integration.steps.then.and
import integration.steps.given.`Given I have article`
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have comment on article`

View File

@@ -1,15 +1,15 @@
package integration
import fr.dcproject.component.citizen.database.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.then.`And the response should be null`
import integration.steps.then.`And the response should contain list`
import integration.steps.then.`And the response should contain`
import integration.steps.then.`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.then.and
import integration.steps.given.`Given I have citizen`
import integration.steps.given.`Given I have workgroup`
import integration.steps.given.`With members`

View File

@@ -1,8 +1,9 @@
package integration.steps
package integration.steps.then
import assert.assertGreaterThan
import assert.assertLessThan
import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.PathNotFoundException
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.TestApplicationCall
import io.ktor.server.testing.TestApplicationResponse
@@ -12,8 +13,10 @@ import org.amshove.kluent.`should be null`
import org.amshove.kluent.`should be`
import org.amshove.kluent.`should not be null`
import org.amshove.kluent.shouldContain
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
fun TestApplicationCall.`Then the response should be`(status: HttpStatusCode? = null, block: TestApplicationResponse.() -> Unit): TestApplicationCall = this.apply {
if (status != null) {
@@ -62,6 +65,26 @@ inline fun <reified T> TestApplicationResponse.`And the response should contain`
}
}
fun TestApplicationResponse.`And the response should not contain`(path: String) {
assertThrows<PathNotFoundException> {
JsonPath.read(content, path)
}
}
fun TestApplicationResponse.`And the response should contain pattern`(path: String, expectedRegex: String): Any {
return JsonPath.read<Any?>(content, path).also {
it.let {
if (it is JSONArray && it.count() == 1) {
it.first()
} else {
it
}
}?.let {
assertTrue(expectedRegex.toRegex().containsMatchIn(it.toString()))
} ?: throw AssertionError("\"$path -> 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 {
assertNotNull(it)