Search in sources :

Example 36 with CommitRequest

use of com.google.firestore.v1beta1.CommitRequest in project google-cloud-java by GoogleCloudPlatform.

the class SessionImplTest method writeAtLeastOnce.

@Test
public void writeAtLeastOnce() throws ParseException {
    String timestampString = "2015-10-01T10:54:20.021Z";
    ArgumentCaptor<CommitRequest> commit = ArgumentCaptor.forClass(CommitRequest.class);
    CommitResponse response = CommitResponse.newBuilder().setCommitTimestamp(Timestamps.parse(timestampString)).build();
    Mockito.when(rpc.commit(commit.capture(), Mockito.eq(options))).thenReturn(response);
    Timestamp timestamp = session.writeAtLeastOnce(Arrays.asList(Mutation.newInsertBuilder("T").set("C").to("x").build()));
    assertThat(timestamp.getSeconds()).isEqualTo(utcTimeSeconds(2015, Calendar.OCTOBER, 1, 10, 54, 20));
    assertThat(timestamp.getNanos()).isEqualTo(TimeUnit.MILLISECONDS.toNanos(21));
    CommitRequest request = commit.getValue();
    assertThat(request.getSingleUseTransaction()).isNotNull();
    assertThat(request.getSingleUseTransaction().getReadWrite()).isNotNull();
    com.google.spanner.v1.Mutation mutation = com.google.spanner.v1.Mutation.newBuilder().setInsert(Write.newBuilder().setTable("T").addColumns("C").addValues(ListValue.newBuilder().addValues(com.google.protobuf.Value.newBuilder().setStringValue("x")))).build();
    assertThat(request.getMutationsList()).containsExactly(mutation);
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) CommitResponse(com.google.spanner.v1.CommitResponse) ByteString(com.google.protobuf.ByteString) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test)

Example 37 with CommitRequest

use of com.google.firestore.v1beta1.CommitRequest 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 38 with CommitRequest

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

the class ReactiveFirestoreTransactionManagerTest method writeTransaction.

@Test
public void writeTransaction() {
    FirestoreTemplate template = getFirestoreTemplate();
    ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent);
    TransactionalOperator operator = TransactionalOperator.create(txManager);
    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().build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).commit(any(), any());
    template.findById(Mono.just("e1"), FirestoreTemplateTests.TestEntity.class).flatMap(testEntity -> template.save(new FirestoreTemplateTests.TestEntity("e2", 100L))).flatMap(testEntity -> template.save(new FirestoreTemplateTests.TestEntity("e3", 100L))).flatMap(testEntity -> template.delete(Mono.just(testEntity))).then().as(operator::transactional).as(StepVerifier::create).verifyComplete();
    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) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CommitResponse(com.google.firestore.v1.CommitResponse) StepVerifier(reactor.test.StepVerifier) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Empty(com.google.protobuf.Empty) StreamObserver(io.grpc.stub.StreamObserver) CommitRequest(com.google.firestore.v1.CommitRequest) Duration(java.time.Duration) Mockito.doAnswer(org.mockito.Mockito.doAnswer) RollbackRequest(com.google.firestore.v1.RollbackRequest) FirestoreDefaultClassMapper(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreDefaultClassMapper) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) FirestoreGrpc(com.google.firestore.v1.FirestoreGrpc) FirestoreTemplateTests(org.springframework.cloud.gcp.data.firestore.FirestoreTemplateTests) Document(com.google.firestore.v1.Document) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.verify(org.mockito.Mockito.verify) ByteString(com.google.protobuf.ByteString) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) FirestoreMappingContext(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreMappingContext) Mockito.mock(org.mockito.Mockito.mock) CommitResponse(com.google.firestore.v1.CommitResponse) FirestoreTemplateTests(org.springframework.cloud.gcp.data.firestore.FirestoreTemplateTests) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) Test(org.junit.Test)

Example 39 with CommitRequest

use of com.google.firestore.v1beta1.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class CopyInMockServerTest method testCopyInWithInvalidRow.

@Test
public void testCopyInWithInvalidRow() throws SQLException {
    setupCopyInformationSchemaResults();
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
        // This row does not contain all the necessary columns.
        SQLException exception = assertThrows(SQLException.class, () -> copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\n")));
        assertTrue(exception.getMessage().contains("Row length mismatched. Expected 3 columns, but only found 1"));
    } finally {
        assertTrue(new File("output.txt").delete());
    }
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertTrue(commitRequests.isEmpty());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) StringReader(java.io.StringReader) CopyManager(org.postgresql.copy.CopyManager) File(java.io.File) BaseConnection(org.postgresql.core.BaseConnection) Test(org.junit.Test)

Example 40 with CommitRequest

use of com.google.firestore.v1beta1.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class CopyInMockServerTest method testCopyInExceedsCommitSizeLimit_FailsInAtomicMode.

@Test
public void testCopyInExceedsCommitSizeLimit_FailsInAtomicMode() throws SQLException {
    setupCopyInformationSchemaResults();
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        System.setProperty("copy_in_commit_limit", "10");
        CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
        SQLException exception = assertThrows(SQLException.class, () -> copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\t5\t5\n6\t6\t6\n7\t7\t7\n")));
        assertTrue(exception.getMessage().contains("Commit size: 20 has exceeded the limit: 10"));
    } finally {
        System.getProperties().remove("copy_in_commit_limit");
    }
    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) BaseConnection(org.postgresql.core.BaseConnection) StringReader(java.io.StringReader) CopyManager(org.postgresql.copy.CopyManager) BaseConnection(org.postgresql.core.BaseConnection) 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