Search in sources :

Example 11 with User

use of com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.

the class FirestoreIntegrationTests method optimisticLockingTest.

@Test
void optimisticLockingTest() {
    User bob = new User("Bob", 60, null);
    this.firestoreTemplate.saveAll(Flux.just(bob)).collectList().block();
    Timestamp bobUpdateTime = bob.getUpdateTime();
    assertThat(bobUpdateTime).isNotNull();
    User bob2 = new User("Bob", 60, null);
    Flux<User> justBob2 = Flux.just(bob2);
    StepVerifier.create(this.firestoreTemplate.saveAll(justBob2)).expectErrorSatisfies(t -> {
        assertThat(t).isInstanceOf(StatusRuntimeException.class);
        assertThat(t).hasMessageContaining("ALREADY_EXISTS");
    }).verify();
    bob.setAge(15);
    this.firestoreTemplate.saveAll(Flux.just(bob)).collectList().block();
    assertThat(bob.getUpdateTime()).isGreaterThan(bobUpdateTime);
    List<User> users = this.firestoreTemplate.findAll(User.class).collectList().block();
    assertThat(users).containsExactly(bob);
    User bob3 = users.get(0);
    bob3.setAge(20);
    this.firestoreTemplate.saveAll(Flux.just(bob3)).collectList().block();
    Flux<User> justBob = Flux.just(bob);
    StepVerifier.create(this.firestoreTemplate.saveAll(justBob)).expectErrorSatisfies(t -> {
        assertThat(t).isInstanceOf(StatusRuntimeException.class);
        assertThat(t).hasMessageContaining("does not match the required base version");
    }).verify();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Timestamp(com.google.cloud.Timestamp) FirestoreTemplate(com.google.cloud.spring.data.firestore.FirestoreTemplate) User(com.google.cloud.spring.data.firestore.entities.User) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) EnabledIfSystemProperty(org.junit.jupiter.api.condition.EnabledIfSystemProperty) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) FirestoreReactiveOperations(com.google.cloud.spring.data.firestore.FirestoreReactiveOperations) FirestoreDataException(com.google.cloud.spring.data.firestore.FirestoreDataException) SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Mockito.times(org.mockito.Mockito.times) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) ReactiveFirestoreTransactionManager(com.google.cloud.spring.data.firestore.transaction.ReactiveFirestoreTransactionManager) Mockito.verify(org.mockito.Mockito.verify) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) Level(ch.qos.logback.classic.Level) PhoneNumber(com.google.cloud.spring.data.firestore.entities.PhoneNumber) List(java.util.List) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Mockito.reset(org.mockito.Mockito.reset) User(com.google.cloud.spring.data.firestore.entities.User) StatusRuntimeException(io.grpc.StatusRuntimeException) Timestamp(com.google.cloud.Timestamp) Test(org.junit.jupiter.api.Test)

Example 12 with User

use of com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.

the class FirestoreIntegrationTests method saveAllBulkTest.

@Test
void saveAllBulkTest() {
    int numEntities = 1000;
    Flux<User> users = Flux.create(sink -> {
        for (int i = 0; i < numEntities; i++) {
            sink.next(new User(UUID.randomUUID().toString(), i));
        }
        sink.complete();
    });
    assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).isEmpty();
    this.firestoreTemplate.saveAll(users).blockLast();
    assertThat(this.firestoreTemplate.findAll(User.class).count().block()).isEqualTo(numEntities);
}
Also used : User(com.google.cloud.spring.data.firestore.entities.User) Test(org.junit.jupiter.api.Test)

Example 13 with User

use of com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.

the class FirestoreIntegrationTests method optimisticLockingTransactionTest.

@Test
void optimisticLockingTransactionTest() {
    User bob = new User("Bob", 60, null);
    TransactionalOperator operator = TransactionalOperator.create(txManager);
    this.firestoreTemplate.saveAll(Flux.just(bob)).collectList().block();
    Timestamp bobUpdateTime = bob.getUpdateTime();
    assertThat(bobUpdateTime).isNotNull();
    User bob2 = new User("Bob", 60, null);
    this.firestoreTemplate.saveAll(Flux.just(bob2)).collectList().as(operator::transactional).as(StepVerifier::create).expectError().verify();
    bob.setAge(15);
    this.firestoreTemplate.saveAll(Flux.just(bob)).as(operator::transactional).collectList().block();
    assertThat(bob.getUpdateTime()).isGreaterThan(bobUpdateTime);
    List<User> users = this.firestoreTemplate.findAll(User.class).collectList().block();
    assertThat(users).containsExactly(bob);
    User bob3 = users.get(0);
    bob3.setAge(20);
    this.firestoreTemplate.saveAll(Flux.just(bob3)).as(operator::transactional).collectList().block();
    this.firestoreTemplate.saveAll(Flux.just(bob)).as(operator::transactional).collectList().as(operator::transactional).as(StepVerifier::create).expectError().verify();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) User(com.google.cloud.spring.data.firestore.entities.User) StepVerifier(reactor.test.StepVerifier) Timestamp(com.google.cloud.Timestamp) Test(org.junit.jupiter.api.Test)

Example 14 with User

use of com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.

the class FirestoreIntegrationTests method saveAllTest.

@Test
void saveAllTest() {
    User u1 = new User("Cloud", 22);
    User u2 = new User("Squall", 17);
    Flux<User> users = Flux.fromArray(new User[] { u1, u2 });
    assertThat(this.firestoreTemplate.count(User.class).block()).isZero();
    this.firestoreTemplate.saveAll(users).blockLast();
    assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(2);
    assertThat(this.firestoreTemplate.deleteAll(User.class).block()).isEqualTo(2);
}
Also used : User(com.google.cloud.spring.data.firestore.entities.User) Test(org.junit.jupiter.api.Test)

Example 15 with User

use of com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.

the class FirestoreIntegrationTests method generateIdTest.

@Test
void generateIdTest() {
    User user = new User(null, 29);
    User bob = new User("Bob", 60);
    List<User> savedUsers = this.firestoreTemplate.saveAll(Flux.just(user, bob)).collectList().block();
    assertThat(savedUsers).extracting("name").doesNotContainNull();
    List<User> users = this.firestoreTemplate.findAll(User.class).collectList().block();
    assertThat(users).containsExactlyInAnyOrder(savedUsers.toArray(new User[0]));
}
Also used : User(com.google.cloud.spring.data.firestore.entities.User) Test(org.junit.jupiter.api.Test)

Aggregations

User (com.google.cloud.spring.data.firestore.entities.User)23 Test (org.junit.jupiter.api.Test)23 TransactionalOperator (org.springframework.transaction.reactive.TransactionalOperator)5 StepVerifier (reactor.test.StepVerifier)5 Address (com.google.cloud.spring.data.firestore.entities.User.Address)4 ReactiveFirestoreTransactionManager (com.google.cloud.spring.data.firestore.transaction.ReactiveFirestoreTransactionManager)4 List (java.util.List)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 EnabledIfSystemProperty (org.junit.jupiter.api.condition.EnabledIfSystemProperty)4 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 Mockito.reset (org.mockito.Mockito.reset)4 Mockito.times (org.mockito.Mockito.times)4 Mockito.verify (org.mockito.Mockito.verify)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 ContextConfiguration (org.springframework.test.context.ContextConfiguration)4 SpringExtension (org.springframework.test.context.junit.jupiter.SpringExtension)4 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)4 Flux (reactor.core.publisher.Flux)4