io.vertx.quiz.JooqBase Maven / Gradle / Ivy
package io.vertx.quiz;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.RunTestOnContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import io.vertx.up.log.Annal;
import io.vertx.up.uca.container.Kv;
import io.vertx.up.unity.Ux;
import io.vertx.up.unity.UxJooq;
import org.jooq.Condition;
import org.jooq.Operator;
import org.jooq.impl.DSL;
import org.junit.Rule;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
@RunWith(VertxUnitRunner.class)
public abstract class JooqBase extends AsyncBase {
@Rule
public final RunTestOnContext rule = new RunTestOnContext();
public UxJooq getDao() {
return null;
}
public void asyncJooq(final TestContext context,
final Supplier> supplier,
final Consumer function) {
asyncJooq(context, supplier, function, this::getDao);
}
private void asyncJooq(final TestContext context,
final Supplier> supplier,
final Consumer consumer,
final Supplier daoSupplier) {
final UxJooq jooq = daoSupplier.get();
if (null != jooq) {
final Future future = supplier.get();
Async.async(context, future, consumer);
}
}
public Condition eq(final String name, final Object value) {
return DSL.field(name).eq(value);
}
public Condition condAnd(final String filename) {
final JsonObject filters = ioJObject(filename);
return UxJooq.transform(filters, Operator.AND);
}
public void notNull(final T entity, final TestContext context) {
context.assertNotNull(entity);
Annal.get(getClass()).debug("[ ZERO ] {0}", entity.getClass());
}
protected void fetchOneAsync(
final TestContext context,
final Class> clazzDao,
final String pojo,
final Object... args) {
final List> kvs = new ArrayList<>();
final int length = args.length / 2;
for (int idx = 0; idx < length; idx++) {
final int index = idx * 2;
final String key = args[index].toString();
final Object value = args[index + 1];
kvs.add(Kv.create(key, value));
}
kvs.forEach(kv -> {
UxJooq jooq = Ux.Jooq.on(clazzDao);
if (null != pojo) {
jooq = jooq.on(pojo);
}
async(context,
jooq.fetchOneAsync(kv.getKey(), kv.getValue()),
context::assertNotNull);
});
}
protected void fetchOneAndAsync(
final TestContext context,
final Class> clazz,
final String pojo,
final String... files) {
final List filters = new ArrayList<>();
Arrays.stream(files).forEach(file -> filters.add(ioJObject(file)));
filters.forEach(filter -> {
UxJooq jooq = Ux.Jooq.on(clazz);
if (null != pojo) {
jooq = jooq.on(pojo);
}
async(context,
jooq.fetchOneAsync(filter),
context::assertNotNull);
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy