Skip to content

Commit

Permalink
improve the formatting slightly, and added an additional test
Browse files Browse the repository at this point in the history
  • Loading branch information
zambrovski committed Oct 11, 2023
1 parent ce9e369 commit 38f0e19
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ internal class JpaPolyflowViewServiceTaskITest {
assertThat(strawhatsInverse.elements.map { it.task.id }).containsExactly(id4, id3)
}

@Suppress("DEPRECATION")
@Test
fun `should sort with empty string, null or empty list correctly`() {
val sortWithNullQuery = jpaPolyflowViewService.query(AllTasksWithDataEntriesQuery(
Expand Down Expand Up @@ -350,7 +351,7 @@ internal class JpaPolyflowViewServiceTaskITest {
jpaPolyflowViewService.query(AllTasksWithDataEntriesQuery(
sort = listOf("")
))
}.message).isEqualTo("Sort parameter can not be blank")
}.message).isEqualTo("Sort parameter must not be blank")

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface PageableSortableQuery {
}

sort.forEach {
require(it.isNotBlank()) { "Sort parameter can not be blank" }
require(it.isNotBlank() && it.isNotEmpty()) { "Sort parameter must not be blank" }
val direction = it.substring(0, 1)
require(
direction == ASCENDING.sign
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ data class TasksWithDataEntriesForGroupQuery(

@Deprecated("Please use other constructor setting sort as List<String>")
constructor(user: User, includeAssigned: Boolean = false, page: Int = 0, size: Int = Int.MAX_VALUE, sort: String?, filters: List<String> = listOf()) : this(
user = user, includeAssigned = includeAssigned, page = page, size = size, sort = if (sort.isNullOrBlank()) {
listOf()
} else {
listOf(sort)
}, filters = filters
user = user,
includeAssigned = includeAssigned,
page = page,
size = size,
sort = if (sort.isNullOrBlank()) {
listOf()
} else {
listOf(sort)
},
filters = filters
)

override fun applyFilter(element: TaskWithDataEntries): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,33 @@ data class TasksWithDataEntriesForUserQuery(

@Deprecated("Please use other constructor setting sort as List<String>")
constructor(user: User, assignedToMeOnly: Boolean = false, page: Int = 0, size: Int = Int.MAX_VALUE, sort: String?, filters: List<String> = listOf()) : this(
user = user, assignedToMeOnly = assignedToMeOnly, page = page, size = size, sort = if (sort.isNullOrBlank()) {
listOf()
} else {
listOf(sort)
}, filters = filters
user = user,
assignedToMeOnly = assignedToMeOnly,
page = page,
size = size,
sort = if (sort.isNullOrBlank()) {
listOf()
} else {
listOf(sort)
},
filters = filters
)

/**
* Compatibility constructor for old clients.
*/
@Deprecated("Please use other constructor setting the assignedToMeOnly.")
constructor(user: User, page: Int = 0, size: Int = Int.MAX_VALUE, sort: String? = null, filters: List<String> = listOf()) : this(
user = user, assignedToMeOnly = false, page = page, size = size, sort = if (sort.isNullOrBlank()) {
listOf()
} else {
listOf(sort)
}, filters = filters
user = user,
assignedToMeOnly = false,
page = page,
size = size,
sort = if (sort.isNullOrBlank()) {
listOf()
} else {
listOf(sort)
},
filters = filters
)

override fun applyFilter(element: TaskWithDataEntries): Boolean =
Expand Down
18 changes: 18 additions & 0 deletions view/view-api/src/test/kotlin/query/PageableSortableQueryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ internal class PageableSortableQueryTest {
private val badOrdering = listOf("*createTime")
private val user = User(username = "kermit", groups = setOf())


@Test
fun should_sanitize_blank_sort() {

class TestQuery(
override val page: Int,
override val size: Int,
override val sort: List<String>,
) : PageableSortableQuery

assertThat(assertThrows<IllegalArgumentException> { TestQuery(1, 1, listOf("")).sanitizeSort(Task::class) }.message).isEqualTo("Sort parameter must not be blank")
assertThat(assertThrows<IllegalArgumentException> { TestQuery(1, 1, listOf("\t")).sanitizeSort(Task::class) }.message).isEqualTo("Sort parameter must not be blank")
assertThat(assertThrows<IllegalArgumentException> { TestQuery(1, 1, listOf("\t\r\n")).sanitizeSort(Task::class) }.message).isEqualTo("Sort parameter must not be blank")
assertThat(assertThrows<IllegalArgumentException> { TestQuery(1, 1, listOf(" \t")).sanitizeSort(Task::class) }.message).isEqualTo("Sort parameter must not be blank")
assertThat(assertThrows<IllegalArgumentException> { TestQuery(1, 1, listOf(" \n")).sanitizeSort(Task::class) }.message).isEqualTo("Sort parameter must not be blank")
}


@Test
fun should_sanitize_task_query() {
assertThat(AllTasksQuery(sort = createTimeAsc).apply { sanitizeSort(Task::class) }.sort).isEqualTo(createTimeAsc)
Expand Down

0 comments on commit 38f0e19

Please sign in to comment.