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
|
||||
|
||||
import com.couchbase.client.java.env.CouchbaseEnvironment
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration
|
||||
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull
|
||||
|
||||
@Configuration
|
||||
@EnableCouchbaseRepositories(basePackages = "springdata")
|
||||
@ComponentScan(basePackages = "springdata")
|
||||
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
|
||||
protected List<String> getBootstrapHosts() {
|
||||
return Collections.singletonList("127.0.0.1")
|
||||
|
@ -24,4 +35,5 @@ class CouchbaseConfig extends AbstractCouchbaseConfiguration {
|
|||
protected String getBucketPassword() {
|
||||
return "test-pass"
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package springdata
|
||||
|
||||
|
||||
import org.springframework.context.ApplicationContext
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||
import org.springframework.data.repository.CrudRepository
|
||||
|
@ -29,6 +30,8 @@ class CouchbaseSpringRepositoryTest extends AbstractCouchbaseTest {
|
|||
DocRepository repo
|
||||
|
||||
def setupSpec() {
|
||||
CouchbaseConfig.setEnvironment(environment)
|
||||
|
||||
// Close all buckets and 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.UserRole
|
||||
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.query.Index
|
||||
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.
|
||||
// Lets automatically retry to avoid the test from failing completely.
|
||||
@RetryOnFailure(delaySeconds = 1)
|
||||
class AbstractCouchbaseTest extends AgentTestRunner {
|
||||
abstract class AbstractCouchbaseTest extends AgentTestRunner {
|
||||
|
||||
private static final USERNAME = "Administrator"
|
||||
private static final PASSWORD = "password"
|
||||
|
@ -85,6 +86,8 @@ class AbstractCouchbaseTest extends AgentTestRunner {
|
|||
@Shared
|
||||
protected CouchbaseCluster cluster
|
||||
@Shared
|
||||
protected CouchbaseEnvironment environment
|
||||
@Shared
|
||||
protected ClusterManager manager
|
||||
|
||||
def setupSpec() {
|
||||
|
@ -99,20 +102,20 @@ class AbstractCouchbaseTest extends AgentTestRunner {
|
|||
clusterUsername: USERNAME,
|
||||
clusterPassword: PASSWORD)
|
||||
couchbaseContainer.start()
|
||||
environment = couchbaseContainer.getCouchbaseEnvironment()
|
||||
cluster = couchbaseContainer.getCouchbaseCluster()
|
||||
println "Couchbase container started"
|
||||
} else {
|
||||
initCluster()
|
||||
cluster = CouchbaseCluster.create(envBuilder().build())
|
||||
environment = envBuilder().build()
|
||||
cluster = CouchbaseCluster.create(environment)
|
||||
println "Using provided couchbase"
|
||||
}
|
||||
manager = cluster.clusterManager(USERNAME, PASSWORD)
|
||||
|
||||
if (!testBucketName.contains(AbstractCouchbaseTest.simpleName)) {
|
||||
resetBucket(cluster, bucketCouchbase)
|
||||
resetBucket(cluster, bucketMemcache)
|
||||
resetBucket(cluster, bucketEphemeral)
|
||||
}
|
||||
resetBucket(cluster, bucketCouchbase)
|
||||
resetBucket(cluster, bucketMemcache)
|
||||
resetBucket(cluster, bucketEphemeral)
|
||||
}
|
||||
|
||||
def cleanupSpec() {
|
||||
|
|
Loading…
Reference in New Issue