|
| 1 | +/* |
| 2 | + * Copyright 2023 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.core; |
| 17 | + |
| 18 | +import static org.assertj.core.api.AssertionsForClassTypes.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.springframework.data.domain.KeysetScrollPosition; |
| 27 | +import org.springframework.data.domain.ScrollPosition; |
| 28 | +import org.springframework.data.domain.Window; |
| 29 | +import org.springframework.data.mongodb.core.EntityOperations.Entity; |
| 30 | +import org.springframework.data.mongodb.core.query.Query; |
| 31 | + |
| 32 | +/** |
| 33 | + * Unit tests for {@link ScrollUtils}. |
| 34 | + * |
| 35 | + * @author Mark Paluch |
| 36 | + */ |
| 37 | +class ScrollUtilsUnitTests { |
| 38 | + |
| 39 | + @Test // GH-4413 |
| 40 | + void positionShouldRetainScrollDirection() { |
| 41 | + |
| 42 | + Query query = new Query(); |
| 43 | + query.with(ScrollPosition.keyset().backward()); |
| 44 | + EntityOperations entityOperationsMock = mock(EntityOperations.class); |
| 45 | + Entity entityMock = mock(Entity.class); |
| 46 | + |
| 47 | + when(entityOperationsMock.forEntity(any())).thenReturn(entityMock); |
| 48 | + when(entityMock.extractKeys(any(), any())).thenReturn(Map.of("k", "v")); |
| 49 | + |
| 50 | + Window<Integer> window = ScrollUtils.createWindow(query, new ArrayList<>(List.of(1, 2, 3)), Integer.class, |
| 51 | + entityOperationsMock); |
| 52 | + |
| 53 | + assertThat(window.positionAt(0)).isInstanceOf(KeysetScrollPosition.class); |
| 54 | + assertThat(((KeysetScrollPosition) window.positionAt(0)).scrollsBackward()).isTrue(); |
| 55 | + } |
| 56 | +} |
0 commit comments