-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Page<> with Slice<> for pagination operations
According to: https://www.youtube.com/watch?v=wi6h9ox1wwM
- Loading branch information
1 parent
da0bbee
commit 914a58f
Showing
11 changed files
with
99 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
src/main/java/by/andd3dfx/persistence/dao/ArticleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package by.andd3dfx.persistence.dao; | ||
|
||
import by.andd3dfx.persistence.entities.Article; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.PagingAndSortingRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ArticleRepository extends PagingAndSortingRepository<Article, Long>, | ||
CrudRepository<Article, Long>, ArticleRepositoryCustom { | ||
public interface ArticleRepository extends CrudRepository<Article, Long>, ArticleRepositoryCustom { | ||
|
||
Slice<Article> findAll(Pageable pageable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/by/andd3dfx/persistence/dao/ArticleRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package by.andd3dfx.persistence.dao; | ||
|
||
import by.andd3dfx.persistence.entities.Article; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static by.andd3dfx.persistence.dao.ArticleRepositoryCustomImplTest.buildArticle; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@DataJpaTest | ||
class ArticleRepositoryTest { | ||
|
||
@Autowired | ||
private ArticleRepository repository; | ||
|
||
private Article entity; | ||
private Article entity2; | ||
private Article entity3; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
repository.deleteAll(); | ||
entity = buildArticle("Ivan", "HD", LocalDateTime.parse("2010-12-03T10:15:30")); | ||
entity2 = buildArticle("Vasily", "HD", LocalDateTime.parse("2011-12-03T10:15:30")); | ||
entity3 = buildArticle("Ivan", "4K", LocalDateTime.parse("2012-12-03T10:15:30")); | ||
repository.saveAll(Arrays.asList(entity, entity2, entity3)); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
repository.deleteAll(); | ||
} | ||
|
||
@Test | ||
public void findAll() { | ||
var result = repository.findAll(Pageable.ofSize(10)); | ||
|
||
assertThat("Wrong records amount", result.getNumberOfElements(), is(3)); | ||
assertTrue(result.getContent().containsAll(List.of(entity, entity2, entity3))); | ||
} | ||
|
||
@Test | ||
public void findAll_withPageNSizeNSorting() { | ||
var result = repository.findAll(PageRequest.of(0, 2, Sort.by("title", "summary"))); | ||
|
||
assertThat("Wrong records amount", result.getNumberOfElements(), is(2)); | ||
var articles = result.getContent(); | ||
assertThat(articles.get(0), is(entity3)); | ||
assertThat(articles.get(1), is(entity)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters