Java Code Examples for com.google.cloud.datastore.Cursor#copyFrom()

The following examples show how to use com.google.cloud.datastore.Cursor#copyFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PartTreeDatastoreQueryTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void pageableQueryNextPage() throws NoSuchMethodException {
	queryWithMockResult("findByActionAndSymbolAndPriceLessThanAndPriceGreater"
					+ "ThanEqualAndIdIsNull", null,
			getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class,
					Pageable.class));

	this.partTreeDatastoreQuery = createQuery(true, false, null);

	PageRequest pageRequest = PageRequest.of(1, 2, Sort.Direction.DESC, "id");
	Cursor cursor = Cursor.copyFrom("abc".getBytes());
	Object[] params = new Object[] { "BUY", "abcd", 8.88, 3.33,
			DatastorePageable.from(pageRequest, cursor, 99L) };

	preparePageResults(2, 2, cursor, Arrays.asList(3, 4), Arrays.asList(1, 2, 3, 4));

	when(this.queryMethod.getCollectionReturnType()).thenReturn(List.class);

	Page result = (Page) this.partTreeDatastoreQuery.execute(params);
	assertThat(result.getTotalElements()).isEqualTo(99L);
	assertThat(result.getTotalPages()).isEqualTo(50);
	assertThat(result.getNumberOfElements()).isEqualTo(2);

	verify(this.datastoreTemplate, times(1))
			.queryKeysOrEntities(any(), any());
}
 
Example 2
Source File: PartTreeDatastoreQueryTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private void prepareDeleteResults(boolean isCollection) {
	Cursor cursor = Cursor.copyFrom("abc".getBytes());
	List<Integer> datastoreMatchingRecords = Arrays.asList(3, 4, 5);
	when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
		StructuredQuery<?> statement = invocation.getArgument(0);
		StructuredQuery.Builder builder = isCollection ? StructuredQuery.newEntityQueryBuilder()
				: StructuredQuery.newKeyQueryBuilder();
		StructuredQuery<?> expected = builder
				.setFilter(PropertyFilter.eq("action", "BUY"))
				.setKind("trades")
				.build();

		assertThat(statement).isEqualTo(expected);
		return new DatastoreResultsIterable(datastoreMatchingRecords.iterator(), cursor);
	});
}
 
Example 3
Source File: SimpleDatastoreRepositoryTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void findAllPageableCursor() {
	Cursor cursor = Cursor.copyFrom("abc".getBytes());
	Pageable pageable = DatastorePageable.from(PageRequest.of(1, 5, Sort.Direction.DESC, "property1", "property2"),
			cursor, 10L);
	this.simpleDatastoreRepository.findAll(pageable);
	verify(this.datastoreTemplate, times(1)).findAll(eq(Object.class),
			eq(new DatastoreQueryOptions.Builder().setLimit(5).setOffset(5).setSort(Sort.by(
					new Sort.Order(Sort.Direction.DESC, "property1"),
					new Sort.Order(Sort.Direction.DESC, "property2"))).setCursor(cursor).build()));
	verify(this.datastoreTemplate, times(0)).count(any());
}
 
Example 4
Source File: SimpleDatastoreRepositoryTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void findAllByExamplePageCursor() {
	Example<Object> example = Example.of(new Object());
	Sort sort = Sort.by("id");
	Cursor cursor = Cursor.copyFrom("abc".getBytes());

	doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2), cursor))
			.when(this.datastoreTemplate).queryByExample(same(example),
					eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort)
							.build()));

	doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(3, 4), null))
			.when(this.datastoreTemplate).queryByExample(same(example),
					eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor)
							.build()));


	doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2, 3, 4, 5), null))
			.when(this.datastoreTemplate).keyQueryByExample(same(example), isNull());


	Page<Object> result = this.simpleDatastoreRepository.findAll(example, PageRequest.of(0, 2, sort));
	assertThat(result).containsExactly(1, 2);
	assertThat(result.getTotalElements()).isEqualTo(5);

	Page<Object> resultNext = this.simpleDatastoreRepository.findAll(example, result.getPageable().next());
	assertThat(resultNext).containsExactly(3, 4);
	assertThat(resultNext.getTotalElements()).isEqualTo(5);

	verify(this.datastoreTemplate, times(1)).queryByExample(same(example),
			eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort)
					.build()));
	verify(this.datastoreTemplate, times(1)).queryByExample(same(example),
			eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor)
					.build()));
	verify(this.datastoreTemplate, times(1)).keyQueryByExample(same(example), isNull());
}
 
