Search in sources :

Example 1 with SimplePojo

use of org.folio.rest.persist.helpers.SimplePojo in project raml-module-builder by folio-org.

the class PostgresClientTransactionsIT method rollback.

private void rollback(TestContext context, String schema) {
    PostgresClient c1 = PostgresClient.getInstance(vertx, TENANT);
    Async async = context.async();
    c1.startTx(handler -> {
        if (handler.succeeded()) {
            SimplePojo z = new SimplePojo();
            z.setId("99");
            z.setName("me");
            c1.update(handler, "z", z, "jsonb", "where (jsonb->>'id')::numeric = 1", true, reply -> {
                if (reply.succeeded()) {
                    c1.rollbackTx(handler, done -> {
                        if (done.succeeded()) {
                            c1.select("SELECT jsonb FROM " + schema + ".z;", reply2 -> {
                                if (!reply2.succeeded()) {
                                    context.fail(reply2.cause());
                                } else {
                                    try {
                                        SimplePojo sp = ObjectMapperTool.getMapper().readValue(reply2.result().getResults().get(0).getString(0), SimplePojo.class);
                                        context.assertEquals(sp.getName(), "me", "Name property should not have been changed");
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        context.fail(e.getMessage());
                                    }
                                    async.complete();
                                }
                            });
                        } else {
                            context.fail(done.cause());
                        }
                    });
                } else {
                    context.fail(reply.cause());
                }
            });
        } else {
            context.fail(handler.cause());
        }
    });
    async.await();
    c1.closeClient(context.asyncAssertSuccess());
}
Also used : Async(io.vertx.ext.unit.Async) SimplePojo(org.folio.rest.persist.helpers.SimplePojo) IOException(java.io.IOException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Example 2 with SimplePojo

use of org.folio.rest.persist.helpers.SimplePojo in project raml-module-builder by folio-org.

the class PostgresClientTransactionsIT method updateTransaction.

private void updateTransaction(TestContext context, String schema) {
    PostgresClient c1 = PostgresClient.getInstance(vertx, TENANT);
    Async async = context.async();
    // create connection
    c1.startTx(handler -> {
        if (handler.succeeded()) {
            SimplePojo z = new SimplePojo();
            z.setId("99");
            z.setName("me");
            // update record
            CQL2PgJSON cql2pgJson = null;
            try {
                cql2pgJson = new CQL2PgJSON("z.jsonb");
            } catch (FieldException e1) {
                e1.printStackTrace();
                context.fail(e1);
            }
            CQLWrapper cql = new CQLWrapper(cql2pgJson, "id==1");
            c1.update(handler, "z", z, cql, true, reply -> {
                if (reply.succeeded()) {
                    // make sure record is not updated since not committed yet
                    c1.select("SELECT jsonb FROM " + schema + ".z;", reply2 -> {
                        if (!reply2.succeeded()) {
                            context.fail(reply2.cause());
                        }
                        try {
                            SimplePojo sp = ObjectMapperTool.getMapper().readValue(reply2.result().getResults().get(0).getString(0), SimplePojo.class);
                            context.assertEquals(sp.getName(), "d", "Name property should not have been changed");
                        } catch (Exception e) {
                            e.printStackTrace();
                            context.fail(e.getMessage());
                        }
                        // end transaction / commit
                        c1.endTx(handler, done -> {
                            if (done.succeeded()) {
                                // record should have been updated
                                c1.select("SELECT jsonb FROM " + schema + ".z;", selectReply -> {
                                    if (!selectReply.succeeded()) {
                                        context.fail(selectReply.cause());
                                    } else {
                                        try {
                                            SimplePojo sp = ObjectMapperTool.getMapper().readValue(selectReply.result().getResults().get(0).getString(0), SimplePojo.class);
                                            context.assertEquals(sp.getName(), "me", "Name property should have been changed");
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                            context.fail(e.getMessage());
                                        }
                                        async.complete();
                                    }
                                });
                            } else {
                                context.fail(done.cause());
                            }
                        });
                    });
                } else {
                    context.fail(reply.cause());
                }
            });
        } else {
            context.fail(handler.cause());
        }
    });
    async.await();
    c1.closeClient(context.asyncAssertSuccess());
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Async(io.vertx.ext.unit.Async) SimplePojo(org.folio.rest.persist.helpers.SimplePojo) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) IOException(java.io.IOException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Aggregations

Async (io.vertx.ext.unit.Async)2 IOException (java.io.IOException)2 SimplePojo (org.folio.rest.persist.helpers.SimplePojo)2 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)2 CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)1 CQL2PgJSON (org.z3950.zing.cql.cql2pgjson.CQL2PgJSON)1