use of com.google.datastore.v1.AllocateIdsResponse in project java-datastore by googleapis.
the class DatastoreClientTest method initializer.
@Test
public void initializer() throws Exception {
options.initializer(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
request.getHeaders().setCookie("magic");
}
});
Datastore datastore = factory.create(options.build());
MockDatastoreFactory mockClient = (MockDatastoreFactory) factory;
AllocateIdsRequest request = AllocateIdsRequest.newBuilder().build();
AllocateIdsResponse response = AllocateIdsResponse.newBuilder().build();
mockClient.setNextResponse(response);
assertEquals(response, datastore.allocateIds(request));
assertEquals("magic", mockClient.lastCookies.get(0));
}
use of com.google.datastore.v1.AllocateIdsResponse in project appengine-java-standard by GoogleCloudPlatform.
the class AsyncCloudDatastoreV1ServiceImplTest method testCommitTriggersGetOnAllFutures.
@Test
public void testCommitTriggersGetOnAllFutures() throws Exception {
// This test will have 2 Put calls and 1 Get call within a transaction.
Entity putEntity1 = new Entity("Foo1");
putEntity1.setProperty("aString", "test1");
Entity putEntity2 = new Entity("Foo2");
putEntity1.setProperty("aString", "test2");
Key getKey1 = KeyFactory.createKey("Foo3", "name1");
Entity expectedGetEntity = new Entity(getKey1);
expectedGetEntity.setProperty("aString", "test3");
ByteString remoteTxn = expectBeginTransaction();
AllocateIdsRequest expectedAllocIdsReq1 = createAllocateIdsRequest(putEntity1);
AllocateIdsRequest expectedAllocIdsReq2 = createAllocateIdsRequest(putEntity2);
LookupRequest expectedLookupRequest = createLookupRequest(remoteTxn, getKey1).build();
// Use TrackingFutures to allow us to assert exactly when Future.get is called.
TrackingFuture<AllocateIdsResponse> trackingAllocIdRespFuture1 = new TrackingFuture<>(createAllocateIdsResponse(expectedAllocIdsReq1, 11L));
TrackingFuture<AllocateIdsResponse> trackingAllocIdRespFuture2 = new TrackingFuture<>(createAllocateIdsResponse(expectedAllocIdsReq2, 22L));
CommitRequest expectedPutRequest = createPutCommitRequest(remoteTxn, copyWithNewId(putEntity2, 22L), copyWithNewId(putEntity1, 11L)).build();
CommitResponse expectedPutResponse = CommitResponse.newBuilder().addMutationResults(MutationResult.getDefaultInstance()).addMutationResults(MutationResult.getDefaultInstance()).build();
TrackingFuture<CommitResponse> trackingPutResponseFuture = new TrackingFuture<>(expectedPutResponse);
TrackingFuture<LookupResponse> trackingLookupResponseFuture = new TrackingFuture<>(createLookupResponse(expectedGetEntity));
expectAllocateIds(expectedAllocIdsReq1, trackingAllocIdRespFuture1);
expectAllocateIds(expectedAllocIdsReq2, trackingAllocIdRespFuture2);
expectLookup(expectedLookupRequest, trackingLookupResponseFuture);
expectCommitRequest(expectedPutRequest, trackingPutResponseFuture);
replay();
AsyncCloudDatastoreV1ServiceImpl ads = newAsyncDatastoreService();
// Begin a transaction.
Transaction txn = ads.beginTransaction().get();
// Issue the async calls.
Future<Key> putFuture1 = ads.put(putEntity1);
Future<Key> putFuture2 = ads.put(putEntity2);
Future<Entity> getFuture = ads.get(getKey1);
// Nothing should have triggered the Future.get calls yet.
assertThat(trackingAllocIdRespFuture1.getNumCallsToGet()).isEqualTo(0);
assertThat(trackingAllocIdRespFuture2.getNumCallsToGet()).isEqualTo(0);
assertThat(trackingPutResponseFuture.getNumCallsToGet()).isEqualTo(0);
assertThat(trackingLookupResponseFuture.getNumCallsToGet()).isEqualTo(0);
// Simulate user code getting one of the Futures.
Key putResult2 = putFuture2.get();
assertThat(putResult2.getId()).isEqualTo(22L);
// That should be the only one of the four underlying Futures that had get() called.
assertThat(trackingAllocIdRespFuture1.getNumCallsToGet()).isEqualTo(0);
assertThat(trackingAllocIdRespFuture2.getNumCallsToGet()).isEqualTo(1);
assertThat(trackingPutResponseFuture.getNumCallsToGet()).isEqualTo(0);
assertThat(trackingLookupResponseFuture.getNumCallsToGet()).isEqualTo(0);
// Commit the transaction. This should trigger the other underlying Futures.
txn.commit();
assertThat(trackingAllocIdRespFuture1.getNumCallsToGet()).isEqualTo(1);
assertThat(trackingAllocIdRespFuture2.getNumCallsToGet()).isEqualTo(1);
assertThat(trackingPutResponseFuture.getNumCallsToGet()).isEqualTo(1);
assertThat(trackingLookupResponseFuture.getNumCallsToGet()).isEqualTo(1);
// User code gets the remaining results after the commit.
Key putResult1 = putFuture1.get();
assertThat(putResult1.getId()).isEqualTo(11L);
Entity getResult = getFuture.get();
assertThat(getResult).isEqualTo(expectedGetEntity);
// This triggers an additional call on the getFuture, but no additional calls to the
// CloudDatastoreV1Proxy.
assertThat(trackingAllocIdRespFuture1.getNumCallsToGet()).isEqualTo(1);
assertThat(trackingAllocIdRespFuture2.getNumCallsToGet()).isEqualTo(1);
assertThat(trackingPutResponseFuture.getNumCallsToGet()).isEqualTo(1);
assertThat(trackingLookupResponseFuture.getNumCallsToGet()).isEqualTo(2);
verify();
}
use of com.google.datastore.v1.AllocateIdsResponse in project appengine-java-standard by GoogleCloudPlatform.
the class BaseCloudDatastoreV1ServiceImplTest method expectAllocateIds.
AllocateIdsResponse expectAllocateIds(List<Key> keys, Long... allocatedIds) {
AllocateIdsRequest allocIdsReq = createAllocateIdsRequest(keys);
AllocateIdsResponse allocIdsResp = createAllocateIdsResponse(allocIdsReq, allocatedIds);
expectAllocateIds(allocIdsReq, Futures.immediateFuture(allocIdsResp));
return allocIdsResp;
}
use of com.google.datastore.v1.AllocateIdsResponse in project java-datastore by googleapis.
the class DatastoreTest method initializer.
@Test
public void initializer() throws Exception {
options.initializer(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
request.getHeaders().setCookie("magic");
}
});
Datastore datastore = factory.create(options.build());
MockDatastoreFactory mockClient = (MockDatastoreFactory) factory;
AllocateIdsRequest request = AllocateIdsRequest.newBuilder().build();
AllocateIdsResponse response = AllocateIdsResponse.newBuilder().build();
mockClient.setNextResponse(response);
assertEquals(response, datastore.allocateIds(request));
assertEquals("magic", mockClient.lastCookies.get(0));
}
Aggregations