Search in sources :

Example 1 with PhoneNumber

use of com.google.cloud.spring.data.firestore.entities.PhoneNumber 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)

Aggregations

FirestoreReactiveOperations (com.google.cloud.spring.data.firestore.FirestoreReactiveOperations)1 PhoneNumber (com.google.cloud.spring.data.firestore.entities.PhoneNumber)1 User (com.google.cloud.spring.data.firestore.entities.User)1 Test (org.junit.jupiter.api.Test)1