pageable controller and test (#1862)
* pageable controller and test * removed unused imports
This commit is contained in:
parent
15fe1f0996
commit
9f88ff0d2e
|
@ -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> {
|
||||
|
||||
Foo retrieveByName(String name);
|
||||
|
||||
Page<Foo> findPaginated(Pageable pageable);
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Foo> implements IFooService {
|
|||
return Lists.newArrayList(getDao().findAll());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Foo> findPaginated(Pageable pageable) {
|
||||
return dao.findAll(pageable);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Foo> findPaginatedWithPageable(Pageable pageable, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
|
||||
final Page<Foo> resultPage = service.findPaginated(pageable);
|
||||
if (pageable.getPageNumber() > resultPage.getTotalPages()) {
|
||||
throw new MyResourceNotFoundException();
|
||||
}
|
||||
eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent<Foo>(Foo.class, uriBuilder, response, pageable.getPageNumber(), resultPage.getTotalPages(), pageable.getPageSize()));
|
||||
|
||||
return resultPage.getContent();
|
||||
}
|
||||
|
||||
// write
|
||||
|
||||
|
|
|
@ -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<Foo> {
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
}
|
|
@ -10,6 +10,7 @@ import org.junit.runners.Suite;
|
|||
JPASpecificationLiveTest.class
|
||||
,FooDiscoverabilityLiveTest.class
|
||||
,FooLiveTest.class
|
||||
,FooPageableLiveTest.class
|
||||
,MyUserLiveTest.class
|
||||
}) //
|
||||
public class LiveTestSuite {
|
||||
|
|
Loading…
Reference in New Issue