Skip to content

Commit 50f2388

Browse files
shubhi22maibin
authored andcommitted
BAEL-2411 Spring Data Jpa Pagingg and Sorting (eugenp#5912)
1 parent 98ef5cd commit 50f2388

3 files changed

Lines changed: 165 additions & 3 deletions

File tree

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.baeldung.dao.repositories.product;
22

33
import com.baeldung.domain.product.Product;
4-
import org.springframework.data.jpa.repository.JpaRepository;
54

6-
public interface ProductRepository extends JpaRepository<Product, Integer> {
5+
import java.util.List;
76

7+
import org.springframework.data.domain.Pageable;
8+
import org.springframework.data.repository.PagingAndSortingRepository;
9+
10+
public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
11+
12+
List<Product> findAllByPrice(double price, Pageable pageable);
813
}

‎persistence-modules/spring-data-jpa/src/main/java/com/baeldung/domain/product/Product.java‎

100644100755
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ public Product() {
1919
super();
2020
}
2121

22+
private Product(int id, String name, double price) {
23+
super();
24+
this.id = id;
25+
this.name = name;
26+
this.price = price;
27+
}
28+
29+
public static Product from(int id, String name, double price) {
30+
return new Product(id, name, price);
31+
}
32+
2233
public int getId() {
2334
return id;
2435
}
@@ -46,7 +57,11 @@ public void setPrice(final double price) {
4657
@Override
4758
public String toString() {
4859
final StringBuilder builder = new StringBuilder();
49-
builder.append("Product [name=").append(name).append(", id=").append(id).append("]");
60+
builder.append("Product [name=")
61+
.append(name)
62+
.append(", id=")
63+
.append(id)
64+
.append("]");
5065
return builder.toString();
5166
}
5267
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package com.baeldung.dao.repositories.product;
2+
3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.hamcrest.Matchers.hasSize;
5+
import static org.junit.Assert.assertThat;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.data.domain.Page;
17+
import org.springframework.data.domain.PageRequest;
18+
import org.springframework.data.domain.Pageable;
19+
import org.springframework.data.domain.Sort;
20+
import org.springframework.test.context.ContextConfiguration;
21+
import org.springframework.test.context.junit4.SpringRunner;
22+
import org.springframework.transaction.annotation.EnableTransactionManagement;
23+
import org.springframework.transaction.annotation.Transactional;
24+
25+
import com.baeldung.config.PersistenceProductConfiguration;
26+
import com.baeldung.domain.product.Product;
27+
28+
@RunWith(SpringRunner.class)
29+
@ContextConfiguration(classes = { PersistenceProductConfiguration.class })
30+
@EnableTransactionManagement
31+
public class ProductRepositoryIntegrationTest {
32+
33+
@Autowired
34+
private ProductRepository productRepository;
35+
36+
@Before
37+
@Transactional("productTransactionManager")
38+
public void setUp() {
39+
productRepository.save(Product.from(1001, "Book", 21));
40+
productRepository.save(Product.from(1002, "Coffee", 10));
41+
productRepository.save(Product.from(1003, "Jeans", 30));
42+
productRepository.save(Product.from(1004, "Shirt", 32));
43+
productRepository.save(Product.from(1005, "Bacon", 10));
44+
}
45+
46+
@Test
47+
public void whenRequestingFirstPageOfSizeTwo_ThenReturnFirstPage() {
48+
Pageable pageRequest = PageRequest.of(0, 2);
49+
50+
Page<Product> result = productRepository.findAll(pageRequest);
51+
52+
assertThat(result.getContent(), hasSize(2));
53+
assertTrue(result.stream()
54+
.map(Product::getId)
55+
.allMatch(id -> Arrays.asList(1001, 1002)
56+
.contains(id)));
57+
}
58+
59+
@Test
60+
public void whenRequestingSecondPageOfSizeTwo_ThenReturnSecondPage() {
61+
Pageable pageRequest = PageRequest.of(1, 2);
62+
63+
Page<Product> result = productRepository.findAll(pageRequest);
64+
65+
assertThat(result.getContent(), hasSize(2));
66+
assertTrue(result.stream()
67+
.map(Product::getId)
68+
.allMatch(id -> Arrays.asList(1003, 1004)
69+
.contains(id)));
70+
}
71+
72+
@Test
73+
public void whenRequestingLastPage_ThenReturnLastPageWithRemData() {
74+
Pageable pageRequest = PageRequest.of(2, 2);
75+
76+
Page<Product> result = productRepository.findAll(pageRequest);
77+
78+
assertThat(result.getContent(), hasSize(1));
79+
assertTrue(result.stream()
80+
.map(Product::getId)
81+
.allMatch(id -> Arrays.asList(1005)
82+
.contains(id)));
83+
}
84+
85+
@Test
86+
public void whenSortingByNameAscAndPaging_ThenReturnSortedPagedResult() {
87+
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("name"));
88+
89+
Page<Product> result = productRepository.findAll(pageRequest);
90+
91+
assertThat(result.getContent(), hasSize(3));
92+
assertThat(result.getContent()
93+
.stream()
94+
.map(Product::getId)
95+
.collect(Collectors.toList()), equalTo(Arrays.asList(1005, 1001, 1002)));
96+
97+
}
98+
99+
@Test
100+
public void whenSortingByPriceDescAndPaging_ThenReturnSortedPagedResult() {
101+
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("price")
102+
.descending());
103+
104+
Page<Product> result = productRepository.findAll(pageRequest);
105+
106+
assertThat(result.getContent(), hasSize(3));
107+
assertThat(result.getContent()
108+
.stream()
109+
.map(Product::getId)
110+
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001)));
111+
112+
}
113+
114+
@Test
115+
public void whenSortingByPriceDescAndNameAscAndPaging_ThenReturnSortedPagedResult() {
116+
Pageable pageRequest = PageRequest.of(0, 5, Sort.by("price")
117+
.descending()
118+
.and(Sort.by("name")));
119+
120+
Page<Product> result = productRepository.findAll(pageRequest);
121+
122+
assertThat(result.getContent(), hasSize(5));
123+
assertThat(result.getContent()
124+
.stream()
125+
.map(Product::getId)
126+
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001, 1005, 1002)));
127+
128+
}
129+
130+
@Test
131+
public void whenRequestingFirstPageOfSizeTwoUsingCustomMethod_ThenReturnFirstPage() {
132+
Pageable pageRequest = PageRequest.of(0, 2);
133+
134+
List<Product> result = productRepository.findAllByPrice(10, pageRequest);
135+
136+
assertThat(result, hasSize(2));
137+
assertTrue(result.stream()
138+
.map(Product::getId)
139+
.allMatch(id -> Arrays.asList(1002, 1005)
140+
.contains(id)));
141+
}
142+
}

0 commit comments

Comments
 (0)