Search in sources :

Example 1 with BeginTransactionResponse

use of com.google.firestore.v1beta1.BeginTransactionResponse in project spring-cloud-gcp by spring-cloud.

the class ReactiveFirestoreTransactionManagerTest method getFirestoreTemplate.

private FirestoreTemplate getFirestoreTemplate() {
    doAnswer(invocation -> {
        StreamObserver<BeginTransactionResponse> streamObserver = invocation.getArgument(1);
        streamObserver.onNext(BeginTransactionResponse.newBuilder().setTransaction(ByteString.copyFromUtf8("transaction1")).build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).beginTransaction(any(), any());
    doAnswer(invocation -> {
        CommitRequest commitRequest = invocation.getArgument(0);
        StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
        assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onNext(CommitResponse.newBuilder().build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).commit(any(), any());
    doAnswer(invocation -> {
        RollbackRequest rollbackRequest = invocation.getArgument(0);
        StreamObserver<Empty> streamObserver = invocation.getArgument(1);
        assertThat(rollbackRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).rollback(any(), any());
    doAnswer(invocation -> {
        GetDocumentRequest request = invocation.getArgument(0);
        StreamObserver<Document> streamObserver = invocation.getArgument(1);
        assertThat(request.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        String name = request.getName();
        streamObserver.onNext(FirestoreTemplateTests.buildDocument(name.substring(name.length() - 2), 100L));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).getDocument(any(), any());
    FirestoreTemplate template = new FirestoreTemplate(this.firestoreStub, this.parent, new FirestoreDefaultClassMapper(), new FirestoreMappingContext());
    StepVerifier.setDefaultTimeout(Duration.ofSeconds(5));
    return template;
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) CommitResponse(com.google.firestore.v1.CommitResponse) ByteString(com.google.protobuf.ByteString) RollbackRequest(com.google.firestore.v1.RollbackRequest) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Document(com.google.firestore.v1.Document) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) FirestoreMappingContext(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreMappingContext) Empty(com.google.protobuf.Empty) FirestoreDefaultClassMapper(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreDefaultClassMapper)

Example 2 with BeginTransactionResponse

use of com.google.firestore.v1beta1.BeginTransactionResponse in project spring-cloud-gcp by GoogleCloudPlatform.

the class ReactiveFirestoreTransactionManagerTest method getFirestoreTemplate.

private FirestoreTemplate getFirestoreTemplate() {
    doAnswer(invocation -> {
        StreamObserver<BeginTransactionResponse> streamObserver = invocation.getArgument(1);
        streamObserver.onNext(BeginTransactionResponse.newBuilder().setTransaction(ByteString.copyFromUtf8("transaction1")).build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).beginTransaction(any(), any());
    doAnswer(invocation -> {
        CommitRequest commitRequest = invocation.getArgument(0);
        StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
        assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onNext(CommitResponse.newBuilder().build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).commit(any(), any());
    doAnswer(invocation -> {
        RollbackRequest rollbackRequest = invocation.getArgument(0);
        StreamObserver<Empty> streamObserver = invocation.getArgument(1);
        assertThat(rollbackRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).rollback(any(), any());
    doAnswer(invocation -> {
        GetDocumentRequest request = invocation.getArgument(0);
        StreamObserver<Document> streamObserver = invocation.getArgument(1);
        assertThat(request.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        String name = request.getName();
        streamObserver.onNext(FirestoreTemplateTests.buildDocument(name.substring(name.length() - 2), 100L));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).getDocument(any(), any());
    FirestoreMappingContext mappingContext = new FirestoreMappingContext();
    FirestoreTemplate template = new FirestoreTemplate(this.firestoreStub, this.parent, new FirestoreDefaultClassMapper(mappingContext), mappingContext);
    StepVerifier.setDefaultTimeout(Duration.ofSeconds(5));
    return template;
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) CommitResponse(com.google.firestore.v1.CommitResponse) ByteString(com.google.protobuf.ByteString) RollbackRequest(com.google.firestore.v1.RollbackRequest) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Document(com.google.firestore.v1.Document) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) FirestoreMappingContext(com.google.cloud.spring.data.firestore.mapping.FirestoreMappingContext) FirestoreTemplate(com.google.cloud.spring.data.firestore.FirestoreTemplate) Empty(com.google.protobuf.Empty) FirestoreDefaultClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreDefaultClassMapper)

Example 3 with BeginTransactionResponse

use of com.google.firestore.v1beta1.BeginTransactionResponse in project appengine-java-standard by GoogleCloudPlatform.

the class CloudDatastoreV1ClientImplTest method testStackTrace.

@Test
public void testStackTrace() throws Throwable {
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    when(datastore.beginTransaction(req)).thenThrow(new DatastoreException("beginTransaction", Code.INVALID_ARGUMENT, "message", null));
    CloudDatastoreV1ClientImpl client = newClient(datastore);
    Future<BeginTransactionResponse> future = makeBeginTransactionCallForTest(client, req);
    try {
        future.get();
        fail("expected exception");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
        // The test helper method used to call beginTransaction() appears in the error message
        // (even though it is no longer on the stack).
        assertThat(e).hasMessageThat().contains("makeBeginTransactionCallForTest");
        // The underlying cause is also present.
        assertThat(e).hasCauseThat().hasCauseThat().isInstanceOf(DatastoreException.class);
    }
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) DatastoreException(com.google.datastore.v1.client.DatastoreException) ExecutionException(java.util.concurrent.ExecutionException) BeginTransactionResponse(com.google.datastore.v1.BeginTransactionResponse) Test(org.junit.Test)

Example 4 with BeginTransactionResponse

use of com.google.firestore.v1beta1.BeginTransactionResponse in project grpc-gcp-java by GoogleCloudPlatform.

the class BeginTransaction method beginTransactionCall.

public void beginTransactionCall() {
    FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
    TransactionOptions tOpts = TransactionOptions.newBuilder().build();
    BeginTransactionRequest beginTransactionRequest = BeginTransactionRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").setOptions(tOpts).build();
    BeginTransactionResponse response;
    try {
        response = blockingStub.beginTransaction(beginTransactionRequest);
    } catch (Exception e) {
        System.out.println("Error during call: " + e.getMessage() + e.getCause());
        return;
    }
    System.out.println("Began Transaction: " + response.getTransaction().toString());
    Main.transactionId = response.getTransaction();
    Menu menu = new Menu();
    menu.draw();
}
Also used : GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) TransactionOptions(com.google.firestore.v1beta1.TransactionOptions) BeginTransactionRequest(com.google.firestore.v1beta1.BeginTransactionRequest) Menu(org.roguewave.grpc.util.gfx.Menu) BeginTransactionResponse(com.google.firestore.v1beta1.BeginTransactionResponse)

Example 5 with BeginTransactionResponse

use of com.google.firestore.v1beta1.BeginTransactionResponse in project spring-cloud-gcp by spring-cloud.

the class ReactiveFirestoreTransactionManager method startTransaction.

private Mono<ReactiveFirestoreResourceHolder> startTransaction(TransactionDefinition definition) {
    TransactionOptions.Builder txOptions = definition.isReadOnly() ? TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.newBuilder().build()) : TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.newBuilder().build());
    BeginTransactionRequest beginTransactionRequest = BeginTransactionRequest.newBuilder().setOptions(txOptions).setDatabase(this.databasePath).build();
    return ObservableReactiveUtil.<BeginTransactionResponse>unaryCall(obs -> this.firestore.beginTransaction(beginTransactionRequest, obs)).map(beginTransactionResponse -> new ReactiveFirestoreResourceHolder(beginTransactionResponse.getTransaction()));
}
Also used : CommitResponse(com.google.firestore.v1.CommitResponse) FirestoreGrpc(com.google.firestore.v1.FirestoreGrpc) Util(org.springframework.cloud.gcp.data.firestore.util.Util) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Mono(reactor.core.publisher.Mono) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest) ObservableReactiveUtil(org.springframework.cloud.gcp.data.firestore.util.ObservableReactiveUtil) AbstractReactiveTransactionManager(org.springframework.transaction.reactive.AbstractReactiveTransactionManager) ByteString(com.google.protobuf.ByteString) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Empty(com.google.protobuf.Empty) SmartTransactionObject(org.springframework.transaction.support.SmartTransactionObject) TransactionSystemException(org.springframework.transaction.TransactionSystemException) GenericReactiveTransaction(org.springframework.transaction.reactive.GenericReactiveTransaction) CommitRequest(com.google.firestore.v1.CommitRequest) TransactionOptions(com.google.firestore.v1.TransactionOptions) RollbackRequest(com.google.firestore.v1.RollbackRequest) TransactionException(org.springframework.transaction.TransactionException) Nullable(org.springframework.lang.Nullable) Assert(org.springframework.util.Assert) TransactionOptions(com.google.firestore.v1.TransactionOptions) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest)

Aggregations

BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)5 ByteString (com.google.protobuf.ByteString)5 CommitRequest (com.google.firestore.v1.CommitRequest)4 CommitResponse (com.google.firestore.v1.CommitResponse)4 RollbackRequest (com.google.firestore.v1.RollbackRequest)4 Empty (com.google.protobuf.Empty)4 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)3 Test (org.junit.Test)3 BeginTransactionResponse (com.google.datastore.v1.BeginTransactionResponse)2 Document (com.google.firestore.v1.Document)2 FirestoreGrpc (com.google.firestore.v1.FirestoreGrpc)2 GetDocumentRequest (com.google.firestore.v1.GetDocumentRequest)2 TransactionOptions (com.google.firestore.v1.TransactionOptions)2 Nullable (org.springframework.lang.Nullable)2 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2 TransactionException (org.springframework.transaction.TransactionException)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)2 AbstractReactiveTransactionManager (org.springframework.transaction.reactive.AbstractReactiveTransactionManager)2 GenericReactiveTransaction (org.springframework.transaction.reactive.GenericReactiveTransaction)2 TransactionSynchronizationManager (org.springframework.transaction.reactive.TransactionSynchronizationManager)2