Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover limit, findFirst, findTop query method keywords with tests #33

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<logback.version>1.5.12</logback.version>
<testcontainers.version>1.20.4</testcontainers.version>
<httpclient.version>4.5.14</httpclient.version>
<assertj.version>3.27.2</assertj.version>
</properties>

<profiles>
Expand Down Expand Up @@ -236,6 +237,12 @@
<version>${httpclient.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import ru.rt.restream.reindexer.Query.Condition;
import ru.rt.restream.reindexer.ReindexerIndex;

import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
Expand Down Expand Up @@ -156,29 +157,48 @@ protected Query<?> complete(Query<?> criteria, Sort sort) {
criteria = this.namespace.query();
}
if (this.returnedType.needsCustomConstruction()) {
criteria = criteria.select(this.returnedType.getInputProperties().toArray(String[]::new));
criteria.select(this.returnedType.getInputProperties().toArray(String[]::new));
}
else if (this.tree.isExistsProjection()) {
criteria = criteria.select(this.entityInformation.getIdFieldName());
criteria.select(this.entityInformation.getIdFieldName());
}
Pageable pageable = this.parameters.getPageable();
if (pageable.isPaged()) {
criteria.limit(pageable.getPageSize()).offset((int) pageable.getOffset());
criteria.limit(pageable.getPageSize()).offset(getOffsetAsInteger(pageable));
if (this.queryMethod.isPageQuery()) {
criteria = criteria.reqTotal();
}
}
if (sort.isSorted()) {
for (Order order : sort) {
criteria = criteria.sort(order.getProperty(), order.isDescending());
criteria.sort(order.getProperty(), order.isDescending());
}
}
if (this.tree.getMaxResults() != null) {
criteria = criteria.limit(this.tree.getMaxResults());
if (pageable.isPaged()) {
/*
* In order to return the correct results, we have to adjust the first result offset to be returned if:
* - a Pageable parameter is present
* - AND the requested page number > 0
* - AND the requested page size was bigger than the derived result limitation via the First/Top keyword.
*/
int firstResult = getOffsetAsInteger(pageable);
if (pageable.getPageSize() > this.tree.getMaxResults() && firstResult > 0) {
criteria.offset(firstResult - (pageable.getPageSize() - this.tree.getMaxResults()));
}
}
criteria.limit(this.tree.getMaxResults());
}
if (this.tree.isExistsProjection()) {
criteria.limit(1);
}
return criteria;
}

