Skip to content

Commit

Permalink
Merge pull request #32617 from mkouba/uniassint-docs-improvement
Browse files Browse the repository at this point in the history
UniAsserterInterceptor - improve the javadoc and docs
  • Loading branch information
mkouba committed Apr 13, 2023
2 parents bbe0ade + 1462a1c commit be53780
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/hibernate-reactive-panache.adoc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit be53780

Please sign in to comment.