From 9f88ff0d2e488b6a14d1b968f317b568b0d542b8 Mon Sep 17 00:00:00 2001 From: lor6 Date: Tue, 16 May 2017 11:52:04 +0300 Subject: [PATCH] pageable controller and test (#1862) * pageable controller and test * removed unused imports --- .../persistence/service/IFooService.java | 4 + .../persistence/service/impl/FooService.java | 7 ++ .../web/controller/FooController.java | 14 ++++ .../org/baeldung/web/FooPageableLiveTest.java | 76 +++++++++++++++++++ .../java/org/baeldung/web/LiveTestSuite.java | 1 + 5 files changed, 102 insertions(+) create mode 100644 spring-security-rest-full/src/test/java/org/baeldung/web/FooPageableLiveTest.java diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java index 60d607b9ef..a3d16d9c15 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java @@ -2,9 +2,13 @@ package org.baeldung.persistence.service; import org.baeldung.persistence.IOperations; import org.baeldung.persistence.model.Foo; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; public interface IFooService extends IOperations { Foo retrieveByName(String name); + + Page findPaginated(Pageable pageable); } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java index 533c0f5082..49f9ec7e97 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java @@ -7,6 +7,8 @@ import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.service.IFooService; import org.baeldung.persistence.service.common.AbstractService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Service; @@ -47,4 +49,9 @@ public class FooService extends AbstractService implements IFooService { return Lists.newArrayList(getDao().findAll()); } + @Override + public Page findPaginated(Pageable pageable) { + return dao.findAll(pageable); + } + } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java index c50e80bec2..484a59f8ef 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java @@ -14,9 +14,11 @@ import org.baeldung.web.util.RestPreconditions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -81,6 +83,18 @@ public class FooController { return resultPage.getContent(); } + + @GetMapping("/pageable") + @ResponseBody + public List findPaginatedWithPageable(Pageable pageable, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) { + final Page resultPage = service.findPaginated(pageable); + if (pageable.getPageNumber() > resultPage.getTotalPages()) { + throw new MyResourceNotFoundException(); + } + eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent(Foo.class, uriBuilder, response, pageable.getPageNumber(), resultPage.getTotalPages(), pageable.getPageSize())); + + return resultPage.getContent(); + } // write diff --git a/spring-security-rest-full/src/test/java/org/baeldung/web/FooPageableLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/web/FooPageableLiveTest.java new file mode 100644 index 0000000000..f43ca34bba --- /dev/null +++ b/spring-security-rest-full/src/test/java/org/baeldung/web/FooPageableLiveTest.java @@ -0,0 +1,76 @@ +package org.baeldung.web; + +import static org.baeldung.Consts.APPLICATION_PORT; +import static org.hamcrest.Matchers.is; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; + +import java.util.List; + +import org.baeldung.common.web.AbstractBasicLiveTest; +import org.baeldung.persistence.model.Foo; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.baeldung.spring.ConfigTest; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import org.junit.runner.RunWith; +import org.junit.Test; +import io.restassured.response.Response; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class) +@ActiveProfiles("test") +public class FooPageableLiveTest extends AbstractBasicLiveTest { + + public FooPageableLiveTest() { + super(Foo.class); + } + + // API + + @Override + public final void create() { + create(new Foo(randomAlphabetic(6))); + } + + @Override + public final String createAsUri() { + return createAsUri(new Foo(randomAlphabetic(6))); + } + + @Test + public void whenResourcesAreRetrievedPaged_then200IsReceived() { + final Response response = givenAuth().get(getPageableURL() + "?page=0&size=10"); + + assertThat(response.getStatusCode(), is(200)); + } + + @Test + public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() { + final String url = getPageableURL() + "?page=" + randomNumeric(5) + "&size=10"; + final Response response = givenAuth().get(url); + + assertThat(response.getStatusCode(), is(404)); + } + + @Test + public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { + create(); + + final Response response = givenAuth().get(getPageableURL() + "?page=0&size=10"); + + assertFalse(response.body().as(List.class).isEmpty()); + } + + protected String getPageableURL() { + return "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/auth/foos/pageable"; + } + +} diff --git a/spring-security-rest-full/src/test/java/org/baeldung/web/LiveTestSuite.java b/spring-security-rest-full/src/test/java/org/baeldung/web/LiveTestSuite.java index c3353fac3c..5d3d7fc518 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/web/LiveTestSuite.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/web/LiveTestSuite.java @@ -10,6 +10,7 @@ import org.junit.runners.Suite; JPASpecificationLiveTest.class ,FooDiscoverabilityLiveTest.class ,FooLiveTest.class + ,FooPageableLiveTest.class ,MyUserLiveTest.class }) // public class LiveTestSuite {