private int getOffsetAsInteger(Pageable pageable) {
if (pageable.getOffset() > Integer.MAX_VALUE) {
throw new InvalidDataAccessApiUsageException("Page offset exceeds Integer.MAX_VALUE (" + Integer.MAX_VALUE + ")");
}
return Math.toIntExact(pageable.getOffset());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ReindexerRepositoryQuery(ReindexerQueryMethod queryMethod, ReindexerEntit
this.namespace = new TransactionalNamespace<>(namespace);
this.tree = new PartTree(queryMethod.getName(), entityInformation.getJavaType());
this.queryExecution = Lazy.of(() -> {
if (queryMethod.isCollectionQuery() && !queryMethod.getParameters().hasPageableParameter()) {
if (queryMethod.isCollectionQuery()) {
return QueryMethodExecution.COLLECTION;
}
if (queryMethod.isStreamQuery()) {
Expand All @@ -95,7 +95,7 @@ public ReindexerRepositoryQuery(ReindexerQueryMethod queryMethod, ReindexerEntit
if (queryMethod.isIteratorQuery()) {
return QueryMethodExecution.ITERATOR;
}
if (queryMethod.getParameters().hasPageableParameter()) {
if (queryMethod.isPageQuery()) {
return QueryMethodExecution.PAGEABLE;
}
if (tree.isCountProjection()) {
Expand Down Expand Up @@ -173,15 +173,8 @@ public Object execute(ReindexerQueryCreator queryCreator) {
while (iterator.hasNext()) {
content.add(iterator.next());
}
if (queryCreator.getQueryMethod().isPageQuery()) {
Pageable pageable = queryCreator.getParameters().getPageable();
return pageable.isPaged() ? PageableExecutionUtils.getPage(content, pageable, iterator::getTotalCount)
: new PageImpl<>(content);
}
if (queryCreator.getQueryMethod().isListQuery()) {
return content;
}
throw new IllegalStateException("Unsupported return type for Pageable query " + queryCreator.getQueryMethod().getReturnType());
Pageable pageable = queryCreator.getParameters().getPageable();
return PageableExecutionUtils.getPage(content, pageable, iterator::getTotalCount);
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.PersistenceCreator;
import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
Expand All @@ -76,7 +78,13 @@
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for {@link ReindexerRepository}.
Expand Down Expand Up @@ -895,6 +903,28 @@ public void findPageByIdIn() {
assertEquals(0, expectedItems.size());
}

@Test
public void findFirst2By() {
TestItem item1 = this.repository.save(new TestItem(1L, "TestName1", "TestValue1"));
TestItem item2 = this.repository.save(new TestItem(2L, "TestName2", "TestValue2"));
TestItem item3 = this.repository.save(new TestItem(3L, "TestName3", "TestValue3"));
Page<TestItem> firstPage = this.repository.findFirst2By(PageRequest.of(0, 3, Direction.ASC, "id"));
assertThat(firstPage.getContent()).contains(item1, item2);
Page<TestItem> secondPage = this.repository.findFirst2By(PageRequest.of(1, 3, Direction.ASC, "id"));
assertThat(secondPage).contains(item3);
}

@Test
public void findFirst3By() {
TestItem item1 = this.repository.save(new TestItem(1L, "TestName1", "TestValue1"));
TestItem item2 = this.repository.save(new TestItem(2L, "TestName2", "TestValue2"));
TestItem item3 = this.repository.save(new TestItem(3L, "TestName3", "TestValue3"));
Page<TestItem> firstPage = this.repository.findFirst3By(PageRequest.of(0, 2, Direction.ASC, "id"));
assertThat(firstPage.getContent()).contains(item1, item2);
Page<TestItem> secondPage = this.repository.findFirst3By(PageRequest.of(1, 2, Direction.ASC, "id"));
assertThat(secondPage).contains(item3);
}

@Test
public void findAllPageable() {
Set<TestItem> expectedItems = new HashSet<>();
Expand Down Expand Up @@ -1005,6 +1035,87 @@ public void saveAndDelete() {
assertFalse(this.repository.existsById(testItem.getId()));
}

@Test
public void findAllByLimit() {
Set<TestItem> expectedItems = new HashSet<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
List<TestItem> foundItems = this.repository.findAllBy(Limit.of(10));
for (TestItem item : foundItems) {
assertTrue(expectedItems.remove(item));
}
assertEquals(90, expectedItems.size());
}

@Test
public void findFirstByOrderByIdAsc() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
TestItem foundItem = this.repository.findFirstByOrderByIdAsc().orElse(null);
assertNotNull(foundItem);
assertEquals(expectedItems.get(0), foundItem);
}

@Test
public void findFirstByOrderByIdDesc() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
TestItem foundItem = this.repository.findFirstByOrderByIdDesc().orElse(null);
assertNotNull(foundItem);
assertEquals(expectedItems.get(expectedItems.size() - 1), foundItem);
}

@Test
public void findTopByOrderByIdAsc() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
TestItem foundItem = this.repository.findTopByOrderByIdAsc().orElse(null);
assertEquals(expectedItems.get(0), foundItem);
}

@Test
public void findTopByOrderByIdDesc() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
TestItem foundItem = this.repository.findTopByOrderByIdDesc().orElse(null);
assertEquals(expectedItems.get(expectedItems.size() - 1), foundItem);
}

@Test
public void findTop10ByOrderByIdAsc() {
Set<TestItem> expectedItems = new HashSet<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
List<TestItem> foundItems = this.repository.findTop10ByOrderByIdAsc();
for (TestItem item : foundItems) {
assertTrue(expectedItems.remove(item));
}
assertEquals(90, expectedItems.size());
}

@Test
public void findTop10ByOrderByIdDesc() {
Set<TestItem> expectedItems = new HashSet<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
List<TestItem> foundItems = this.repository.findTop10ByOrderByIdDesc();
for (TestItem item : foundItems) {
assertTrue(expectedItems.remove(item));
}
assertEquals(90, expectedItems.size());
}

@Configuration
@EnableReindexerRepositories(basePackageClasses = TestItemReindexerRepository.class, considerNestedRepositories = true)
@EnableTransactionManagement
Expand Down Expand Up @@ -1155,6 +1266,10 @@ Optional<TestItem> findOneSqlByNameAndValueManyParams(String name1, String name2

Page<TestItem> findPageByIdIn(List<Long> ids, Pageable pageable);

Page<TestItem> findFirst2By(Pageable pageable);

Page<TestItem> findFirst3By(Pageable pageable);

List<TestItemProjection> findItemProjectionByIdIn(List<Long> ids);

List<TestItemDto> findItemDtoByIdIn(List<Long> ids);
Expand All @@ -1166,6 +1281,20 @@ Optional<TestItem> findOneSqlByNameAndValueManyParams(String name1, String name2
List<TestItemPreferredConstructorRecord> findItemPreferredConstructorRecordByIdIn(List<Long> ids);

<T> List<T> findByIdIn(List<Long> ids, Class<T> type);

List<TestItem> findAllBy(Limit limit);

Optional<TestItem> findFirstByOrderByIdAsc();

Optional<TestItem> findFirstByOrderByIdDesc();

Optional<TestItem> findTopByOrderByIdAsc();

Optional<TestItem> findTopByOrderByIdDesc();

List<TestItem> findTop10ByOrderByIdAsc();

List<TestItem> findTop10ByOrderByIdDesc();
}

@Namespace(name = NAMESPACE_NAME)
Expand Down
Loading