Search in sources :

Example 46 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class DdlTransactionModeAutocommitImplicitTest method testErrorHandlingOfDdl.

@Test
public void testErrorHandlingOfDdl() throws SQLException {
    addDdlExceptionToSpannerAdmin();
    String sql = String.format("%s; %s; %s", INSERT_STATEMENT, INVALID_DDL, UPDATE_STATEMENT);
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (Statement statement = connection.createStatement()) {
            SQLException exception = assertThrows(SQLException.class, () -> statement.execute(sql));
            assertTrue(exception.getMessage(), exception.getMessage().contains("INVALID_ARGUMENT: Statement is invalid."));
        }
    }
    // Verify that the execution is stopped after the first error.
    List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
    assertEquals(1, requests.size());
    assertEquals(INSERT_STATEMENT.getSql(), requests.get(0).getSql());
    // The first insert statement is committed when the implicit transaction encounters a DDL
    // statement. This deviates from PostgreSQL, as PostgreSQL would have included the DDL statement
    // in the implicit transaction, and then rolled back the transaction when the DDL statement
    // failed.
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(1, commitRequests.size());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) SQLException(java.sql.SQLException) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 47 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project spring-cloud-gcp by GoogleCloudPlatform.

the class ReactiveFirestoreTransactionManagerTest method writeTransaction.

@Test
void writeTransaction() {
    FirestoreTemplate template = getFirestoreTemplate();
    ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent, this.classMapper);
    TransactionalOperator operator = TransactionalOperator.create(txManager);
    Timestamp commitTime = Timestamp.newBuilder().setSeconds(3456).build();
    doAnswer(invocation -> {
        CommitRequest commitRequest = invocation.getArgument(0);
        StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
        assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        assertThat(commitRequest.getWritesList().get(0).getUpdate().getName()).isEqualTo(this.parent + "/testEntities/" + "e2");
        assertThat(commitRequest.getWritesList().get(1).getUpdate().getName()).isEqualTo(this.parent + "/testEntities/" + "e3");
        assertThat(commitRequest.getWritesList().get(2).getDelete()).isEqualTo(this.parent + "/testEntities/" + "e3");
        streamObserver.onNext(CommitResponse.newBuilder().setCommitTime(commitTime).build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).commit(any(), any());
    TestEntity e2 = new TestEntity("e2", 100L);
    TestEntity e3 = new TestEntity("e3", 100L);
    template.findById(Mono.just("e1"), FirestoreTemplateTests.TestEntity.class).flatMap(testEntity -> template.save(e2)).flatMap(testEntity -> template.save(e3)).flatMap(testEntity -> template.delete(Mono.just(testEntity))).then().as(operator::transactional).as(StepVerifier::create).verifyComplete();
    assertThat(e2.getUpdateTimestamp().toProto()).isEqualTo(commitTime);
    assertThat(e3.getUpdateTimestamp().toProto()).isEqualTo(commitTime);
    verify(this.firestoreStub).beginTransaction(any(), any());
    verify(this.firestoreStub).commit(any(), any());
    GetDocumentRequest request1 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e1").setTransaction(ByteString.copyFromUtf8("transaction1")).build();
    verify(this.firestoreStub, times(1)).getDocument(eq(request1), any());
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) CommitRequest(com.google.firestore.v1.CommitRequest) TestEntity(com.google.cloud.spring.data.firestore.FirestoreTemplateTests.TestEntity) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CommitResponse(com.google.firestore.v1.CommitResponse) StepVerifier(reactor.test.StepVerifier) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestEntity(com.google.cloud.spring.data.firestore.FirestoreTemplateTests.TestEntity) FirestoreTemplate(com.google.cloud.spring.data.firestore.FirestoreTemplate) Timestamp(com.google.protobuf.Timestamp) FirestoreTemplateTests(com.google.cloud.spring.data.firestore.FirestoreTemplateTests) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Empty(com.google.protobuf.Empty) StreamObserver(io.grpc.stub.StreamObserver) CommitRequest(com.google.firestore.v1.CommitRequest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) RollbackRequest(com.google.firestore.v1.RollbackRequest) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) FirestoreGrpc(com.google.firestore.v1.FirestoreGrpc) FirestoreDataException(com.google.cloud.spring.data.firestore.FirestoreDataException) Document(com.google.firestore.v1.Document) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Mono(reactor.core.publisher.Mono) Mockito.verify(org.mockito.Mockito.verify) FirestoreDefaultClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreDefaultClassMapper) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) FirestoreMappingContext(com.google.cloud.spring.data.firestore.mapping.FirestoreMappingContext) Mockito.mock(org.mockito.Mockito.mock) CommitResponse(com.google.firestore.v1.CommitResponse) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Timestamp(com.google.protobuf.Timestamp) FirestoreTemplate(com.google.cloud.spring.data.firestore.FirestoreTemplate) Test(org.junit.jupiter.api.Test)

