|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.repository.util; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.Mockito.spy; |
| 20 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 21 | + |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import org.bson.Document; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | +import org.springframework.data.domain.PageRequest; |
| 29 | +import org.springframework.data.domain.Pageable; |
| 30 | +import org.springframework.data.domain.Sort; |
| 31 | +import org.springframework.data.domain.Sort.Direction; |
| 32 | +import org.springframework.data.mongodb.core.query.BasicQuery; |
| 33 | +import org.springframework.data.mongodb.core.query.Query; |
| 34 | + |
| 35 | +/** |
| 36 | + * Unit test for {@link SliceUtils}. |
| 37 | + * |
| 38 | + * @author Christoph Strobl |
| 39 | + */ |
| 40 | +class SliceUtilsUnitTests { |
| 41 | + |
| 42 | + @ParameterizedTest // GH-4889 |
| 43 | + @MethodSource("paged") |
| 44 | + void pagedPageableModifiesQuery(Pageable page) { |
| 45 | + |
| 46 | + Query source = new BasicQuery(Document.parse("{ 'spring' : 'data' }")); |
| 47 | + |
| 48 | + Query target = SliceUtils.limitResult(source, page); |
| 49 | + |
| 50 | + assertThat(target.getQueryObject()).isEqualTo(source.getQueryObject()); |
| 51 | + assertThat(target).isNotSameAs(source); |
| 52 | + assertThat(target.isLimited()).isTrue(); |
| 53 | + assertThat(target.getSkip()).isEqualTo(page.getOffset()); |
| 54 | + assertThat(target.getLimit()).isEqualTo(page.toLimit().max() + 1); |
| 55 | + assertThat(target.getSortObject()).isEqualTo(source.getSortObject()); |
| 56 | + } |
| 57 | + |
| 58 | + @ParameterizedTest // GH-4889 |
| 59 | + @MethodSource("unpaged") |
| 60 | + void unpagedPageableDoesNotModifyQuery(Pageable page) { |
| 61 | + |
| 62 | + Query source = spy(new BasicQuery(Document.parse("{ 'spring' : 'data' }"))); |
| 63 | + |
| 64 | + Query target = SliceUtils.limitResult(source, page); |
| 65 | + |
| 66 | + verifyNoInteractions(source); |
| 67 | + |
| 68 | + assertThat(target).isSameAs(source); |
| 69 | + assertThat(target.isLimited()).isFalse(); |
| 70 | + } |
| 71 | + |
| 72 | + public static Stream<Arguments> paged() { |
| 73 | + return Stream.of(Arguments.of(Pageable.ofSize(1)), Arguments.of(PageRequest.of(0, 10)), |
| 74 | + Arguments.of(PageRequest.of(0, 10, Direction.ASC, "name"))); |
| 75 | + } |
| 76 | + |
| 77 | + public static Stream<Arguments> unpaged() { |
| 78 | + return Stream.of(Arguments.of(Pageable.unpaged()), Arguments.of(Pageable.unpaged(Sort.by("name")))); |
| 79 | + } |
| 80 | +} |
0 commit comments