Search in sources :

Example 1 with AllocateIdsRequest

use of com.google.datastore.v1.AllocateIdsRequest 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));
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) AllocateIdsResponse(com.google.datastore.v1.AllocateIdsResponse) AllocateIdsRequest(com.google.datastore.v1.AllocateIdsRequest) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) Test(org.junit.Test)

Example 2 with AllocateIdsRequest

use of com.google.datastore.v1.AllocateIdsRequest 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();
}
Also used : CommitRequest(com.google.datastore.v1.CommitRequest) ByteString(com.google.protobuf.ByteString) CommitResponse(com.google.datastore.v1.CommitResponse) AllocateIdsRequest(com.google.datastore.v1.AllocateIdsRequest) LookupResponse(com.google.datastore.v1.LookupResponse) LookupRequest(com.google.datastore.v1.LookupRequest) AllocateIdsResponse(com.google.datastore.v1.AllocateIdsResponse) Test(org.junit.Test)

Example 3 with AllocateIdsRequest

use of com.google.datastore.v1.AllocateIdsRequest 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;
}
Also used : AllocateIdsResponse(com.google.datastore.v1.AllocateIdsResponse) AllocateIdsRequest(com.google.datastore.v1.AllocateIdsRequest)

Example 4 with AllocateIdsRequest

use of com.google.datastore.v1.AllocateIdsRequest 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));
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) AllocateIdsResponse(com.google.datastore.v1.AllocateIdsResponse) AllocateIdsRequest(com.google.datastore.v1.AllocateIdsRequest) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) Test(org.junit.Test)

Aggregations

AllocateIdsRequest (com.google.datastore.v1.AllocateIdsRequest)4 AllocateIdsResponse (com.google.datastore.v1.AllocateIdsResponse)4 Test (org.junit.Test)3 HttpRequest (com.google.api.client.http.HttpRequest)2 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)2 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)2 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)2 CommitRequest (com.google.datastore.v1.CommitRequest)1 CommitResponse (com.google.datastore.v1.CommitResponse)1 LookupRequest (com.google.datastore.v1.LookupRequest)1 LookupResponse (com.google.datastore.v1.LookupResponse)1 ByteString (com.google.protobuf.ByteString)1