use of com.google.firestore.v1beta1.RollbackRequest in project java-firestore by googleapis.
the class FirestoreClientTest method rollbackTest.
@Test
public void rollbackTest() throws Exception {
Empty expectedResponse = Empty.newBuilder().build();
mockFirestore.addResponse(expectedResponse);
String database = "database1789464955";
ByteString transaction = ByteString.EMPTY;
client.rollback(database, transaction);
List<AbstractMessage> actualRequests = mockFirestore.getRequests();
Assert.assertEquals(1, actualRequests.size());
RollbackRequest actualRequest = ((RollbackRequest) actualRequests.get(0));
Assert.assertEquals(database, actualRequest.getDatabase());
Assert.assertEquals(transaction, actualRequest.getTransaction());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.firestore.v1beta1.RollbackRequest 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;
}
use of com.google.firestore.v1beta1.RollbackRequest 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;
}
use of com.google.firestore.v1beta1.RollbackRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testErrorHandlingInExplicitTransactionWithoutCommit.
@Test
public void testErrorHandlingInExplicitTransactionWithoutCommit() throws SQLException {
String sql = String.format("%s; %s; %s; %s;", INSERT_STATEMENT, "BEGIN", UPDATE_STATEMENT, INVALID_DML);
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (java.sql.Statement statement = connection.createStatement()) {
SQLException exception = assertThrows(SQLException.class, () -> statement.execute(sql));
assertThat(exception.getMessage(), containsString("INVALID_ARGUMENT: Statement is invalid."));
}
}
// Verify that the DML statements were batched together by PgAdapter.
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(1, requests.size());
assertEquals(INSERT_STATEMENT.getSql(), requests.get(0).getSql());
List<ExecuteBatchDmlRequest> batchDmlRequests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
assertEquals(1, requests.size());
ExecuteBatchDmlRequest request = batchDmlRequests.get(0);
assertEquals(2, request.getStatementsCount());
assertEquals(UPDATE_STATEMENT.getSql(), request.getStatements(0).getSql());
assertEquals(INVALID_DML.getSql(), request.getStatements(1).getSql());
// The aborted transaction should be rolled back by PGAdapter.
List<RollbackRequest> rollbackRequests = mockSpanner.getRequestsOfType(RollbackRequest.class);
assertEquals(1, rollbackRequests.size());
// BEGIN statement converts the implicit transaction to an explicit transaction, but is
// otherwise a no-op. We should therefore not receive any commits.
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(0, commitRequests.size());
}
use of com.google.firestore.v1beta1.RollbackRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testAbortedTransactionRollbackInAutocommit.
@Test
public void testAbortedTransactionRollbackInAutocommit() throws SQLException {
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (java.sql.Statement statement = connection.createStatement()) {
assertFalse(statement.execute("BEGIN"));
assertEquals(0, statement.getUpdateCount());
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
assertFalse(statement.execute(UPDATE_STATEMENT.getSql()));
assertEquals(2, statement.getUpdateCount());
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
SQLException exception = assertThrows(SQLException.class, () -> statement.execute(INVALID_DML.getSql()));
assertThat(exception.getMessage(), containsString("INVALID_ARGUMENT: Statement is invalid."));
assertFalse(statement.execute("COMMIT"));
assertEquals(0, statement.getUpdateCount());
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(2, requests.size());
assertEquals(UPDATE_STATEMENT.getSql(), requests.get(0).getSql());
assertTrue(requests.get(0).getTransaction().hasBegin());
assertEquals(INVALID_DML.getSql(), requests.get(1).getSql());
assertTrue(requests.get(1).getTransaction().hasId());
List<RollbackRequest> rollbackRequests = mockSpanner.getRequestsOfType(RollbackRequest.class);
assertEquals(1, rollbackRequests.size());
}
Aggregations