Search in sources :

Example 1 with TenantJob

use of org.folio.rest.jaxrs.model.TenantJob in project raml-module-builder by folio-org.

the class TenantAPIIT method postTenantWithLoadFailAsync.

@Test
public void postTenantWithLoadFailAsync(TestContext context) {
    TenantAPI tenantAPI = new TenantAPI() {

        @Override
        Future<Integer> loadData(TenantAttributes attributes, String tenantId, Map<String, String> headers, Context ctx) {
            return Future.failedFuture("Load Failure");
        }
    };
    String id = tenantPost(tenantAPI, context, new TenantAttributes());
    tenantAPI.getTenantByOperationId(id, 0, okapiHeaders, onSuccess(context, result -> {
        assertThat(result.getStatus(), is(200));
        TenantJob job = (TenantJob) result.getEntity();
        assertThat(job.getComplete(), is(true));
        assertThat(job.getError(), is("Load Failure"));
    }), vertx.getOrCreateContext());
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Context(io.vertx.core.Context) TestContext(io.vertx.ext.unit.TestContext) CoreMatchers(org.hamcrest.CoreMatchers) Async(io.vertx.ext.unit.Async) Date(java.util.Date) PostgresTesterContainer(org.folio.postgres.testing.PostgresTesterContainer) TemplateException(freemarker.template.TemplateException) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) VertxUtils(org.folio.rest.tools.utils.VertxUtils) Context(io.vertx.core.Context) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Schema(org.folio.dbschema.Schema) JsonObject(io.vertx.core.json.JsonObject) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AsyncResult(io.vertx.core.AsyncResult) TenantJob(org.folio.rest.jaxrs.model.TenantJob) TimeZone(java.util.TimeZone) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) UUID(java.util.UUID) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) TenantTool(org.folio.rest.tools.utils.TenantTool) Future(io.vertx.core.Future) PostgresClient(org.folio.rest.persist.PostgresClient) Book(org.folio.rest.jaxrs.model.Book) Mockito(org.mockito.Mockito) List(java.util.List) PgUtil(org.folio.rest.persist.PgUtil) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) TenantAttributes(org.folio.rest.jaxrs.model.TenantAttributes) org.junit(org.junit) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TenantJob(org.folio.rest.jaxrs.model.TenantJob) TenantAttributes(org.folio.rest.jaxrs.model.TenantAttributes) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with TenantJob

use of org.folio.rest.jaxrs.model.TenantJob in project raml-module-builder by folio-org.

the class TenantInit method exec.

/**
 * Perform tenant post, wait for result and delete job.
 * @param client Tenant Client.
 * @param tenantAttributes attributes for tenant post.
 * @param waitMs number of milliseconds to wait for job completion (0=no wait).
 * @return async result.
 */
public static Future<Void> exec(TenantClient client, TenantAttributes tenantAttributes, int waitMs) {
    Promise<Void> promise = Promise.promise();
    client.postTenant(tenantAttributes, res1 -> {
        if (res1.failed()) {
            promise.fail(res1.cause());
            return;
        }
        int status1 = res1.result().statusCode();
        if (status1 == 204) {
            promise.complete();
            return;
        } else if (status1 != 201) {
            promise.fail("tenant post returned " + status1 + " " + res1.result().bodyAsString());
            return;
        }
        try {
            TenantJob job = res1.result().bodyAsJson(TenantJob.class);
            execGet(client, job.getId(), waitMs, promise);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            promise.fail(e);
        }
    });
    return promise.future();
}
Also used : TenantJob(org.folio.rest.jaxrs.model.TenantJob)

Example 3 with TenantJob

use of org.folio.rest.jaxrs.model.TenantJob in project raml-module-builder by folio-org.

the class TenantInit method execGet.

