Search in sources :

Example 1 with ResultInfo

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

the class PostgresClient method processResult.

private Results processResult(io.vertx.ext.sql.ResultSet rs, Class<?> clazz, boolean count, boolean setId) {
    long start = System.nanoTime();
    Object[] ret = new Object[2];
    List<Object> list = new ArrayList<>();
    List<JsonObject> tempList = rs.getRows();
    List<String> columnNames = rs.getColumnNames();
    int columnNamesCount = columnNames.size();
    Map<String, org.folio.rest.jaxrs.model.Facet> rInfo = new HashMap<>();
    // this is incorrect in facet queries which add a row per facet value
    int rowCount = rs.getNumRows();
    boolean countSet = false;
    if (rowCount > 0 && count) {
        // if facet query, this wont set the count as it doesnt have a count column at this location,
        Object firstColFirstVal = rs.getResults().get(0).getValue(0);
        if (null != firstColFirstVal && "Integer".equals(firstColFirstVal.getClass().getSimpleName())) {
            // regular query with count requested since count is the first column for each record
            rowCount = rs.getResults().get(0).getInteger(0);
        }
    }
    /* an exception to having the jsonb column get mapped to the corresponding clazz is a case where the
     * clazz has an jsonb field, for example an audit class which contains a field called
     * jsonb - meaning it encapsulates the real object for example for auditing purposes
     * (contains the jsonb object as well as some other fields). In such a
     * case, do not map the clazz to the content of the jsonb - but rather set the jsonb named field of the clazz
     * with the jsonb column value */
    boolean isAuditFlavored = false;
    try {
        clazz.getField(DEFAULT_JSONB_FIELD_NAME);
        isAuditFlavored = true;
    } catch (NoSuchFieldException nse) {
        if (log.isDebugEnabled()) {
            log.debug("non audit table, no " + DEFAULT_JSONB_FIELD_NAME + " found in json");
        }
    }
    int facetEntriesInResultSet = 0;
    for (int i = 0; i < tempList.size(); i++) {
        try {
            Object jo = tempList.get(i).getValue(DEFAULT_JSONB_FIELD_NAME);
            Object id = tempList.get(i).getValue(idField);
            Object o = null;
            if (!isAuditFlavored && jo != null) {
                try {
                    // is this a facet entry - if so process it, otherwise will throw an exception
                    // and continue trying to map to the pojos
                    o = mapper.readValue(jo.toString(), org.folio.rest.jaxrs.model.Facet.class);
                    org.folio.rest.jaxrs.model.Facet facet = rInfo.get(((org.folio.rest.jaxrs.model.Facet) o).getType());
                    if (facet == null) {
                        rInfo.put(((org.folio.rest.jaxrs.model.Facet) o).getType(), (org.folio.rest.jaxrs.model.Facet) o);
                    } else {
                        facet.getFacetValues().add(((org.folio.rest.jaxrs.model.Facet) o).getFacetValues().get(0));
                    }
                    facetEntriesInResultSet = facetEntriesInResultSet + 1;
                    continue;
                } catch (Exception e) {
                    try {
                        o = mapper.readValue(jo.toString(), clazz);
                    } catch (UnrecognizedPropertyException e1) {
                        // this is a facet query , and this is the count entry {"count": 11}
                        rowCount = new JsonObject(tempList.get(i).getString("jsonb")).getInteger("count");
                        continue;
                    }
                }
            } else {
                o = clazz.newInstance();
            }
            /* attempt to populate jsonb object with values from external columns - for example:
         * if there is an update_date column in the record - try to populate a field updateDate in the
         * jsonb object - this allows to use the DB for things like triggers to populate the update_date
         * automatically, but still push them into the jsonb object - the json schema must declare this field
         * as well - also support the audit mode descrbed above.
         * NOTE that the query must request any field it wants to get populated into the jsonb obj*/
            for (int j = 0; j < columnNamesCount; j++) {
                /*          if(columnNames.get(j).equals("count") && !countSet){
          //check if this is reachable
            rowCount = tempList.get(i).getLong(columnNames.get(j)).intValue();
          }*/
                if ((isAuditFlavored || !columnNames.get(j).equals(DEFAULT_JSONB_FIELD_NAME)) && !columnNames.get(j).equals(idField)) {
                    try {
                        Method[] m = o.getClass().getMethods();
                        for (int k = 0; k < m.length; k++) {
                            if (m[k].getName().equals(columnNametoCamelCaseWithset(columnNames.get(j)))) {
                                o.getClass().getMethod(columnNametoCamelCaseWithset(columnNames.get(j)), m[k].getParameterTypes()).invoke(o, new Object[] { tempList.get(i).getValue(columnNames.get(j)) });
                            }
                        }
                    } catch (Exception e) {
                        log.warn("Unable to populate field " + columnNametoCamelCaseWithset(columnNames.get(j)) + " for object of type " + clazz.getName());
                    }
                }
            }
            if (setId) {
                o.getClass().getMethod(columnNametoCamelCaseWithset(idField), new Class[] { String.class }).invoke(o, new String[] { id.toString() });
            }
            list.add(o);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            list.add(null);
        }
    }
    ResultInfo rn = new ResultInfo();
    rInfo.forEach((k, v) -> {
        rn.getFacets().add(v);
    });
    rn.setTotalRecords(rowCount);
    Results r = new Results();
    r.setResults(list);
    r.setResultInfo(rn);
    long end = System.nanoTime();
    StatsTracker.addStatElement(STATS_KEY + ".processResult", (end - start));
    if (log.isDebugEnabled()) {
        log.debug("timer: process results (ns) " + (end - start));
    }
    return r;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) Method(java.lang.reflect.Method) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) SQLException(java.sql.SQLException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Results(org.folio.rest.persist.interfaces.Results) JsonObject(io.vertx.core.json.JsonObject) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo)

