Skip to content

Commit f291f62

Browse files
author
icreated
committed
pagination
1 parent 6135e25 commit f291f62

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Cosmo Tech.
2+
// Licensed under the MIT license.
3+
package com.cosmotech.api.utils
4+
5+
import org.springframework.data.domain.PageRequest
6+
7+
fun constructPageRequest(page: Int?, size: Int?, maxSize: Int): PageRequest? {
8+
return when {
9+
page != null && size != null -> PageRequest.of(page, size)
10+
page != null && size == null -> PageRequest.of(page, maxSize)
11+
page == null && size != null -> PageRequest.of(0, size)
12+
else -> null
13+
}
14+
}
15+
16+
fun <T> findAllPaginated(
17+
maxResult: Int,
18+
findAllLambda: (pageRequest: PageRequest) -> MutableList<T>
19+
): MutableList<T> {
20+
var pageRequest = PageRequest.ofSize(maxResult)
21+
var connectorList = mutableListOf<T>()
22+
do {
23+
var objectList = findAllLambda(pageRequest)
24+
pageRequest = pageRequest.next()
25+
connectorList.addAll(objectList)
26+
} while (objectList.isNotEmpty())
27+
return connectorList
28+
}

src/test/kotlin/com/cosmotech/api/utils/AnyExtTests.kt renamed to src/test/kotlin/com/cosmotech/api/utils/AnyExtensionsTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlin.test.assertFalse
1010
import kotlin.test.assertTrue
1111
import org.junit.jupiter.api.assertThrows
1212

13-
class AnyExtTests {
13+
class AnyExtensionsTests {
1414

1515
@Test
1616
fun `changed - change when comparing null to non-null`() {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Cosmo Tech.
2+
// Licensed under the MIT license.
3+
package com.cosmotech.api.utils
4+
5+
import kotlin.test.Test
6+
import kotlin.test.assertEquals
7+
import org.springframework.data.domain.PageRequest
8+
9+
const val DEFAULT_MAX_PAGE_SIZE = 100
10+
11+
class RedisUtilsTests {
12+
13+
@Test
14+
fun `constructPageRequest - should check multiple cases for page and size`() {
15+
listOf(
16+
Pair(1, 10) to PageRequest.of(1, 10),
17+
Pair(null, 10) to PageRequest.of(0, 10),
18+
Pair(1, null) to PageRequest.of(1, DEFAULT_MAX_PAGE_SIZE),
19+
Pair(null, null) to null)
20+
.forEach { (input, expected) ->
21+
val actual = constructPageRequest(input.first, input.second, DEFAULT_MAX_PAGE_SIZE)
22+
assertEquals(expected, actual)
23+
}
24+
}
25+
26+
@Test
27+
fun `findAllPaginated - should check empty List`() {
28+
val maxResult = 100
29+
val findAllLambda = { _: PageRequest -> mutableListOf<Any>() }
30+
val actual = findAllPaginated(maxResult, findAllLambda)
31+
assertEquals(0, actual.size)
32+
}
33+
34+
@Test
35+
fun `findAllPaginated - should check non-empty List`() {
36+
var counter = 3
37+
val findAllLambda = { _: PageRequest ->
38+
if (counter-- == 0) mutableListOf() else mutableListOf(Any(), Any(), Any(), Any(), Any())
39+
}
40+
listOf(5, 10, 15, 20, 50).forEach { maxResult ->
41+
counter = 3
42+
val actual = findAllPaginated(maxResult, findAllLambda)
43+
assertEquals(15, actual.size)
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)