Search in sources :

Example 11 with EntityQuery

use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQueryTests method nullSort.

@Test
public void nullSort() throws NoSuchMethodException {
    queryWithMockResult("findByActionAndSymbolAndPriceLessThanAndPriceGreater" + "ThanEqualAndIdIsNullOrderByIdDesc", null, getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class, Sort.class));
    Object[] params = new Object[] { "BUY", "abcd", 8.88, 3.33, null };
    when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setOrderBy(OrderBy.desc("__key__")).build();
        assertThat(statement).isEqualTo(expected);
        return EMPTY_RESPONSE;
    });
    when(this.queryMethod.getCollectionReturnType()).thenReturn(List.class);
    this.partTreeDatastoreQuery.execute(params);
    verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), any());
}
Also used : Sort(org.springframework.data.domain.Sort) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 12 with EntityQuery

use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQueryTests method prepareSliceResults.

private void prepareSliceResults(int offset, Integer queryLimit, Boolean hasNext) {
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    List<Integer> datastoreMatchingRecords = Arrays.asList(3, 4, 5);
    when(this.datastoreTemplate.queryEntitiesSlice(isA(EntityQuery.class), any(), any())).thenAnswer((invocation) -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setOffset(offset).setOrderBy(OrderBy.desc("__key__")).setLimit(queryLimit).build();
        assertThat(statement).isEqualTo(expected);
        return new SliceImpl(new DatastoreResultsIterable(datastoreMatchingRecords.iterator(), cursor).toList(), Pageable.unpaged(), hasNext);
    });
}
Also used : DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Cursor(com.google.cloud.datastore.Cursor) EntityQuery(com.google.cloud.datastore.EntityQuery) SliceImpl(org.springframework.data.domain.SliceImpl)

Example 13 with EntityQuery

use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQueryTests method nullPageable.

@Test
public void nullPageable() throws NoSuchMethodException {
    queryWithMockResult("findTop333ByActionAndSymbolAndPriceLessThanAndPriceGreater" + "ThanEqualAndIdIsNullOrderByIdDesc", null, getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class, Pageable.class));
    Object[] params = new Object[] { "BUY", "abcd", 8.88, 3.33, null };
    when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setLimit(333).setOrderBy(OrderBy.desc("__key__")).build();
        assertThat(statement).isEqualTo(expected);
        return EMPTY_RESPONSE;
    });
    when(this.queryMethod.getCollectionReturnType()).thenReturn(List.class);
    this.partTreeDatastoreQuery.execute(params);
    verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), any());
}
Also used : Pageable(org.springframework.data.domain.Pageable) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 14 with EntityQuery

use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQueryTests method nonCollectionReturnType.

@Test
public void nonCollectionReturnType() throws NoSuchMethodException {
    Trade trade = new Trade();
    queryWithMockResult("findByAction", null, getClass().getMethod("findByAction", String.class), true, null);
    Object[] params = new Object[] { "BUY" };
    when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(PropertyFilter.eq("action", "BUY")).setKind("trades").setLimit(1).build();
        assertThat(statement).isEqualTo(expected);
        List<Trade> results = Collections.singletonList(trade);
        return new DatastoreResultsIterable(results.iterator(), null);
    });
    assertThat(this.partTreeDatastoreQuery.execute(params)).isEqualTo(trade);
}
Also used : DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 15 with EntityQuery

use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplate method resolveDescendantProperties.

private <T> void resolveDescendantProperties(DatastorePersistentEntity datastorePersistentEntity, BaseEntity entity, T convertedObject, ReadContext context) {
    datastorePersistentEntity.doWithDescendantProperties((descendantPersistentProperty) -> {
        Class descendantType = descendantPersistentProperty.getComponentType();
        Key entityKey = (Key) entity.getKey();
        Key ancestorKey = KeyUtil.getKeyWithoutAncestors(entityKey);
        EntityQuery descendantQuery = Query.newEntityQueryBuilder().setKind(this.datastoreMappingContext.getPersistentEntity(descendantType).kindName()).setFilter(PropertyFilter.hasAncestor(ancestorKey)).build();
        List entities = convertEntitiesForRead(getDatastoreReadWriter().run(descendantQuery), descendantType, context);
        datastorePersistentEntity.getPropertyAccessor(convertedObject).setProperty(descendantPersistentProperty, // Converting the collection type.
        this.datastoreEntityConverter.getConversions().convertOnRead(entities, descendantPersistentProperty.getType(), descendantType));
    });
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) BaseKey(com.google.cloud.datastore.BaseKey)

Aggregations

EntityQuery (com.google.cloud.datastore.EntityQuery)15 Test (org.junit.Test)11 DatastoreResultsIterable (org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable)4 Pageable (org.springframework.data.domain.Pageable)3 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)2 StructuredQuery (com.google.cloud.datastore.StructuredQuery)2 ArrayList (java.util.ArrayList)2 Sort (org.springframework.data.domain.Sort)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 Credentials (com.google.auth.Credentials)1 BaseKey (com.google.cloud.datastore.BaseKey)1 Cursor (com.google.cloud.datastore.Cursor)1 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 Entity (com.google.cloud.datastore.Entity)1 IncompleteKey (com.google.cloud.datastore.IncompleteKey)1 Key (com.google.cloud.datastore.Key)1 KeyQuery (com.google.cloud.datastore.KeyQuery)1 Query (com.google.cloud.datastore.Query)1