From 2f0db5e519e405c26ba13ea619fcaf26dcb9cd36 Mon Sep 17 00:00:00 2001 From: haerong22 Date: Tue, 16 Aug 2022 03:34:18 +0900 Subject: [PATCH] #16 board : pagination service impl & test --- .../board/service/PaginationService.java | 23 +++++++ .../board/service/PaginationServiceTest.java | 67 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 board/src/main/java/com/example/board/service/PaginationService.java create mode 100644 board/src/test/java/com/example/board/service/PaginationServiceTest.java diff --git a/board/src/main/java/com/example/board/service/PaginationService.java b/board/src/main/java/com/example/board/service/PaginationService.java new file mode 100644 index 00000000..d70c9d23 --- /dev/null +++ b/board/src/main/java/com/example/board/service/PaginationService.java @@ -0,0 +1,23 @@ +package com.example.board.service; + +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.IntStream; + +@Service +public class PaginationService { + + private static final int BAR_LENGTH = 5; + + public List getPaginationBarNumbers(int currentPageNumber, int totalPages) { + int startNumber = Math.max(currentPageNumber - (BAR_LENGTH / 2), 0); + int endNumber = Math.min(startNumber + BAR_LENGTH, totalPages); + + return IntStream.range(startNumber, endNumber).boxed().toList(); + } + + public int currentBarLength() { + return BAR_LENGTH; + } +} diff --git a/board/src/test/java/com/example/board/service/PaginationServiceTest.java b/board/src/test/java/com/example/board/service/PaginationServiceTest.java new file mode 100644 index 00000000..6ecd90e8 --- /dev/null +++ b/board/src/test/java/com/example/board/service/PaginationServiceTest.java @@ -0,0 +1,67 @@ +package com.example.board.service; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +@DisplayName("비지니스 로직 - 페이지네이션") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = PaginationService.class) +class PaginationServiceTest { + + private final PaginationService sut; + + + public PaginationServiceTest(@Autowired PaginationService paginationService) { + this.sut = paginationService; + } + + @DisplayName("현재 페이지 번호와 총 페이지 수를 주면, 페이징 바 리스트를 만들어준다.") + @MethodSource + @ParameterizedTest(name = "[{index}] {0}, {1} => {2}") + void givenCurrentPageNumberAndTotalPages_whenCalculating_thenReturnsPaginationBarNumbers(int currentPageNumber, int totalPages, List expected) { + // given + + // when + List actual = sut.getPaginationBarNumbers(currentPageNumber, totalPages); + + // then + assertThat(actual).isEqualTo(expected); + } + + static Stream givenCurrentPageNumberAndTotalPages_whenCalculating_thenReturnsPaginationBarNumbers() { + return Stream.of( + arguments(0, 13, List.of(0, 1, 2, 3, 4)), + arguments(1, 13, List.of(0, 1, 2, 3, 4)), + arguments(2, 13, List.of(0, 1, 2, 3, 4)), + arguments(3, 13, List.of(1, 2, 3, 4, 5)), + arguments(4, 13, List.of(2, 3, 4, 5, 6)), + arguments(5, 13, List.of(3, 4, 5, 6, 7)), + arguments(6, 13, List.of(4, 5, 6, 7, 8)), + arguments(10, 13, List.of(8, 9, 10, 11, 12)), + arguments(11, 13, List.of(9, 10, 11, 12)), + arguments(12, 13, List.of(10, 11, 12)) + ); + } + + @DisplayName("현재 설정되어 있는 페이지네이션 바의 길이를 알려준다.") + @Test + void givenNothing_whenCalling_thenReturnsCurrentBarLength() { + // given + + // when + int barLength = sut.currentBarLength(); + + // then + assertThat(barLength).isEqualTo(5); + } +} \ No newline at end of file