Search in sources :

Example 1 with FirestoreDataException

use of org.springframework.cloud.gcp.data.firestore.FirestoreDataException 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();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) PhoneNumber(org.springframework.cloud.gcp.data.firestore.entities.PhoneNumber) SpringRunner(org.springframework.test.context.junit4.SpringRunner) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Before(org.junit.Before) User(org.springframework.cloud.gcp.data.firestore.entities.User) FirestoreReactiveOperations(org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) UUID(java.util.UUID) Mockito.verify(org.mockito.Mockito.verify) Flux(reactor.core.publisher.Flux) List(java.util.List) Level(ch.qos.logback.classic.Level) ReactiveFirestoreTransactionManager(org.springframework.cloud.gcp.data.firestore.transaction.ReactiveFirestoreTransactionManager) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Matchers.is(org.hamcrest.Matchers.is) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) Mockito.reset(org.mockito.Mockito.reset) User(org.springframework.cloud.gcp.data.firestore.entities.User) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) Test(org.junit.Test)

Example 2 with FirestoreDataException

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

the class PartTreeFirestoreQuery method createBuilderWithFilter.

private StructuredQuery.Builder createBuilderWithFilter(Object[] parameters) {
    StructuredQuery.Builder builder = StructuredQuery.newBuilder();
    Iterator it = Arrays.asList(parameters).iterator();
    StructuredQuery.CompositeFilter.Builder compositeFilter = StructuredQuery.CompositeFilter.newBuilder();
    compositeFilter.setOp(StructuredQuery.CompositeFilter.Operator.AND);
    this.tree.getParts().forEach(part -> {
        StructuredQuery.FieldReference fieldReference = StructuredQuery.FieldReference.newBuilder().setFieldPath(buildName(part)).build();
        StructuredQuery.Filter.Builder filter = StructuredQuery.Filter.newBuilder();
        if (part.getType() == Part.Type.IS_NULL) {
            filter.getUnaryFilterBuilder().setField(fieldReference).setOp(StructuredQuery.UnaryFilter.Operator.IS_NULL);
        } else {
            if (!it.hasNext()) {
                throw new FirestoreDataException("Too few parameters are provided for query method: " + getQueryMethod().getName());
            }
            Object value = it.next();
            filter.getFieldFilterBuilder().setField(fieldReference).setOp(getOperator(part, value)).setValue(this.classMapper.toFirestoreValue(value));
        }
        compositeFilter.addFilters(filter.build());
    });
    builder.setWhere(StructuredQuery.Filter.newBuilder().setCompositeFilter(compositeFilter.build()));
    return builder;
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) Iterator(java.util.Iterator) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference)

Example 3 with FirestoreDataException

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

the class PartTreeFirestoreQuery method validate.

private void validate() {
    List parts = this.tree.get().collect(Collectors.toList());
    if (parts.size() > 1 && parts.get(0) instanceof PartTree.OrPart) {
        throw new FirestoreDataException("Cloud Firestore doesn't support 'OR' (method name: " + this.getQueryMethod().getName() + ")");
    }
    List<String> unsupportedParts = this.tree.getParts().stream().filter(part -> !isSupportedPart(part.getType())).map(part -> part.getType().toString()).collect(Collectors.toList());
    if (!unsupportedParts.isEmpty()) {
        throw new FirestoreDataException("Unsupported predicate keywords: " + unsupportedParts + " in " + this.getQueryMethod().getName());
    }
}
Also used : GREATER_THAN(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN) Arrays(java.util.Arrays) Order(org.springframework.data.domain.Sort.Order) FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference) QueryMethod(org.springframework.data.repository.query.QueryMethod) LESS_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN_EQUAL) Part(org.springframework.data.repository.query.parser.Part) ArrayList(java.util.ArrayList) NOT_IN(org.springframework.data.repository.query.parser.Part.Type.NOT_IN) NEGATING_SIMPLE_PROPERTY(org.springframework.data.repository.query.parser.Part.Type.NEGATING_SIMPLE_PROPERTY) Map(java.util.Map) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) StreamSupport(java.util.stream.StreamSupport) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Direction(org.springframework.data.domain.Sort.Direction) Int32Value(com.google.protobuf.Int32Value) LESS_THAN(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) Iterator(java.util.Iterator) StructuredQuery(com.google.firestore.v1.StructuredQuery) FirestorePersistentEntity(org.springframework.cloud.gcp.data.firestore.mapping.FirestorePersistentEntity) PartTree(org.springframework.data.repository.query.parser.PartTree) FirestoreReactiveOperations(org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) SIMPLE_PROPERTY(org.springframework.data.repository.query.parser.Part.Type.SIMPLE_PROPERTY) Collectors(java.util.stream.Collectors) CONTAINING(org.springframework.data.repository.query.parser.Part.Type.CONTAINING) List(java.util.List) IN(org.springframework.data.repository.query.parser.Part.Type.IN) ReturnedType(org.springframework.data.repository.query.ReturnedType) GREATER_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN_EQUAL) FirestoreClassMapper(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreClassMapper) PropertyPath(org.springframework.data.mapping.PropertyPath) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) FirestoreMappingContext(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreMappingContext) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) ArrayList(java.util.ArrayList) List(java.util.List) PartTree(org.springframework.data.repository.query.parser.PartTree)

Example 4 with FirestoreDataException

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

the class ReactiveFirestoreTransactionManagerTest method triggerRollbackCorrectly.

@Test
public void triggerRollbackCorrectly() {
    FirestoreTemplate template = getFirestoreTemplate();
    ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent);
    TransactionalOperator operator = TransactionalOperator.create(txManager);
    template.findById(Mono.defer(() -> {
        throw new FirestoreDataException("BOOM!");
    }), FirestoreTemplateTests.TestEntity.class).as(operator::transactional).as(StepVerifier::create).expectError().verify();
    verify(this.firestoreStub, times(1)).beginTransaction(any(), any());
    verify(this.firestoreStub, times(0)).commit(any(), any());
    verify(this.firestoreStub, times(1)).rollback(any(), any());
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) FirestoreTemplateTests(org.springframework.cloud.gcp.data.firestore.FirestoreTemplateTests) StepVerifier(reactor.test.StepVerifier) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) Test(org.junit.Test)

Aggregations

FirestoreDataException (org.springframework.cloud.gcp.data.firestore.FirestoreDataException)4 StructuredQuery (com.google.firestore.v1.StructuredQuery)2 FieldReference (com.google.firestore.v1.StructuredQuery.FieldReference)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Test (org.junit.Test)2 FirestoreReactiveOperations (org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations)2 FirestoreTemplate (org.springframework.cloud.gcp.data.firestore.FirestoreTemplate)2 Level (ch.qos.logback.classic.Level)1 Int32Value (com.google.protobuf.Int32Value)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Matchers.is (org.hamcrest.Matchers.is)1 Assume.assumeThat (org.junit.Assume.assumeThat)1 Before (org.junit.Before)1