forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First try to add tests for list based spring repositories
- Loading branch information
1 parent
77734b5
commit d1183d1
Showing
8 changed files
with
761 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
259 changes: 259 additions & 0 deletions
259
...eployment/src/test/java/io/quarkus/spring/data/rest/crud/DefaultListCrudResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
package io.quarkus.spring.data.rest.crud; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static io.restassured.RestAssured.when; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hamcrest.Matchers.endsWith; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasItems; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.path.json.JsonPath; | ||
import io.restassured.response.Response; | ||
|
||
class DefaultListCrudResourceTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(AbstractEntity.class, Record.class, DefaultRecordsListRepository.class) | ||
.addAsResource("application.properties") | ||
.addAsResource("import.sql")); | ||
|
||
@Inject | ||
DefaultRecordsListRepository recordsListRepository; | ||
|
||
@Test | ||
void givenDbContainsBooks_whenFindBooksByAuthor_thenReturnBooksByAuthor() { | ||
|
||
Record record3 = new Record(); | ||
record3.setName("Third"); | ||
Record record4 = new Record(); | ||
record4.setName("Four"); | ||
Record record5 = new Record(); | ||
record5.setName("Five"); | ||
|
||
List<Record> list = Arrays.asList(record3, record4, record5); | ||
|
||
recordsListRepository.saveAll(list); | ||
|
||
} | ||
|
||
@Test | ||
void shouldGet() { | ||
given().accept("application/json") | ||
.when().get("/default-records-list/1") | ||
.then().statusCode(200) | ||
.and().body("id", is(equalTo(1))) | ||
.and().body("name", is(equalTo("first"))); | ||
} | ||
|
||
@Test | ||
void shouldNotGetNonExistent() { | ||
given().accept("application/json") | ||
.when().get("/default-records-list/1000") | ||
.then().statusCode(404); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
void shouldGetHal() { | ||
given().accept("application/hal+json") | ||
.when().get("/default-records-list/1") | ||
.then().statusCode(200) | ||
.and().body("id", is(equalTo(1))) | ||
.and().body("name", is(equalTo("first"))) | ||
.and().body("_links.add.href", endsWith("/default-records-list")) | ||
.and().body("_links.list.href", endsWith("/default-records-list")) | ||
.and().body("_links.self.href", endsWith("/default-records-list/1")) | ||
.and().body("_links.update.href", endsWith("/default-records-list/1")) | ||
.and().body("_links.remove.href", endsWith("/default-records-list/1")); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
void shouldNotGetNonExistentHal() { | ||
given().accept("application/hal+json") | ||
.when().get("/default-records-list/1000") | ||
.then().statusCode(404); | ||
} | ||
|
||
@Test | ||
void shouldList() { | ||
given().accept("application/json") | ||
.when().get("/default-records-list") | ||
.then().statusCode(200) | ||
.and().body("id", hasItems(1, 2)) | ||
.and().body("name", hasItems("first", "second")); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
void shouldListHal() { | ||
given().accept("application/hal+json") | ||
.when().get("/default-records-list") | ||
.then().statusCode(200) | ||
.and().body("_embedded.default-records.id", hasItems(1, 2)) | ||
.and().body("_embedded.default-records.name", hasItems("first", "second")) | ||
.and() | ||
.body("_embedded.default-records._links.add.href", | ||
hasItems(endsWith("/default-records-list"), endsWith("/default-records-list"))) | ||
.and() | ||
.body("_embedded.default-records._links.list.href", | ||
hasItems(endsWith("/default-records-list"), endsWith("/default-records-list"))) | ||
.and() | ||
.body("_embedded.default-records._links.self.href", | ||
hasItems(endsWith("/default-records-list/1"), endsWith("/default-records-list/2"))) | ||
.and() | ||
.body("_embedded.default-records._links.update.href", | ||
hasItems(endsWith("/default-records-list/1"), endsWith("/default-records-list/2"))) | ||
.and() | ||
.body("_embedded.default-records._links.remove.href", | ||
hasItems(endsWith("/default-records-list/1"), endsWith("/default-records-list/2"))) | ||
.and().body("_links.add.href", endsWith("/default-records-list")) | ||
.and().body("_links.list.href", endsWith("/default-records-list")); | ||
} | ||
|
||
@Test | ||
void shouldCreate() { | ||
Response response = given().accept("application/json") | ||
.and().contentType("application/json") | ||
.and().body("{\"name\": \"test-create\"}") | ||
.when().post("/default-records-list") | ||
.thenReturn(); | ||
assertThat(response.statusCode()).isEqualTo(201); | ||
|
||
String location = response.header("Location"); | ||
int id = Integer.parseInt(location.substring(response.header("Location").lastIndexOf("/") + 1)); | ||
JsonPath body = response.body().jsonPath(); | ||
assertThat(body.getInt("id")).isEqualTo(id); | ||
assertThat(body.getString("name")).isEqualTo("test-create"); | ||
|
||
given().accept("application/json") | ||
.when().get(location) | ||
.then().statusCode(200) | ||
.and().body("id", is(equalTo(id))) | ||
.and().body("name", is(equalTo("test-create"))); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
void shouldCreateHal() { | ||
Response response = given().accept("application/hal+json") | ||
.and().contentType("application/json") | ||
.and().body("{\"name\": \"test-create-hal\"}") | ||
.when().post("/default-records-list") | ||
.thenReturn(); | ||
assertThat(response.statusCode()).isEqualTo(201); | ||
|
||
String location = response.header("Location"); | ||
int id = Integer.parseInt(location.substring(response.header("Location").lastIndexOf("/") + 1)); | ||
JsonPath body = response.body().jsonPath(); | ||
assertThat(body.getInt("id")).isEqualTo(id); | ||
assertThat(body.getString("name")).isEqualTo("test-create-hal"); | ||
assertThat(body.getString("_links.add.href")).endsWith("/default-records-list"); | ||
assertThat(body.getString("_links.list.href")).endsWith("/default-records-list"); | ||
assertThat(body.getString("_links.self.href")).endsWith("/default-records-list/" + id); | ||
assertThat(body.getString("_links.update.href")).endsWith("/default-records-list/" + id); | ||
assertThat(body.getString("_links.remove.href")).endsWith("/default-records-list/" + id); | ||
|
||
given().accept("application/json") | ||
.when().get(location) | ||
.then().statusCode(200) | ||
.and().body("id", is(equalTo(id))) | ||
.and().body("name", is(equalTo("test-create-hal"))); | ||
} | ||
|
||
@Test | ||
void shouldCreateAndUpdate() { | ||
Response createResponse = given().accept("application/json") | ||
.and().contentType("application/json") | ||
.and().body("{\"id\": \"101\", \"name\": \"test-update-create\"}") | ||
.when().put("/default-records-list/101") | ||
.thenReturn(); | ||
assertThat(createResponse.statusCode()).isEqualTo(201); | ||
|
||
String location = createResponse.header("Location"); | ||
int id = Integer.parseInt(location.substring(createResponse.header("Location").lastIndexOf("/") + 1)); | ||
JsonPath body = createResponse.body().jsonPath(); | ||
assertThat(body.getInt("id")).isEqualTo(id); | ||
assertThat(body.getString("name")).isEqualTo("test-update-create"); | ||
|
||
given().accept("application/json") | ||
.and().contentType("application/json") | ||
.and().body("{\"id\": \"" + id + "\", \"name\": \"test-update\"}") | ||
.when().put(location) | ||
.then() | ||
.statusCode(204); | ||
given().accept("application/json") | ||
.when().get(location) | ||
.then().statusCode(200) | ||
.and().body("id", is(equalTo(id))) | ||
.and().body("name", is(equalTo("test-update"))); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
void shouldCreateAndUpdateHal() { | ||
Response createResponse = given().accept("application/hal+json") | ||
.and().contentType("application/json") | ||
.and().body("{\"id\": \"102\", \"name\": \"test-update-create-hal\"}") | ||
.when().put("/default-records-list/102") | ||
.thenReturn(); | ||
assertThat(createResponse.statusCode()).isEqualTo(201); | ||
|
||
String location = createResponse.header("Location"); | ||
int id = Integer.parseInt(location.substring(createResponse.header("Location").lastIndexOf("/") + 1)); | ||
JsonPath body = createResponse.body().jsonPath(); | ||
assertThat(body.getInt("id")).isEqualTo(id); | ||
assertThat(body.getString("name")).isEqualTo("test-update-create-hal"); | ||
assertThat(body.getString("_links.add.href")).endsWith("/default-records-list"); | ||
assertThat(body.getString("_links.list.href")).endsWith("/default-records-list"); | ||
assertThat(body.getString("_links.self.href")).endsWith("/default-records-list/" + id); | ||
assertThat(body.getString("_links.update.href")).endsWith("/default-records-list/" + id); | ||
assertThat(body.getString("_links.remove.href")).endsWith("/default-records-list/" + id); | ||
|
||
given().accept("application/json") | ||
.and().contentType("application/json") | ||
.and().body("{\"id\": \"" + id + "\", \"name\": \"test-update-hal\"}") | ||
.when().put(location) | ||
.then() | ||
.statusCode(204); | ||
given().accept("application/json") | ||
.when().get(location) | ||
.then().statusCode(200) | ||
.and().body("id", is(equalTo(id))) | ||
.and().body("name", is(equalTo("test-update-hal"))); | ||
} | ||
|
||
@Test | ||
void shouldCreateAndDelete() { | ||
Response createResponse = given().accept("application/json") | ||
.and().contentType("application/json") | ||
.and().body("{\"name\": \"test-delete\"}") | ||
.when().post("/default-records-list") | ||
.thenReturn(); | ||
assertThat(createResponse.statusCode()).isEqualTo(201); | ||
|
||
String location = createResponse.header("Location"); | ||
when().delete(location) | ||
.then().statusCode(204); | ||
when().get(location) | ||
.then().statusCode(404); | ||
} | ||
|
||
@Test | ||
void shouldNotDeleteNonExistent() { | ||
when().delete("/default-records-list/1000") | ||
.then().statusCode(404); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ployment/src/test/java/io/quarkus/spring/data/rest/crud/DefaultRecordsListRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.quarkus.spring.data.rest.crud; | ||
|
||
import org.springframework.data.repository.ListCrudRepository; | ||
|
||
public interface DefaultRecordsListRepository extends ListCrudRepository<Record, Long> { | ||
} |
7 changes: 7 additions & 0 deletions
7
...oyment/src/test/java/io/quarkus/spring/data/rest/paged/CrudAndPagedRecordsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.quarkus.spring.data.rest.paged; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.PagingAndSortingRepository; | ||
|
||
public interface CrudAndPagedRecordsRepository extends PagingAndSortingRepository<Record, Long>, CrudRepository<Record, Long> { | ||
} |
Oops, something went wrong.