Skip to content

Commit 9f88ff0

Browse files
lor6pivovarit
authored andcommitted
pageable controller and test (eugenp#1862)
* pageable controller and test * removed unused imports
1 parent 15fe1f0 commit 9f88ff0

5 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import org.baeldung.persistence.IOperations;
44
import org.baeldung.persistence.model.Foo;
5+
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.Pageable;
57

68
public interface IFooService extends IOperations<Foo> {
79

810
Foo retrieveByName(String name);
11+
12+
Page<Foo> findPaginated(Pageable pageable);
913

1014
}

‎spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.baeldung.persistence.service.IFooService;
88
import org.baeldung.persistence.service.common.AbstractService;
99
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.data.domain.Page;
11+
import org.springframework.data.domain.Pageable;
1012
import org.springframework.data.repository.PagingAndSortingRepository;
1113
import org.springframework.security.access.prepost.PreAuthorize;
1214
import org.springframework.stereotype.Service;
@@ -47,4 +49,9 @@ public List<Foo> findAll() {
4749
return Lists.newArrayList(getDao().findAll());
4850
}
4951

52+
@Override
53+
public Page<Foo> findPaginated(Pageable pageable) {
54+
return dao.findAll(pageable);
55+
}
56+
5057
}

‎spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
import org.springframework.beans.factory.annotation.Autowired;
1515
import org.springframework.context.ApplicationEventPublisher;
1616
import org.springframework.data.domain.Page;
17+
import org.springframework.data.domain.Pageable;
1718
import org.springframework.http.HttpStatus;
1819
import org.springframework.http.MediaType;
1920
import org.springframework.stereotype.Controller;
21+
import org.springframework.web.bind.annotation.GetMapping;
2022
import org.springframework.web.bind.annotation.PathVariable;
2123
import org.springframework.web.bind.annotation.RequestBody;
2224
import org.springframework.web.bind.annotation.RequestMapping;
@@ -81,6 +83,18 @@ public List<Foo> findPaginated(@RequestParam("page") final int page, @RequestPar
8183

8284
return resultPage.getContent();
8385
}
86+
87+
@GetMapping("/pageable")
88+
@ResponseBody
89+
public List<Foo> findPaginatedWithPageable(Pageable pageable, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
90+
final Page<Foo> resultPage = service.findPaginated(pageable);
91+
if (pageable.getPageNumber() > resultPage.getTotalPages()) {
92+
throw new MyResourceNotFoundException();
93+
}
94+
eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent<Foo>(Foo.class, uriBuilder, response, pageable.getPageNumber(), resultPage.getTotalPages(), pageable.getPageSize()));
95+
96+
return resultPage.getContent();
97+
}
8498

8599
// write
86100

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.baeldung.web;
2+
3+
import static org.baeldung.Consts.APPLICATION_PORT;
4+
import static org.hamcrest.Matchers.is;
5+
6+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
7+
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
8+
9+
import java.util.List;
10+
11+
import org.baeldung.common.web.AbstractBasicLiveTest;
12+
import org.baeldung.persistence.model.Foo;
13+
import org.springframework.test.context.ActiveProfiles;
14+
import org.springframework.test.context.ContextConfiguration;
15+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16+
import org.baeldung.spring.ConfigTest;
17+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
18+
19+
import org.junit.runner.RunWith;
20+
import org.junit.Test;
21+
import io.restassured.response.Response;
22+
23+
import static org.hamcrest.Matchers.is;
24+
import static org.junit.Assert.assertFalse;
25+
import static org.junit.Assert.assertThat;
26+
27+
@RunWith(SpringJUnit4ClassRunner.class)
28+
@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class)
29+
@ActiveProfiles("test")
30+
public class FooPageableLiveTest extends AbstractBasicLiveTest<Foo> {
31+
32+
public FooPageableLiveTest() {
33+
super(Foo.class);
34+
}
35+
36+
// API
37+
38+
@Override
39+
public final void create() {
40+
create(new Foo(randomAlphabetic(6)));
41+
}
42+
43+
@Override
44+
public final String createAsUri() {
45+
return createAsUri(new Foo(randomAlphabetic(6)));
46+
}
47+
48+
@Test
49+
public void whenResourcesAreRetrievedPaged_then200IsReceived() {
50+
final Response response = givenAuth().get(getPageableURL() + "?page=0&size=10");
51+
52+
assertThat(response.getStatusCode(), is(200));
53+
}
54+
55+
@Test
56+
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() {
57+
final String url = getPageableURL() + "?page=" + randomNumeric(5) + "&size=10";
58+
final Response response = givenAuth().get(url);
59+
60+
assertThat(response.getStatusCode(), is(404));
61+
}
62+
63+
@Test
64+
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
65+
create();
66+
67+
final Response response = givenAuth().get(getPageableURL() + "?page=0&size=10");
68+
69+
assertFalse(response.body().as(List.class).isEmpty());
70+
}
71+
72+
protected String getPageableURL() {
73+
return "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/auth/foos/pageable";
74+
}
75+
76+
}

‎spring-security-rest-full/src/test/java/org/baeldung/web/LiveTestSuite.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
JPASpecificationLiveTest.class
1111
,FooDiscoverabilityLiveTest.class
1212
,FooLiveTest.class
13+
,FooPageableLiveTest.class
1314
,MyUserLiveTest.class
1415
}) //
1516
public class LiveTestSuite {

0 commit comments

Comments
 (0)