-
Notifications
You must be signed in to change notification settings - Fork 1
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
eb8441b
commit 5dc6d5d
Showing
8 changed files
with
95 additions
and
14 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
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: 6 additions & 1 deletion
7
src/main/java/by/andd3dfx/templateapp/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,13 +1,18 @@ | ||
package by.andd3dfx.templateapp.persistence.dao; | ||
|
||
import by.andd3dfx.templateapp.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; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface ArticleRepository extends PagingAndSortingRepository<Article, Long>, ArticleRepositoryCustom { | ||
public interface ArticleRepository extends CrudRepository<Article, Long>, ArticleRepositoryCustom { | ||
|
||
List<Article> getArticleByCountryNCity(String country, String city); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
@NoArgsConstructor | ||
@Data | ||
public class Location { | ||
|
||
private String country; | ||
private String city; | ||
} |
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
76 changes: 76 additions & 0 deletions
76
src/test/java/by/andd3dfx/templateapp/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,76 @@ | ||
package by.andd3dfx.templateapp.persistence.dao; | ||
|
||
import by.andd3dfx.templateapp.IntegrationTestInitializer; | ||
import by.andd3dfx.templateapp.persistence.entities.Article; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
@ContextConfiguration(initializers = IntegrationTestInitializer.class) | ||
@SpringBootTest | ||
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)); | ||
} | ||
|
||
@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).getTitle(), is(entity3.getTitle())); | ||
assertThat(articles.get(0).getSummary(), is(entity3.getSummary())); | ||
|
||
assertThat(articles.get(1).getTitle(), is(entity.getTitle())); | ||
assertThat(articles.get(1).getSummary(), is(entity.getSummary())); | ||
} | ||
|
||
private static Article buildArticle(String title, String summary, LocalDateTime timestamp) { | ||
var result = new Article(); | ||
result.setTitle(title); | ||
result.setSummary(summary); | ||
result.setText("any text"); | ||
result.setTimestamp(timestamp); | ||
result.setAuthor("Some author"); | ||
return result; | ||
} | ||
} |