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[]
}
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);
}
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);
}
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();
}
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");
}
Aggregations