Example 5
Source File: GqlDatastoreQueryTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void pageableTestPage() {

	String gql = "SELECT * FROM trades WHERE price=@price";
	String expected = "SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset";

	Object[] paramVals = new Object[] {1, PageRequest.of(0, 2)};

	String[] paramNames = new String[] { "price", null };

	Parameters parameters = buildParameters(paramVals, paramNames);

	Mockito.<Class>when(this.queryMethod.getReturnedObjectType())
			.thenReturn(Trade.class);
	when(parameters.hasPageableParameter()).thenReturn(true);
	when(parameters.getPageableIndex()).thenReturn(1);

	GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, true, true);

	Cursor cursor = Cursor.copyFrom("abc".getBytes());

	doAnswer((invocation) -> {
		GqlQuery statement = invocation.getArgument(0);

		assertThat(statement.getQueryString().equals(gql) || statement.getQueryString().equals(expected))
				.isEqualTo(true);
		Map<String, Value> paramMap = statement.getNamedBindings();

		if (statement.getQueryString().equals(expected)) {
			assertThat(paramMap.size()).isEqualTo(3);
			assertThat(paramMap.get("price").get()).isEqualTo(1L);
			assertThat(paramMap.get("limit").get()).isEqualTo(2L);
			assertThat(paramMap.get("offset").get()).isEqualTo(0L);
			return new DatastoreResultsIterable(Collections.emptyList(), cursor);
		}
		else if (statement.getQueryString().equals(gql)) {
			assertThat(paramMap.size()).isEqualTo(1);
			assertThat(paramMap.get("price").get()).isEqualTo(1L);
			return new DatastoreResultsIterable(Arrays.asList(1L, 2L), cursor);
		}
		return null;
	}).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));

	doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
	doAnswer((invocation) -> invocation.getArgument(0)).when(gqlDatastoreQuery)
			.processRawObjectForProjection(any());

	Slice result = (Page) gqlDatastoreQuery.execute(paramVals);

	assertThat(((DatastorePageable) result.getPageable()).toCursor()).isEqualTo(cursor);
	assertThat(((DatastorePageable) result.getPageable()).getTotalCount()).isEqualTo(2L);

	assertThat(((Page) result).getTotalElements()).isEqualTo(2L);

	verify(this.datastoreTemplate, times(2))
			.queryKeysOrEntities(any(), eq(Trade.class));
}
 
Example 6
Source File: GqlDatastoreQueryTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void pageableTestPageCursor() {
	String gql = "SELECT * FROM trades WHERE price=@price";
	String expected = "SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset";

	Cursor cursorInPageable = Cursor.copyFrom("cde".getBytes());
	long countInPageable = 123L;
	Object[] paramVals = new Object[] { 1,
			new DatastorePageable(PageRequest.of(0, 2), cursorInPageable, countInPageable) };

	String[] paramNames = new String[] { "price", null };

	Parameters parameters = buildParameters(paramVals, paramNames);

	Mockito.<Class>when(this.queryMethod.getReturnedObjectType())
			.thenReturn(Trade.class);
	when(parameters.hasPageableParameter()).thenReturn(true);
	when(parameters.getPageableIndex()).thenReturn(1);

	GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, true, true);

	Cursor cursor = Cursor.copyFrom("abc".getBytes());

	doAnswer((invocation) -> {
		GqlQuery statement = invocation.getArgument(0);

		assertThat(statement.getQueryString()).isEqualTo(expected);
		Map<String, Object> paramMap = statement.getNamedBindings();

			assertThat(paramMap.size()).isEqualTo(3);
			assertThat(((Value) paramMap.get("price")).get()).isEqualTo(1L);
			assertThat(((Value) paramMap.get("limit")).get()).isEqualTo(2L);
			assertThat(paramMap.get("offset")).isEqualTo(cursorInPageable);
			return new DatastoreResultsIterable(Collections.emptyList(), cursor);
	}).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));

	doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
	doAnswer((invocation) -> invocation.getArgument(0)).when(gqlDatastoreQuery)
			.processRawObjectForProjection(any());

	Slice result = (Page) gqlDatastoreQuery.execute(paramVals);

	assertThat(((DatastorePageable) result.getPageable()).toCursor()).isEqualTo(cursor);
	assertThat(((DatastorePageable) result.getPageable()).getTotalCount()).isEqualTo(countInPageable);

	assertThat(((Page) result).getTotalElements()).isEqualTo(countInPageable);

	verify(this.datastoreTemplate, times(1))
			.queryKeysOrEntities(any(), eq(Trade.class));
}
 
Example 7
Source File: GqlDatastoreQueryTests.java    From spring-cloud-gcp with Apache License 2.0 2 votes vote down vote up
@Test
public void pageableTestSlice() {

	String gql = "SELECT * FROM trades WHERE price=@price";

	Object[] paramVals = new Object[] {1, PageRequest.of(0, 2)};

	String[] paramNames = new String[] { "price", null };

	Parameters parameters = buildParameters(paramVals, paramNames);

	Mockito.<Class>when(this.queryMethod.getReturnedObjectType())
			.thenReturn(Trade.class);
	when(parameters.hasPageableParameter()).thenReturn(true);
	when(parameters.getPageableIndex()).thenReturn(1);

	GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, false, true);

	Cursor cursor = Cursor.copyFrom("abc".getBytes());
	List<Map> params = new ArrayList<>();
	doAnswer((invocation) -> {
		GqlQuery statement = invocation.getArgument(0);

		assertThat(statement.getQueryString()).isEqualTo("SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset");

		Map paramMap = statement.getNamedBindings();

		params.add(paramMap);


		return new DatastoreResultsIterable(Collections.emptyList(), cursor);
	}).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));

	doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
	doAnswer((invocation) -> invocation.getArgument(0)).when(gqlDatastoreQuery)
			.processRawObjectForProjection(any());

	Slice result = (Slice) gqlDatastoreQuery.execute(paramVals);

	assertThat(((DatastorePageable) result.getPageable()).toCursor()).isEqualTo(cursor);

	verify(this.datastoreTemplate, times(2))
			.queryKeysOrEntities(any(), eq(Trade.class));

	assertThat(((Value) params.get(0).get("price")).get()).isEqualTo(1L);
	assertThat(((Value) params.get(0).get("limit")).get()).isEqualTo(2L);
	assertThat(((Value) params.get(0).get("offset")).get()).isEqualTo(0L);

	assertThat(((Value) params.get(1).get("price")).get()).isEqualTo(1L);
	assertThat(((Value) params.get(1).get("limit")).get()).isEqualTo(1L);
	assertThat(params.get(1).get("offset")).isEqualTo(cursor);

}