|
| 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 | +} |
0 commit comments