Example 2 with ResultInfo

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

the class PostgresClientIT method getCQLWrapperNoFacets.

@Test
public void getCQLWrapperNoFacets(TestContext context) throws FieldException {
    final String tableDefiniton = "id UUID PRIMARY KEY , jsonb JSONB NOT NULL, distinct_test_field TEXT";
    createTableWithPoLines(context, MOCK_POLINES_TABLE, tableDefiniton);
    List<FacetField> facets = null;
    CQL2PgJSON cql2pgJson = new CQL2PgJSON("jsonb");
    {
        CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
        Async async = context.async();
        postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, null, handler -> {
            context.assertTrue(handler.succeeded());
            ResultInfo resultInfo = handler.result().getResultInfo();
            context.assertEquals(6, resultInfo.getTotalRecords());
            List<Facet> retFacets = resultInfo.getFacets();
            context.assertEquals(0, retFacets.size());
            async.complete();
        });
        async.awaitSuccess();
    }
    String distinctOn = "jsonb->>'order_format'";
    List<FacetField> emptyFacets = new ArrayList<FacetField>();
    {
        CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
        Async async = context.async();
        postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, emptyFacets, distinctOn, handler -> {
            context.assertTrue(handler.succeeded());
            ResultInfo resultInfo = handler.result().getResultInfo();
            context.assertEquals(4, resultInfo.getTotalRecords());
            List<Facet> retFacets = resultInfo.getFacets();
            context.assertEquals(0, retFacets.size());
            async.complete();
        });
        async.awaitSuccess();
    }
    {
        Async async = context.async();
        CQLWrapper cqlWrapperNull = null;
        postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapperNull, true, true, facets, null, handler -> {
            context.assertTrue(handler.succeeded());
            ResultInfo resultInfo = handler.result().getResultInfo();
            context.assertEquals(6, resultInfo.getTotalRecords());
            List<Facet> retFacets = resultInfo.getFacets();
            context.assertEquals(0, retFacets.size());
            async.complete();
        });
        async.awaitSuccess();
    }
    {
        Async async = context.async();
        CQLWrapper cqlWrapperNull = null;
        postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapperNull, true, true, facets, distinctOn, handler -> {
            context.assertTrue(handler.succeeded());
            ResultInfo resultInfo = handler.result().getResultInfo();
            context.assertEquals(4, resultInfo.getTotalRecords());
            try {
                List<Object> objs = handler.result().getResults();
                context.assertEquals(4, objs.size());
            } catch (Exception ex) {
                context.fail(ex);
            }
            List<Facet> retFacets = resultInfo.getFacets();
            context.assertEquals(0, retFacets.size());
            async.complete();
        });
        async.awaitSuccess();
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestContext(io.vertx.ext.unit.TestContext) RowStream(io.vertx.sqlclient.RowStream) Arrays(java.util.Arrays) PgNotification(io.vertx.pgclient.PgNotification) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) VertxUtils(org.folio.rest.tools.utils.VertxUtils) Tuple(io.vertx.sqlclient.Tuple) UpdateSection(org.folio.rest.persist.Criteria.UpdateSection) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) RowIterator(io.vertx.sqlclient.RowIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SqlResult(io.vertx.sqlclient.SqlResult) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) Offset(org.folio.rest.persist.Criteria.Offset) Collector(java.util.stream.Collector) Transaction(io.vertx.sqlclient.Transaction) AfterClass(org.junit.AfterClass) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RowImpl(io.vertx.pgclient.impl.RowImpl) Set(java.util.Set) UUID(java.util.UUID) FieldException(org.folio.cql2pgjson.exception.FieldException) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) IOUtils(org.apache.commons.io.IOUtils) CQL2PgJSON(org.folio.cql2pgjson.CQL2PgJSON) Base64(java.util.Base64) List(java.util.List) Stream(java.util.stream.Stream) Criterion(org.folio.rest.persist.Criteria.Criterion) Results(org.folio.rest.persist.interfaces.Results) Facet(org.folio.rest.jaxrs.model.Facet) RowDesc(io.vertx.sqlclient.impl.RowDesc) Async(io.vertx.ext.unit.Async) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) BeforeClass(org.junit.BeforeClass) FacetField(org.folio.rest.persist.facets.FacetField) PostgresTesterContainer(org.folio.postgres.testing.PostgresTesterContainer) Criteria(org.folio.rest.persist.Criteria.Criteria) CoreMatchers.not(org.hamcrest.CoreMatchers.not) RunWith(org.junit.runner.RunWith) Timeout(io.vertx.ext.unit.junit.Timeout) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) Function(java.util.function.Function) TotaledResults(org.folio.rest.persist.PostgresClient.TotaledResults) ArrayList(java.util.ArrayList) PreparedStatement(io.vertx.sqlclient.PreparedStatement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Poline(org.folio.rest.persist.helpers.Poline) PrepareOptions(io.vertx.sqlclient.PrepareOptions) SqlConnection(io.vertx.sqlclient.SqlConnection) Limit(org.folio.rest.persist.Criteria.Limit) QueryHelper(org.folio.rest.persist.PostgresClient.QueryHelper) RowSet(io.vertx.sqlclient.RowSet) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AsyncResult(io.vertx.core.AsyncResult) LinkedList(java.util.LinkedList) DatabaseMetadata(io.vertx.sqlclient.spi.DatabaseMetadata) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Before(org.junit.Before) Files(java.nio.file.Files) Query(io.vertx.sqlclient.Query) Promise(io.vertx.core.Promise) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) PgPool(io.vertx.pgclient.PgPool) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) PreparedQuery(io.vertx.sqlclient.PreparedQuery) JsonArray(io.vertx.core.json.JsonArray) PgConnection(io.vertx.pgclient.PgConnection) Rule(org.junit.Rule) Paths(java.nio.file.Paths) Row(io.vertx.sqlclient.Row) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) SimplePojo(org.folio.rest.persist.helpers.SimplePojo) Collections(java.util.Collections) InputStream(java.io.InputStream) CQL2PgJSON(org.folio.cql2pgjson.CQL2PgJSON) Async(io.vertx.ext.unit.Async) ArrayList(java.util.ArrayList) FacetField(org.folio.rest.persist.facets.FacetField) JsonObject(io.vertx.core.json.JsonObject) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) FieldException(org.folio.cql2pgjson.exception.FieldException) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with ResultInfo

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

the class PostgresClientIT method streamGetResultException.

@Test
public void streamGetResultException(TestContext context) {
    createTableWithPoLines(context);
    ResultInfo resultInfo = new ResultInfo();
    context.assertNotNull(vertx);
    RowStream<Row> sqlRowStream = new MySQLRowStream();
    StringBuilder events = new StringBuilder();
    Async async = context.async();
    PostgresClientStreamResult<Object> streamResult = new PostgresClientStreamResult(resultInfo);
    Transaction transaction = null;
    postgresClient.doStreamRowResults(sqlRowStream, Object.class, transaction, new QueryHelper("table_name"), streamResult, context.asyncAssertSuccess(sr -> {
        sr.handler(streamHandler -> {
            events.append("[handler]");
        });
        sr.endHandler(x -> {
            events.append("[endHandler]");
            throw new NullPointerException("null");
        });
        sr.exceptionHandler(x -> {
            events.append("[exception]");
            context.assertEquals("SQLRowStream exception", x.getMessage());
            async.complete();
        });
    }));
    async.await(1000);
    context.assertEquals("[exception]", events.toString());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestContext(io.vertx.ext.unit.TestContext) RowStream(io.vertx.sqlclient.RowStream) Arrays(java.util.Arrays) PgNotification(io.vertx.pgclient.PgNotification) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) VertxUtils(org.folio.rest.tools.utils.VertxUtils) Tuple(io.vertx.sqlclient.Tuple) UpdateSection(org.folio.rest.persist.Criteria.UpdateSection) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) RowIterator(io.vertx.sqlclient.RowIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SqlResult(io.vertx.sqlclient.SqlResult) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) Offset(org.folio.rest.persist.Criteria.Offset) Collector(java.util.stream.Collector) Transaction(io.vertx.sqlclient.Transaction) AfterClass(org.junit.AfterClass) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RowImpl(io.vertx.pgclient.impl.RowImpl) Set(java.util.Set) UUID(java.util.UUID) FieldException(org.folio.cql2pgjson.exception.FieldException) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) IOUtils(org.apache.commons.io.IOUtils) CQL2PgJSON(org.folio.cql2pgjson.CQL2PgJSON) Base64(java.util.Base64) List(java.util.List) Stream(java.util.stream.Stream) Criterion(org.folio.rest.persist.Criteria.Criterion) Results(org.folio.rest.persist.interfaces.Results) Facet(org.folio.rest.jaxrs.model.Facet) RowDesc(io.vertx.sqlclient.impl.RowDesc) Async(io.vertx.ext.unit.Async) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) BeforeClass(org.junit.BeforeClass) FacetField(org.folio.rest.persist.facets.FacetField) PostgresTesterContainer(org.folio.postgres.testing.PostgresTesterContainer) Criteria(org.folio.rest.persist.Criteria.Criteria) CoreMatchers.not(org.hamcrest.CoreMatchers.not) RunWith(org.junit.runner.RunWith) Timeout(io.vertx.ext.unit.junit.Timeout) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) Function(java.util.function.Function) TotaledResults(org.folio.rest.persist.PostgresClient.TotaledResults) ArrayList(java.util.ArrayList) PreparedStatement(io.vertx.sqlclient.PreparedStatement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Poline(org.folio.rest.persist.helpers.Poline) PrepareOptions(io.vertx.sqlclient.PrepareOptions) SqlConnection(io.vertx.sqlclient.SqlConnection) Limit(org.folio.rest.persist.Criteria.Limit) QueryHelper(org.folio.rest.persist.PostgresClient.QueryHelper) RowSet(io.vertx.sqlclient.RowSet) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AsyncResult(io.vertx.core.AsyncResult) LinkedList(java.util.LinkedList) DatabaseMetadata(io.vertx.sqlclient.spi.DatabaseMetadata) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Before(org.junit.Before) Files(java.nio.file.Files) Query(io.vertx.sqlclient.Query) Promise(io.vertx.core.Promise) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) PgPool(io.vertx.pgclient.PgPool) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) PreparedQuery(io.vertx.sqlclient.PreparedQuery) JsonArray(io.vertx.core.json.JsonArray) PgConnection(io.vertx.pgclient.PgConnection) Rule(org.junit.Rule) Paths(java.nio.file.Paths) Row(io.vertx.sqlclient.Row) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) SimplePojo(org.folio.rest.persist.helpers.SimplePojo) Collections(java.util.Collections) InputStream(java.io.InputStream) QueryHelper(org.folio.rest.persist.PostgresClient.QueryHelper) Transaction(io.vertx.sqlclient.Transaction) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) Row(io.vertx.sqlclient.Row) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) Test(org.junit.Test)

Example 4 with ResultInfo

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

the class PostgresClientIT method getCQLWrapperJsonbField.

@Test
public void getCQLWrapperJsonbField(TestContext context) throws FieldException {
    final String tableDefiniton = "id UUID PRIMARY KEY , jsonb JSONB NOT NULL, distinct_test_field TEXT";
    List<FacetField> facets = null;
    createTableWithPoLines(context, MOCK_POLINES_TABLE, tableDefiniton);
    CQL2PgJSON cql2pgJson = new CQL2PgJSON(MOCK_POLINES_TABLE + ".jsonb");
    {
        CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
        Async async = context.async();
        postgresClient.get(MOCK_POLINES_TABLE, Object.class, "jsonb", cqlWrapper, true, true, facets, null, handler -> {
            context.assertTrue(handler.succeeded());
            ResultInfo resultInfo = handler.result().getResultInfo();
            context.assertEquals(6, resultInfo.getTotalRecords());
            async.complete();
        });
        async.awaitSuccess();
    }
    String distinctOn = "jsonb->>'order_format'";
    {
        CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
        Async async = context.async();
        postgresClient.get(MOCK_POLINES_TABLE, Object.class, "jsonb", cqlWrapper, true, true, facets, distinctOn, handler -> {
            context.assertTrue(handler.succeeded());
            ResultInfo resultInfo = handler.result().getResultInfo();
            context.assertEquals(4, resultInfo.getTotalRecords());
            async.complete();
        });
        async.awaitSuccess();
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestContext(io.vertx.ext.unit.TestContext) RowStream(io.vertx.sqlclient.RowStream) Arrays(java.util.Arrays) PgNotification(io.vertx.pgclient.PgNotification) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) VertxUtils(org.folio.rest.tools.utils.VertxUtils) Tuple(io.vertx.sqlclient.Tuple) UpdateSection(org.folio.rest.persist.Criteria.UpdateSection) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) RowIterator(io.vertx.sqlclient.RowIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SqlResult(io.vertx.sqlclient.SqlResult) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) Offset(org.folio.rest.persist.Criteria.Offset) Collector(java.util.stream.Collector) Transaction(io.vertx.sqlclient.Transaction) AfterClass(org.junit.AfterClass) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RowImpl(io.vertx.pgclient.impl.RowImpl) Set(java.util.Set) UUID(java.util.UUID) FieldException(org.folio.cql2pgjson.exception.FieldException) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) IOUtils(org.apache.commons.io.IOUtils) CQL2PgJSON(org.folio.cql2pgjson.CQL2PgJSON) Base64(java.util.Base64) List(java.util.List) Stream(java.util.stream.Stream) Criterion(org.folio.rest.persist.Criteria.Criterion) Results(org.folio.rest.persist.interfaces.Results) Facet(org.folio.rest.jaxrs.model.Facet) RowDesc(io.vertx.sqlclient.impl.RowDesc) Async(io.vertx.ext.unit.Async) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) BeforeClass(org.junit.BeforeClass) FacetField(org.folio.rest.persist.facets.FacetField) PostgresTesterContainer(org.folio.postgres.testing.PostgresTesterContainer) Criteria(org.folio.rest.persist.Criteria.Criteria) CoreMatchers.not(org.hamcrest.CoreMatchers.not) RunWith(org.junit.runner.RunWith) Timeout(io.vertx.ext.unit.junit.Timeout) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) Function(java.util.function.Function) TotaledResults(org.folio.rest.persist.PostgresClient.TotaledResults) ArrayList(java.util.ArrayList) PreparedStatement(io.vertx.sqlclient.PreparedStatement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Poline(org.folio.rest.persist.helpers.Poline) PrepareOptions(io.vertx.sqlclient.PrepareOptions) SqlConnection(io.vertx.sqlclient.SqlConnection) Limit(org.folio.rest.persist.Criteria.Limit) QueryHelper(org.folio.rest.persist.PostgresClient.QueryHelper) RowSet(io.vertx.sqlclient.RowSet) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AsyncResult(io.vertx.core.AsyncResult) LinkedList(java.util.LinkedList) DatabaseMetadata(io.vertx.sqlclient.spi.DatabaseMetadata) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Before(org.junit.Before) Files(java.nio.file.Files) Query(io.vertx.sqlclient.Query) Promise(io.vertx.core.Promise) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) PgPool(io.vertx.pgclient.PgPool) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) PreparedQuery(io.vertx.sqlclient.PreparedQuery) JsonArray(io.vertx.core.json.JsonArray) PgConnection(io.vertx.pgclient.PgConnection) Rule(org.junit.Rule) Paths(java.nio.file.Paths) Row(io.vertx.sqlclient.Row) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) SimplePojo(org.folio.rest.persist.helpers.SimplePojo) Collections(java.util.Collections) InputStream(java.io.InputStream) CQL2PgJSON(org.folio.cql2pgjson.CQL2PgJSON) Async(io.vertx.ext.unit.Async) FacetField(org.folio.rest.persist.facets.FacetField) JsonObject(io.vertx.core.json.JsonObject) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Test(org.junit.Test)

Example 5 with ResultInfo

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

the class PostgresClientIT method getCQLWrapperWithFacets.

@Test
public void getCQLWrapperWithFacets(TestContext context) throws FieldException {
    final String tableDefiniton = "id UUID PRIMARY KEY , jsonb JSONB NOT NULL, distinct_test_field TEXT";
    createTableWithPoLines(context, MOCK_POLINES_TABLE, tableDefiniton);
    CQL2PgJSON cql2pgJson = new CQL2PgJSON("jsonb");
    List<FacetField> facets = List.of(new FacetField("jsonb->>'edition'"));
    {
        CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
        Async async = context.async();
        postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, null, handler -> {
            context.assertTrue(handler.succeeded());
            ResultInfo resultInfo = handler.result().getResultInfo();
            context.assertEquals(6, resultInfo.getTotalRecords());
            List<Facet> retFacets = resultInfo.getFacets();
            context.assertEquals(1, retFacets.size());
            async.complete();
        });
        async.awaitSuccess();
    }
    String distinctOn = "jsonb->>'order_format'";
    {
        CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
        Async async = context.async();
        postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, distinctOn, handler -> {
            context.assertTrue(handler.succeeded());
            ResultInfo resultInfo = handler.result().getResultInfo();
            context.assertEquals(4, resultInfo.getTotalRecords());
            List<Facet> retFacets = resultInfo.getFacets();
            context.assertEquals(1, retFacets.size());
            async.complete();
        });
        async.awaitSuccess();
    }
    {
        CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "order_format==Other");
        Async async = context.async();
        postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, distinctOn, context.asyncAssertSuccess(res -> {
            ResultInfo resultInfo = res.getResultInfo();
            context.assertEquals(1, resultInfo.getTotalRecords());
            List<Object> objs = res.getResults();
            ObjectMapper mapper = new ObjectMapper();
            List<Facet> retFacets = resultInfo.getFacets();
            context.assertEquals(1, retFacets.size());
            context.assertEquals("edition", retFacets.get(0).getType());
            context.assertEquals(1, retFacets.get(0).getFacetValues().get(0).getCount());
            context.assertEquals("First edition", retFacets.get(0).getFacetValues().get(0).getValue().toString());
            context.assertEquals(1, objs.size());
            try {
                context.assertEquals("70fb4e66-cdf1-11e8-a8d5-f2801f1b9fd1", new JsonObject(mapper.writeValueAsString(objs.get(0))).getString("id"));
            } catch (JsonProcessingException e) {
                context.fail(e);
            }
            async.complete();
        }));
        async.awaitSuccess();
    }
    {
        CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "order_format==Other");
        Async async = context.async();
        postgresClient.get(MOCK_POLINES_TABLE, Poline.class, "*", cqlWrapper, true, true, facets, distinctOn, context.asyncAssertSuccess(res -> {
            ResultInfo resultInfo = res.getResultInfo();
            context.assertEquals(1, resultInfo.getTotalRecords());
            List<Poline> objs = res.getResults();
            List<Facet> retFacets = resultInfo.getFacets();
            context.assertEquals(1, retFacets.size());
            context.assertEquals("edition", retFacets.get(0).getType());
            context.assertEquals(1, retFacets.get(0).getFacetValues().get(0).getCount());
            context.assertEquals("First edition", retFacets.get(0).getFacetValues().get(0).getValue().toString());
            context.assertEquals(1, objs.size());
            context.assertEquals("70fb4e66-cdf1-11e8-a8d5-f2801f1b9fd1", objs.get(0).getId());
            async.complete();
        }));
        async.awaitSuccess();
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestContext(io.vertx.ext.unit.TestContext) RowStream(io.vertx.sqlclient.RowStream) Arrays(java.util.Arrays) PgNotification(io.vertx.pgclient.PgNotification) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) VertxUtils(org.folio.rest.tools.utils.VertxUtils) Tuple(io.vertx.sqlclient.Tuple) UpdateSection(org.folio.rest.persist.Criteria.UpdateSection) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) RowIterator(io.vertx.sqlclient.RowIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SqlResult(io.vertx.sqlclient.SqlResult) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) Offset(org.folio.rest.persist.Criteria.Offset) Collector(java.util.stream.Collector) Transaction(io.vertx.sqlclient.Transaction) AfterClass(org.junit.AfterClass) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RowImpl(io.vertx.pgclient.impl.RowImpl) Set(java.util.Set) UUID(java.util.UUID) FieldException(org.folio.cql2pgjson.exception.FieldException) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) IOUtils(org.apache.commons.io.IOUtils) CQL2PgJSON(org.folio.cql2pgjson.CQL2PgJSON) Base64(java.util.Base64) List(java.util.List) Stream(java.util.stream.Stream) Criterion(org.folio.rest.persist.Criteria.Criterion) Results(org.folio.rest.persist.interfaces.Results) Facet(org.folio.rest.jaxrs.model.Facet) RowDesc(io.vertx.sqlclient.impl.RowDesc) Async(io.vertx.ext.unit.Async) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) BeforeClass(org.junit.BeforeClass) FacetField(org.folio.rest.persist.facets.FacetField) PostgresTesterContainer(org.folio.postgres.testing.PostgresTesterContainer) Criteria(org.folio.rest.persist.Criteria.Criteria) CoreMatchers.not(org.hamcrest.CoreMatchers.not) RunWith(org.junit.runner.RunWith) Timeout(io.vertx.ext.unit.junit.Timeout) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) Function(java.util.function.Function) TotaledResults(org.folio.rest.persist.PostgresClient.TotaledResults) ArrayList(java.util.ArrayList) PreparedStatement(io.vertx.sqlclient.PreparedStatement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Poline(org.folio.rest.persist.helpers.Poline) PrepareOptions(io.vertx.sqlclient.PrepareOptions) SqlConnection(io.vertx.sqlclient.SqlConnection) Limit(org.folio.rest.persist.Criteria.Limit) QueryHelper(org.folio.rest.persist.PostgresClient.QueryHelper) RowSet(io.vertx.sqlclient.RowSet) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AsyncResult(io.vertx.core.AsyncResult) LinkedList(java.util.LinkedList) DatabaseMetadata(io.vertx.sqlclient.spi.DatabaseMetadata) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Before(org.junit.Before) Files(java.nio.file.Files) Query(io.vertx.sqlclient.Query) Promise(io.vertx.core.Promise) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) PgPool(io.vertx.pgclient.PgPool) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) PreparedQuery(io.vertx.sqlclient.PreparedQuery) JsonArray(io.vertx.core.json.JsonArray) PgConnection(io.vertx.pgclient.PgConnection) Rule(org.junit.Rule) Paths(java.nio.file.Paths) Row(io.vertx.sqlclient.Row) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) SimplePojo(org.folio.rest.persist.helpers.SimplePojo) Collections(java.util.Collections) InputStream(java.io.InputStream) Poline(org.folio.rest.persist.helpers.Poline) FacetField(org.folio.rest.persist.facets.FacetField) JsonObject(io.vertx.core.json.JsonObject) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CQL2PgJSON(org.folio.cql2pgjson.CQL2PgJSON) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Facet(org.folio.rest.jaxrs.model.Facet) Test(org.junit.Test)

Aggregations

ResultInfo (org.folio.rest.jaxrs.model.ResultInfo)12 Results (org.folio.rest.persist.interfaces.Results)11 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)10 JsonObject (io.vertx.core.json.JsonObject)10 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 AsyncResult (io.vertx.core.AsyncResult)9 Future (io.vertx.core.Future)9 Handler (io.vertx.core.Handler)9 Promise (io.vertx.core.Promise)9 Vertx (io.vertx.core.Vertx)9 JsonArray (io.vertx.core.json.JsonArray)9 PgConnection (io.vertx.pgclient.PgConnection)9 PgPool (io.vertx.pgclient.PgPool)9 PreparedStatement (io.vertx.sqlclient.PreparedStatement)9 Row (io.vertx.sqlclient.Row)9 RowIterator (io.vertx.sqlclient.RowIterator)9 RowSet (io.vertx.sqlclient.RowSet)9 RowStream (io.vertx.sqlclient.RowStream)9