Search in sources :

Example 16 with User

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

the class FirestoreRepositoryIntegrationTests method writeReadDeleteTest.

@Test
public // tag::repository_built_in[]
void writeReadDeleteTest() {
    List<User.Address> addresses = Arrays.asList(new User.Address("123 Alice st", "US"), new User.Address("1 Alice ave", "US"));
    User.Address homeAddress = new User.Address("10 Alice blvd", "UK");
    User alice = new User("Alice", 29, null, addresses, homeAddress);
    User bob = new User("Bob", 60);
    this.userRepository.save(alice).block();
    this.userRepository.save(bob).block();
    assertThat(this.userRepository.count().block()).isEqualTo(2);
    assertThat(this.userRepository.findAll().map(User::getName).collectList().block()).containsExactlyInAnyOrder("Alice", "Bob");
    User aliceLoaded = this.userRepository.findById("Alice").block();
    assertThat(aliceLoaded.getAddresses()).isEqualTo(addresses);
    assertThat(aliceLoaded.getHomeAddress()).isEqualTo(homeAddress);
}
Also used : Address(org.springframework.cloud.gcp.data.firestore.entities.User.Address) User(org.springframework.cloud.gcp.data.firestore.entities.User) Address(org.springframework.cloud.gcp.data.firestore.entities.User.Address) Test(org.junit.Test)

Example 17 with User

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

the class FirestoreRepositoryIntegrationTests method sortQueryTest.

@Test
public void sortQueryTest() {
    Flux<User> users = Flux.fromStream(IntStream.range(1, 11).boxed()).map(n -> new User("blah-person" + n, n));
    this.userRepository.saveAll(users).blockLast();
    List<String> pagedUsers = this.userRepository.findByAgeGreaterThan(7, Sort.by(Order.asc("age"))).map(User::getName).collectList().block();
    assertThat(pagedUsers).containsExactlyInAnyOrder("blah-person8", "blah-person9", "blah-person10");
}
Also used : User(org.springframework.cloud.gcp.data.firestore.entities.User) Test(org.junit.Test)

Example 18 with User

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

the class FirestoreRepositoryIntegrationTests method transactionalOperatorTest.

// end::repository_built_in[]
@Test
public 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) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) Order(org.springframework.data.domain.Sort.Order) Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) UserRepository(org.springframework.cloud.gcp.data.firestore.entities.UserRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) Sort(org.springframework.data.domain.Sort) 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) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) PageRequest(org.springframework.data.domain.PageRequest) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) Flux(reactor.core.publisher.Flux) List(java.util.List) Address(org.springframework.cloud.gcp.data.firestore.entities.User.Address) ReactiveFirestoreTransactionManager(org.springframework.cloud.gcp.data.firestore.transaction.ReactiveFirestoreTransactionManager) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Mockito.reset(org.mockito.Mockito.reset) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) 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