Skip to content

Non-reactive template.save() use non-reactive auditor and close clusters #1765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

import com.couchbase.client.java.Collection;
import com.couchbase.client.java.query.QueryScanConsistency;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import reactor.core.publisher.Mono;

/**
* Implements lower-level couchbase operations on top of the SDK with entity mapping capabilities.
Expand Down Expand Up @@ -83,8 +86,49 @@ public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final Couch

@Override
public <T> T save(T entity, String... scopeAndCollection) {
return reactive().save(entity, scopeAndCollection).block();
}
Assert.notNull(entity, "Entity must not be null!");

String scope = scopeAndCollection.length > 0 ? scopeAndCollection[0] : null;
String collection = scopeAndCollection.length > 1 ? scopeAndCollection[1] : null;
final CouchbasePersistentEntity<?> mapperEntity = getConverter().getMappingContext()
.getPersistentEntity(entity.getClass());
final CouchbasePersistentProperty versionProperty = mapperEntity.getVersionProperty();
final boolean versionPresent = versionProperty != null;
final Long version = versionProperty == null || versionProperty.getField() == null ? null
: (Long) ReflectionUtils.getField(versionProperty.getField(),
entity);
final boolean existingDocument = version != null && version > 0;

Class clazz = entity.getClass();

if (!versionPresent) { // the entity doesn't have a version property
// No version field - no cas
// If in a transaction, insert is the only thing that will work
return (T)TransactionalSupport.checkForTransactionInThreadLocalStorage()
.map(ctx -> {
if (ctx.isPresent()) {
return (T) insertById(clazz).inScope(scope)
.inCollection(collection)
.one(entity);
} else { // if not in a tx, then upsert will work
return (T) upsertById(clazz).inScope(scope)
.inCollection(collection)
.one(entity);
}
}).block();

} else if (existingDocument) { // there is a version property, and it is non-zero
// Updating existing document with cas
return (T)replaceById(clazz).inScope(scope)
.inCollection(collection)
.one(entity);
} else { // there is a version property, but it's zero or not set.
// Creating new document
return (T)insertById(clazz).inScope(scope)
.inCollection(collection)
.one(entity);
}
}

@Override
public <T> Long count(Query query, Class<T> domainType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,30 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.util.Capabilities;
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.CollectionAwareIntegrationTests;
import org.springframework.data.couchbase.util.IgnoreWhen;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.data.couchbase.domain.Config;

/**
* CouchbaseCache tests Theses tests rely on a cb server running.
*
* @author Michael Reiche
*/
@IgnoreWhen(clusterTypes = ClusterType.MOCKED, missesCapabilities = { Capabilities.COLLECTIONS })
@SpringJUnitConfig(Config.class)
@DirtiesContext
class CouchbaseCacheCollectionIntegrationTests extends CollectionAwareIntegrationTests {

volatile CouchbaseCache cache;

@Autowired CouchbaseTemplate couchbaseTemplate;

@BeforeEach
@Override
public void beforeEach() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.domain.Config;
import org.springframework.data.couchbase.domain.User;
import org.springframework.data.couchbase.domain.UserRepository;
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen;
import org.springframework.data.couchbase.util.JavaIntegrationTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
Expand All @@ -43,11 +43,13 @@
*/
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
@SpringJUnitConfig(Config.class)
@DirtiesContext
class CouchbaseCacheIntegrationTests extends JavaIntegrationTests {

volatile CouchbaseCache cache;
@Autowired CouchbaseCacheManager cacheManager; // autowired not working
@Autowired UserRepository userRepository; // autowired not working
@Autowired CouchbaseTemplate couchbaseTemplate;

@BeforeEach
@Override
Expand All @@ -56,9 +58,6 @@ public void beforeEach() {
cache = CouchbaseCacheManager.create(couchbaseTemplate.getCouchbaseClientFactory()).createCouchbaseCache("myCache",
CouchbaseCacheConfiguration.defaultCacheConfig());
cache.clear();
ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
cacheManager = ac.getBean(CouchbaseCacheManager.class);
userRepository = ac.getBean(UserRepository.class);
}

@AfterEach
Expand Down Expand Up @@ -134,5 +133,4 @@ public void clearWithDelayOk() throws InterruptedException {

@Test
public void noOpt() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.dao.OptimisticLockingFailureException;
Expand Down Expand Up @@ -69,6 +70,7 @@
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen;
import org.springframework.data.couchbase.util.JavaIntegrationTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

Expand All @@ -93,6 +95,7 @@
*/
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
@SpringJUnitConfig(Config.class)
@DirtiesContext
@TestPropertySource(properties = { "valid.document.durability = MAJORITY" })
class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests {

Expand All @@ -102,6 +105,7 @@ class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests {
@BeforeEach
@Override
public void beforeEach() {
super.beforeEach();
couchbaseTemplate.removeByQuery(User.class).all();
couchbaseTemplate.removeByQuery(UserAnnotated.class).all();
couchbaseTemplate.removeByQuery(UserAnnotated2.class).all();
Expand Down Expand Up @@ -1337,11 +1341,9 @@ void sampleScanId() {

}


private void sleepSecs(int i) {
try {
Thread.sleep(i * 1000);
} catch (InterruptedException ie) {}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import org.springframework.data.couchbase.core.query.QueryCriteria;
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.Airport;
import org.springframework.data.couchbase.domain.CollectionsConfig;
import org.springframework.data.couchbase.domain.Course;
import org.springframework.data.couchbase.domain.ConfigScoped;
import org.springframework.data.couchbase.domain.NaiveAuditorAware;
import org.springframework.data.couchbase.domain.Submission;
import org.springframework.data.couchbase.domain.User;
Expand All @@ -57,6 +57,7 @@
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.CollectionAwareIntegrationTests;
import org.springframework.data.couchbase.util.IgnoreWhen;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import com.couchbase.client.core.error.AmbiguousTimeoutException;
Expand All @@ -80,7 +81,8 @@
* @author Michael Reiche
*/
@IgnoreWhen(missesCapabilities = { Capabilities.QUERY, Capabilities.COLLECTIONS }, clusterTypes = ClusterType.MOCKED)
@SpringJUnitConfig(CollectionsConfig.class)
@SpringJUnitConfig(ConfigScoped.class)
@DirtiesContext
class CouchbaseTemplateQueryCollectionIntegrationTests extends CollectionAwareIntegrationTests {

@Autowired public CouchbaseTemplate couchbaseTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.core.query.QueryCriteria;
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.Airport;
import org.springframework.data.couchbase.domain.AssessmentDO;
import org.springframework.data.couchbase.domain.Config;
import org.springframework.data.couchbase.domain.Course;
import org.springframework.data.couchbase.domain.Config;
import org.springframework.data.couchbase.domain.NaiveAuditorAware;
import org.springframework.data.couchbase.domain.PersonWithMaps;
import org.springframework.data.couchbase.domain.Submission;
Expand All @@ -60,6 +61,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
Expand All @@ -72,6 +74,7 @@
*/
@IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED)
@SpringJUnitConfig(Config.class)
@DirtiesContext
class CouchbaseTemplateQueryIntegrationTests extends JavaIntegrationTests {

@Autowired public CouchbaseTemplate couchbaseTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.convert.DefaultCouchbaseTypeMapper;
import org.springframework.data.couchbase.domain.User;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import com.couchbase.client.core.deps.io.netty.handler.ssl.util.InsecureTrustManagerFactory;
Expand All @@ -45,6 +44,7 @@
*/
@SpringJUnitConfig(CustomTypeKeyIntegrationTests.Config.class)
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
@DirtiesContext
public class CustomTypeKeyIntegrationTests extends ClusterAwareIntegrationTests {

private static final String CUSTOM_TYPE_KEY = "javaClass";
Expand All @@ -70,43 +70,11 @@ void saveSimpleEntityCorrectlyWithDifferentTypeKey() {
operations.removeById(User.class).one(user.getId());
}

@Configuration
@EnableCouchbaseRepositories("org.springframework.data.couchbase")
static class Config extends AbstractCouchbaseConfiguration {

@Override
public String getConnectionString() {
return connectionString();
}

@Override
public String getUserName() {
return config().adminUsername();
}

@Override
public String getPassword() {
return config().adminPassword();
}

@Override
public String getBucketName() {
return bucketName();
}

@Override
protected void configureEnvironment(ClusterEnvironment.Builder builder) {
if (config().isUsingCloud()) {
builder.securityConfig(
SecurityConfig.builder().trustManagerFactory(InsecureTrustManagerFactory.INSTANCE).enableTls(true));
}
}

static class Config extends org.springframework.data.couchbase.domain.Config {
@Override
public String typeKey() {
return CUSTOM_TYPE_KEY;
}

}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package org.springframework.data.couchbase.core;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.concurrent.Semaphore;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.core.support.PseudoArgs;
import org.springframework.data.couchbase.domain.Config;
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
@SpringJUnitConfig(Config.class)
@DirtiesContext
public class ReactiveCouchbaseTemplateConcurrencyTests {

@Autowired public CouchbaseTemplate couchbaseTemplate;
Expand Down
Loading