Search in sources :

Example 1 with User

use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.

the class FirestoreIntegrationTests method transactionTest.

@Test
public void transactionTest() {
    User alice = new User("Alice", 29);
    User bob = new User("Bob", 60);
    User user = new User(null, 40);
    DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
    transactionDefinition.setReadOnly(false);
    TransactionalOperator operator = TransactionalOperator.create(this.txManager, transactionDefinition);
    reset(this.txManager);
    this.firestoreTemplate.save(alice).then(this.firestoreTemplate.save(bob)).then(this.firestoreTemplate.save(user)).as(operator::transactional).block();
    assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).containsExactlyInAnyOrder(bob, alice, user);
    verify(this.txManager, times(1)).commit(any());
    verify(this.txManager, times(0)).rollback(any());
    verify(this.txManager, times(1)).getReactiveTransaction(any());
    reset(this.txManager);
    // test rollback
    this.firestoreTemplate.saveAll(Mono.defer(() -> {
        throw new FirestoreDataException("BOOM!");
    })).then(this.firestoreTemplate.deleteAll(User.class)).as(operator::transactional).onErrorResume(throwable -> Mono.empty()).block();
    verify(this.txManager, times(0)).commit(any());
    verify(this.txManager, times(1)).rollback(any());
    verify(this.txManager, times(1)).getReactiveTransaction(any());
    assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(3);
    this.firestoreTemplate.findAll(User.class).flatMap(a -> {
        a.setAge(a.getAge() - 1);
        return this.firestoreTemplate.save(a);
    }).as(operator::transactional).collectList().block();
    List<User> users = this.firestoreTemplate.findAll(User.class).collectList().block();
    assertThat(users).extracting("age").containsExactlyInAnyOrder(28, 59, 39);
    assertThat(users).extracting("name").doesNotContainNull();
    this.firestoreTemplate.deleteAll(User.class).as(operator::transactional).block();
    assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).isEmpty();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) PhoneNumber(org.springframework.cloud.gcp.data.firestore.entities.PhoneNumber) SpringRunner(org.springframework.test.context.junit4.SpringRunner) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Before(org.junit.Before) User(org.springframework.cloud.gcp.data.firestore.entities.User) FirestoreReactiveOperations(org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) UUID(java.util.UUID) Mockito.verify(org.mockito.Mockito.verify) Flux(reactor.core.publisher.Flux) List(java.util.List) Level(ch.qos.logback.classic.Level) ReactiveFirestoreTransactionManager(org.springframework.cloud.gcp.data.firestore.transaction.ReactiveFirestoreTransactionManager) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Matchers.is(org.hamcrest.Matchers.is) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) Mockito.reset(org.mockito.Mockito.reset) User(org.springframework.cloud.gcp.data.firestore.entities.User) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) Test(org.junit.Test)

Example 2 with User

use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.

the class FirestoreIntegrationTests method saveAllBulkTest.

@Test
public 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(org.springframework.cloud.gcp.data.firestore.entities.User) Test(org.junit.Test)

Example 3 with User

use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.

the class FirestoreIntegrationTests method generateIdTest.

@Test
public 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(org.springframework.cloud.gcp.data.firestore.entities.User) Test(org.junit.Test)

Example 4 with User

use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.

the class FirestoreIntegrationTests method saveTest.

@Test
public void saveTest() {
    assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(0);
    User u1 = new User("Cloud", 22);
    this.firestoreTemplate.save(u1).block();
    assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).containsExactly(u1);
}
Also used : User(org.springframework.cloud.gcp.data.firestore.entities.User) Test(org.junit.Test)

Example 5 with User

use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.

the class FirestoreRepositoryIntegrationTests method declarativeTransactionCommitTest.

@Test
public void declarativeTransactionCommitTest() {
    User alice = new User("Alice", 29);
    User bob = new User("Bob", 60);
    this.userRepository.save(alice).then(this.userRepository.save(bob)).block();
    this.userService.updateUsers().block();
    verify(this.transactionManager, times(1)).commit(any());
    verify(this.transactionManager, times(0)).rollback(any());
    verify(this.transactionManager, times(1)).getReactiveTransaction(any());
    assertThat(this.userRepository.findAll().map(User::getAge).collectList().block()).containsExactlyInAnyOrder(28, 59);
}
Also used : User(org.springframework.cloud.gcp.data.firestore.entities.User) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 User (org.springframework.cloud.gcp.data.firestore.entities.User)18 Address (org.springframework.cloud.gcp.data.firestore.entities.User.Address)4 List (java.util.List)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Matchers.is (org.hamcrest.Matchers.is)3 Assume.assumeThat (org.junit.Assume.assumeThat)3 Before (org.junit.Before)3 BeforeClass (org.junit.BeforeClass)3 RunWith (org.junit.runner.RunWith)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 Mockito.reset (org.mockito.Mockito.reset)3 Mockito.times (org.mockito.Mockito.times)3 Mockito.verify (org.mockito.Mockito.verify)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 ReactiveFirestoreTransactionManager (org.springframework.cloud.gcp.data.firestore.transaction.ReactiveFirestoreTransactionManager)3 PageRequest (org.springframework.data.domain.PageRequest)3 ContextConfiguration (org.springframework.test.context.ContextConfiguration)3 SpringRunner (org.springframework.test.context.junit4.SpringRunner)3 TransactionalOperator (org.springframework.transaction.reactive.TransactionalOperator)3