Search in sources :

Example 1 with FieldException

use of org.z3950.zing.cql.cql2pgjson.FieldException in project raml-module-builder by folio-org.

the class CQLWrapperTest method invalidCQLvalidation.

@Test
public void invalidCQLvalidation() throws FieldException {
    CQLWrapper wrapper = new CQLWrapper().setField(new CQL2PgJSON("field")).setQuery("or name=miller");
    try {
        wrapper.toString();
        fail("exception expected");
    } catch (CQLQueryValidationException e) {
        assertThat(e.getMessage(), allOf(containsString("ParseException"), containsString("unexpected relation")));
    }
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Test(org.junit.Test)

Example 2 with FieldException

use of org.z3950.zing.cql.cql2pgjson.FieldException in project raml-module-builder by folio-org.

the class CQLWrapperTest method sortBy.

@Test
public void sortBy() throws FieldException {
    CQL2PgJSON cql2pgJson = new CQL2PgJSON("field");
    CQLWrapper wrapper = new CQLWrapper().setField(cql2pgJson).setQuery("cql.allRecords=1 sortBy name");
    assertThat(wrapper.toString(), stringContainsInOrder(" WHERE true ORDER BY ", "name"));
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Test(org.junit.Test)

Example 3 with FieldException

use of org.z3950.zing.cql.cql2pgjson.FieldException in project mod-inventory-storage by folio-org.

the class LocationAPI method getLocations.

// Note, this is the way to get rid of unnecessary try-catch blocks. Use the
// same everywhere!
@Override
public void getLocations(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
    String tenantId = getTenant(okapiHeaders);
    CQLWrapper cql;
    try {
        cql = getCQL(query, limit, offset, LOCATION_TABLE);
    } catch (FieldException e) {
        String message = logAndSaveError(e);
        asyncResultHandler.handle(Future.succeededFuture(GetLocationsResponse.withPlainBadRequest(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LOCATION_TABLE, Location.class, new String[] { "*" }, cql, true, true, reply -> {
        // netbeans, please indent here!
        if (reply.failed()) {
            String message = logAndSaveError(reply.cause());
            asyncResultHandler.handle(Future.succeededFuture(GetLocationsResponse.withPlainBadRequest(message)));
        } else {
            Locations shelfLocations = new Locations();
            List<Location> shelfLocationsList = (List<Location>) reply.result().getResults();
            shelfLocations.setLocations(shelfLocationsList);
            shelfLocations.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
            asyncResultHandler.handle(Future.succeededFuture(GetLocationsResponse.withJsonOK(shelfLocations)));
        }
    });
}
Also used : FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Locations(org.folio.rest.jaxrs.model.Locations) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Location(org.folio.rest.jaxrs.model.Location)

Example 4 with FieldException

use of org.z3950.zing.cql.cql2pgjson.FieldException 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)

Example 5 with FieldException

use of org.z3950.zing.cql.cql2pgjson.FieldException in project raml-module-builder by folio-org.

the class CQLWrapperTest method returnsWhere.

@Test
public void returnsWhere() throws FieldException {
    CQLWrapper wrapper = new CQLWrapper().setField(new CQL2PgJSON("field")).setQuery("name=miller");
    assertThat(wrapper.toString(), startsWith(" WHERE "));
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Test(org.junit.Test)

Aggregations

CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)8 CQL2PgJSON (org.z3950.zing.cql.cql2pgjson.CQL2PgJSON)7 Test (org.junit.Test)6 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)2 Async (io.vertx.ext.unit.Async)1 IOException (java.io.IOException)1 List (java.util.List)1 Location (org.folio.rest.jaxrs.model.Location)1 Locations (org.folio.rest.jaxrs.model.Locations)1 SimplePojo (org.folio.rest.persist.helpers.SimplePojo)1