Skip to content

Commit

Permalink
UniAsserter - introduce UniAsserterInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Feb 8, 2023
1 parent 2e99481 commit 72cb87b
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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 <T>
* @param uniSupplier
* @return
*/
protected <T> Supplier<Uni<T>> transformUni(Supplier<Uni<T>> uniSupplier) {
return uniSupplier;
}

@Override
public <T> UniAsserter assertThat(Supplier<Uni<T>> uni, Consumer<T> asserter) {
delegate.assertThat(transformUni(uni), asserter);
return this;
}

@Override
public <T> UniAsserter execute(Supplier<Uni<T>> uni) {
delegate.execute(transformUni(uni));
return this;
}

@Override
public UniAsserter execute(Runnable c) {
delegate.execute(c);
return this;
}

@Override
public <T> UniAsserter assertEquals(Supplier<Uni<T>> uni, T t) {
delegate.assertEquals(transformUni(uni), t);
return this;
}

@Override
public <T> UniAsserter assertNotEquals(Supplier<Uni<T>> uni, T t) {
delegate.assertNotEquals(transformUni(uni), t);
return this;
}

@Override
public <T> UniAsserter assertSame(Supplier<Uni<T>> uni, T t) {
delegate.assertSame(transformUni(uni), t);
return this;
}

@Override
public <T> UniAsserter assertNotSame(Supplier<Uni<T>> uni, T t) {
delegate.assertNotSame(transformUni(uni), t);
return this;
}

@Override
public <T> UniAsserter assertNull(Supplier<Uni<T>> uni) {
delegate.assertNull(transformUni(uni));
return this;
}

@Override
public <T> UniAsserter assertNotNull(Supplier<Uni<T>> uni) {
delegate.assertNotNull(transformUni(uni));
return this;
}

@Override
public <T> UniAsserter surroundWith(Function<Uni<T>, Uni<T>> uni) {
delegate.surroundWith(uni);
return this;
}

@Override
public UniAsserter fail() {
delegate.fail();
return this;
}

@Override
public UniAsserter assertTrue(Supplier<Uni<Boolean>> uni) {
delegate.assertTrue(transformUni(uni));
return this;
}

@Override
public UniAsserter assertFalse(Supplier<Uni<Boolean>> uni) {
delegate.assertFalse(transformUni(uni));
return this;
}

@Override
public <T> UniAsserter assertFailedWith(Supplier<Uni<T>> uni, Consumer<Throwable> c) {
delegate.assertFailedWith(transformUni(uni), c);
return this;
}

@Override
public <T> UniAsserter assertFailedWith(Supplier<Uni<T>> uni, Class<? extends Throwable> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 <T> Supplier<Uni<T>> transformUni(Supplier<Uni<T>> 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 <T> void testAsserter(Consumer<UniAsserter> assertion) {
Expand Down

0 comments on commit 72cb87b

Please sign in to comment.