|
1 | 1 | package org.baeldung.web.service; |
2 | 2 |
|
| 3 | +import static io.restassured.RestAssured.given; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | +import static org.hamcrest.core.IsCollectionContaining.hasItems; |
| 6 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 7 | +import static org.hamcrest.core.StringContains.containsString; |
3 | 8 | import static org.junit.Assert.assertFalse; |
4 | 9 | import static org.junit.Assert.assertTrue; |
| 10 | +import static org.springframework.http.MediaType.APPLICATION_JSON; |
| 11 | +import static org.springframework.util.Assert.isTrue; |
5 | 12 |
|
6 | 13 | import org.apache.commons.lang3.RandomStringUtils; |
7 | 14 | import org.baeldung.web.main.Application; |
8 | 15 | import org.junit.Test; |
9 | 16 | import org.junit.runner.RunWith; |
10 | 17 | import org.springframework.boot.test.IntegrationTest; |
11 | 18 | import org.springframework.boot.test.SpringApplicationConfiguration; |
| 19 | +import org.springframework.boot.test.TestRestTemplate; |
| 20 | +import org.springframework.http.HttpHeaders; |
| 21 | +import org.springframework.http.ResponseEntity; |
12 | 22 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
13 | 23 | import org.springframework.test.context.web.WebAppConfiguration; |
14 | 24 |
|
15 | 25 | import io.restassured.RestAssured; |
16 | 26 | import io.restassured.response.Response; |
| 27 | +import org.springframework.web.client.RestTemplate; |
17 | 28 |
|
18 | 29 | @RunWith(SpringJUnit4ClassRunner.class) |
19 | 30 | @SpringApplicationConfiguration(classes = Application.class) |
20 | | -@WebAppConfiguration |
21 | | -@IntegrationTest("server.port:8080") |
22 | | -public class StudentServiceTest{ |
23 | | - |
24 | | - private String getURL() { |
25 | | - return "/StudentDirectory/student/get"; |
26 | | - } |
27 | | - |
28 | | - @Test |
29 | | - public void whenResourcesAreRetrievedPaged_then200IsReceived(){ |
30 | | - Response response = RestAssured.given().get(getURL()+ "?page=0&size=2").andReturn(); |
31 | | - |
32 | | - assertTrue(response.getStatusCode() == 200 ); |
33 | | - } |
34 | | - |
35 | | - @Test |
36 | | - public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived(){ |
37 | | - String url = getURL()+ "?page=" + RandomStringUtils.randomNumeric(5) + "&size=2"; |
38 | | - Response response = RestAssured.given().get(url); |
39 | | - |
40 | | - assertTrue(response.getStatusCode() == 500 ); |
41 | | - } |
42 | | - |
43 | | - @Test |
44 | | - public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources(){ |
45 | | - Response response = RestAssured.given().get(getURL() + "?page=1&size=2" ); |
46 | | - assertFalse(response.getBody().jsonPath().getList("content").isEmpty() ); |
47 | | - } |
48 | | - |
| 31 | +@WebAppConfiguration |
| 32 | +@IntegrationTest("server.port:8888") |
| 33 | +public class StudentServiceTest { |
| 34 | + |
| 35 | + private static final String ENDPOINT = "http://localhost:8080/StudentDirectory/student/get"; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenRequestForStudents_whenPageIsOne_expectContainsNames() { |
| 39 | + given().params("page", "1", "size", "2").get(ENDPOINT) |
| 40 | + .then() |
| 41 | + .assertThat().body("content.name", hasItems("Bryan", "Ben")); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void givenRequestForStudents_whenSizeIsTwo_expectTwoItems() { |
| 46 | + given().params("page", "1", "size", "2").get(ENDPOINT) |
| 47 | + .then() |
| 48 | + .assertThat().body("size", equalTo(2)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() { |
| 53 | + given().params("page", "1", "size", "2").get(ENDPOINT) |
| 54 | + .then() |
| 55 | + .assertThat().body("numberOfElements", equalTo(2)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() { |
| 60 | + given().params("page", "1", "size", "2").get(ENDPOINT) |
| 61 | + .then() |
| 62 | + .statusCode(200); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void givenRequestForStudents_whenPageOfResourcesAreRetrievedOutOfBounds_thenExpect500() { |
| 67 | + given().params("page", "1000", "size", "2").get(ENDPOINT) |
| 68 | + .then() |
| 69 | + .statusCode(500); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void givenRequestForStudents_whenPageNotValid_thenExpect500() { |
| 74 | + given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(ENDPOINT) |
| 75 | + .then() |
| 76 | + .statusCode(500); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { |
| 81 | + given().params("page", "1", "size", "2").get(ENDPOINT) |
| 82 | + .then() |
| 83 | + .assertThat().body("first", equalTo(true)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void givenRequestForStudents_whenPageIsFive_expectFiveItems() { |
| 88 | + given().params("page", "1", "size", "5").get(ENDPOINT) |
| 89 | + .then() |
| 90 | + .body("content.studentId.max()", equalTo("5")); |
| 91 | + } |
49 | 92 | } |
0 commit comments