static void execGet(TenantClient client, String id, int waitMs, Promise<Void> promise) {
    client.getTenantByOperationId(id, waitMs, res2 -> {
        if (res2.failed()) {
            promise.fail(res2.cause());
            return;
        }
        int status2 = res2.result().statusCode();
        if (status2 != 200) {
            promise.fail("tenant get returned " + status2 + " " + res2.result().bodyAsString());
            return;
        }
        try {
            TenantJob job2 = res2.result().bodyAsJson(TenantJob.class);
            client.deleteTenantByOperationId(id, res3 -> {
                // we don't care about errors returned by delete
                String error = job2.getError();
                if (!Boolean.TRUE.equals(job2.getComplete())) {
                    promise.fail("tenant job did not complete");
                } else if (error != null) {
                    promise.fail(error);
                } else {
                    promise.complete();
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            promise.fail(e);
        }
    });
}
Also used : TenantJob(org.folio.rest.jaxrs.model.TenantJob)

Example 4 with TenantJob

use of org.folio.rest.jaxrs.model.TenantJob in project raml-module-builder by folio-org.

the class TenantAPI method getJob.

Future<TenantJob> getJob(String tenantId, UUID jobId, Context context) {
    String table = PostgresClient.convertToPsqlStandard(tenantId) + ".rmb_job";
    String sql = "SELECT jsonb FROM " + table + " WHERE id = $1";
    return postgresClient(context).selectSingle(sql, Tuple.of(jobId)).compose(reply -> {
        if (reply == null) {
            return Future.failedFuture("Job not found " + jobId);
        }
        JsonObject o = reply.getJsonObject(0);
        return Future.succeededFuture(o.mapTo(TenantJob.class));
    });
}
Also used : TenantJob(org.folio.rest.jaxrs.model.TenantJob) JsonObject(io.vertx.core.json.JsonObject)

Example 5 with TenantJob

use of org.folio.rest.jaxrs.model.TenantJob in project raml-module-builder by folio-org.

the class TenantAPIIT method postTenantWithSqlErrorAsync.

@Test
public void postTenantWithSqlErrorAsync(TestContext context) {
    TenantAPI tenantAPI = new TenantAPI();
    TenantJob job = new TenantJob();
    tenantAPI.runAsync(null, "SELECT (", job, okapiHeaders, vertx.getOrCreateContext()).onComplete(context.asyncAssertFailure(cause -> {
        assertThat(job.getError(), is("SQL error"));
        // for some bizarre reason a space is put in front the returned stmt
        assertThat(job.getMessages(), containsInAnyOrder(CoreMatchers.startsWith(" SELECT (\n")));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) CoreMatchers(org.hamcrest.CoreMatchers) Async(io.vertx.ext.unit.Async) Date(java.util.Date) PostgresTesterContainer(org.folio.postgres.testing.PostgresTesterContainer) TemplateException(freemarker.template.TemplateException) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) VertxUtils(org.folio.rest.tools.utils.VertxUtils) Context(io.vertx.core.Context) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Schema(org.folio.dbschema.Schema) JsonObject(io.vertx.core.json.JsonObject) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AsyncResult(io.vertx.core.AsyncResult) TenantJob(org.folio.rest.jaxrs.model.TenantJob) TimeZone(java.util.TimeZone) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) UUID(java.util.UUID) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) TenantTool(org.folio.rest.tools.utils.TenantTool) Future(io.vertx.core.Future) PostgresClient(org.folio.rest.persist.PostgresClient) Book(org.folio.rest.jaxrs.model.Book) Mockito(org.mockito.Mockito) List(java.util.List) PgUtil(org.folio.rest.persist.PgUtil) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) TenantAttributes(org.folio.rest.jaxrs.model.TenantAttributes) org.junit(org.junit) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TenantJob(org.folio.rest.jaxrs.model.TenantJob)

Aggregations

TenantJob (org.folio.rest.jaxrs.model.TenantJob)7 JsonObject (io.vertx.core.json.JsonObject)5 TemplateException (freemarker.template.TemplateException)4 AsyncResult (io.vertx.core.AsyncResult)4 Context (io.vertx.core.Context)4 Future (io.vertx.core.Future)4 Handler (io.vertx.core.Handler)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 UUID (java.util.UUID)4 Schema (org.folio.dbschema.Schema)4 TenantAttributes (org.folio.rest.jaxrs.model.TenantAttributes)4 PostgresClient (org.folio.rest.persist.PostgresClient)4 TenantTool (org.folio.rest.tools.utils.TenantTool)4 Vertx (io.vertx.core.Vertx)3 Async (io.vertx.ext.unit.Async)3 TestContext (io.vertx.ext.unit.TestContext)3 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)3