Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UniAsserterInterceptor - improve the javadoc and docs #32617

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/hibernate-reactive-panache.adoc
Expand Up @@ -858,7 +858,7 @@ Testing reactive Panache entities in a `@QuarkusTest` is slightly more complicat
The `quarkus-test-vertx` dependency provides the `@io.quarkus.test.vertx.RunOnVertxContext` annotation and the `io.quarkus.test.vertx.UniAsserter` class which are intended precisely for this purpose.
The usage is described in the xref:hibernate-reactive.adoc#testing[Hibernate Reactive] guide.

You can also extend the `io.quarkus.test.vertx.UniAsserterInterceptor` to customize the behavior of the injected `UniAsserter`.
You can also extend the `io.quarkus.test.vertx.UniAsserterInterceptor` to wrap the injected `UniAsserter` and customize the behavior.
For example, the interceptor can be used to execute the assert methods within a separate database transaction.

.`UniAsserterInterceptor` Example
Expand Down
26 changes: 26 additions & 0 deletions docs/src/main/asciidoc/hibernate-reactive.adoc
Expand Up @@ -245,6 +245,32 @@ public class SomeTest {

NOTE: See the Javadoc of `UniAsserter` for a full description of the various methods that can be used for creating assertions.

[TIP]
====
You can also extend the `io.quarkus.test.vertx.UniAsserterInterceptor` to wrap the injected `UniAsserter` and customize the default behavior. For example, the interceptor can be used to execute the assert methods within a separate database transaction.:

[source,java]
----
@QuarkusTest
public class SomeTest {

@Test
@RunOnVertxContext
public void testEntity(UniAsserter asserter) {
asserter = new UniAsserterInterceptor(asserter) {
@Override
protected <T> Supplier<Uni<T>> transformUni(Supplier<Uni<T>> uniSupplier) {
return () -> Panache.withTransaction(uniSupplier);
}
};
asserter.execute(() -> new MyEntity().persist());
asserter.assertEquals(() -> MyEntity.count(), 1l);
asserter.execute(() -> MyEntity.deleteAll());
}
}
----
====

[[hr-limitations]]
== Limitations and other things you should know

Expand Down
Expand Up @@ -7,10 +7,11 @@
import io.smallrye.mutiny.Uni;

/**
* Subclasses can be used to customize the behavior of the injected {@link UniAsserter}.
*
* A subclass can be used to wrap the injected {@link UniAsserter} and customize the default behavior.
* <p>
* Specifically, it can intercept selected methods and perform some additional logic. The {@link #transformUni(Supplier)} method
* can be used to transform the provided {@link Uni} supplier for assertion and {@link UniAsserter#execute(Supplier)} methods.
* <p>
* For example, it can be used to perform all assertions within the scope of a database transaction:
*
* <pre>
Expand All @@ -24,7 +25,7 @@
* }
*
* &#64;Override
* protected <T> Supplier<Uni<T>> transformUni(Supplier<Uni<T>> uniSupplier) {
* protected &lt;T&gt; Supplier&lt;Uni&lt;T&gt;&gt; transformUni(Supplier&lt;Uni&lt;T&gt;&gt; uniSupplier) {
* // Assert/execute methods are invoked within a database transaction
* return () -> Panache.withTransaction(uniSupplier);
* }
Expand All @@ -35,12 +36,34 @@
* public void testEntity(UniAsserter asserter) {
* asserter = new TransactionalUniAsserterInterceptor(asserter);
* asserter.execute(() -> new MyEntity().persist());
* asserter.assertEquals(() -> MyEntity.count(), 1l);
* asserter.assertEquals(() -&gt; MyEntity.count(), 1l);
* asserter.execute(() -> MyEntity.deleteAll());
* }
* }
* </pre>
*
* Alternatively, an anonymous class can be used as well:
*
* <pre>
* &#64;QuarkusTest
* public class SomeTest {
*
* &#64;Test
* &#64;RunOnVertxContext
* public void testEntity(UniAsserter asserter) {
* asserter = new UniAsserterInterceptor(asserter) {
* &#64;Override
* protected &lt;T&gt; Supplier&lt;Uni&lt;T&gt;&gt; transformUni(Supplier&lt;Uni&lt;T&gt;&gt; uniSupplier) {
* return () -&gt; Panache.withTransaction(uniSupplier);
* }
* };
* asserter.execute(() -&gt; new MyEntity().persist());
* asserter.assertEquals(() -&gt; MyEntity.count(), 1l);
* asserter.execute(() -&gt; MyEntity.deleteAll());
* }
* }
* </pre>
*
*/
public abstract class UniAsserterInterceptor implements UniAsserter {

Expand Down
Expand Up @@ -181,7 +181,12 @@ public void testInterceptorFailures() {
@Test
public void testInterceptorData() {
testAsserter(ua -> {
UniAsserter asserter = new DataUniAsserterInterceptor(ua);
UniAsserter asserter = new UniAsserterInterceptor(ua) {
@Override
public Object getData(String key) {
return "found";
}
};
asserter.assertEquals(() -> Uni.createFrom().item(asserter.getData("foo")), "found");
});
}
Expand All @@ -198,20 +203,6 @@ protected <T> Supplier<Uni<T>> transformUni(Supplier<Uni<T>> uniSupplier) {
}

}

static class DataUniAsserterInterceptor extends UniAsserterInterceptor {

public DataUniAsserterInterceptor(UniAsserter asserter) {
super(asserter);
}

@Override
public Object getData(String key) {
return "found";
}

}

// utils

private <T> void testAsserter(Consumer<UniAsserter> assertion) {
Expand Down