Configure Spring Couchbase client with proper couchbase environment
It looks like Couchbase container may have some of the ports mapped randomly. This means it is possible default environment settings might not work - and this is what Spring is using by default. This change makes Spring use environment provided by cluster container. This fixes tests when they are run locally (at least for macos).
This commit is contained in:
parent
2c4366cae7
commit
422ded1d5b
|
@ -1,15 +1,26 @@
|
||||||
package springdata
|
package springdata
|
||||||
|
|
||||||
|
import com.couchbase.client.java.env.CouchbaseEnvironment
|
||||||
import org.springframework.context.annotation.ComponentScan
|
import org.springframework.context.annotation.ComponentScan
|
||||||
import org.springframework.context.annotation.Configuration
|
import org.springframework.context.annotation.Configuration
|
||||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration
|
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration
|
||||||
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories
|
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableCouchbaseRepositories(basePackages = "springdata")
|
@EnableCouchbaseRepositories(basePackages = "springdata")
|
||||||
@ComponentScan(basePackages = "springdata")
|
@ComponentScan(basePackages = "springdata")
|
||||||
class CouchbaseConfig extends AbstractCouchbaseConfiguration {
|
class CouchbaseConfig extends AbstractCouchbaseConfiguration {
|
||||||
|
|
||||||
|
// This needs to be set before this class can be used by Spring
|
||||||
|
static CouchbaseEnvironment environment
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CouchbaseEnvironment getEnvironment() {
|
||||||
|
return checkNotNull(environment)
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<String> getBootstrapHosts() {
|
protected List<String> getBootstrapHosts() {
|
||||||
return Collections.singletonList("127.0.0.1")
|
return Collections.singletonList("127.0.0.1")
|
||||||
|
@ -24,4 +35,5 @@ class CouchbaseConfig extends AbstractCouchbaseConfiguration {
|
||||||
protected String getBucketPassword() {
|
protected String getBucketPassword() {
|
||||||
return "test-pass"
|
return "test-pass"
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package springdata
|
package springdata
|
||||||
|
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext
|
import org.springframework.context.ApplicationContext
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||||
import org.springframework.data.repository.CrudRepository
|
import org.springframework.data.repository.CrudRepository
|
||||||
|
@ -29,6 +30,8 @@ class CouchbaseSpringRepositoryTest extends AbstractCouchbaseTest {
|
||||||
DocRepository repo
|
DocRepository repo
|
||||||
|
|
||||||
def setupSpec() {
|
def setupSpec() {
|
||||||
|
CouchbaseConfig.setEnvironment(environment)
|
||||||
|
|
||||||
// Close all buckets and disconnect
|
// Close all buckets and disconnect
|
||||||
cluster.disconnect()
|
cluster.disconnect()
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ import com.couchbase.client.java.cluster.ClusterManager
|
||||||
import com.couchbase.client.java.cluster.DefaultBucketSettings
|
import com.couchbase.client.java.cluster.DefaultBucketSettings
|
||||||
import com.couchbase.client.java.cluster.UserRole
|
import com.couchbase.client.java.cluster.UserRole
|
||||||
import com.couchbase.client.java.cluster.UserSettings
|
import com.couchbase.client.java.cluster.UserSettings
|
||||||
|
import com.couchbase.client.java.env.CouchbaseEnvironment
|
||||||
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment
|
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment
|
||||||
import com.couchbase.client.java.query.Index
|
import com.couchbase.client.java.query.Index
|
||||||
import com.couchbase.client.java.view.DefaultView
|
import com.couchbase.client.java.view.DefaultView
|
||||||
|
@ -40,7 +41,7 @@ import java.util.concurrent.TimeUnit
|
||||||
// Couchbase client sometimes throws com.couchbase.client.java.error.TemporaryFailureException.
|
// Couchbase client sometimes throws com.couchbase.client.java.error.TemporaryFailureException.
|
||||||
// Lets automatically retry to avoid the test from failing completely.
|
// Lets automatically retry to avoid the test from failing completely.
|
||||||
@RetryOnFailure(delaySeconds = 1)
|
@RetryOnFailure(delaySeconds = 1)
|
||||||
class AbstractCouchbaseTest extends AgentTestRunner {
|
abstract class AbstractCouchbaseTest extends AgentTestRunner {
|
||||||
|
|
||||||
private static final USERNAME = "Administrator"
|
private static final USERNAME = "Administrator"
|
||||||
private static final PASSWORD = "password"
|
private static final PASSWORD = "password"
|
||||||
|
@ -85,6 +86,8 @@ class AbstractCouchbaseTest extends AgentTestRunner {
|
||||||
@Shared
|
@Shared
|
||||||
protected CouchbaseCluster cluster
|
protected CouchbaseCluster cluster
|
||||||
@Shared
|
@Shared
|
||||||
|
protected CouchbaseEnvironment environment
|
||||||
|
@Shared
|
||||||
protected ClusterManager manager
|
protected ClusterManager manager
|
||||||
|
|
||||||
def setupSpec() {
|
def setupSpec() {
|
||||||
|
@ -99,21 +102,21 @@ class AbstractCouchbaseTest extends AgentTestRunner {
|
||||||
clusterUsername: USERNAME,
|
clusterUsername: USERNAME,
|
||||||
clusterPassword: PASSWORD)
|
clusterPassword: PASSWORD)
|
||||||
couchbaseContainer.start()
|
couchbaseContainer.start()
|
||||||
|
environment = couchbaseContainer.getCouchbaseEnvironment()
|
||||||
cluster = couchbaseContainer.getCouchbaseCluster()
|
cluster = couchbaseContainer.getCouchbaseCluster()
|
||||||
println "Couchbase container started"
|
println "Couchbase container started"
|
||||||
} else {
|
} else {
|
||||||
initCluster()
|
initCluster()
|
||||||
cluster = CouchbaseCluster.create(envBuilder().build())
|
environment = envBuilder().build()
|
||||||
|
cluster = CouchbaseCluster.create(environment)
|
||||||
println "Using provided couchbase"
|
println "Using provided couchbase"
|
||||||
}
|
}
|
||||||
manager = cluster.clusterManager(USERNAME, PASSWORD)
|
manager = cluster.clusterManager(USERNAME, PASSWORD)
|
||||||
|
|
||||||
if (!testBucketName.contains(AbstractCouchbaseTest.simpleName)) {
|
|
||||||
resetBucket(cluster, bucketCouchbase)
|
resetBucket(cluster, bucketCouchbase)
|
||||||
resetBucket(cluster, bucketMemcache)
|
resetBucket(cluster, bucketMemcache)
|
||||||
resetBucket(cluster, bucketEphemeral)
|
resetBucket(cluster, bucketEphemeral)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
def cleanupSpec() {
|
def cleanupSpec() {
|
||||||
// Close all buckets and disconnect
|
// Close all buckets and disconnect
|
||||||
|
|
Loading…
Reference in New Issue