Skip to content
Snippets Groups Projects
Commit 7fd68b2a authored by Ruben Backx's avatar Ruben Backx :coffee:
Browse files

Merge branch 'delete-pageutil-from-queue' into 'development'

[Refactor PageUtil] Refactor Queue usage of Pageutil to use Librador PageUtil

See merge request !790
parents 622f0085 200b373a
No related branches found
No related tags found
2 merge requests!797Deploy,!790[Refactor PageUtil] Refactor Queue usage of Pageutil to use Librador PageUtil
/*
* Queue - A Queueing system that can be used to handle labs in higher education
* Copyright (C) 2016-2024 Delft University of Technology
*
* 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 nl.tudelft.queue;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
/**
* A utility class used for turning lists into pages.
*/
public class PageUtil {
/**
* Turns the given list of values into a page of values which represent a sublist of the original.
*
* @param pageable The pageable describing what to show on the page.
* @param values The total list of values of which a page needs to be shown.
* @param comparator The comparator used to sort the values
* @param <T> The type of the values in the page.
* @return The Page cut out from the original list of values.
*/
public static <T> Page<T> toPage(Pageable pageable, List<T> values, Comparator<T> comparator) {
if (values == null || values.isEmpty())
return new PageImpl<>(new ArrayList<>(), pageable, 0);
values = new ArrayList<>(values);
if (comparator != null) {
values.sort(comparator);
}
int start = (int) pageable.getOffset();
int end = Math.min(start + pageable.getPageSize(), values.size());
if (start < 0 || start >= values.size()) {
return new PageImpl<>(List.of(), pageable, values.size());
}
return new PageImpl<>(values.subList(start, end), pageable, values.size());
}
/**
* Turns the given list of values into a page of values which represent a sublist of the original.
*
* @param pageable The pageable describing what to show on the page.
* @param values The total list of values of which a page needs to be shown.
* @param <T> The type of the values in the page.
* @return The Page cut out from the original list of values.
*/
public static <T> Page<T> toPage(Pageable pageable, List<T> values) {
return toPage(pageable, values, null);
}
}
......@@ -18,7 +18,7 @@
package nl.tudelft.queue.controller;
import static java.time.LocalDateTime.now;
import static nl.tudelft.queue.PageUtil.toPage;
import static nl.tudelft.librador.Util.PageUtil.toPage;
import java.io.IOException;
import java.util.Comparator;
......@@ -54,8 +54,8 @@ import nl.tudelft.labracore.api.dto.*;
import nl.tudelft.labracore.lib.security.user.AuthenticatedPerson;
import nl.tudelft.labracore.lib.security.user.DefaultRole;
import nl.tudelft.labracore.lib.security.user.Person;
import nl.tudelft.librador.Util.PageUtil;
import nl.tudelft.librador.dto.view.View;
import nl.tudelft.queue.PageUtil;
import nl.tudelft.queue.cache.*;
import nl.tudelft.queue.csv.EmptyCsvException;
import nl.tudelft.queue.csv.InvalidCsvException;
......
......@@ -31,8 +31,8 @@ import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import nl.tudelft.labracore.lib.security.user.AuthenticatedPerson;
import nl.tudelft.labracore.lib.security.user.Person;
import nl.tudelft.librador.Util.PageUtil;
import nl.tudelft.librador.dto.view.View;
import nl.tudelft.queue.PageUtil;
import nl.tudelft.queue.cache.PersonCacheManager;
import nl.tudelft.queue.dto.view.FeedbackViewDTO;
import nl.tudelft.queue.model.Feedback;
......
......@@ -40,8 +40,8 @@ import nl.tudelft.labracore.api.EditionControllerApi;
import nl.tudelft.labracore.api.SessionControllerApi;
import nl.tudelft.labracore.api.dto.*;
import nl.tudelft.labracore.lib.security.user.Person;
import nl.tudelft.librador.Util.PageUtil;
import nl.tudelft.librador.dto.view.View;
import nl.tudelft.queue.PageUtil;
import nl.tudelft.queue.cache.*;
import nl.tudelft.queue.dto.util.RequestTableFilterDTO;
import nl.tudelft.queue.dto.view.QueueSessionSummaryDTO;
......
/*
* Queue - A Queueing system that can be used to handle labs in higher education
* Copyright (C) 2016-2024 Delft University of Technology
*
* 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 nl.tudelft.queue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
class PageUtilTest {
String s1;
String s2;
String s3;
String s4;
String s5;
String s6;
Comparator<String> stringComparator;
@BeforeEach
void setup() {
s1 = "1111";
s2 = "2222";
s3 = "3333";
s4 = "4444";
s5 = "5555";
s6 = "6666";
stringComparator = String::compareToIgnoreCase;
}
@Test
void testNullAndEmpty() {
Pageable pageable = PageRequest.of(0, 1);
Page<String> expectedResult = new PageImpl<>(List.of(), pageable, 0);
assertThat(PageUtil.toPage(pageable, null, stringComparator)).isEqualTo(expectedResult);
assertThat(PageUtil.toPage(pageable, Collections.emptyList(), stringComparator))
.isEqualTo(expectedResult);
}
@Test
void immutableListStillReturnsCorrectPageSorted() {
List<String> values = List.of(s2, s3, s5, s4, s6, s1);
Pageable pageable = PageRequest.of(0, 3);
assertThat(PageUtil.toPage(pageable, values, stringComparator).getContent()).containsExactly(s1, s2,
s3);
}
@Test
void outOfBoundsJustReturnsAnEmptyPage() {
List<String> values = List.of(s2, s3, s5, s4, s6, s1);
var sut = PageUtil.toPage(PageRequest.of(3, 2), values, stringComparator);
assertThat(sut.getContent()).isEmpty();
assertThat(sut.getTotalPages()).isEqualTo(3);
assertThat(sut.getTotalElements()).isEqualTo(6L);
}
@Test
void pageSizeBiggerReturnsTheWholeContent() {
List<String> values = List.of(s2, s3, s5, s4, s6, s1);
var sut = PageUtil.toPage(PageRequest.of(0, 100), values, stringComparator.reversed());
assertThat(sut.getContent()).containsExactlyInAnyOrderElementsOf(values);
assertThat(sut.getContent()).isSortedAccordingTo(stringComparator.reversed());
}
@Test
void edgeCasePage() {
List<String> values = List.of(s2, s3, s5, s4, s6, s1);
var sut = PageUtil.toPage(PageRequest.of(5, 1), values, stringComparator);
assertThat(sut.getNumberOfElements()).isEqualTo(1);
assertThat(sut.getContent().get(0)).isEqualTo(s6);
}
@Test
void nullComparatorDoesNotSortTheListWhenConvertingToAPage() {
List<String> values = List.of(s2, s3, s5, s4, s6, s1);
var sut = PageUtil.toPage(PageRequest.of(0, 99), values);
assertThat(sut.getContent()).isEqualTo(values);
}
@Test
void nullComparatorTakesTheCorrectValuesIfPageSizeIsSmallerThanResultSet() {
List<String> values = List.of(s2, s3, s5, s4, s6, s1);
var sut = PageUtil.toPage(PageRequest.of(1, 2), values);
assertThat(sut.getContent()).isEqualTo(List.of(s5, s4));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment