Search in sources :

Example 6 with GetOptions

use of io.syndesis.server.jsondb.GetOptions in project syndesis by syndesisio.

the class JsonDBTest method testGetEndBeforeWithDESC.

@Test
public void testGetEndBeforeWithDESC() throws IOException {
    jsondb.set("/test", mapper.writeValueAsString(map("user1", "test 1", "user2", "test 2", "user3", "test 3", "user4", "test 4", "user5", "test 5", "user6", "test 6")));
    String json = jsondb.getAsString("/test", new GetOptions().endBefore("user2").order(GetOptions.Order.DESC));
    assertThat(json).isEqualTo("{\"user6\":\"test 6\",\"user5\":\"test 5\",\"user4\":\"test 4\",\"user3\":\"test 3\"}");
}
Also used : GetOptions(io.syndesis.server.jsondb.GetOptions) Test(org.junit.Test)

Example 7 with GetOptions

use of io.syndesis.server.jsondb.GetOptions in project syndesis by syndesisio.

the class JsonDBTest method testGetPrettyPrint.

@Test
public void testGetPrettyPrint() throws IOException {
    jsondb.set("/test", mapper.writeValueAsString(map("name", "Hiram Chirino")));
    // Check that the result is pretty printed
    String json = jsondb.getAsString("/", new GetOptions().prettyPrint(true));
    assertThat(json).isEqualTo("{\n" + "  \"test\" : {\n" + "    \"name\" : \"Hiram Chirino\"\n" + "  }\n" + "}");
    // Check that the result is not pretty printed
    json = jsondb.getAsString("/", new GetOptions().prettyPrint(false));
    assertThat(json).isEqualTo("{\"test\":{\"name\":\"Hiram Chirino\"}}");
    // We default to not pretty printing
    json = jsondb.getAsString("/");
    assertThat(json).isEqualTo("{\"test\":{\"name\":\"Hiram Chirino\"}}");
}
Also used : GetOptions(io.syndesis.server.jsondb.GetOptions) Test(org.junit.Test)

Example 8 with GetOptions

use of io.syndesis.server.jsondb.GetOptions in project syndesis by syndesisio.

the class JsonDBTest method testGetOrder.

@Test
public void testGetOrder() throws IOException {
    jsondb.set("/test", mapper.writeValueAsString(map("user1", "test 1", "user2", "test 2", "user3", "test 3")));
    // Default order is ASC
    String json = jsondb.getAsString("/test", new GetOptions());
    assertThat(json).isEqualTo("{\"user1\":\"test 1\",\"user2\":\"test 2\",\"user3\":\"test 3\"}");
    // Explicit ASC should give us the same.
    json = jsondb.getAsString("/test", new GetOptions().order(GetOptions.Order.ASC));
    assertThat(json).isEqualTo("{\"user1\":\"test 1\",\"user2\":\"test 2\",\"user3\":\"test 3\"}");
    // DESC ord should reverse the output order.
    json = jsondb.getAsString("/test", new GetOptions().order(GetOptions.Order.DESC));
    assertThat(json).isEqualTo("{\"user3\":\"test 3\",\"user2\":\"test 2\",\"user1\":\"test 1\"}");
}
Also used : GetOptions(io.syndesis.server.jsondb.GetOptions) Test(org.junit.Test)

Example 9 with GetOptions

use of io.syndesis.server.jsondb.GetOptions in project syndesis by syndesisio.

the class JsonDBTest method testGetStartAtWithDESC.

@Test
public void testGetStartAtWithDESC() throws IOException {
    jsondb.set("/test", mapper.writeValueAsString(map("user1", "test 1", "user2", "test 2", "user3", "test 3", "user4", "test 4", "user5", "test 5", "user6", "test 6")));
    String json = jsondb.getAsString("/test", new GetOptions().startAt("user2").order(GetOptions.Order.DESC));
    assertThat(json).isEqualTo("{\"user2\":\"test 2\",\"user1\":\"test 1\"}");
}
Also used : GetOptions(io.syndesis.server.jsondb.GetOptions) Test(org.junit.Test)

Example 10 with GetOptions

use of io.syndesis.server.jsondb.GetOptions in project syndesis by syndesisio.

the class JsonDbDao method fetchAll.

@Override
@SuppressWarnings({ "unchecked", "PMD.CyclomaticComplexity" })
public ListResult<T> fetchAll(Function<ListResult<T>, ListResult<T>>... operators) {
    try {
        GetOptions options = new GetOptions();
        // Try to convert operators to equivalent DB queries.
        if (operators != null) {
            for (int i = 0; i < operators.length; i++) {
                Function<ListResult<T>, ListResult<T>> operator = operators[i];
                if (operator.getClass() == IdPrefixFilter.class) {
                    IdPrefixFilter<T> filter = (IdPrefixFilter<T>) operator;
                    options.startAt(":" + filter.getPrefix());
                    options.endAt(":" + filter.getPrefix());
                    // Take it out of the list.
                    operators[i] = null;
                }
            }
        }
        // get the data out..
        byte[] json = jsondb.getAsByteArray(getCollectionPath(), options);
        ListResult<T> result;
        if (json != null && json.length > 0) {
            // Lets use jackson to parse the map of keys to our model instances
            ObjectReader reader = Json.reader();
            TypeFactory typeFactory = reader.getTypeFactory();
            MapType mapType = typeFactory.constructMapType(LinkedHashMap.class, String.class, getType());
            LinkedHashMap<String, T> map = reader.forType(mapType).readValue(json);
            result = ListResult.of(map.values());
        } else {
            result = ListResult.of(Collections.<T>emptyList());
        }
        if (operators == null) {
            return result;
        }
        for (Function<ListResult<T>, ListResult<T>> operator : operators) {
            if (operator != null) {
                result = operator.apply(result);
            }
        }
        return result;
    } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException | IOException e) {
        throw SyndesisServerException.launderThrowable(e);
    }
}
Also used : IOException(java.io.IOException) GetOptions(io.syndesis.server.jsondb.GetOptions) MapType(com.fasterxml.jackson.databind.type.MapType) ListResult(io.syndesis.common.model.ListResult) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) IdPrefixFilter(io.syndesis.server.dao.manager.operators.IdPrefixFilter)

Aggregations

GetOptions (io.syndesis.server.jsondb.GetOptions)13 Test (org.junit.Test)9 Map (java.util.Map)3 Consumer (java.util.function.Consumer)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 MapType (com.fasterxml.jackson.databind.type.MapType)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 PodListBuilder (io.fabric8.kubernetes.api.model.PodListBuilder)1 ListResult (io.syndesis.common.model.ListResult)1 IdPrefixFilter (io.syndesis.server.dao.manager.operators.IdPrefixFilter)1 Activity (io.syndesis.server.endpoint.v1.handler.activity.Activity)1 ActivityStep (io.syndesis.server.endpoint.v1.handler.activity.ActivityStep)1 JsonDB (io.syndesis.server.jsondb.JsonDB)1 JsonDBException (io.syndesis.server.jsondb.JsonDBException)1 InputStream (java.io.InputStream)1