diff options
3 files changed, 118 insertions, 16 deletions
diff --git a/src/main/kotlin/dev/dnpm/etl/processor/functions.kt b/src/main/kotlin/dev/dnpm/etl/processor/functions.kt new file mode 100644 index 0000000..aa4fc75 --- /dev/null +++ b/src/main/kotlin/dev/dnpm/etl/processor/functions.kt @@ -0,0 +1,35 @@ +/* + * This file is part of ETL-Processor + * + * Copyright (c) 2026 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package dev.dnpm.etl.processor + +import org.springframework.data.domain.Page +import org.springframework.data.domain.PageImpl +import org.springframework.data.domain.Pageable + +fun <T : Any> List<T>.toPage(pageable: Pageable): Page<T> { + return PageImpl( + this.subList( + pageable.offset.toInt().coerceIn(0, this.size), + (pageable.offset.toInt() + pageable.pageSize).coerceIn(0, this.size) + ), + pageable, + this.size.toLong() + ) +} diff --git a/src/main/kotlin/dev/dnpm/etl/processor/services/RequestService.kt b/src/main/kotlin/dev/dnpm/etl/processor/services/RequestService.kt index 6e7c2f3..ba33fd6 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/services/RequestService.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/services/RequestService.kt @@ -23,11 +23,11 @@ import dev.dnpm.etl.processor.PatientPseudonym import dev.dnpm.etl.processor.RequestId import dev.dnpm.etl.processor.Tan import dev.dnpm.etl.processor.monitoring.* -import java.util.* +import dev.dnpm.etl.processor.toPage import org.springframework.data.domain.Page -import org.springframework.data.domain.PageImpl import org.springframework.data.domain.Pageable import org.springframework.stereotype.Service +import java.util.* @Service class RequestService(private val requestRepository: RequestRepository) { @@ -98,18 +98,16 @@ class RequestService(private val requestRepository: RequestRepository) { } fun List<Request>.filter(filter: RequestService.Filter, pageable: Pageable): Page<Request> { - val list = - this - .toList() - .filter { - it.type == RequestType.MTB_FILE - && sequenceOf(RequestStatus.SUCCESS, RequestStatus.WARNING).contains(it.status) - } - .filter { - filter == RequestService.Filter.ALL_DIP - || filter == RequestService.Filter.CONFIRMED && it.submissionAccepted - || filter == RequestService.Filter.UNCONFIRMED && !it.submissionAccepted - } - - return PageImpl(list, pageable, list.size.toLong()) + return this + .toList() + .filter { + it.type == RequestType.MTB_FILE + && sequenceOf(RequestStatus.SUCCESS, RequestStatus.WARNING).contains(it.status) + } + .filter { + filter == RequestService.Filter.ALL_DIP + || filter == RequestService.Filter.CONFIRMED && it.submissionAccepted + || filter == RequestService.Filter.UNCONFIRMED && !it.submissionAccepted + } + .toPage(pageable) } diff --git a/src/test/kotlin/dev/dnpm/etl/processor/FunctionsTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/FunctionsTest.kt new file mode 100644 index 0000000..716529b --- /dev/null +++ b/src/test/kotlin/dev/dnpm/etl/processor/FunctionsTest.kt @@ -0,0 +1,69 @@ +/* + * This file is part of ETL-Processor + * + * Copyright (c) 2026 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package dev.dnpm.etl.processor + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource +import org.springframework.data.domain.PageRequest +import org.springframework.data.domain.Pageable +import java.util.stream.Stream + +class FunctionsTest { + @ParameterizedTest + @MethodSource("listToPageTestData") + fun shouldConvertListToPage( + list: List<Int>, + pageable: Pageable, + expected: List<Int>, + ) { + val actual = list.toPage(pageable) + assertThat(actual.content).containsExactlyElementsOf(expected) + assertThat(actual.totalElements).isEqualTo(list.size.toLong()) + } + + companion object { + @JvmStatic + fun listToPageTestData(): Stream<Arguments> = + Stream.of( + Arguments.of( + listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), + PageRequest.of(0, 5), + listOf(1, 2, 3, 4, 5), + ), + Arguments.of( + listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), + PageRequest.of(1, 5), + listOf(6, 7, 8, 9, 10), + ), + Arguments.of( + listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), + PageRequest.of(2, 5), + emptyList<Int>(), + ), + Arguments.of( + emptyList<Int>(), + PageRequest.of(1, 5), + emptyList<Int>(), + ), + ) + } +} |