Example 48 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest 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 49 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest 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());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RollbackRequest(com.google.spanner.v1.RollbackRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Statement(java.sql.Statement) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Example 50 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class JdbcSimpleModeMockServerTest method testBeginInExplicitTransaction.

@Test
public void testBeginInExplicitTransaction() throws SQLException {
    String sql = "BEGIN TRANSACTION; INSERT INTO FOO VALUES (1); UPDATE FOO SET BAR=1 WHERE BAZ=2; COMMIT;";
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (java.sql.Statement statement = connection.createStatement()) {
            // Start an explicit transaction before executing batch
            assertFalse(statement.execute("BEGIN; SELECT 1"));
            assertEquals(0, statement.getUpdateCount());
            assertTrue(statement.getMoreResults());
            try (ResultSet resultSet = statement.getResultSet()) {
                assertTrue(resultSet.next());
                assertEquals(1L, resultSet.getLong(1));
                assertFalse(resultSet.next());
            }
            assertFalse(statement.getMoreResults());
            assertEquals(-1, statement.getUpdateCount());
            // The BEGIN statement should be no-op and not cause exception.
            assertFalse(statement.execute(sql));
            // The BEGIN statement should not return anything.
            assertEquals(0, statement.getUpdateCount());
            // INSERT
            assertFalse(statement.getMoreResults());
            assertEquals(1, statement.getUpdateCount());
            // UPDATE
            assertFalse(statement.getMoreResults());
            assertEquals(2, statement.getUpdateCount());
            // COMMIT
            assertFalse(statement.getMoreResults());
            assertEquals(0, statement.getUpdateCount());
            assertFalse(statement.getMoreResults());
            assertEquals(-1, statement.getUpdateCount());
        }
    }
    List<ExecuteBatchDmlRequest> batchDmlRequests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
    assertEquals(1, batchDmlRequests.size());
    ExecuteBatchDmlRequest batchDmlRequest = batchDmlRequests.get(0);
    // Verify that the BEGIN statement is no-op
    assertFalse(batchDmlRequest.getTransaction().hasBegin());
    assertTrue(batchDmlRequest.getTransaction().hasId());
    assertEquals(INSERT_STATEMENT.getSql(), batchDmlRequest.getStatements(0).getSql());
    assertEquals(UPDATE_STATEMENT.getSql(), batchDmlRequest.getStatements(1).getSql());
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(1, commitRequests.size());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) Connection(java.sql.Connection) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)85 CommitRequest (com.google.spanner.v1.CommitRequest)47 CommitRequest (com.google.firestore.v1.CommitRequest)40 CommitResponse (com.google.firestore.v1.CommitResponse)40 ArrayList (java.util.ArrayList)26 Connection (java.sql.Connection)23 ByteString (com.google.protobuf.ByteString)14 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)12 SQLException (java.sql.SQLException)12 Statement (java.sql.Statement)12 CopyManager (org.postgresql.copy.CopyManager)11 BaseConnection (org.postgresql.core.BaseConnection)11 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)10 ArrayValue (com.google.firestore.v1.ArrayValue)9 MapValue (com.google.firestore.v1.MapValue)9 Value (com.google.firestore.v1.Value)9 Write (com.google.firestore.v1.Write)9 StringReader (java.io.StringReader)9 HashMap (java.util.HashMap)9 AbstractMessage (com.google.protobuf.AbstractMessage)8