diff --git a/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java index c0965a6b9..9479afb9c 100644 --- a/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java @@ -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. @@ -83,8 +86,49 @@ public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final Couch @Override public 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 Long count(Query query, Class domainType) { diff --git a/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheCollectionIntegrationTests.java index 10ad82aa0..556e83f15 100644 --- a/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheCollectionIntegrationTests.java @@ -24,10 +24,15 @@ 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. @@ -35,10 +40,14 @@ * @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() { diff --git a/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheIntegrationTests.java index a365cac14..ac32941bf 100644 --- a/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/cache/CouchbaseCacheIntegrationTests.java @@ -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; /** @@ -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 @@ -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 @@ -134,5 +133,4 @@ public void clearWithDelayOk() throws InterruptedException { @Test public void noOpt() {} - } diff --git a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java index 40d1cf2c4..baf1aa8f9 100644 --- a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java @@ -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; @@ -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; @@ -93,6 +95,7 @@ */ @IgnoreWhen(clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(Config.class) +@DirtiesContext @TestPropertySource(properties = { "valid.document.durability = MAJORITY" }) class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests { @@ -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(); @@ -1337,11 +1341,9 @@ void sampleScanId() { } - private void sleepSecs(int i) { try { Thread.sleep(i * 1000); } catch (InterruptedException ie) {} } - } diff --git a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryCollectionIntegrationTests.java index 5a9d469b9..0e130c93e 100644 --- a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryCollectionIntegrationTests.java @@ -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; @@ -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; @@ -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; diff --git a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryIntegrationTests.java index 07b67fea2..229ed0e8f 100644 --- a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryIntegrationTests.java @@ -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; @@ -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; /** @@ -72,6 +74,7 @@ */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(Config.class) +@DirtiesContext class CouchbaseTemplateQueryIntegrationTests extends JavaIntegrationTests { @Autowired public CouchbaseTemplate couchbaseTemplate; diff --git a/src/test/java/org/springframework/data/couchbase/core/CustomTypeKeyIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/core/CustomTypeKeyIntegrationTests.java index acde4e621..725bb1cc9 100644 --- a/src/test/java/org/springframework/data/couchbase/core/CustomTypeKeyIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/CustomTypeKeyIntegrationTests.java @@ -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; @@ -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"; @@ -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; } - } } diff --git a/src/test/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateConcurrencyTests.java b/src/test/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateConcurrencyTests.java index 188f6b8e7..296129366 100644 --- a/src/test/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateConcurrencyTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateConcurrencyTests.java @@ -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; diff --git a/src/test/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateKeyValueIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateKeyValueIntegrationTests.java index 4174e9fc0..f543c24ad 100644 --- a/src/test/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateKeyValueIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateKeyValueIntegrationTests.java @@ -28,15 +28,19 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.time.Duration; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.UUID; -import com.couchbase.client.core.error.TimeoutException; -import com.couchbase.client.core.msg.kv.DurabilityLevel; -import com.couchbase.client.core.retry.RetryReason; -import com.couchbase.client.java.query.QueryScanConsistency; 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; @@ -47,14 +51,31 @@ import org.springframework.data.couchbase.core.support.OneAndAllIdReactive; import org.springframework.data.couchbase.core.support.WithDurability; import org.springframework.data.couchbase.core.support.WithExpiry; -import org.springframework.data.couchbase.domain.*; +import org.springframework.data.couchbase.domain.Address; +import org.springframework.data.couchbase.domain.Config; +import org.springframework.data.couchbase.domain.MutableUser; +import org.springframework.data.couchbase.domain.PersonValue; +import org.springframework.data.couchbase.domain.ReactiveNaiveAuditorAware; +import org.springframework.data.couchbase.domain.User; +import org.springframework.data.couchbase.domain.UserAnnotated; +import org.springframework.data.couchbase.domain.UserAnnotated2; +import org.springframework.data.couchbase.domain.UserAnnotated3; +import org.springframework.data.couchbase.domain.UserAnnotatedDurability; +import org.springframework.data.couchbase.domain.UserAnnotatedPersistTo; +import org.springframework.data.couchbase.domain.UserAnnotatedReplicateTo; +import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; 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; +import com.couchbase.client.core.error.TimeoutException; +import com.couchbase.client.core.msg.kv.DurabilityLevel; +import com.couchbase.client.core.retry.RetryReason; import com.couchbase.client.java.kv.PersistTo; import com.couchbase.client.java.kv.ReplicateTo; +import com.couchbase.client.java.query.QueryScanConsistency; /** * KV tests Theses tests rely on a cb server running. @@ -65,6 +86,7 @@ */ @IgnoreWhen(clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(Config.class) +@DirtiesContext class ReactiveCouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests { @Autowired public CouchbaseTemplate couchbaseTemplate; diff --git a/src/test/java/org/springframework/data/couchbase/core/query/ReactiveCouchbaseTemplateQueryCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/core/query/ReactiveCouchbaseTemplateQueryCollectionIntegrationTests.java index f26a54707..c2cdc8373 100644 --- a/src/test/java/org/springframework/data/couchbase/core/query/ReactiveCouchbaseTemplateQueryCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/query/ReactiveCouchbaseTemplateQueryCollectionIntegrationTests.java @@ -38,12 +38,13 @@ import org.junit.jupiter.api.Disabled; 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.CouchbaseTemplate; import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; import org.springframework.data.couchbase.core.RemoveResult; 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.ConfigScoped; import org.springframework.data.couchbase.domain.Course; import org.springframework.data.couchbase.domain.NaiveAuditorAware; import org.springframework.data.couchbase.domain.Submission; @@ -52,10 +53,12 @@ import org.springframework.data.couchbase.domain.UserSubmission; import org.springframework.data.couchbase.domain.UserSubmissionProjected; import org.springframework.data.couchbase.domain.time.AuditingDateTimeProvider; +import org.springframework.data.couchbase.repository.config.EnableReactiveCouchbaseRepositories; 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 com.couchbase.client.core.error.AmbiguousTimeoutException; @@ -78,7 +81,8 @@ * @author Michael Reiche */ @IgnoreWhen(missesCapabilities = { Capabilities.QUERY, Capabilities.COLLECTIONS }, clusterTypes = ClusterType.MOCKED) -@SpringJUnitConfig(CollectionsConfig.class) +@SpringJUnitConfig(ConfigScoped.class) +@DirtiesContext class ReactiveCouchbaseTemplateQueryCollectionIntegrationTests extends CollectionAwareIntegrationTests { @Autowired public CouchbaseTemplate couchbaseTemplate; diff --git a/src/test/java/org/springframework/data/couchbase/domain/ConfigScoped.java b/src/test/java/org/springframework/data/couchbase/domain/ConfigScoped.java new file mode 100644 index 000000000..4cf584ec9 --- /dev/null +++ b/src/test/java/org/springframework/data/couchbase/domain/ConfigScoped.java @@ -0,0 +1,27 @@ +/* + * Copyright 2012-2023 the original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.couchbase.domain; + +/** + * @author Michael Reiche + */ +public class ConfigScoped extends Config{ + @Override + protected java.lang.String getScopeName() { + return "my_scope"; + } +} diff --git a/src/test/java/org/springframework/data/couchbase/domain/FluxIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/domain/FluxIntegrationTests.java index ab29839df..8914d281f 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/FluxIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/domain/FluxIntegrationTests.java @@ -17,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import org.springframework.test.annotation.DirtiesContext; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.core.publisher.ParallelFlux; @@ -33,14 +34,12 @@ 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.context.annotation.Configuration; import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; -import org.springframework.data.couchbase.config.BeanNames; import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; import org.springframework.data.couchbase.core.RemoveResult; +import org.springframework.data.couchbase.domain.Config; import org.springframework.data.couchbase.repository.config.EnableReactiveCouchbaseRepositories; import org.springframework.data.couchbase.util.Capabilities; import org.springframework.data.couchbase.util.ClusterType; @@ -64,7 +63,8 @@ /** * @author Michael Reiche */ -@SpringJUnitConfig(FluxIntegrationTests.Config.class) +@SpringJUnitConfig(Config.class) +@DirtiesContext @IgnoreWhen(clusterTypes = ClusterType.MOCKED) public class FluxIntegrationTests extends JavaIntegrationTests { @@ -74,21 +74,17 @@ public class FluxIntegrationTests extends JavaIntegrationTests { @BeforeEach @Override public void beforeEach() { - + super.beforeEach(); /** * The couchbaseTemplate inherited from JavaIntegrationTests uses org.springframework.data.couchbase.domain.Config * It has typeName = 't' (instead of _class). Don't use it. */ - ApplicationContext ac = new AnnotationConfigApplicationContext(FluxIntegrationTests.Config.class); - couchbaseTemplate = (CouchbaseTemplate) ac.getBean(BeanNames.COUCHBASE_TEMPLATE); - reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(BeanNames.REACTIVE_COUCHBASE_TEMPLATE); collection = couchbaseTemplate.getCouchbaseClientFactory().getBucket().defaultCollection(); rCollection = couchbaseTemplate.getCouchbaseClientFactory().getBucket().reactive().defaultCollection(); for (String k : keyList) { couchbaseTemplate.getCouchbaseClientFactory().getBucket().defaultCollection().upsert(k, JsonObject.create().put("x", k)); } - super.beforeEach(); } @AfterEach @@ -242,37 +238,4 @@ static String tab(int len) { return sb.toString(); } - @Configuration - @EnableReactiveCouchbaseRepositories("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)); - } - } - - } } diff --git a/src/test/java/org/springframework/data/couchbase/domain/PersonWithoutVersion.java b/src/test/java/org/springframework/data/couchbase/domain/PersonWithoutVersion.java index 13dfe8d32..9cef832e6 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/PersonWithoutVersion.java +++ b/src/test/java/org/springframework/data/couchbase/domain/PersonWithoutVersion.java @@ -46,4 +46,25 @@ public PersonWithoutVersion(UUID id, String firstname, String lastname) { this.lastname = Optional.of(lastname); setId(id); } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("PersonWithoutVersion : {\n"); + sb.append(" id : " + getId()); + sb.append(optional(", firstname", firstname)); + sb.append(optional(", lastname", lastname)); + sb.append("\n}"); + return sb.toString(); + } + + static String optional(String name, Optional obj) { + if (obj != null) { + if (obj.isPresent()) { + return (" " + name + ": '" + obj.get() + "'"); + } else { + return " " + name + ": null"; + } + } + return ""; + } } diff --git a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseAbstractRepositoryIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseAbstractRepositoryIntegrationTests.java index 254af4c63..4f7464512 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseAbstractRepositoryIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseAbstractRepositoryIntegrationTests.java @@ -39,6 +39,7 @@ 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; @@ -51,6 +52,7 @@ * @author Michael Reiche */ @SpringJUnitConfig(CouchbaseAbstractRepositoryIntegrationTests.Config.class) +@DirtiesContext @IgnoreWhen(clusterTypes = ClusterType.MOCKED) public class CouchbaseAbstractRepositoryIntegrationTests extends ClusterAwareIntegrationTests { @@ -108,38 +110,7 @@ void saveAndFindAbstract() { } - @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 { /** * This uses a CustomMappingCouchbaseConverter instead of MappingCouchbaseConverter, which in turn uses * AbstractTypeMapper which has special mapping for AbstractUser diff --git a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryAutoQueryIndexIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryAutoQueryIndexIntegrationTests.java index 951e5d705..aefa6de0b 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryAutoQueryIndexIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryAutoQueryIndexIntegrationTests.java @@ -32,12 +32,14 @@ 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.java.Cluster; import com.couchbase.client.java.manager.query.QueryIndex; @SpringJUnitConfig(CouchbaseRepositoryAutoQueryIndexIntegrationTests.Config.class) +@DirtiesContext @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) public class CouchbaseRepositoryAutoQueryIndexIntegrationTests extends ClusterAwareIntegrationTests { diff --git a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryFieldLevelEncryptionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryFieldLevelEncryptionIntegrationTests.java index 7730747f3..6a97518d3 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryFieldLevelEncryptionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryFieldLevelEncryptionIntegrationTests.java @@ -42,10 +42,10 @@ import org.springframework.data.couchbase.domain.TestEncrypted; import org.springframework.data.couchbase.domain.UserEncrypted; import org.springframework.data.couchbase.domain.UserEncryptedRepository; -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; @@ -66,6 +66,7 @@ * @author Michael Reiche */ @SpringJUnitConfig(CouchbaseRepositoryFieldLevelEncryptionIntegrationTests.Config.class) +@DirtiesContext @IgnoreWhen(clusterTypes = ClusterType.MOCKED) public class CouchbaseRepositoryFieldLevelEncryptionIntegrationTests extends ClusterAwareIntegrationTests { @@ -329,29 +330,7 @@ void testFromMigration() { } } - @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(); - } + static class Config extends org.springframework.data.couchbase.domain.Config { @Override public ObjectMapper couchbaseObjectMapper() { @@ -360,14 +339,6 @@ public ObjectMapper couchbaseObjectMapper() { return om; } - @Override - protected void configureEnvironment(ClusterEnvironment.Builder builder) { - if (config().isUsingCloud()) { - builder.securityConfig( - SecurityConfig.builder().trustManagerFactory(InsecureTrustManagerFactory.INSTANCE).enableTls(true)); - } - } - @Override protected CryptoManager cryptoManager() { Map keyMap = new HashMap(); diff --git a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryKeyValueIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryKeyValueIntegrationTests.java index a0a015fdf..a39b0688f 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryKeyValueIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryKeyValueIntegrationTests.java @@ -41,6 +41,7 @@ import org.springframework.data.couchbase.domain.Airline; import org.springframework.data.couchbase.domain.AirlineRepository; import org.springframework.data.couchbase.domain.Course; +import org.springframework.data.couchbase.domain.Config; import org.springframework.data.couchbase.domain.Library; import org.springframework.data.couchbase.domain.LibraryRepository; import org.springframework.data.couchbase.domain.PersonValue; @@ -52,10 +53,10 @@ import org.springframework.data.couchbase.domain.UserRepository; import org.springframework.data.couchbase.domain.UserSubmission; import org.springframework.data.couchbase.domain.UserSubmissionRepository; -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; @@ -69,7 +70,8 @@ * @author Michael Nitschinger * @author Michael Reiche */ -@SpringJUnitConfig(CouchbaseRepositoryKeyValueIntegrationTests.Config.class) +@SpringJUnitConfig(Config.class) +@DirtiesContext @IgnoreWhen(clusterTypes = ClusterType.MOCKED) public class CouchbaseRepositoryKeyValueIntegrationTests extends ClusterAwareIntegrationTests { @@ -198,37 +200,4 @@ void saveAndFindByWithNestedId() { userSubmissionRepository.delete(user); } - @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)); - } - } - } - } diff --git a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java index 76e976b7c..47b3fe2a2 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java @@ -30,7 +30,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE; -import com.couchbase.client.core.error.UnambiguousTimeoutException; import jakarta.validation.ConstraintViolationException; import junit.framework.AssertionFailedError; @@ -58,9 +57,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.OptimisticLockingFailureException; -import org.springframework.data.auditing.DateTimeProvider; import org.springframework.data.couchbase.CouchbaseClientFactory; -import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; import org.springframework.data.couchbase.core.CouchbaseQueryExecutionException; import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.RemoveResult; @@ -84,13 +81,15 @@ import org.springframework.data.couchbase.domain.NaiveAuditorAware; import org.springframework.data.couchbase.domain.Person; import org.springframework.data.couchbase.domain.PersonRepository; +import org.springframework.data.couchbase.domain.ReactiveAirportRepository; +import org.springframework.data.couchbase.domain.ReactiveNaiveAuditorAware; import org.springframework.data.couchbase.domain.User; import org.springframework.data.couchbase.domain.UserAnnotated; import org.springframework.data.couchbase.domain.UserRepository; import org.springframework.data.couchbase.domain.UserSubmission; import org.springframework.data.couchbase.domain.UserSubmissionRepository; -import org.springframework.data.couchbase.domain.time.AuditingDateTimeProvider; import org.springframework.data.couchbase.repository.auditing.EnableCouchbaseAuditing; +import org.springframework.data.couchbase.repository.auditing.EnableReactiveCouchbaseAuditing; import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; import org.springframework.data.couchbase.repository.query.CouchbaseQueryMethod; import org.springframework.data.couchbase.repository.query.CouchbaseRepositoryQuery; @@ -105,20 +104,19 @@ import org.springframework.data.domain.Sort; import org.springframework.data.projection.SpelAwareProxyProjectionFactory; import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; -import com.couchbase.client.core.deps.io.netty.handler.ssl.util.InsecureTrustManagerFactory; -import com.couchbase.client.core.env.SecurityConfig; -import com.couchbase.client.core.error.AmbiguousTimeoutException; import com.couchbase.client.core.error.CouchbaseException; import com.couchbase.client.core.error.IndexFailureException; -import com.couchbase.client.java.env.ClusterEnvironment; +import com.couchbase.client.core.error.UnambiguousTimeoutException; import com.couchbase.client.java.json.JsonArray; import com.couchbase.client.java.json.JsonObject; import com.couchbase.client.java.kv.GetResult; import com.couchbase.client.java.kv.InsertOptions; import com.couchbase.client.java.kv.MutationState; +import com.couchbase.client.java.kv.UpsertOptions; import com.couchbase.client.java.query.QueryOptions; import com.couchbase.client.java.query.QueryScanConsistency; @@ -131,12 +129,15 @@ */ @SpringJUnitConfig(CouchbaseRepositoryQueryIntegrationTests.Config.class) @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) +@DirtiesContext public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegrationTests { @Autowired CouchbaseClientFactory clientFactory; @Autowired AirportRepository airportRepository; + @Autowired ReactiveAirportRepository reactiveAirportRepository; + @Autowired AirportJsonValueRepository airportJsonValueRepository; @Autowired AirlineRepository airlineRepository; @@ -448,85 +449,100 @@ public void saveNotBoundedRequestPlus() { CouchbaseTemplate couchbaseTemplateRP = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); AirportRepository airportRepositoryRP = (AirportRepository) ac.getBean("airportRepository"); - // save() followed by query with NOT_BOUNDED will result in not finding the document - Airport vie = new Airport("airports::vie", "vie", "low9"); - Airport airport2 = null; - for (int i = 1; i <= 100; i++) { - // set version == 0 so save() will be an upsert, not a replace - Airport saved = airportRepositoryRP.save(vie.clearVersion()); - try { - airport2 = airportRepositoryRP.iata(saved.getIata()); - if (airport2 == null) { - break; - } - } catch (DataRetrievalFailureException drfe) { - airport2 = null; // - } finally { - // airportRepository.delete(vie); - // instead of delete, use removeResult to test QueryOptions.consistentWith() - RemoveResult removeResult = couchbaseTemplateRP.removeById().one(vie.getId()); - assertEquals(vie.getId(), removeResult.getId()); - assertTrue(removeResult.getCas() != 0); - assertTrue(removeResult.getMutationToken().isPresent()); - Airport airport3 = airportRepositoryRP.iata(vie.getIata()); - assertNull(airport3, "should have been removed"); - } - } - assertNotNull(airport2, "airport2 should have never been null"); - Airport saved = airportRepositoryRP.save(vie.clearVersion()); - List airports = couchbaseTemplateRP.findByQuery(Airport.class).withConsistency(NOT_BOUNDED).all(); - RemoveResult removeResult = couchbaseTemplateRP.removeById().one(saved.getId()); - if (!config().isUsingCloud()) { - assertTrue(airports.isEmpty(), "airports should have been empty"); + try { + // save() followed by query with NOT_BOUNDED will result in not finding the document + Airport vie = new Airport("airports::vie", "vie", "low9"); + Airport airport2 = null; + for (int i = 1; i <= 100; i++) { + // set version == 0 so save() will be an upsert, not a replace + Airport saved = couchbaseTemplateRP.upsertById(Airport.class) + .withOptions(UpsertOptions.upsertOptions().timeout(Duration.ofSeconds(10))) + .one(vie.clearVersion()); + try { + airport2 = airportRepositoryRP.iata(saved.getIata()); + if (airport2 == null) { + break; + } + } catch (DataRetrievalFailureException drfe) { + airport2 = null; // + } finally { + // airportRepository.delete(vie); + // instead of delete, use removeResult to test QueryOptions.consistentWith() + RemoveResult removeResult = couchbaseTemplateRP.removeById().one(vie.getId()); + assertEquals(vie.getId(), removeResult.getId()); + assertTrue(removeResult.getCas() != 0); + assertTrue(removeResult.getMutationToken().isPresent()); + Airport airport3 = airportRepositoryRP.iata(vie.getIata()); + assertNull(airport3, "should have been removed"); + } + } + assertNotNull(airport2, "airport2 should have never been null"); + Airport saved = airportRepositoryRP.save(vie.clearVersion()); + List airports = couchbaseTemplateRP.findByQuery(Airport.class).withConsistency(NOT_BOUNDED).all(); + RemoveResult removeResult = couchbaseTemplateRP.removeById().one(saved.getId()); + if (!config().isUsingCloud()) { + assertTrue(airports.isEmpty(), "airports should have been empty"); + } + } finally { + logDisconnect(couchbaseTemplateRP.getCouchbaseClientFactory().getCluster(), + this.getClass().getSimpleName()); } } @Test - public void saveNotBoundedWithDefaultRepository() { - if (config().isUsingCloud()) { // I don't think the query following the insert will be quick enough for the test - return; - } - airportRepository.withOptions(QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS)).deleteAll(); - ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); - // the Config class has been modified, these need to be loaded again - CouchbaseTemplate couchbaseTemplateRP = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); - AirportRepositoryScanConsistencyTest airportRepositoryRP = (AirportRepositoryScanConsistencyTest) ac - .getBean("airportRepositoryScanConsistencyTest"); - - List sizeBeforeTest = (List) airportRepositoryRP.findAll(); - assertEquals(0, sizeBeforeTest.size()); - - boolean notFound = false; - for (int i = 0; i < 100; i++) { - Airport vie = new Airport("airports::vie", "vie", "low9"); - Airport saved = airportRepositoryRP.save(vie); - List allSaved = (List) airportRepositoryRP.findAll(); - couchbaseTemplate.removeById(Airport.class).one(saved.getId()); - if (allSaved.isEmpty()) { - notFound = true; - break; - } - } - assertTrue(notFound, "the doc should not have been found. maybe"); - } + public void saveNotBoundedWithDefaultRepository() { + if (config().isUsingCloud()) { // I don't think the query following the insert will be quick enough for the test + return; + } + airportRepository.withOptions(QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS)).deleteAll(); + ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); + // the Config class has been modified, these need to be loaded again + + AirportRepositoryScanConsistencyTest airportRepositoryRP = (AirportRepositoryScanConsistencyTest) ac + .getBean("airportRepositoryScanConsistencyTest"); + + try { + List sizeBeforeTest = (List) airportRepositoryRP.findAll(); + assertEquals(0, sizeBeforeTest.size()); + + boolean notFound = false; + for (int i = 0; i < 100; i++) { + Airport vie = new Airport("airports::vie", "vie", "low9"); + Airport saved = airportRepositoryRP.save(vie); + List allSaved = (List) airportRepositoryRP.findAll(); + couchbaseTemplate.removeById(Airport.class).one(saved.getId()); + if (allSaved.isEmpty()) { + notFound = true; + break; + } + } + assertTrue(notFound, "the doc should not have been found. maybe"); + } finally { + logDisconnect(airportRepositoryRP.getOperations().getCouchbaseClientFactory().getCluster(), "b"); + } + } @Test - public void saveRequestPlusWithDefaultRepository() { + public void saveRequestPlusWithDefaultRepository() { - ApplicationContext ac = new AnnotationConfigApplicationContext(ConfigRequestPlus.class); - // the Config class has been modified, these need to be loaded again - AirportRepositoryScanConsistencyTest airportRepositoryRP = (AirportRepositoryScanConsistencyTest) ac - .getBean("airportRepositoryScanConsistencyTest"); + ApplicationContext ac = new AnnotationConfigApplicationContext(ConfigRequestPlus.class); + // the Config class has been modified, these need to be loaded again + AirportRepositoryScanConsistencyTest airportRepositoryRP = (AirportRepositoryScanConsistencyTest) ac + .getBean("airportRepositoryScanConsistencyTest"); - List sizeBeforeTest = airportRepositoryRP.findAll(); - assertEquals(0, sizeBeforeTest.size()); + try { + List sizeBeforeTest = airportRepositoryRP.findAll(); + assertEquals(0, sizeBeforeTest.size()); - Airport vie = new Airport("airports::vie", "vie", "low9"); - Airport saved = airportRepositoryRP.save(vie); - List allSaved = airportRepositoryRP.findAll(REQUEST_PLUS); - couchbaseTemplate.removeById(Airport.class).one(saved.getId()); - assertEquals(1, allSaved.size(), "should have found 1 airport"); - } + Airport vie = new Airport("airports::vie", "vie", "low9"); + Airport saved = airportRepositoryRP.save(vie); + List allSaved = airportRepositoryRP.findAll(REQUEST_PLUS); + couchbaseTemplate.removeById(Airport.class).one(saved.getId()); + } finally { + logDisconnect(airportRepositoryRP.getOperations().getCouchbaseClientFactory().getCluster(), "c"); + } + + } @Test void findByTypeAlias() { @@ -536,9 +552,9 @@ void findByTypeAlias() { vie = airportRepository.save(vie); List airports = couchbaseTemplate.findByQuery(Airport.class).withConsistency(REQUEST_PLUS) .matching(org.springframework.data.couchbase.core.query.Query - .query(QueryCriteria.where(N1QLExpression.x("_class")).is("airport"))) + .query(QueryCriteria.where(N1QLExpression.x("t")).is("airport"))) .all(); - assertFalse(airports.isEmpty(), "should have found aiport"); + assertFalse(airports.isEmpty(), "should have found airport"); } finally { airportRepository.delete(vie); } @@ -1114,7 +1130,21 @@ void findBySimplePropertyAudited() { Airport saved = airportRepository.save(vie); List airports1 = airportRepository.findAllByIata("vie"); assertEquals(saved, airports1.get(0)); - assertEquals(saved.getCreatedBy(), NaiveAuditorAware.AUDITOR); // NaiveAuditorAware will provide this + assertEquals(NaiveAuditorAware.AUDITOR, saved.getCreatedBy()); // NaiveAuditorAware will provide this + } finally { + airportRepository.delete(vie); + } + } + + @Test + void findBySimplePropertyAuditedReactive() { + Airport vie = null; + try { + vie = new Airport("airports::vie", "vie", "low2"); + Airport saved = reactiveAirportRepository.save(vie).block(); + List airports1 = airportRepository.findAllByIata("vie"); + assertEquals(saved, airports1.get(0)); + assertEquals(ReactiveNaiveAuditorAware.AUDITOR, saved.getCreatedBy()); // ReactiveNaiveAuditorAware will provide this } finally { airportRepository.delete(vie); } @@ -1184,47 +1214,9 @@ private void sleep(int millis) { @Configuration @EnableCouchbaseRepositories("org.springframework.data.couchbase") - @EnableCouchbaseAuditing(dateTimeProviderRef = "dateTimeProviderRef") - 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(); - } - - @Bean(name = "auditorAwareRef") - public NaiveAuditorAware testAuditorAware() { - return new NaiveAuditorAware(); - } - - @Override - public void configureEnvironment(final ClusterEnvironment.Builder builder) { - // builder.ioConfig().maxHttpConnections(11).idleHttpConnectionTimeout(Duration.ofSeconds(4)); - if (config().isUsingCloud()) { - builder.securityConfig( - SecurityConfig.builder().trustManagerFactory(InsecureTrustManagerFactory.INSTANCE).enableTls(true)); - } - } - - @Bean(name = "dateTimeProviderRef") - public DateTimeProvider testDateTimeProvider() { - return new AuditingDateTimeProvider(); - } + @EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") + @EnableReactiveCouchbaseAuditing(auditorAwareRef = "reactiveAuditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") + static class Config extends org.springframework.data.couchbase.domain.Config { @Bean public LocalValidatorFactoryBean validator() { @@ -1240,46 +1232,8 @@ public ValidatingCouchbaseEventListener validationEventListener() { @Configuration @EnableCouchbaseRepositories("org.springframework.data.couchbase") @EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") - static class ConfigRequestPlus 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)); - } - } - - @Bean(name = "auditorAwareRef") - public NaiveAuditorAware testAuditorAware() { - return new NaiveAuditorAware(); - } - - @Bean(name = "dateTimeProviderRef") - public DateTimeProvider testDateTimeProvider() { - return new AuditingDateTimeProvider(); - } - + @EnableReactiveCouchbaseAuditing(auditorAwareRef = "reactiveAuditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") + static class ConfigRequestPlus extends org.springframework.data.couchbase.domain.Config { @Override public QueryScanConsistency getDefaultConsistency() { return REQUEST_PLUS; diff --git a/src/test/java/org/springframework/data/couchbase/repository/ReactiveCouchbaseRepositoryKeyValueIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/ReactiveCouchbaseRepositoryKeyValueIntegrationTests.java index 0e964bc4a..f6b006b96 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/ReactiveCouchbaseRepositoryKeyValueIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/ReactiveCouchbaseRepositoryKeyValueIntegrationTests.java @@ -34,6 +34,7 @@ import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; import org.springframework.data.couchbase.domain.Airline; import org.springframework.data.couchbase.domain.Airport; +import org.springframework.data.couchbase.domain.Config; import org.springframework.data.couchbase.domain.ReactiveAirlineRepository; import org.springframework.data.couchbase.domain.ReactiveAirportRepository; import org.springframework.data.couchbase.domain.ReactiveNaiveAuditorAware; @@ -45,6 +46,7 @@ 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; @@ -54,7 +56,8 @@ /** * @author Michael Reiche */ -@SpringJUnitConfig(ReactiveCouchbaseRepositoryKeyValueIntegrationTests.Config.class) +@SpringJUnitConfig(Config.class) +@DirtiesContext @IgnoreWhen(clusterTypes = ClusterType.MOCKED) public class ReactiveCouchbaseRepositoryKeyValueIntegrationTests extends ClusterAwareIntegrationTests { @@ -117,49 +120,4 @@ void findByIdAudited() { } } - @Configuration - @EnableReactiveCouchbaseRepositories("org.springframework.data.couchbase") - @EnableReactiveCouchbaseAuditing(dateTimeProviderRef = "dateTimeProviderRef") - 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)); - } - } - - @Bean(name = "auditorAwareRef") - public ReactiveNaiveAuditorAware testAuditorAware() { - return new ReactiveNaiveAuditorAware(); - } - - @Bean(name = "dateTimeProviderRef") - public DateTimeProvider testDateTimeProvider() { - return new AuditingDateTimeProvider(); - } - - } - } diff --git a/src/test/java/org/springframework/data/couchbase/repository/ReactiveCouchbaseRepositoryQueryIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/ReactiveCouchbaseRepositoryQueryIntegrationTests.java index 42560e8f1..c6e96c99a 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/ReactiveCouchbaseRepositoryQueryIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/ReactiveCouchbaseRepositoryQueryIntegrationTests.java @@ -22,6 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.springframework.data.couchbase.domain.Config; +import org.springframework.test.annotation.DirtiesContext; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -66,7 +68,8 @@ * @author Michael Nitschinger * @author Michael Reiche */ -@SpringJUnitConfig(ReactiveCouchbaseRepositoryQueryIntegrationTests.Config.class) +@SpringJUnitConfig(Config.class) +@DirtiesContext @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) public class ReactiveCouchbaseRepositoryQueryIntegrationTests extends JavaIntegrationTests { @@ -295,37 +298,4 @@ void deleteOne() { } } - @Configuration - @EnableReactiveCouchbaseRepositories("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)); - } - } - } - } diff --git a/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java index 7aed5d2e6..634a4f78c 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java @@ -30,6 +30,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.DataRetrievalFailureException; import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; @@ -39,7 +40,7 @@ import org.springframework.data.couchbase.domain.Airport; import org.springframework.data.couchbase.domain.AirportRepository; import org.springframework.data.couchbase.domain.AirportRepositoryAnnotated; -import org.springframework.data.couchbase.domain.CollectionsConfig; +import org.springframework.data.couchbase.domain.ConfigScoped; import org.springframework.data.couchbase.domain.User; import org.springframework.data.couchbase.domain.UserCol; import org.springframework.data.couchbase.domain.UserColRepository; @@ -51,6 +52,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.IndexFailureException; @@ -64,7 +66,8 @@ * @author Michael Reiche */ @IgnoreWhen(missesCapabilities = { Capabilities.QUERY, Capabilities.COLLECTIONS }, clusterTypes = ClusterType.MOCKED) -@SpringJUnitConfig(CollectionsConfig.class) +@SpringJUnitConfig(ConfigScoped.class) +@DirtiesContext public class CouchbaseRepositoryQueryCollectionIntegrationTests extends CollectionAwareIntegrationTests { @Autowired AirportRepositoryAnnotated airportRepositoryAnnotated; diff --git a/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQuerydslIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQuerydslIntegrationTests.java index 18c56438a..174e74b35 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQuerydslIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQuerydslIntegrationTests.java @@ -17,9 +17,9 @@ package org.springframework.data.couchbase.repository.query; import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.springframework.data.couchbase.util.Util.comprises; import static org.springframework.data.couchbase.util.Util.exactly; @@ -31,6 +31,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -49,6 +50,7 @@ import org.springframework.data.couchbase.domain.QAirline; import org.springframework.data.couchbase.domain.time.AuditingDateTimeProvider; import org.springframework.data.couchbase.repository.auditing.EnableCouchbaseAuditing; +import org.springframework.data.couchbase.repository.auditing.EnableReactiveCouchbaseAuditing; import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; import org.springframework.data.couchbase.repository.support.BasicQuery; import org.springframework.data.couchbase.repository.support.SpringDataCouchbaseSerializer; @@ -57,13 +59,13 @@ import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.data.couchbase.util.JavaIntegrationTests; import org.springframework.data.domain.Sort; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import com.couchbase.client.core.deps.io.netty.handler.ssl.util.InsecureTrustManagerFactory; import com.couchbase.client.core.env.SecurityConfig; import com.couchbase.client.java.env.ClusterEnvironment; -import com.couchbase.client.java.query.QueryScanConsistency; import com.querydsl.core.types.Predicate; import com.querydsl.core.types.dsl.BooleanExpression; @@ -74,6 +76,7 @@ * @author Tigran Babloyan */ @SpringJUnitConfig(CouchbaseRepositoryQuerydslIntegrationTests.Config.class) +@DirtiesContext @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) public class CouchbaseRepositoryQuerydslIntegrationTests extends JavaIntegrationTests { @@ -92,7 +95,15 @@ public class CouchbaseRepositoryQuerydslIntegrationTests extends JavaIntegration static Airline sleepByDay = new Airline("1002", "Sleep By Day", "CA"); static Airline[] notSaved = new Airline[] { flyByNight, sleepByDay }; - SpringDataCouchbaseSerializer serializer = new SpringDataCouchbaseSerializer(couchbaseTemplate.getConverter()); + @Autowired CouchbaseTemplate couchbaseTemplate; + + SpringDataCouchbaseSerializer serializer = null; + + @BeforeEach + public void beforeEach() { + super.beforeEach(); + serializer = new SpringDataCouchbaseSerializer(couchbaseTemplate.getConverter()); + } @BeforeAll static public void beforeAll() { @@ -104,6 +115,7 @@ static public void beforeAll() { template.insertById(Airline.class).one(airline); } template.findByQuery(Airline.class).withConsistency(REQUEST_PLUS).all(); + logDisconnect(template.getCouchbaseClientFactory().getCluster(), "queryDsl-before"); } @AfterAll @@ -115,6 +127,7 @@ static public void afterAll() { template.removeById(Airline.class).one(airline.getId()); } template.findByQuery(Airline.class).withConsistency(REQUEST_PLUS).all(); + logDisconnect(template.getCouchbaseClientFactory().getCluster(), "queryDsl-after"); callSuperAfterAll(new Object() {}); } @@ -611,56 +624,12 @@ void testStringLength() { } } - private void sleep(int millis) { - try { - Thread.sleep(millis); // so they are executed out-of-order - } catch (InterruptedException ie) { - ; - } - } - @Configuration @EnableCouchbaseRepositories("org.springframework.data.couchbase") - @EnableCouchbaseAuditing(dateTimeProviderRef = "dateTimeProviderRef") - 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(); - } - - @Bean(name = "auditorAwareRef") - public NaiveAuditorAware testAuditorAware() { - return new NaiveAuditorAware(); - } - - @Override - public void configureEnvironment(final ClusterEnvironment.Builder builder) { - if (config().isUsingCloud()) { - builder.securityConfig( - SecurityConfig.builder().trustManagerFactory(InsecureTrustManagerFactory.INSTANCE).enableTls(true)); - } - } + @EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") + @EnableReactiveCouchbaseAuditing(auditorAwareRef = "reactiveAuditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") - @Bean(name = "dateTimeProviderRef") - public DateTimeProvider testDateTimeProvider() { - return new AuditingDateTimeProvider(); - } + static class Config extends org.springframework.data.couchbase.domain.Config { @Bean public LocalValidatorFactoryBean validator() { @@ -678,52 +647,4 @@ String bq(Predicate predicate) { return basicQuery.export(new int[1]); } - @Configuration - @EnableCouchbaseRepositories("org.springframework.data.couchbase") - @EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") - static class ConfigRequestPlus 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)); - } - } - - @Bean(name = "auditorAwareRef") - public NaiveAuditorAware testAuditorAware() { - return new NaiveAuditorAware(); - } - - @Bean(name = "dateTimeProviderRef") - public DateTimeProvider testDateTimeProvider() { - return new AuditingDateTimeProvider(); - } - - @Override - public QueryScanConsistency getDefaultConsistency() { - return REQUEST_PLUS; - } - } } diff --git a/src/test/java/org/springframework/data/couchbase/repository/query/ReactiveCouchbaseRepositoryQueryCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/query/ReactiveCouchbaseRepositoryQueryCollectionIntegrationTests.java index 6f41d2070..1d224024c 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/query/ReactiveCouchbaseRepositoryQueryCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/query/ReactiveCouchbaseRepositoryQueryCollectionIntegrationTests.java @@ -30,7 +30,7 @@ import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; import org.springframework.data.couchbase.domain.Airport; -import org.springframework.data.couchbase.domain.CollectionsConfig; +import org.springframework.data.couchbase.domain.ConfigScoped;; import org.springframework.data.couchbase.domain.ReactiveAirportRepository; import org.springframework.data.couchbase.domain.ReactiveAirportRepositoryAnnotated; import org.springframework.data.couchbase.domain.ReactiveUserColRepository; @@ -40,6 +40,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.IndexFailureException; @@ -53,8 +54,9 @@ * * @author Michael Reiche */ -@SpringJUnitConfig(CollectionsConfig.class) +@SpringJUnitConfig(ConfigScoped.class) @IgnoreWhen(missesCapabilities = { Capabilities.QUERY, Capabilities.COLLECTIONS }, clusterTypes = ClusterType.MOCKED) +@DirtiesContext public class ReactiveCouchbaseRepositoryQueryCollectionIntegrationTests extends CollectionAwareIntegrationTests { @Autowired ReactiveAirportRepository reactiveAirportRepository; diff --git a/src/test/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreatorIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreatorIntegrationTests.java index 3e35d8699..7d4567794 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreatorIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreatorIntegrationTests.java @@ -22,11 +22,8 @@ import java.util.Properties; import java.util.UUID; -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.config.AbstractCouchbaseConfiguration; import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.ExecutableFindByQueryOperation.ExecutableFindByQuery; import org.springframework.data.couchbase.core.convert.CouchbaseConverter; @@ -34,8 +31,8 @@ import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty; import org.springframework.data.couchbase.core.query.Query; import org.springframework.data.couchbase.domain.Airline; +import org.springframework.data.couchbase.domain.Config; import org.springframework.data.couchbase.domain.AirlineRepository; -import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; import org.springframework.data.couchbase.util.Capabilities; import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests; import org.springframework.data.couchbase.util.ClusterType; @@ -51,19 +48,18 @@ import org.springframework.data.repository.query.ParametersParameterAccessor; import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider; import org.springframework.expression.spel.standard.SpelExpressionParser; +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; -import com.couchbase.client.core.env.SecurityConfig; -import com.couchbase.client.java.env.ClusterEnvironment; import com.couchbase.client.java.query.QueryScanConsistency; /** * @author Michael Nitschinger * @author Michael Reiche */ -@SpringJUnitConfig(StringN1qlQueryCreatorIntegrationTests.Config.class) +@SpringJUnitConfig(Config.class) @IgnoreWhen(clusterTypes = ClusterType.MOCKED) +@DirtiesContext class StringN1qlQueryCreatorIntegrationTests extends ClusterAwareIntegrationTests { @Autowired MappingContext, CouchbasePersistentProperty> context; @@ -71,9 +67,6 @@ class StringN1qlQueryCreatorIntegrationTests extends ClusterAwareIntegrationTest @Autowired CouchbaseTemplate couchbaseTemplate; static NamedQueries namedQueries = new PropertiesBasedNamedQueries(new Properties()); - @BeforeEach - public void beforeEach() {} - @Test @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) void findUsingStringNq1l() throws Exception { @@ -148,42 +141,10 @@ private ParameterAccessor getAccessor(Parameters params, Object... values) return new DefaultParameters(method); } - @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)); - } - } - - @Override - protected boolean autoIndexCreation() { - return true; - } - - } + // static class Config extends org.springframework.data.couchbase.domain.Config { + // @Override + // protected boolean autoIndexCreation() { + // return true; + // } + // } } diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbasePersonTransactionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbasePersonTransactionIntegrationTests.java index 77aff69d4..a59bb2e18 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbasePersonTransactionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbasePersonTransactionIntegrationTests.java @@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import lombok.Data; +import org.springframework.test.annotation.DirtiesContext; import reactor.core.publisher.Mono; import java.util.List; @@ -66,7 +67,9 @@ * @author Michael Reiche */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) -@SpringJUnitConfig(classes = { TransactionsConfig.class, PersonService.class }) +@SpringJUnitConfig( + classes = { TransactionsConfig.class, PersonService.class }) +@DirtiesContext public class CouchbasePersonTransactionIntegrationTests extends JavaIntegrationTests { // intellij flags "Could not autowire" when config classes are specified with classes={...}. But they are populated. @Autowired CouchbaseClientFactory couchbaseClientFactory; diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbasePersonTransactionReactiveIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbasePersonTransactionReactiveIntegrationTests.java index 37ca01aa7..afe451aff 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbasePersonTransactionReactiveIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbasePersonTransactionReactiveIntegrationTests.java @@ -19,7 +19,7 @@ import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS; import lombok.Data; -import org.springframework.data.couchbase.domain.PersonWithoutVersion; +import org.springframework.test.annotation.DirtiesContext; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -37,6 +37,7 @@ import org.springframework.data.couchbase.core.RemoveResult; import org.springframework.data.couchbase.domain.Person; import org.springframework.data.couchbase.domain.PersonRepository; +import org.springframework.data.couchbase.domain.PersonWithoutVersion; import org.springframework.data.couchbase.domain.ReactivePersonRepository; import org.springframework.data.couchbase.transaction.error.TransactionSystemUnambiguousException; import org.springframework.data.couchbase.transactions.util.TransactionTestUtil; @@ -58,6 +59,7 @@ */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(classes = { TransactionsConfig.class, PersonServiceReactive.class }) +@DirtiesContext public class CouchbasePersonTransactionReactiveIntegrationTests extends JavaIntegrationTests { // intellij flags "Could not autowire" when config classes are specified with classes={...}. But they are populated. @Autowired CouchbaseClientFactory couchbaseClientFactory; @@ -234,4 +236,5 @@ public EventLog(String id, String action) { String action; @Version Long version; } + } diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseReactiveTransactionNativeIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseReactiveTransactionNativeIntegrationTests.java index 909f1d502..2c35ab3a6 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseReactiveTransactionNativeIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseReactiveTransactionNativeIntegrationTests.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.springframework.test.annotation.DirtiesContext; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -55,6 +56,7 @@ */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(TransactionsConfig.class) +@DirtiesContext public class CouchbaseReactiveTransactionNativeIntegrationTests extends JavaIntegrationTests { @Autowired CouchbaseClientFactory couchbaseClientFactory; @@ -228,4 +230,5 @@ public Mono savePerson(Person person) { .as(txOperator::transactional); } + static public class TransactionsConfig extends org.springframework.data.couchbase.transactions.TransactionsConfig {} } diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionNativeIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionNativeIntegrationTests.java index d8f9b8f43..f54b14106 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionNativeIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionNativeIntegrationTests.java @@ -41,6 +41,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.junit.jupiter.SpringJUnitConfig; import org.springframework.transaction.TransactionManager; import org.springframework.transaction.reactive.TransactionalOperator; @@ -54,6 +55,7 @@ */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(TransactionsConfig.class) +@DirtiesContext // I think these are all redundant (see CouchbaseReactiveTransactionNativeTests). There does not seem to be a blocking // form of TransactionalOperator. Also there does not seem to be a need for a CouchbaseTransactionalOperator as // TransactionalOperator.create(reactiveCouchbaseTransactionManager) seems to work just fine. (I don't recall what @@ -185,4 +187,5 @@ public void replacePersonRbSpringTransactional() { assertEquals(person.getFirstname(), pFound.getFirstname(), "firstname should be Walter"); } + static public class TransactionsConfig extends org.springframework.data.couchbase.transactions.TransactionsConfig {} } diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalNonAllowableOperationsIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalNonAllowableOperationsIntegrationTests.java index 1241933e0..783513113 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalNonAllowableOperationsIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalNonAllowableOperationsIntegrationTests.java @@ -37,8 +37,8 @@ import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.data.couchbase.util.JavaIntegrationTests; import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; /** @@ -50,6 +50,7 @@ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(classes = { TransactionsConfig.class, CouchbaseTransactionalNonAllowableOperationsIntegrationTests.PersonService.class }) +@DirtiesContext public class CouchbaseTransactionalNonAllowableOperationsIntegrationTests extends JavaIntegrationTests { @Autowired CouchbaseClientFactory couchbaseClientFactory; @@ -127,4 +128,5 @@ public T doInTransaction(AtomicInteger tryCount, Function callback) { callback.accept(ops); } } + } diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalRepositoryCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalRepositoryCollectionIntegrationTests.java index cbf3649d0..c94bf30e8 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalRepositoryCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalRepositoryCollectionIntegrationTests.java @@ -39,6 +39,7 @@ import org.springframework.data.couchbase.util.CollectionAwareIntegrationTests; import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.transaction.annotation.Transactional; @@ -48,17 +49,17 @@ * @author Michael Reiche */ @IgnoreWhen(clusterTypes = ClusterType.MOCKED) -@SpringJUnitConfig(classes = { TransactionsConfig.class, - CouchbaseTransactionalRepositoryCollectionIntegrationTests.UserService.class }) +@SpringJUnitConfig(classes = { TransactionsConfig.class, CouchbaseTransactionalRepositoryCollectionIntegrationTests.UserService.class }) +@DirtiesContext public class CouchbaseTransactionalRepositoryCollectionIntegrationTests extends CollectionAwareIntegrationTests { // intellij flags "Could not autowire" when config classes are specified with classes={...}. But they are populated. - @Autowired UserColRepository userRepo; - @Autowired UserService userService; + @Autowired UserColRepository userRepo; + @Autowired UserService userService; //@Autowired CouchbaseTemplate operations; @BeforeAll public static void beforeAll() { - callSuperBeforeAll(new Object() {}); + callSuperBeforeAll(new Object() {}); } @BeforeEach @@ -121,7 +122,7 @@ public void saveRolledBack() { @Service // this will work in the unit tests even without @Service because of explicit loading by @SpringJUnitConfig static class UserService { - @Autowired UserColRepository userRepo; + @Autowired UserColRepository userRepo; @Transactional public void run(Consumer callback) { diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalRepositoryIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalRepositoryIntegrationTests.java index 6ce7d2dfc..90e125419 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalRepositoryIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalRepositoryIntegrationTests.java @@ -41,8 +41,8 @@ import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.data.couchbase.util.JavaIntegrationTests; import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; /** @@ -52,7 +52,8 @@ */ @IgnoreWhen(clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig( - classes = { TransactionsConfig.class, CouchbaseTransactionalRepositoryIntegrationTests.UserService.class }) + classes = { TransactionsConfig.class, CouchbaseTransactionalRepositoryIntegrationTests.UserService.class }) +@DirtiesContext public class CouchbaseTransactionalRepositoryIntegrationTests extends JavaIntegrationTests { // intellij flags "Could not autowire" when config classes are specified with classes={...}. But they are populated. @Autowired UserRepository userRepo; diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateCollectionDefaultScopeIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateCollectionDefaultScopeIntegrationTests.java index 946010cd1..e5dc5e10c 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateCollectionDefaultScopeIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateCollectionDefaultScopeIntegrationTests.java @@ -53,6 +53,7 @@ import org.springframework.data.couchbase.util.CollectionAwareDefaultScopeIntegrationTests; import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.transaction.annotation.Transactional; @@ -65,7 +66,8 @@ */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig( - classes = { TransactionsConfig.class, CouchbaseTransactionalTemplateCollectionDefaultScopeIntegrationTests.PersonService.class }) + classes = { TransactionsConfig.class, CouchbaseTransactionalTemplateCollectionDefaultScopeIntegrationTests.PersonService.class }) +@DirtiesContext public class CouchbaseTransactionalTemplateCollectionDefaultScopeIntegrationTests extends CollectionAwareDefaultScopeIntegrationTests { // intellij flags "Could not autowire" when config classes are specified with classes={...}. But they are populated. @Autowired CouchbaseClientFactory couchbaseClientFactory; @@ -496,4 +498,5 @@ static void assertInAnnotationTransaction(boolean inTransaction) { throw new RuntimeException( "in transaction = " + (!inTransaction) + " but expected in annotation transaction = " + inTransaction); } + } diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateCollectionIntegrationTests.java index 52dae913f..0fb5d40fd 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateCollectionIntegrationTests.java @@ -50,10 +50,10 @@ import org.springframework.data.couchbase.transaction.error.TransactionSystemUnambiguousException; import org.springframework.data.couchbase.util.Capabilities; import org.springframework.data.couchbase.util.ClusterType; -import org.springframework.data.couchbase.util.CollectionAwareDefaultScopeIntegrationTests; import org.springframework.data.couchbase.util.CollectionAwareIntegrationTests; import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.transaction.annotation.Transactional; @@ -66,7 +66,8 @@ */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig( - classes = { TransactionsConfig.class, CouchbaseTransactionalTemplateCollectionIntegrationTests.PersonService.class }) + classes = { TransactionsConfig.class, CouchbaseTransactionalTemplateCollectionIntegrationTests.PersonService.class }) +@DirtiesContext public class CouchbaseTransactionalTemplateCollectionIntegrationTests extends CollectionAwareIntegrationTests { // intellij flags "Could not autowire" when config classes are specified with classes={...}. But they are populated. @Autowired CouchbaseClientFactory couchbaseClientFactory; @@ -497,4 +498,5 @@ static void assertInAnnotationTransaction(boolean inTransaction) { throw new RuntimeException( "in transaction = " + (!inTransaction) + " but expected in annotation transaction = " + inTransaction); } + } diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateIntegrationTests.java index 89219517c..d31026c16 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalTemplateIntegrationTests.java @@ -53,8 +53,8 @@ import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.data.couchbase.util.JavaIntegrationTests; import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; 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.error.transaction.AttemptExpiredException; @@ -66,7 +66,8 @@ */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig( - classes = { TransactionsConfig.class, CouchbaseTransactionalTemplateIntegrationTests.PersonService.class }) + classes = { TransactionsConfig.class, CouchbaseTransactionalTemplateIntegrationTests.PersonService.class }) +@DirtiesContext public class CouchbaseTransactionalTemplateIntegrationTests extends JavaIntegrationTests { // intellij flags "Could not autowire" when config classes are specified with classes={...}. But they are populated. @Autowired CouchbaseClientFactory couchbaseClientFactory; @@ -497,4 +498,5 @@ static void assertInAnnotationTransaction(boolean inTransaction) { throw new RuntimeException( "in transaction = " + (!inTransaction) + " but expected in annotation transaction = " + inTransaction); } + } diff --git a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalUnsettableParametersIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalUnsettableParametersIntegrationTests.java index d8c82e52f..2fe92786d 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalUnsettableParametersIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalUnsettableParametersIntegrationTests.java @@ -39,6 +39,7 @@ import org.springframework.data.couchbase.util.IgnoreWhen; import org.springframework.data.couchbase.util.JavaIntegrationTests; import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.transaction.annotation.Transactional; @@ -59,8 +60,8 @@ * @author Tigran Babloyan */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) -@SpringJUnitConfig(classes = { TransactionsConfig.class, - CouchbaseTransactionalUnsettableParametersIntegrationTests.PersonService.class }) +@SpringJUnitConfig(classes = { TransactionsConfig.class, CouchbaseTransactionalUnsettableParametersIntegrationTests.PersonService.class }) +@DirtiesContext public class CouchbaseTransactionalUnsettableParametersIntegrationTests extends JavaIntegrationTests { @Autowired CouchbaseClientFactory couchbaseClientFactory; @@ -265,4 +266,5 @@ public T doInTransaction(AtomicInteger tryCount, Function Flux doInTransactionReturningFlux(AtomicInteger tryCount, }); } } + + static public class TransactionsConfig extends org.springframework.data.couchbase.transactions.TransactionsConfig {} } diff --git a/src/test/java/org/springframework/data/couchbase/transactions/TransactionTemplateIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/TransactionTemplateIntegrationTests.java index 826e7c6fd..66efd8821 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/TransactionTemplateIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/TransactionTemplateIntegrationTests.java @@ -49,6 +49,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.junit.jupiter.SpringJUnitConfig; import org.springframework.transaction.IllegalTransactionStateException; import org.springframework.transaction.TransactionDefinition; @@ -63,6 +64,7 @@ */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(TransactionsConfig.class) +@DirtiesContext public class TransactionTemplateIntegrationTests extends JavaIntegrationTests { @Autowired TransactionTemplate transactionTemplate; @Autowired CouchbaseCallbackTransactionManager transactionManager; diff --git a/src/test/java/org/springframework/data/couchbase/transactions/sdk/SDKReactiveTransactionsNonAllowableOperationsIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/sdk/SDKReactiveTransactionsNonAllowableOperationsIntegrationTests.java index ba88d765e..76e1fd28b 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/sdk/SDKReactiveTransactionsNonAllowableOperationsIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/sdk/SDKReactiveTransactionsNonAllowableOperationsIntegrationTests.java @@ -18,6 +18,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import org.springframework.data.couchbase.transactions.TransactionsConfig; +import org.springframework.test.annotation.DirtiesContext; import reactor.core.publisher.Mono; import java.util.concurrent.atomic.AtomicInteger; @@ -31,7 +33,6 @@ import org.springframework.data.couchbase.CouchbaseClientFactory; import org.springframework.data.couchbase.core.ReactiveCouchbaseOperations; import org.springframework.data.couchbase.domain.Person; -import org.springframework.data.couchbase.transactions.TransactionsConfig; import org.springframework.data.couchbase.transactions.util.TransactionTestUtil; import org.springframework.data.couchbase.util.Capabilities; import org.springframework.data.couchbase.util.ClusterType; @@ -51,6 +52,7 @@ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(classes = { TransactionsConfig.class, SDKReactiveTransactionsNonAllowableOperationsIntegrationTests.PersonService.class }) +@DirtiesContext public class SDKReactiveTransactionsNonAllowableOperationsIntegrationTests extends JavaIntegrationTests { @Autowired CouchbaseClientFactory couchbaseClientFactory; @@ -129,4 +131,5 @@ public Mono doInService(AtomicInteger tryCount, Function T doInService(AtomicInteger tryCount, Function { PersonWithoutVersion p = new PersonWithoutVersion("Walter", "White"); return reactiveOps.save(p).then(assertInReactiveTransaction()); diff --git a/src/test/java/org/springframework/data/couchbase/transactions/sdk/SDKTransactionsTemplateIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/transactions/sdk/SDKTransactionsTemplateIntegrationTests.java index 696af25a3..503e8d6aa 100644 --- a/src/test/java/org/springframework/data/couchbase/transactions/sdk/SDKTransactionsTemplateIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/transactions/sdk/SDKTransactionsTemplateIntegrationTests.java @@ -24,6 +24,8 @@ import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertInTransaction; import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertNotInTransaction; +import org.springframework.data.couchbase.transactions.TransactionsConfig; +import org.springframework.test.annotation.DirtiesContext; import reactor.util.annotation.Nullable; import java.time.Duration; @@ -46,7 +48,6 @@ import org.springframework.data.couchbase.transaction.error.UncategorizedTransactionDataAccessException; import org.springframework.data.couchbase.transactions.ReplaceLoopThread; import org.springframework.data.couchbase.transactions.SimulateFailureException; -import org.springframework.data.couchbase.transactions.TransactionsConfig; import org.springframework.data.couchbase.util.Capabilities; import org.springframework.data.couchbase.util.ClusterType; import org.springframework.data.couchbase.util.IgnoreWhen; @@ -69,6 +70,7 @@ */ @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) @SpringJUnitConfig(TransactionsConfig.class) +@DirtiesContext public class SDKTransactionsTemplateIntegrationTests extends JavaIntegrationTests { // intellij flags "Could not autowire" when config classes are specified with classes={...}. But they are populated. @Autowired CouchbaseClientFactory couchbaseClientFactory; diff --git a/src/test/java/org/springframework/data/couchbase/util/ClusterAwareIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/util/ClusterAwareIntegrationTests.java index 0921e8803..005b621b8 100644 --- a/src/test/java/org/springframework/data/couchbase/util/ClusterAwareIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/util/ClusterAwareIntegrationTests.java @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import java.time.Duration; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; @@ -34,6 +35,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.couchbase.CouchbaseClientFactory; import org.springframework.data.couchbase.SimpleCouchbaseClientFactory; @@ -43,6 +45,7 @@ import com.couchbase.client.core.env.SecurityConfig; import com.couchbase.client.core.env.SeedNode; import com.couchbase.client.core.error.IndexFailureException; +import com.couchbase.client.java.Cluster; import com.couchbase.client.java.env.ClusterEnvironment; import com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions; import com.couchbase.client.java.manager.query.CreateQueryIndexOptions; @@ -61,6 +64,8 @@ public abstract class ClusterAwareIntegrationTests { private static TestClusterConfig testClusterConfig; public static final Logger LOGGER = LoggerFactory.getLogger(ClusterAwareIntegrationTests.class); + @Autowired Cluster cluster; // so we can save it to clusterToDisconnect in @BeforeEach + static public Cluster clusterToDisconnect; // so we can disconnect it in @AfterAll @BeforeAll static void setup(TestClusterConfig config) { @@ -70,8 +75,10 @@ static void setup(TestClusterConfig config) { .transactionsConfig(TransactionsConfig.cleanupConfig(TransactionsCleanupConfig.cleanupLostAttempts(false))) .build(); String connectString = connectionString(); + Cluster tmpCluster = null; try (CouchbaseClientFactory couchbaseClientFactory = new SimpleCouchbaseClientFactory(connectString, authenticator(), bucketName(), null, env)) { + tmpCluster = couchbaseClientFactory.getCluster(); couchbaseClientFactory.getCluster().queryIndexes().createPrimaryIndex(bucketName(), CreatePrimaryQueryIndexOptions .createPrimaryQueryIndexOptions().ignoreIfExists(true).timeout(Duration.ofSeconds(300))); // this is for the N1qlJoin test @@ -80,8 +87,10 @@ static void setup(TestClusterConfig config) { couchbaseClientFactory.getCluster().queryIndexes().createIndex(bucketName(), "parent_idx", fieldList, CreateQueryIndexOptions.createQueryIndexOptions().ignoreIfExists(true).timeout(Duration.ofSeconds(300))); // .with("_class", "org.springframework.data.couchbase.domain.Address")); + logCluster(couchbaseClientFactory.getCluster(), "$"); } catch (IndexFailureException ife) { LOGGER.warn("IndexFailureException occurred - ignoring: ", ife); + logDisconnect(tmpCluster, "IndexFailureException"); } catch (IOException ioe) { throw new RuntimeException(ioe); } @@ -160,17 +169,66 @@ protected static Set seedNodes() { .collect(Collectors.toSet()); } + static Set flagged = new HashSet<>(); + + @AfterEach + public void afterEach() { + if (clusterToDisconnect == null && !flagged.contains(this.getClass().getSimpleName())) { + flagged.add(this.getClass().getSimpleName()); + logMessage("CoreDisconnected:\"coreId\":\"" + this.getClass().getSimpleName() + "\""); + } + } + + @AfterAll + public static void afterAll() { + //if (clusterToDisconnect != null) { + // logDisconnect(clusterToDisconnect, "$"); + // clusterToDisconnect = null; + //} else { + // already flaged by afterEach + //} + logMessage("CoreDisconnected:\"coreId\":\"------------------\""); + callSuperAfterAll(new Object() {}); + } + + static Set disconnectedMap = new HashSet<>(); + + public static void logCluster(Cluster cluster, String s) { + if (cluster == null || disconnectedMap.contains(cluster.core().context().id())) { + org.slf4j.LoggerFactory.getLogger("com.couchbase.core").info("CoreDisconnectedAutoAlready:\"coreId\":\"" + + String.format("0x%16x", 0) + "auto missed<" + s + ">\""); + } else { + disconnectedMap.add(cluster.core().context().id()); + org.slf4j.LoggerFactory.getLogger("com.couchbase.core").info("CoreDisconnectedAuto:\"coreId\":\"" + + String.format("0x%x", cluster.core().context().id()) + "(" + s + ")\""); + //cluster.environment().shutdown(Duration.ofSeconds(60)); needs to happend after auto disconnect() + } + } + + public static void logDisconnect(Cluster cluster, String s) { + if (cluster == null || disconnectedMap.contains(cluster.core().context().id())) { + org.slf4j.LoggerFactory.getLogger("com.couchbase.core").info( + "CoreDisconnectedAlready:\"coreId\":\"" + String.format("0x%16x", 0) + " missed{" + s + "}\""); + } else { + disconnectedMap.add(cluster.core().context().id()); + org.slf4j.LoggerFactory.getLogger("com.couchbase.core").info("CoreDisconnected:\"coreId\":\"" + + String.format("0x%x", cluster.core().context().id()) + "[" + s + "]\""); + cluster.disconnect(); + cluster.environment().shutdown(Duration.ofSeconds(60)); + } + } + + public static void logMessage(String message) { + org.slf4j.LoggerFactory.getLogger("com.couchbase.core").info(message); + } + @BeforeAll() public static void beforeAll() {} - @AfterAll - public static void afterAll() {} - @BeforeEach - public void beforeEach() {} - - @AfterEach - public void afterEach() {} + public void beforeEach() { + clusterToDisconnect = cluster; + } /** * This should probably be the first call in the @BeforeAll method of a test class. This will call super @BeforeAll diff --git a/src/test/java/org/springframework/data/couchbase/util/CollectionAwareDefaultScopeIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/util/CollectionAwareDefaultScopeIntegrationTests.java index f84b38ec2..6d71d108f 100644 --- a/src/test/java/org/springframework/data/couchbase/util/CollectionAwareDefaultScopeIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/util/CollectionAwareDefaultScopeIntegrationTests.java @@ -15,19 +15,12 @@ */ package org.springframework.data.couchbase.util; -import static org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE; -import static org.springframework.data.couchbase.config.BeanNames.REACTIVE_COUCHBASE_TEMPLATE; - import java.time.Duration; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.data.couchbase.core.CouchbaseTemplate; -import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; import org.springframework.data.couchbase.domain.Config; import com.couchbase.client.core.error.IndexExistsException; @@ -42,7 +35,7 @@ * * @Author Michael Reiche */ -public class CollectionAwareDefaultScopeIntegrationTests extends CollectionAwareIntegrationTests { +public class CollectionAwareDefaultScopeIntegrationTests extends JavaIntegrationTests { public static String scopeName = "_default";// + randomString(); public static String collectionName = "my_collection";// + randomString(); @@ -56,7 +49,7 @@ public static void beforeAll() { callSuperBeforeAll(new Object() { }); Cluster cluster = Cluster.connect(connectionString(), - ClusterOptions.clusterOptions(authenticator()).environment(environment().build())); + ClusterOptions.clusterOptions(authenticator()).environment(environment().build())); Bucket bucket = cluster.bucket(config().bucketname()); bucket.waitUntilReady(Duration.ofSeconds(30)); waitForService(bucket, ServiceType.QUERY); @@ -78,25 +71,26 @@ public static void beforeAll() { List fieldList = new ArrayList<>(); fieldList.add("parentId"); cluster.query("CREATE INDEX `parent_idx` ON default:`" + bucketName() + "`." + scopeName + "." + collectionName2 - + "(parentId)"); + + "(parentId)"); } catch (IndexExistsException ife) { LOGGER.warn("IndexFailureException occurred - ignoring: ", ife.toString()); } - + logDisconnect(cluster, CollectionAwareDefaultScopeIntegrationTests.class.getSimpleName()); Config.setScopeName(scopeName); - ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); + // ApplicationContext ac = new AnnotationConfigApplicationContext(); + // System.out.println(ac); // the Config class has been modified, these need to be loaded again - couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); - reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE); + // couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); + // reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE); } @AfterAll public static void afterAll() { Config.setScopeName(null); - ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); + // ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); // the Config class has been modified, these need to be loaded again - couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); - reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE); + // couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); + // reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE); callSuperAfterAll(new Object() { }); } diff --git a/src/test/java/org/springframework/data/couchbase/util/CollectionAwareIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/util/CollectionAwareIntegrationTests.java index 9879c2b47..a21491e87 100644 --- a/src/test/java/org/springframework/data/couchbase/util/CollectionAwareIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/util/CollectionAwareIntegrationTests.java @@ -15,19 +15,12 @@ */ package org.springframework.data.couchbase.util; -import static org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE; -import static org.springframework.data.couchbase.config.BeanNames.REACTIVE_COUCHBASE_TEMPLATE; - import java.time.Duration; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.data.couchbase.core.CouchbaseTemplate; -import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; import org.springframework.data.couchbase.domain.Config; import com.couchbase.client.core.error.IndexExistsException; @@ -80,21 +73,22 @@ public static void beforeAll() { } catch (IndexExistsException ife) { LOGGER.warn("IndexFailureException occurred - ignoring: ", ife.toString()); } - + logDisconnect(cluster, CollectionAwareIntegrationTests.class.getSimpleName()); Config.setScopeName(scopeName); - ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); + // ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); + // System.out.println(ac); // the Config class has been modified, these need to be loaded again - couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); - reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE); + // couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); + // reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE); } @AfterAll public static void afterAll() { Config.setScopeName(null); - ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); + // ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); // the Config class has been modified, these need to be loaded again - couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); - reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE); + // couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); + // reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE); callSuperAfterAll(new Object() {}); } } diff --git a/src/test/java/org/springframework/data/couchbase/util/JavaIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/util/JavaIntegrationTests.java index a10b485e5..34585bad6 100644 --- a/src/test/java/org/springframework/data/couchbase/util/JavaIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/util/JavaIntegrationTests.java @@ -23,8 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE; -import static org.springframework.data.couchbase.config.BeanNames.REACTIVE_COUCHBASE_TEMPLATE; import static org.springframework.data.couchbase.util.Util.waitUntilCondition; import java.io.IOException; @@ -44,12 +42,8 @@ import org.junit.jupiter.api.function.Executable; import org.junit.platform.commons.util.UnrecoverableExceptions; import org.opentest4j.AssertionFailedError; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.couchbase.CouchbaseClientFactory; import org.springframework.data.couchbase.SimpleCouchbaseClientFactory; -import org.springframework.data.couchbase.core.CouchbaseTemplate; -import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; import org.springframework.data.couchbase.domain.Config; import org.springframework.data.couchbase.transactions.SimulateFailureException; @@ -85,6 +79,8 @@ import com.couchbase.client.java.search.SearchQuery; import com.couchbase.client.java.search.result.SearchResult; +; + /** * Extends the {@link ClusterAwareIntegrationTests} with java-client specific code. * @@ -94,21 +90,15 @@ @Timeout(value = 10, unit = TimeUnit.MINUTES) // Safety timer so tests can't block CI executors public class JavaIntegrationTests extends ClusterAwareIntegrationTests { - // Autowired annotation is not supported on static fields - static public CouchbaseTemplate couchbaseTemplate; - static public ReactiveCouchbaseTemplate reactiveCouchbaseTemplate; - @BeforeAll public static void beforeAll() { Config.setScopeName(null); callSuperBeforeAll(new Object() {}); - ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); - couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); - reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE); try (CouchbaseClientFactory couchbaseClientFactory = new SimpleCouchbaseClientFactory(connectionString(), authenticator(), bucketName())) { couchbaseClientFactory.getCluster().queryIndexes().createPrimaryIndex(bucketName(), CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions().ignoreIfExists(true)); + logCluster(couchbaseClientFactory.getCluster(), "-"); } catch (IOException ioe) { throw new RuntimeException(ioe); } diff --git a/src/test/java/org/springframework/data/couchbase/util/UnmanagedTestCluster.java b/src/test/java/org/springframework/data/couchbase/util/UnmanagedTestCluster.java index 78e378756..d7b5b4bbe 100644 --- a/src/test/java/org/springframework/data/couchbase/util/UnmanagedTestCluster.java +++ b/src/test/java/org/springframework/data/couchbase/util/UnmanagedTestCluster.java @@ -54,6 +54,7 @@ public class UnmanagedTestCluster extends TestCluster { private final String hostname; private volatile String bucketname; private long startTime = System.currentTimeMillis(); + private boolean usingCloud; UnmanagedTestCluster(final Properties properties) { String seed = properties.getProperty("cluster.unmanaged.seed"); @@ -74,6 +75,7 @@ public class UnmanagedTestCluster extends TestCluster { } adminUsername = properties.getProperty("cluster.adminUsername"); adminPassword = properties.getProperty("cluster.adminPassword"); + bucketname = properties.getProperty("cluster.unmanaged.bucket"); numReplicas = Integer.parseInt(properties.getProperty("cluster.unmanaged.numReplicas")); HandshakeCertificates clientCertificates = new HandshakeCertificates.Builder().addPlatformTrustedCertificates() @@ -93,8 +95,11 @@ ClusterType type() { TestClusterConfig _start() throws Exception { // no means to create a bucket on Capella // have not created config() yet. - boolean usingCloud = seedHost.endsWith("cloud.couchbase.com"); - bucketname = usingCloud ? "my_bucket" : UUID.randomUUID().toString(); + usingCloud = seedHost.endsWith("cloud.couchbase.com"); + if (usingCloud && bucketname == null) { + throw new RuntimeException("cloud must use an existing bucket. Must specify cluster.unmanaged.bucket"); + } + bucketname = bucketname != null ? bucketname : UUID.randomUUID().toString(); if (!usingCloud) { Response postResponse = httpClient .newCall(new Request.Builder().header("Authorization", Credentials.basic(adminUsername, adminPassword)) @@ -177,7 +182,7 @@ private void waitUntilAllNodesHealthy() throws Exception { @Override public void close() { try { - if (!bucketname.equals("my_bucket")) { + if (!bucketname.equals("my_bucket") && !usingCloud) { httpClient .newCall(new Request.Builder().header("Authorization", Credentials.basic(adminUsername, adminPassword)) .url(protocol + "://" + hostname + ":" + seedPort + "/pools/default/buckets/" + bucketname).delete()