use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.
the class FirestoreIntegrationTests method transactionTest.
@Test
public void transactionTest() {
User alice = new User("Alice", 29);
User bob = new User("Bob", 60);
User user = new User(null, 40);
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
transactionDefinition.setReadOnly(false);
TransactionalOperator operator = TransactionalOperator.create(this.txManager, transactionDefinition);
reset(this.txManager);
this.firestoreTemplate.save(alice).then(this.firestoreTemplate.save(bob)).then(this.firestoreTemplate.save(user)).as(operator::transactional).block();
assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).containsExactlyInAnyOrder(bob, alice, user);
verify(this.txManager, times(1)).commit(any());
verify(this.txManager, times(0)).rollback(any());
verify(this.txManager, times(1)).getReactiveTransaction(any());
reset(this.txManager);
// test rollback
this.firestoreTemplate.saveAll(Mono.defer(() -> {
throw new FirestoreDataException("BOOM!");
})).then(this.firestoreTemplate.deleteAll(User.class)).as(operator::transactional).onErrorResume(throwable -> Mono.empty()).block();
verify(this.txManager, times(0)).commit(any());
verify(this.txManager, times(1)).rollback(any());
verify(this.txManager, times(1)).getReactiveTransaction(any());
assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(3);
this.firestoreTemplate.findAll(User.class).flatMap(a -> {
a.setAge(a.getAge() - 1);
return this.firestoreTemplate.save(a);
}).as(operator::transactional).collectList().block();
List<User> users = this.firestoreTemplate.findAll(User.class).collectList().block();
assertThat(users).extracting("age").containsExactlyInAnyOrder(28, 59, 39);
assertThat(users).extracting("name").doesNotContainNull();
this.firestoreTemplate.deleteAll(User.class).as(operator::transactional).block();
assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).isEmpty();
}
use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.
the class FirestoreIntegrationTests method saveAllBulkTest.
@Test
public void saveAllBulkTest() {
int numEntities = 1000;
Flux<User> users = Flux.create(sink -> {
for (int i = 0; i < numEntities; i++) {
sink.next(new User(UUID.randomUUID().toString(), i));
}
sink.complete();
});
assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).isEmpty();
this.firestoreTemplate.saveAll(users).blockLast();
assertThat(this.firestoreTemplate.findAll(User.class).count().block()).isEqualTo(numEntities);
}
use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.
the class FirestoreIntegrationTests method generateIdTest.
@Test
public void generateIdTest() {
User user = new User(null, 29);
User bob = new User("Bob", 60);
List<User> savedUsers = this.firestoreTemplate.saveAll(Flux.just(user, bob)).collectList().block();
assertThat(savedUsers).extracting("name").doesNotContainNull();
List<User> users = this.firestoreTemplate.findAll(User.class).collectList().block();
assertThat(users).containsExactlyInAnyOrder(savedUsers.toArray(new User[0]));
}
use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.
the class FirestoreIntegrationTests method saveTest.
@Test
public void saveTest() {
assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(0);
User u1 = new User("Cloud", 22);
this.firestoreTemplate.save(u1).block();
assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).containsExactly(u1);
}
use of org.springframework.cloud.gcp.data.firestore.entities.User in project spring-cloud-gcp by spring-cloud.
the class FirestoreRepositoryIntegrationTests method declarativeTransactionCommitTest.
@Test
public 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);
}
Aggregations