Search in sources :

Example 21 with User

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

the class FirestoreRepositoryIntegrationTests method inFilterQueryTest.

@Test
void inFilterQueryTest() {
    User u1 = new User("Cloud", 22);
    User u2 = new User("Squall", 17);
    Flux<User> users = Flux.fromArray(new User[] { u1, u2 });
    this.userRepository.saveAll(users).blockLast();
    List<String> pagedUsers = this.userRepository.findByAgeIn(Arrays.asList(22, 23, 24)).map(User::getName).collectList().block();
    assertThat(pagedUsers).containsExactly("Cloud");
    pagedUsers = this.userRepository.findByAgeIn(Arrays.asList(17, 22)).map(User::getName).collectList().block();
    assertThat(pagedUsers).containsExactly("Cloud", "Squall");
    pagedUsers = this.userRepository.findByAgeIn(Arrays.asList(18, 23)).map(User::getName).collectList().block();
    assertThat(pagedUsers).isEmpty();
    pagedUsers = this.userRepository.findByAgeNotIn(Arrays.asList(17, 22, 33)).map(User::getName).collectList().block();
    assertThat(pagedUsers).isEmpty();
    pagedUsers = this.userRepository.findByAgeNotIn(Arrays.asList(10, 20, 30)).map(User::getName).collectList().block();
    assertThat(pagedUsers).containsExactlyInAnyOrder("Cloud", "Squall");
    pagedUsers = this.userRepository.findByAgeNotIn(Arrays.asList(17, 33)).map(User::getName).collectList().block();
    assertThat(pagedUsers).containsExactly("Cloud");
}
Also used : User(com.google.cloud.spring.data.firestore.entities.User) Test(org.junit.jupiter.api.Test)

Example 22 with User

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

the class FirestoreRepositoryIntegrationTests method transactionalOperatorTest.

// end::repository_built_in[]
@Test
void transactionalOperatorTest() {
    // tag::repository_transactional_operator[]
    DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
    transactionDefinition.setReadOnly(false);
    TransactionalOperator operator = TransactionalOperator.create(this.txManager, transactionDefinition);
    // end::repository_transactional_operator[]
    // tag::repository_operations_in_a_transaction[]
    User alice = new User("Alice", 29);
    User bob = new User("Bob", 60);
    this.userRepository.save(alice).then(this.userRepository.save(bob)).as(operator::transactional).block();
    this.userRepository.findAll().flatMap(a -> {
        a.setAge(a.getAge() - 1);
        return this.userRepository.save(a);
    }).as(operator::transactional).collectList().block();
    assertThat(this.userRepository.findAll().map(User::getAge).collectList().block()).containsExactlyInAnyOrder(28, 59);
// end::repository_operations_in_a_transaction[]
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Order(org.springframework.data.domain.Sort.Order) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Address(com.google.cloud.spring.data.firestore.entities.User.Address) Autowired(org.springframework.beans.factory.annotation.Autowired) AopTestUtils(org.springframework.test.util.AopTestUtils) User(com.google.cloud.spring.data.firestore.entities.User) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) EnabledIfSystemProperty(org.junit.jupiter.api.condition.EnabledIfSystemProperty) Sort(org.springframework.data.domain.Sort) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) PageRequest(org.springframework.data.domain.PageRequest) Mockito.times(org.mockito.Mockito.times) Mono(reactor.core.publisher.Mono) UserRepository(com.google.cloud.spring.data.firestore.entities.UserRepository) ReactiveFirestoreTransactionManager(com.google.cloud.spring.data.firestore.transaction.ReactiveFirestoreTransactionManager) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) SimpleFirestoreReactiveRepository(com.google.cloud.spring.data.firestore.SimpleFirestoreReactiveRepository) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Mockito.reset(org.mockito.Mockito.reset) Collections(java.util.Collections) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) User(com.google.cloud.spring.data.firestore.entities.User) Test(org.junit.jupiter.api.Test)

Example 23 with User

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

the class FirestoreRepositoryIntegrationTests method countTest.

@Test
void countTest() {
    Flux<User> users = Flux.fromStream(IntStream.range(1, 10).boxed()).map(n -> new User("blah-person" + n, n));
    this.userRepository.saveAll(users).blockLast();
    long count = this.userRepository.countByAgeIsGreaterThan(5).block();
    assertThat(count).isEqualTo(4);
}
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