Add rejectNonTransactionalOperations to ReactiveFindByIdOperationSupport.

Closes #1494.
This commit is contained in:
Michael Reiche
2022-07-13 15:34:20 -07:00
parent 9af74cfb7c
commit fab3f55436
2 changed files with 42 additions and 1 deletions

View File

@@ -18,6 +18,9 @@ package org.springframework.data.couchbase.core;
import static com.couchbase.client.java.kv.GetAndTouchOptions.getAndTouchOptions;
import static com.couchbase.client.java.transactions.internal.ConverterUtil.makeCollectionIdentifier;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.ReplicateTo;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -107,6 +110,7 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati
pArgs.getScope(), pArgs.getCollection(), null, null));
}
} else {
rejectInvalidTransactionalOptions();
return ctxOpt.get().getCore().get(makeCollectionIdentifier(rc.async()), id)
.flatMap(result -> support.decodeEntity(id, new String(result.contentAsBytes(), StandardCharsets.UTF_8),
result.cas(), domainType, pArgs.getScope(), pArgs.getCollection(),
@@ -129,6 +133,18 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati
}
private void rejectInvalidTransactionalOptions() {
if (this.expiry != null) {
throw new IllegalArgumentException("withExpiry is not supported in a transaction");
}
if (this.options != null) {
throw new IllegalArgumentException("withOptions is not supported in a transaction");
}
if (this.fields != null) {
throw new IllegalArgumentException("project is not supported in a transaction");
}
}
@Override
public Flux<? extends T> all(final Collection<String> ids) {
return Flux.fromIterable(ids).flatMap(this::one);

View File

@@ -23,7 +23,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
@@ -43,6 +42,8 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
import com.couchbase.client.java.kv.GetOptions;
import com.couchbase.client.java.kv.InsertOptions;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.RemoveOptions;
@@ -177,6 +178,30 @@ public class CouchbaseTransactionalUnsettableParametersIntegrationTests extends
});
}
@DisplayName("Using findById().withExpiry in a transaction is rejected at runtime")
@Test
public void findWithExpiry() {
test((ops) -> {
ops.replaceById(Person.class).withExpiry(Duration.ofSeconds(3)).one(WalterWhite);
});
}
@DisplayName("Using findById().project in a transaction is rejected at runtime")
@Test
public void findProject() {
test((ops) -> {
ops.findById(Person.class).project(new String[] { "someField" }).one(WalterWhite.id());
});
}
@DisplayName("Using findById().withOptions in a transaction is rejected at runtime")
@Test
public void findWithOptions() {
test((ops) -> {
ops.findById(Person.class).withOptions(GetOptions.getOptions()).one(WalterWhite.id());
});
}
@Service
@Component
@EnableTransactionManagement