Search in sources :

Example 1 with PhoneNumber

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

the class FirestoreIntegrationTests method writeReadDeleteTest.

@Test
public 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(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()).isEqualTo(0);
    this.firestoreTemplate.save(alice).block();
    this.firestoreTemplate.save(bob).block();
    assertThat(this.firestoreTemplate.deleteAll(User.class).block()).isEqualTo(2);
    assertThat(usersBeforeDelete).containsExactlyInAnyOrder(alice, bob);
    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(org.springframework.cloud.gcp.data.firestore.entities.User) FirestoreReactiveOperations(org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations) PhoneNumber(org.springframework.cloud.gcp.data.firestore.entities.PhoneNumber) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 FirestoreReactiveOperations (org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations)1 PhoneNumber (org.springframework.cloud.gcp.data.firestore.entities.PhoneNumber)1 User (org.springframework.cloud.gcp.data.firestore.entities.User)1