-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27193 from Sgitario/panache_total
Panache Rest Data: Generate "/count" endpoint
- Loading branch information
Showing
19 changed files
with
312 additions
and
0 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
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
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
18 changes: 18 additions & 0 deletions
18
...t/java/io/quarkus/hibernate/orm/rest/data/panache/deployment/AbstractCountMethodTest.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,18 @@ | ||
package io.quarkus.hibernate.orm.rest.data.panache.deployment; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public abstract class AbstractCountMethodTest { | ||
|
||
@Test | ||
void shouldGetTotalNumberOfEntities() { | ||
given().get("/collections/count") | ||
.then().statusCode(HttpStatus.SC_OK) | ||
.and().body(is(equalTo("2"))); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...bernate/orm/rest/data/panache/deployment/entity/PanacheEntityResourceCountMethodTest.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,18 @@ | ||
package io.quarkus.hibernate.orm.rest.data.panache.deployment.entity; | ||
|
||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.rest.data.panache.deployment.AbstractCountMethodTest; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
class PanacheEntityResourceCountMethodTest extends AbstractCountMethodTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(Collection.class, CollectionsResource.class, AbstractEntity.class, AbstractItem.class, | ||
Item.class, ItemsResource.class, | ||
EmptyListItem.class, EmptyListItemsResource.class) | ||
.addAsResource("application.properties") | ||
.addAsResource("import.sql")); | ||
} |
19 changes: 19 additions & 0 deletions
19
...orm/rest/data/panache/deployment/repository/PanacheRepositoryResourceCountMethodTest.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,19 @@ | ||
package io.quarkus.hibernate.orm.rest.data.panache.deployment.repository; | ||
|
||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.rest.data.panache.deployment.AbstractCountMethodTest; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
class PanacheRepositoryResourceCountMethodTest extends AbstractCountMethodTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(Collection.class, CollectionsResource.class, CollectionsRepository.class, | ||
AbstractEntity.class, AbstractItem.class, Item.class, ItemsResource.class, | ||
ItemsRepository.class, EmptyListItem.class, EmptyListItemsRepository.class, | ||
EmptyListItemsResource.class) | ||
.addAsResource("application.properties") | ||
.addAsResource("import.sql")); | ||
} |
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
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
18 changes: 18 additions & 0 deletions
18
...a/io/quarkus/hibernate/reactive/rest/data/panache/deployment/AbstractCountMethodTest.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,18 @@ | ||
package io.quarkus.hibernate.reactive.rest.data.panache.deployment; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public abstract class AbstractCountMethodTest { | ||
|
||
@Test | ||
void shouldGetTotalNumberOfEntities() { | ||
given().get("/collections/count") | ||
.then().statusCode(HttpStatus.SC_OK) | ||
.and().body(is(equalTo("2"))); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...te/reactive/rest/data/panache/deployment/entity/PanacheEntityResourceCountMethodTest.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,18 @@ | ||
package io.quarkus.hibernate.reactive.rest.data.panache.deployment.entity; | ||
|
||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.reactive.rest.data.panache.deployment.AbstractCountMethodTest; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
class PanacheEntityResourceCountMethodTest extends AbstractCountMethodTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(Collection.class, CollectionsResource.class, AbstractEntity.class, AbstractItem.class, | ||
Item.class, ItemsResource.class, | ||
EmptyListItem.class, EmptyListItemsResource.class) | ||
.addAsResource("application.properties") | ||
.addAsResource("import.sql")); | ||
} |
19 changes: 19 additions & 0 deletions
19
...ive/rest/data/panache/deployment/repository/PanacheRepositoryResourceCountMethodTest.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,19 @@ | ||
package io.quarkus.hibernate.reactive.rest.data.panache.deployment.repository; | ||
|
||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.reactive.rest.data.panache.deployment.AbstractCountMethodTest; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
class PanacheRepositoryResourceCountMethodTest extends AbstractCountMethodTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(Collection.class, CollectionsResource.class, CollectionsRepository.class, | ||
AbstractEntity.class, AbstractItem.class, Item.class, ItemsResource.class, | ||
ItemsRepository.class, EmptyListItem.class, EmptyListItemsRepository.class, | ||
EmptyListItemsResource.class) | ||
.addAsResource("application.properties") | ||
.addAsResource("import.sql")); | ||
} |
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
114 changes: 114 additions & 0 deletions
114
...src/main/java/io/quarkus/rest/data/panache/deployment/methods/CountMethodImplementor.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,114 @@ | ||
package io.quarkus.rest.data.panache.deployment.methods; | ||
|
||
import static io.quarkus.gizmo.MethodDescriptor.ofMethod; | ||
import static io.quarkus.rest.data.panache.deployment.utils.SignatureMethodCreator.ofType; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import io.quarkus.gizmo.ClassCreator; | ||
import io.quarkus.gizmo.FieldDescriptor; | ||
import io.quarkus.gizmo.MethodCreator; | ||
import io.quarkus.gizmo.ResultHandle; | ||
import io.quarkus.gizmo.TryBlock; | ||
import io.quarkus.rest.data.panache.RestDataResource; | ||
import io.quarkus.rest.data.panache.deployment.ResourceMetadata; | ||
import io.quarkus.rest.data.panache.deployment.properties.ResourceProperties; | ||
import io.quarkus.rest.data.panache.deployment.utils.SignatureMethodCreator; | ||
import io.quarkus.rest.data.panache.deployment.utils.UniImplementor; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public final class CountMethodImplementor extends StandardMethodImplementor { | ||
|
||
private static final String METHOD_NAME = "count"; | ||
|
||
private static final String RESOURCE_METHOD_NAME = "count"; | ||
|
||
private static final String EXCEPTION_MESSAGE = "Failed to count the entities"; | ||
|
||
private static final String REL = "count"; | ||
|
||
public CountMethodImplementor(boolean isResteasyClassic, boolean isReactivePanache) { | ||
super(isResteasyClassic, isReactivePanache); | ||
} | ||
|
||
/** | ||
* Generate JAX-RS GET method. | ||
* | ||
* The RESTEasy Classic version exposes {@link RestDataResource#count()}. | ||
* | ||
* <pre> | ||
* {@code | ||
* @GET | ||
* @Path("/count") | ||
* public long count() { | ||
* try { | ||
* return resource.count(); | ||
* } catch (Throwable t) { | ||
* throw new RestDataPanacheException(t); | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* The RESTEasy Reactive version exposes {@link io.quarkus.rest.data.panache.ReactiveRestDataResource#count()} | ||
* and the generated code looks more or less like this: | ||
* | ||
* <pre> | ||
* {@code | ||
* @GET | ||
* @Path("/count") | ||
* @LinkResource(rel = "count", entityClassName = "com.example.Entity") | ||
* public Uni<Long> count() { | ||
* try { | ||
* return resource.count(); | ||
* } catch (Throwable t) { | ||
* throw new RestDataPanacheException(t); | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
@Override | ||
protected void implementInternal(ClassCreator classCreator, ResourceMetadata resourceMetadata, | ||
ResourceProperties resourceProperties, FieldDescriptor resourceField) { | ||
// Method parameters: sort strings, page index, page size, uri info | ||
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(RESOURCE_METHOD_NAME, classCreator, | ||
isNotReactivePanache() ? ofType(Response.class) : ofType(Uni.class, Long.class)); | ||
|
||
// Add method annotations | ||
addGetAnnotation(methodCreator); | ||
addProducesAnnotation(methodCreator, APPLICATION_JSON); | ||
addPathAnnotation(methodCreator, appendToPath(resourceProperties.getPath(RESOURCE_METHOD_NAME), RESOURCE_METHOD_NAME)); | ||
if (!isResteasyClassic()) { | ||
// We only add the Links annotation in Resteasy Reactive because Resteasy Classic ignores the REL parameter: | ||
// it always uses "list" for GET methods, so it interferes with the list implementation. | ||
addLinksAnnotation(methodCreator, resourceMetadata.getEntityType(), REL); | ||
} | ||
|
||
ResultHandle resource = methodCreator.readInstanceField(resourceField, methodCreator.getThis()); | ||
|
||
if (isNotReactivePanache()) { | ||
TryBlock tryBlock = implementTryBlock(methodCreator, EXCEPTION_MESSAGE); | ||
ResultHandle count = tryBlock.invokeVirtualMethod( | ||
ofMethod(resourceMetadata.getResourceClass(), METHOD_NAME, long.class), | ||
resource); | ||
|
||
// Return response | ||
tryBlock.returnValue(responseImplementor.ok(tryBlock, count)); | ||
tryBlock.close(); | ||
} else { | ||
ResultHandle uniCount = methodCreator.invokeVirtualMethod( | ||
ofMethod(resourceMetadata.getResourceClass(), METHOD_NAME, Uni.class), | ||
resource); | ||
methodCreator.returnValue(UniImplementor.map(methodCreator, uniCount, EXCEPTION_MESSAGE, | ||
(countBody, count) -> countBody.returnValue(responseImplementor.ok(countBody, count)))); | ||
} | ||
|
||
methodCreator.close(); | ||
} | ||
|
||
@Override | ||
protected String getResourceMethodName() { | ||
return RESOURCE_METHOD_NAME; | ||
} | ||
} |
Oops, something went wrong.