From 72cb87bed8f7c38314d2f28e49223bb07ea2cf45 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Wed, 8 Feb 2023 16:58:54 +0100 Subject: [PATCH] UniAsserter - introduce UniAsserterInterceptor --- .../test/vertx/UniAsserterInterceptor.java | 141 ++++++++++++++++++ .../quarkus/test/vertx/UniAsserterTest.java | 44 ++++++ 2 files changed, 185 insertions(+) create mode 100644 test-framework/vertx/src/main/java/io/quarkus/test/vertx/UniAsserterInterceptor.java diff --git a/test-framework/vertx/src/main/java/io/quarkus/test/vertx/UniAsserterInterceptor.java b/test-framework/vertx/src/main/java/io/quarkus/test/vertx/UniAsserterInterceptor.java new file mode 100644 index 00000000000000..f801c0ee218585 --- /dev/null +++ b/test-framework/vertx/src/main/java/io/quarkus/test/vertx/UniAsserterInterceptor.java @@ -0,0 +1,141 @@ +package io.quarkus.test.vertx; + +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +import io.smallrye.mutiny.Uni; + +/** + * Makes it possible to intercept the methods of an injected {@link UniAsserter} 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. For example, it can be used to perform all assertions within the scope of a + * database transaction. + */ +public abstract class UniAsserterInterceptor implements UniAsserter { + + private final UniAsserter delegate; + + public UniAsserterInterceptor(UniAsserter asserter) { + this.delegate = asserter; + } + + /** + * This method can be used to transform the provided {@link Uni} supplier for assertion methods and + * {@link UniAsserter#execute(Supplier)}. + * + * @param + * @param uniSupplier + * @return + */ + protected Supplier> transformUni(Supplier> uniSupplier) { + return uniSupplier; + } + + @Override + public UniAsserter assertThat(Supplier> uni, Consumer asserter) { + delegate.assertThat(transformUni(uni), asserter); + return this; + } + + @Override + public UniAsserter execute(Supplier> uni) { + delegate.execute(transformUni(uni)); + return this; + } + + @Override + public UniAsserter execute(Runnable c) { + delegate.execute(c); + return this; + } + + @Override + public UniAsserter assertEquals(Supplier> uni, T t) { + delegate.assertEquals(transformUni(uni), t); + return this; + } + + @Override + public UniAsserter assertNotEquals(Supplier> uni, T t) { + delegate.assertNotEquals(transformUni(uni), t); + return this; + } + + @Override + public UniAsserter assertSame(Supplier> uni, T t) { + delegate.assertSame(transformUni(uni), t); + return this; + } + + @Override + public UniAsserter assertNotSame(Supplier> uni, T t) { + delegate.assertNotSame(transformUni(uni), t); + return this; + } + + @Override + public UniAsserter assertNull(Supplier> uni) { + delegate.assertNull(transformUni(uni)); + return this; + } + + @Override + public UniAsserter assertNotNull(Supplier> uni) { + delegate.assertNotNull(transformUni(uni)); + return this; + } + + @Override + public UniAsserter surroundWith(Function, Uni> uni) { + delegate.surroundWith(uni); + return this; + } + + @Override + public UniAsserter fail() { + delegate.fail(); + return this; + } + + @Override + public UniAsserter assertTrue(Supplier> uni) { + delegate.assertTrue(transformUni(uni)); + return this; + } + + @Override + public UniAsserter assertFalse(Supplier> uni) { + delegate.assertFalse(transformUni(uni)); + return this; + } + + @Override + public UniAsserter assertFailedWith(Supplier> uni, Consumer c) { + delegate.assertFailedWith(transformUni(uni), c); + return this; + } + + @Override + public UniAsserter assertFailedWith(Supplier> uni, Class c) { + delegate.assertFailedWith(transformUni(uni), c); + return this; + } + + @Override + public Object getData(String key) { + return delegate.getData(key); + } + + @Override + public Object putData(String key, Object value) { + return delegate.putData(key, value); + } + + @Override + public void clearData() { + delegate.clearData(); + } + +} \ No newline at end of file diff --git a/test-framework/vertx/src/test/java/io/quarkus/test/vertx/UniAsserterTest.java b/test-framework/vertx/src/test/java/io/quarkus/test/vertx/UniAsserterTest.java index 2ddd35b4e4a44f..aa14d540d11f99 100644 --- a/test-framework/vertx/src/test/java/io/quarkus/test/vertx/UniAsserterTest.java +++ b/test-framework/vertx/src/test/java/io/quarkus/test/vertx/UniAsserterTest.java @@ -9,6 +9,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.function.Predicate; +import java.util.function.Supplier; import org.junit.jupiter.api.Test; @@ -168,6 +169,49 @@ public void testData() { })); } + @Test + public void testInterceptorFailures() { + // UniAsserter should fail even though the supplier returns a non-null value + testAsserterFailure(ua -> { + UniAsserter asserter = new AlwaysFailingUniAsserterInterceptor(ua); + asserter.assertNotNull(() -> Uni.createFrom().item(Boolean.TRUE)); + }, t -> IllegalStateException.class.isInstance(t)); + } + + @Test + public void testInterceptorData() { + testAsserter(ua -> { + UniAsserter asserter = new DataUniAsserterInterceptor(ua); + asserter.assertEquals(() -> Uni.createFrom().item(asserter.getData("foo")), "found"); + }); + } + + static class AlwaysFailingUniAsserterInterceptor extends UniAsserterInterceptor { + + public AlwaysFailingUniAsserterInterceptor(UniAsserter asserter) { + super(asserter); + } + + @Override + protected Supplier> transformUni(Supplier> uniSupplier) { + return () -> Uni.createFrom().failure(new IllegalStateException()); + } + + } + + static class DataUniAsserterInterceptor extends UniAsserterInterceptor { + + public DataUniAsserterInterceptor(UniAsserter asserter) { + super(asserter); + } + + @Override + public Object getData(String key) { + return "found"; + } + + } + // utils private void testAsserter(Consumer assertion) {