Search in sources :

Example 6 with TenantJob

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

the class TenantAPIIT method tenantPost.

public String tenantPost(TenantAPI api, TestContext context, TenantAttributes tenantAttributes) {
    Async async = context.async();
    StringBuilder id = new StringBuilder();
    api.postTenant(tenantAttributes, okapiHeaders, onSuccess(context, res1 -> {
        TenantJob job = (TenantJob) res1.getEntity();
        id.append(job.getId());
        api.getTenantByOperationId(job.getId(), TIMER_WAIT, okapiHeaders, onSuccess(context, res2 -> {
            TenantJob o = (TenantJob) res2.getEntity();
            context.assertTrue(o.getComplete());
            api.tenantExists(Vertx.currentContext(), tenantId).onComplete(onSuccess(context, bool -> {
                context.assertTrue(bool, "tenant exists after post");
                async.complete();
            }));
        }), vertx.getOrCreateContext());
    }), vertx.getOrCreateContext());
    async.await();
    return id.toString();
}
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) Async(io.vertx.ext.unit.Async)

Example 7 with TenantJob

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

the class TenantAPI method postTenant.

private void postTenant(boolean async, TenantAttributes tenantAttributes, Map<String, String> headers, Handler<AsyncResult<Response>> handler, Context context) {
    String tenantId = TenantTool.tenantId(headers);
    String id = UUID.randomUUID().toString();
    TenantJob job = new TenantJob();
    job.setId(id);
    job.setTenant(tenantId);
    job.setTenantAttributes(tenantAttributes);
    job.setComplete(false);
    String location = "/_/tenant/" + id;
    tenantExists(context, tenantId).compose(exists -> sqlFile(context, tenantId, tenantAttributes, exists)).onFailure(cause -> {
        log.error(cause.getMessage(), cause);
        handler.handle(Future.succeededFuture(PostTenantResponse.respond400WithTextPlain(cause.getMessage())));
    }).onSuccess(files -> postgresClient(context).runSQLFile(files[0], true).compose(res -> {
        if (!res.isEmpty()) {
            return Future.failedFuture(res.get(0));
        }
        if (files.length == 1) {
            // not saving job for disable or purge
            return Future.succeededFuture();
        }
        return saveJob(job, tenantId, id, context);
    }).onFailure(cause -> {
        log.error(cause.getMessage(), cause);
        handler.handle(Future.succeededFuture(PostTenantResponse.respond400WithTextPlain(cause.getMessage())));
    }).onSuccess(result -> {
        if (files.length == 1) {
            // disable or purge?
            PostgresClient.closeAllClients(tenantId);
            handler.handle(Future.succeededFuture(PostTenantResponse.respond204()));
            return;
        }
        if (async) {
            handler.handle(Future.succeededFuture(PostTenantResponse.respond201WithApplicationJson(job, PostTenantResponse.headersFor201().withLocation(location))));
        }
        runAsync(tenantAttributes, files[1], job, headers, context).onComplete(res -> {
            log.info("job {} completed", id);
            if (async) {
                completeJob(job, context);
            } else {
                if (job.getError() != null) {
                    handler.handle(Future.succeededFuture(PostTenantResponse.respond400WithTextPlain(job.getError())));
                    return;
                }
                handler.handle(Future.succeededFuture(PostTenantResponse.respond204()));
            }
        });
    }));
}
Also used : TemplateException(freemarker.template.TemplateException) SchemaMaker(org.folio.rest.persist.ddlgen.SchemaMaker) HashMap(java.util.HashMap) Context(io.vertx.core.Context) Tuple(io.vertx.sqlclient.Tuple) TenantOperation(org.folio.dbschema.TenantOperation) ObjectMapperTool(org.folio.dbschema.ObjectMapperTool) Map(java.util.Map) Schema(org.folio.dbschema.Schema) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) LinkedList(java.util.LinkedList) TenantJob(org.folio.rest.jaxrs.model.TenantJob) Tenant(org.folio.rest.jaxrs.resource.Tenant) Promise(io.vertx.core.Promise) IOException(java.io.IOException) UUID(java.util.UUID) Future(io.vertx.core.Future) Validate(org.folio.rest.annotations.Validate) TenantTool(org.folio.rest.tools.utils.TenantTool) StandardCharsets(java.nio.charset.StandardCharsets) PostgresClient(org.folio.rest.persist.PostgresClient) ResponseException(org.folio.rest.tools.client.exceptions.ResponseException) UncheckedIOException(java.io.UncheckedIOException) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Response(javax.ws.rs.core.Response) TenantAttributes(org.folio.rest.jaxrs.model.TenantAttributes) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) 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