use of com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.
the class FirestoreIntegrationTests method optimisticLockingTest.
@Test
void optimisticLockingTest() {
User bob = new User("Bob", 60, null);
this.firestoreTemplate.saveAll(Flux.just(bob)).collectList().block();
Timestamp bobUpdateTime = bob.getUpdateTime();
assertThat(bobUpdateTime).isNotNull();
User bob2 = new User("Bob", 60, null);
Flux<User> justBob2 = Flux.just(bob2);
StepVerifier.create(this.firestoreTemplate.saveAll(justBob2)).expectErrorSatisfies(t -> {
assertThat(t).isInstanceOf(StatusRuntimeException.class);
assertThat(t).hasMessageContaining("ALREADY_EXISTS");
}).verify();
bob.setAge(15);
this.firestoreTemplate.saveAll(Flux.just(bob)).collectList().block();
assertThat(bob.getUpdateTime()).isGreaterThan(bobUpdateTime);
List<User> users = this.firestoreTemplate.findAll(User.class).collectList().block();
assertThat(users).containsExactly(bob);
User bob3 = users.get(0);
bob3.setAge(20);
this.firestoreTemplate.saveAll(Flux.just(bob3)).collectList().block();
Flux<User> justBob = Flux.just(bob);
StepVerifier.create(this.firestoreTemplate.saveAll(justBob)).expectErrorSatisfies(t -> {
assertThat(t).isInstanceOf(StatusRuntimeException.class);
assertThat(t).hasMessageContaining("does not match the required base version");
}).verify();
}
use of com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.
the class FirestoreIntegrationTests method saveAllBulkTest.
@Test
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 com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.
the class FirestoreIntegrationTests method optimisticLockingTransactionTest.
@Test
void optimisticLockingTransactionTest() {
User bob = new User("Bob", 60, null);
TransactionalOperator operator = TransactionalOperator.create(txManager);
this.firestoreTemplate.saveAll(Flux.just(bob)).collectList().block();
Timestamp bobUpdateTime = bob.getUpdateTime();
assertThat(bobUpdateTime).isNotNull();
User bob2 = new User("Bob", 60, null);
this.firestoreTemplate.saveAll(Flux.just(bob2)).collectList().as(operator::transactional).as(StepVerifier::create).expectError().verify();
bob.setAge(15);
this.firestoreTemplate.saveAll(Flux.just(bob)).as(operator::transactional).collectList().block();
assertThat(bob.getUpdateTime()).isGreaterThan(bobUpdateTime);
List<User> users = this.firestoreTemplate.findAll(User.class).collectList().block();
assertThat(users).containsExactly(bob);
User bob3 = users.get(0);
bob3.setAge(20);
this.firestoreTemplate.saveAll(Flux.just(bob3)).as(operator::transactional).collectList().block();
this.firestoreTemplate.saveAll(Flux.just(bob)).as(operator::transactional).collectList().as(operator::transactional).as(StepVerifier::create).expectError().verify();
}
use of com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.
the class FirestoreIntegrationTests method saveAllTest.
@Test
void saveAllTest() {
User u1 = new User("Cloud", 22);
User u2 = new User("Squall", 17);
Flux<User> users = Flux.fromArray(new User[] { u1, u2 });
assertThat(this.firestoreTemplate.count(User.class).block()).isZero();
this.firestoreTemplate.saveAll(users).blockLast();
assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(2);
assertThat(this.firestoreTemplate.deleteAll(User.class).block()).isEqualTo(2);
}
use of com.google.cloud.spring.data.firestore.entities.User in project spring-cloud-gcp by GoogleCloudPlatform.
the class FirestoreIntegrationTests method generateIdTest.
@Test
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]));
}
Aggregations