Search in sources :

Example 1 with User

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

the class FirestoreIntegrationTests method writeReadDeleteTest.

@Test
void writeReadDeleteTest() {
    User alice = new User("Alice", 29);
    User bob = new User("Bob", 60);
    this.firestoreTemplate.save(alice).block();
    this.firestoreTemplate.save(bob).block();
    assertThat(this.firestoreTemplate.findById(Mono.just("Saitama"), User.class).block()).isNull();
    assertThat(this.firestoreTemplate.findById(Mono.just("Bob"), User.class).block()).isEqualTo(bob);
    assertThat(this.firestoreTemplate.findAllById(Flux.just("Bob", "Saitama", "Alice"), User.class).collectList().block()).containsExactlyInAnyOrder(bob, alice);
    List<User> usersBeforeDelete = this.firestoreTemplate.findAll(User.class).collectList().block();
    assertThat(usersBeforeDelete).containsExactlyInAnyOrder(alice, bob);
    assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(2);
    this.firestoreTemplate.delete(Mono.just(bob)).block();
    assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(1);
    assertThat(this.firestoreTemplate.existsById(Mono.just("Alice"), User.class).block()).isEqualTo(Boolean.TRUE);
    assertThat(this.firestoreTemplate.existsById(Mono.just("Bob"), User.class).block()).isEqualTo(Boolean.FALSE);
    this.firestoreTemplate.deleteById(Mono.just("Alice"), User.class).block();
    assertThat(this.firestoreTemplate.count(User.class).block()).isZero();
    alice.setUpdateTime(null);
    this.firestoreTemplate.save(alice).block();
    bob.setUpdateTime(null);
    this.firestoreTemplate.save(bob).block();
    assertThat(this.firestoreTemplate.deleteAll(User.class).block()).isEqualTo(2);
    assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).isEmpty();
    // tag::subcollection[]
    FirestoreReactiveOperations bobTemplate = this.firestoreTemplate.withParent(new User("Bob", 60));
    PhoneNumber phoneNumber = new PhoneNumber("111-222-333");
    bobTemplate.save(phoneNumber).block();
    assertThat(bobTemplate.findAll(PhoneNumber.class).collectList().block()).containsExactly(phoneNumber);
    bobTemplate.deleteAll(PhoneNumber.class).block();
    assertThat(bobTemplate.findAll(PhoneNumber.class).collectList().block()).isEmpty();
// end::subcollection[]
}
Also used : User(com.google.cloud.spring.data.firestore.entities.User) FirestoreReactiveOperations(com.google.cloud.spring.data.firestore.FirestoreReactiveOperations) PhoneNumber(com.google.cloud.spring.data.firestore.entities.PhoneNumber) Test(org.junit.jupiter.api.Test)

Example 2 with User

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

the class FirestoreRepositoryIntegrationTests method partTreeRepositoryMethodTest.

@Test
// tag::repository_part_tree[]
void partTreeRepositoryMethodTest() {
    User u1 = new User("Cloud", 22, null, null, new Address("1 First st., NYC", "USA"));
    u1.favoriteDrink = "tea";
    User u2 = new User("Squall", 17, Arrays.asList("cat", "dog"), null, new Address("2 Second st., London", "UK"));
    u2.favoriteDrink = "wine";
    Flux<User> users = Flux.fromArray(new User[] { u1, u2 });
    this.userRepository.saveAll(users).blockLast();
    assertThat(this.userRepository.count().block()).isEqualTo(2);
    assertThat(this.userRepository.findBy(PageRequest.of(0, 10)).collectList().block()).containsExactly(u1, u2);
    assertThat(this.userRepository.findByAge(22).collectList().block()).containsExactly(u1);
    assertThat(this.userRepository.findByAgeNot(22).collectList().block()).containsExactly(u2);
    assertThat(this.userRepository.findByHomeAddressCountry("USA").collectList().block()).containsExactly(u1);
    assertThat(this.userRepository.findByFavoriteDrink("wine").collectList().block()).containsExactly(u2);
    assertThat(this.userRepository.findByAgeGreaterThanAndAgeLessThan(20, 30).collectList().block()).containsExactly(u1);
    assertThat(this.userRepository.findByAgeGreaterThan(10).collectList().block()).containsExactlyInAnyOrder(u1, u2);
    assertThat(this.userRepository.findByNameAndAge("Cloud", 22).collectList().block()).containsExactly(u1);
    assertThat(this.userRepository.findByNameAndPetsContains("Squall", Collections.singletonList("cat")).collectList().block()).containsExactly(u2);
}
Also used : User(com.google.cloud.spring.data.firestore.entities.User) Address(com.google.cloud.spring.data.firestore.entities.User.Address) Test(org.junit.jupiter.api.Test)

Example 3 with User

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

the class FirestoreRepositoryIntegrationTests method declarativeTransactionCommitTest.

@Test
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(com.google.cloud.spring.data.firestore.entities.User) Test(org.junit.jupiter.api.Test)

Example 4 with User

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

the class FirestoreRepositoryIntegrationTests method writeReadDeleteTest.

@Test
// 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);
    // cast to SimpleFirestoreReactiveRepository for method be reachable with Spring Boot 2.4
    SimpleFirestoreReactiveRepository repository = AopTestUtils.getTargetObject(this.userRepository);
    StepVerifier.create(repository.deleteAllById(Arrays.asList("Alice", "Bob")).then(this.userRepository.count())).expectNext(0L).verifyComplete();
}
Also used : Address(com.google.cloud.spring.data.firestore.entities.User.Address) User(com.google.cloud.spring.data.firestore.entities.User) Address(com.google.cloud.spring.data.firestore.entities.User.Address) SimpleFirestoreReactiveRepository(com.google.cloud.spring.data.firestore.SimpleFirestoreReactiveRepository) Test(org.junit.jupiter.api.Test)

Example 5 with User

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

the class FirestoreRepositoryIntegrationTests method sortQueryTest.

@Test
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(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