| 
									
										
										
										
											2019-01-20 18:19:52 -02:00
										 |  |  | package com.baeldung.web;
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 18:19:52 -02:00
										 |  |  | import static com.baeldung.Consts.APPLICATION_PORT;
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  | import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
 | 
					
						
							|  |  |  | import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:39:04 +03:00
										 |  |  | import static org.hamcrest.Matchers.is;
 | 
					
						
							|  |  |  | import static org.junit.Assert.assertFalse;
 | 
					
						
							|  |  |  | import static org.junit.Assert.assertThat;
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | import java.util.List;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:39:04 +03:00
										 |  |  | import org.junit.Test;
 | 
					
						
							|  |  |  | import org.junit.runner.RunWith;
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  | import org.springframework.test.context.ActiveProfiles;
 | 
					
						
							|  |  |  | import org.springframework.test.context.ContextConfiguration;
 | 
					
						
							|  |  |  | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 | 
					
						
							|  |  |  | import org.springframework.test.context.support.AnnotationConfigContextLoader;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 18:19:52 -02:00
										 |  |  | import com.baeldung.common.web.AbstractBasicLiveTest;
 | 
					
						
							|  |  |  | import com.baeldung.persistence.model.Foo;
 | 
					
						
							|  |  |  | import com.baeldung.spring.ConfigIntegrationTest;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import io.restassured.RestAssured;
 | 
					
						
							|  |  |  | import io.restassured.response.Response;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  | @RunWith(SpringJUnit4ClassRunner.class)
 | 
					
						
							| 
									
										
										
										
											2018-06-05 17:05:55 +05:30
										 |  |  | @ContextConfiguration(classes = { ConfigIntegrationTest.class }, loader = AnnotationConfigContextLoader.class)
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  | @ActiveProfiles("test")
 | 
					
						
							|  |  |  | public class FooPageableLiveTest extends AbstractBasicLiveTest<Foo> {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public FooPageableLiveTest() {
 | 
					
						
							|  |  |  |         super(Foo.class);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // API
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Override
 | 
					
						
							|  |  |  |     public final void create() {
 | 
					
						
							| 
									
										
										
										
											2019-01-20 18:19:52 -02:00
										 |  |  |         super.create(new Foo(randomAlphabetic(6)));
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Override
 | 
					
						
							|  |  |  |     public final String createAsUri() {
 | 
					
						
							|  |  |  |         return createAsUri(new Foo(randomAlphabetic(6)));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:39:04 +03:00
										 |  |  |     @Override
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  |     @Test
 | 
					
						
							|  |  |  |     public void whenResourcesAreRetrievedPaged_then200IsReceived() {
 | 
					
						
							| 
									
										
										
										
											2019-01-20 18:19:52 -02:00
										 |  |  |         this.create();
 | 
					
						
							|  |  |  |         
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:39:04 +03:00
										 |  |  |         final Response response = RestAssured.get(getPageableURL() + "?page=0&size=10");
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         assertThat(response.getStatusCode(), is(200));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:39:04 +03:00
										 |  |  |     @Override
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  |     @Test
 | 
					
						
							|  |  |  |     public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() {
 | 
					
						
							|  |  |  |         final String url = getPageableURL() + "?page=" + randomNumeric(5) + "&size=10";
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:39:04 +03:00
										 |  |  |         final Response response = RestAssured.get(url);
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         assertThat(response.getStatusCode(), is(404));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:39:04 +03:00
										 |  |  |     @Override
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  |     @Test
 | 
					
						
							|  |  |  |     public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
 | 
					
						
							|  |  |  |         create();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 19:39:04 +03:00
										 |  |  |         final Response response = RestAssured.get(getPageableURL() + "?page=0&size=10");
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         assertFalse(response.body().as(List.class).isEmpty());
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected String getPageableURL() {
 | 
					
						
							| 
									
										
										
										
											2019-03-17 12:26:00 +02:00
										 |  |  |         return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/foos/pageable";
 | 
					
						
							| 
									
										
										
										
											2017-05-16 11:52:04 +03:00
										 |  |  |     }
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  | }
 |