Skip to content

Commit

Permalink
First try to add tests for list based spring repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Apr 9, 2024
1 parent 77734b5 commit d1183d1
Show file tree
Hide file tree
Showing 8 changed files with 761 additions and 231 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.spring.data.rest.deployment;

import static io.quarkus.spring.data.rest.deployment.RepositoryMethodsImplementor.LIST_PAGED;

import jakarta.enterprise.context.ApplicationScoped;

import org.jboss.logging.Logger;
Expand Down Expand Up @@ -47,11 +45,6 @@ public String implement(ClassOutput classOutput, String resourceType, String ent
methodsImplementor.implementUpdate(classCreator, resourceType, entityType);
methodsImplementor.implementDelete(classCreator, resourceType);

boolean contains = classCreator.getExistingMethods().contains(LIST_PAGED);
// String string = classCreator.getExistingMethods().stream()
// .filter(methodDescriptor -> methodDescriptor.getName().equals(LIST_PAGED.getName())).findAny().get()
// .getDescriptor().toString();
// LOGGER.infof("Method descriptor '%s'", string);
classCreator.close();

LOGGER.tracef("Completed generation of '%s'", className);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,48 +104,6 @@ unremovableBeansProducer, new RepositoryMethodsImplementor(index, entityClassHel
repositoriesToImplement);
}

// @BuildStep
// void registerCrudRepositories(CombinedIndexBuildItem indexBuildItem, Capabilities capabilities,
// BuildProducer<GeneratedBeanBuildItem> implementationsProducer,
// BuildProducer<RestDataResourceBuildItem> restDataResourceProducer,
// BuildProducer<ResourcePropertiesBuildItem> resourcePropertiesProducer,
// BuildProducer<UnremovableBeanBuildItem> unremovableBeansProducer) {
// IndexView index = indexBuildItem.getIndex();
//
// implementResources(capabilities, implementationsProducer, restDataResourceProducer, resourcePropertiesProducer,
// unremovableBeansProducer, new CrudMethodsImplementor(index), new CrudPropertiesProvider(index),
// getRepositoriesToImplement(index, CRUD_REPOSITORY_INTERFACE, LIST_CRUD_REPOSITORY_INTERFACE,
// JPA_REPOSITORY_INTERFACE));
// }
//
// @BuildStep
// void registerJpaRepositories(CombinedIndexBuildItem indexBuildItem, Capabilities capabilities,
// BuildProducer<GeneratedBeanBuildItem> implementationsProducer,
// BuildProducer<RestDataResourceBuildItem> restDataResourceProducer,
// BuildProducer<ResourcePropertiesBuildItem> resourcePropertiesProducer,
// BuildProducer<UnremovableBeanBuildItem> unremovableBeansProducer) {
// IndexView index = indexBuildItem.getIndex();
//
// implementResources(capabilities, implementationsProducer, restDataResourceProducer, resourcePropertiesProducer,
// unremovableBeansProducer, new RepositoryMethodsImplementor(index), new RepositoryPropertiesProvider(index),
// getRepositoriesToImplement(index, JPA_REPOSITORY_INTERFACE));
// }

// @BuildStep
// void registerPagingAndSortingRepositories(CombinedIndexBuildItem indexBuildItem, Capabilities capabilities,
// BuildProducer<GeneratedBeanBuildItem> implementationsProducer,
// BuildProducer<RestDataResourceBuildItem> restDataResourceProducer,
// BuildProducer<ResourcePropertiesBuildItem> resourcePropertiesProducer,
// BuildProducer<UnremovableBeanBuildItem> unremovableBeansProducer) {
// IndexView index = indexBuildItem.getIndex();
//
// implementResources(capabilities, implementationsProducer, restDataResourceProducer, resourcePropertiesProducer,
// unremovableBeansProducer, new PagingAndSortingMethodsImplementor(index),
// new PagingAndSortingPropertiesProvider(index),
// getRepositoriesToImplement(index, PAGING_AND_SORTING_REPOSITORY_INTERFACE,
// LIST_PAGING_AND_SORTING_REPOSITORY_INTERFACE, JPA_REPOSITORY_INTERFACE));
// }

/**
* Implement the {@link io.quarkus.rest.data.panache.RestDataResource} interface for each given Spring Data
* repository and register its metadata and properties to be later picked up by the `rest-data-panache` extension.
Expand Down
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);
}
}
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> {
}
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> {
}
Loading

0 comments on commit d1183d1

Please sign in to comment.