From 6e08121f6158b691308089917ee47ac182575215 Mon Sep 17 00:00:00 2001 From: Alex Theedom Date: Mon, 8 Aug 2016 20:42:44 +0100 Subject: [PATCH] Add a few more tests --- .../StudentDirectory/pom.xml | 3 + .../rest/StudentDirectoryRestController.java | 5 +- .../web/service/StudentServiceTest.java | 97 +++++++++++++------ 3 files changed, 77 insertions(+), 28 deletions(-) diff --git a/spring-rest-angular-pagination/StudentDirectory/pom.xml b/spring-rest-angular-pagination/StudentDirectory/pom.xml index 8dab851ef2..7a0f3e7b31 100644 --- a/spring-rest-angular-pagination/StudentDirectory/pom.xml +++ b/spring-rest-angular-pagination/StudentDirectory/pom.xml @@ -66,6 +66,7 @@ angular-spring-rest-sample + org.apache.maven.plugins maven-compiler-plugin @@ -74,6 +75,7 @@ 1.8 + org.apache.maven.plugins maven-war-plugin @@ -81,6 +83,7 @@ false + diff --git a/spring-rest-angular-pagination/StudentDirectory/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java b/spring-rest-angular-pagination/StudentDirectory/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java index 5ff24ec0f2..b655d401a5 100644 --- a/spring-rest-angular-pagination/StudentDirectory/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java +++ b/spring-rest-angular-pagination/StudentDirectory/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java @@ -4,18 +4,21 @@ import org.baeldung.web.service.StudentService; import org.baeldung.web.vo.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import static org.springframework.http.MediaType.APPLICATION_JSON; + @RestController public class StudentDirectoryRestController { @Autowired private StudentService service; - @RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET) + @RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET, produces = "application/json") public Page findPaginated(@RequestParam("page") int page, @RequestParam("size") int size){ Page resultPage = service.findPaginated(page, size); diff --git a/spring-rest-angular-pagination/StudentDirectory/src/test/java/org/baeldung/web/service/StudentServiceTest.java b/spring-rest-angular-pagination/StudentDirectory/src/test/java/org/baeldung/web/service/StudentServiceTest.java index 3e476bf0d0..55855c4c7d 100644 --- a/spring-rest-angular-pagination/StudentDirectory/src/test/java/org/baeldung/web/service/StudentServiceTest.java +++ b/spring-rest-angular-pagination/StudentDirectory/src/test/java/org/baeldung/web/service/StudentServiceTest.java @@ -1,7 +1,14 @@ package org.baeldung.web.service; +import static io.restassured.RestAssured.given; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsCollectionContaining.hasItems; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.StringContains.containsString; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.util.Assert.isTrue; import org.apache.commons.lang3.RandomStringUtils; import org.baeldung.web.main.Application; @@ -9,41 +16,77 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import io.restassured.RestAssured; import io.restassured.response.Response; +import org.springframework.web.client.RestTemplate; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) -@WebAppConfiguration -@IntegrationTest("server.port:8080") -public class StudentServiceTest{ +@WebAppConfiguration +@IntegrationTest("server.port:8888") +public class StudentServiceTest { - private String getURL() { - return "/StudentDirectory/student/get"; + private static final String ENDPOINT = "http://localhost:8080/StudentDirectory/student/get"; + + @Test + public void givenRequestForStudents_whenPageIsOne_expectContainsNames() { + given().params("page", "1", "size", "2").get(ENDPOINT) + .then() + .assertThat().body("content.name", hasItems("Bryan", "Ben")); + } + + @Test + public void givenRequestForStudents_whenSizeIsTwo_expectTwoItems() { + given().params("page", "1", "size", "2").get(ENDPOINT) + .then() + .assertThat().body("size", equalTo(2)); + } + + @Test + public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() { + given().params("page", "1", "size", "2").get(ENDPOINT) + .then() + .assertThat().body("numberOfElements", equalTo(2)); + } + + @Test + public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() { + given().params("page", "1", "size", "2").get(ENDPOINT) + .then() + .statusCode(200); + } + + @Test + public void givenRequestForStudents_whenPageOfResourcesAreRetrievedOutOfBounds_thenExpect500() { + given().params("page", "1000", "size", "2").get(ENDPOINT) + .then() + .statusCode(500); + } + + @Test + public void givenRequestForStudents_whenPageNotValid_thenExpect500() { + given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(ENDPOINT) + .then() + .statusCode(500); + } + + @Test + public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { + given().params("page", "1", "size", "2").get(ENDPOINT) + .then() + .assertThat().body("first", equalTo(true)); + } + + @Test + public void givenRequestForStudents_whenPageIsFive_expectFiveItems() { + given().params("page", "1", "size", "5").get(ENDPOINT) + .then() + .body("content.studentId.max()", equalTo("5")); } - - @Test - public void whenResourcesAreRetrievedPaged_then200IsReceived(){ - Response response = RestAssured.given().get(getURL()+ "?page=0&size=2").andReturn(); - - assertTrue(response.getStatusCode() == 200 ); - } - - @Test - public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived(){ - String url = getURL()+ "?page=" + RandomStringUtils.randomNumeric(5) + "&size=2"; - Response response = RestAssured.given().get(url); - - assertTrue(response.getStatusCode() == 500 ); - } - - @Test - public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources(){ - Response response = RestAssured.given().get(getURL() + "?page=1&size=2" ); - assertFalse(response.getBody().jsonPath().getList("content").isEmpty() ); - } - }