Browse Source
This commit separates the basic setup of Couchbase from Spring Data so that a `Bucket` and `Cluster` bucket beans are exposed even if Spring Data is not available. A basic setup happens if `spring.couchbase.bootstrap-hosts` is specified, configuring the `default` bucket with no authentication unless specified otherwise. If Spring Data is available, those beans are re-used by default to configure the `CouchbaseTemplate` and other repository-related beans. Closes gh-5347pull/5337/merge
20 changed files with 835 additions and 258 deletions
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.data.couchbase; |
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration.CouchbaseConfiguration; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.data.couchbase.config.CouchbaseConfigurer; |
||||
|
||||
/** |
||||
* Adapt the core Couchbase configuration to an expected {@link CouchbaseConfigurer} |
||||
* if necessary. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
@Configuration |
||||
@ConditionalOnClass(CouchbaseConfigurer.class) |
||||
@ConditionalOnBean(CouchbaseConfiguration.class) |
||||
class CouchbaseConfigurerAdapterConfiguration { |
||||
|
||||
private final CouchbaseConfiguration configuration; |
||||
|
||||
CouchbaseConfigurerAdapterConfiguration(CouchbaseConfiguration configuration) { |
||||
this.configuration = configuration; |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public CouchbaseConfigurer springBootCouchbaseConfigurer() throws Exception { |
||||
return new SpringBootCouchbaseConfigurer(this.configuration.couchbaseEnvironment(), |
||||
this.configuration.couchbaseCluster(), this.configuration.couchbaseClusterInfo(), |
||||
this.configuration.couchbaseClient()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,63 @@
@@ -0,0 +1,63 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.data.couchbase; |
||||
|
||||
import javax.validation.Validator; |
||||
|
||||
import com.couchbase.client.java.Bucket; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; |
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener; |
||||
import org.springframework.data.couchbase.repository.CouchbaseRepository; |
||||
|
||||
/** |
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Couchbase support. |
||||
* |
||||
* @author Eddú Meléndez |
||||
* @author Stephane Nicoll |
||||
* @since 1.4.0 |
||||
*/ |
||||
@Configuration |
||||
@ConditionalOnClass({Bucket.class, CouchbaseRepository.class}) |
||||
@AutoConfigureAfter(CouchbaseAutoConfiguration.class) |
||||
@EnableConfigurationProperties(CouchbaseDataProperties.class) |
||||
@Import({ CouchbaseConfigurerAdapterConfiguration.class, |
||||
SpringBootCouchbaseDataConfiguration.class }) |
||||
public class CouchbaseDataAutoConfiguration { |
||||
|
||||
@Configuration |
||||
@ConditionalOnClass(Validator.class) |
||||
public static class ValidationConfiguration { |
||||
|
||||
@Bean |
||||
@ConditionalOnBean(Validator.class) |
||||
public ValidatingCouchbaseEventListener validationEventListener( |
||||
Validator validator) { |
||||
return new ValidatingCouchbaseEventListener(validator); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.data.couchbase; |
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.data.couchbase.core.query.Consistency; |
||||
|
||||
/** |
||||
* Configuration properties for Spring Data Couchbase. |
||||
* |
||||
* @author Stephane Nicoll |
||||
* @since 1.4.0 |
||||
*/ |
||||
@ConfigurationProperties(prefix = "spring.data.couchbase") |
||||
public class CouchbaseDataProperties { |
||||
|
||||
/** |
||||
* Automatically create views and indexes. Use the meta-data provided by |
||||
* "@ViewIndexed", "@N1qlPrimaryIndexed" and "@N1qlSecondaryIndexed". |
||||
*/ |
||||
private boolean autoIndex; |
||||
|
||||
/** |
||||
* Consistency to apply by default on generated queries. |
||||
*/ |
||||
private Consistency consistency = Consistency.READ_YOUR_OWN_WRITES; |
||||
|
||||
public boolean isAutoIndex() { |
||||
return this.autoIndex; |
||||
} |
||||
|
||||
public void setAutoIndex(boolean autoIndex) { |
||||
this.autoIndex = autoIndex; |
||||
} |
||||
|
||||
public Consistency getConsistency() { |
||||
return this.consistency; |
||||
} |
||||
|
||||
public void setConsistency(Consistency consistency) { |
||||
this.consistency = consistency; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.data.couchbase; |
||||
|
||||
import com.couchbase.client.java.Bucket; |
||||
import com.couchbase.client.java.Cluster; |
||||
import com.couchbase.client.java.cluster.ClusterInfo; |
||||
import com.couchbase.client.java.env.CouchbaseEnvironment; |
||||
|
||||
import org.springframework.data.couchbase.config.CouchbaseConfigurer; |
||||
|
||||
/** |
||||
* A simple {@link CouchbaseConfigurer} implementation. |
||||
* |
||||
* @author Stephane Nicoll |
||||
* @since 1.4.0 |
||||
*/ |
||||
public class SpringBootCouchbaseConfigurer implements CouchbaseConfigurer { |
||||
|
||||
private final CouchbaseEnvironment env; |
||||
|
||||
private final Cluster cluster; |
||||
|
||||
private final ClusterInfo clusterInfo; |
||||
|
||||
private final Bucket bucket; |
||||
|
||||
public SpringBootCouchbaseConfigurer(CouchbaseEnvironment env, Cluster cluster, |
||||
ClusterInfo clusterInfo, Bucket bucket) { |
||||
this.env = env; |
||||
this.cluster = cluster; |
||||
this.clusterInfo = clusterInfo; |
||||
this.bucket = bucket; |
||||
} |
||||
|
||||
@Override |
||||
public CouchbaseEnvironment couchbaseEnvironment() throws Exception { |
||||
return this.env; |
||||
} |
||||
|
||||
@Override |
||||
public Cluster couchbaseCluster() throws Exception { |
||||
return this.cluster; |
||||
} |
||||
|
||||
@Override |
||||
public ClusterInfo couchbaseClusterInfo() throws Exception { |
||||
return this.clusterInfo; |
||||
} |
||||
|
||||
@Override |
||||
public Bucket couchbaseClient() throws Exception { |
||||
return this.bucket; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.data.couchbase; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseDataConfiguration; |
||||
import org.springframework.data.couchbase.config.BeanNames; |
||||
import org.springframework.data.couchbase.config.CouchbaseConfigurer; |
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate; |
||||
import org.springframework.data.couchbase.core.query.Consistency; |
||||
import org.springframework.data.couchbase.repository.support.IndexManager; |
||||
|
||||
/** |
||||
* Configure Spring Data's couchbase support. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
@Configuration |
||||
@ConditionalOnMissingBean(AbstractCouchbaseDataConfiguration.class) |
||||
@ConditionalOnBean(CouchbaseConfigurer.class) |
||||
class SpringBootCouchbaseDataConfiguration extends AbstractCouchbaseDataConfiguration { |
||||
|
||||
@Autowired |
||||
private CouchbaseDataProperties properties; |
||||
|
||||
@Autowired(required = false) |
||||
private CouchbaseConfigurer couchbaseConfigurer; |
||||
|
||||
@Override |
||||
protected CouchbaseConfigurer couchbaseConfigurer() { |
||||
return this.couchbaseConfigurer; |
||||
} |
||||
|
||||
@Override |
||||
protected Consistency getDefaultConsistency() { |
||||
return this.properties.getConsistency(); |
||||
} |
||||
|
||||
@Override |
||||
@ConditionalOnMissingBean(name = BeanNames.COUCHBASE_TEMPLATE) |
||||
@Bean(name = BeanNames.COUCHBASE_TEMPLATE) |
||||
public CouchbaseTemplate couchbaseTemplate() throws Exception { |
||||
return super.couchbaseTemplate(); |
||||
} |
||||
|
||||
@Override |
||||
@ConditionalOnMissingBean(name = BeanNames.COUCHBASE_INDEX_MANAGER) |
||||
@Bean(name = BeanNames.COUCHBASE_INDEX_MANAGER) |
||||
public IndexManager indexManager() { |
||||
if (this.properties.isAutoIndex()) { |
||||
return new IndexManager(true, true, true); |
||||
} |
||||
else { |
||||
return new IndexManager(false, false, false); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.couchbase; |
||||
|
||||
import org.junit.After; |
||||
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; |
||||
import org.springframework.boot.test.EnvironmentTestUtils; |
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||
|
||||
/** |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
public abstract class AbstractCouchbaseAutoConfigurationTests { |
||||
|
||||
protected AnnotationConfigApplicationContext context; |
||||
|
||||
@After |
||||
public void close() { |
||||
if (this.context != null) { |
||||
this.context.close(); |
||||
} |
||||
} |
||||
|
||||
protected void load(Class<?> config, String... environment) { |
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
||||
EnvironmentTestUtils.addEnvironment(context, environment); |
||||
if (config != null) { |
||||
context.register(config); |
||||
} |
||||
context.register(PropertyPlaceholderAutoConfiguration.class, |
||||
CouchbaseAutoConfiguration.class); |
||||
context.refresh(); |
||||
this.context = context; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,78 @@
@@ -0,0 +1,78 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.couchbase; |
||||
|
||||
import com.couchbase.client.java.Bucket; |
||||
import com.couchbase.client.java.Cluster; |
||||
import com.couchbase.client.java.CouchbaseBucket; |
||||
import com.couchbase.client.java.cluster.ClusterInfo; |
||||
import com.couchbase.client.java.env.CouchbaseEnvironment; |
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Integration tests for {@link CouchbaseAutoConfiguration}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
public class CouchbaseAutoConfigurationIntegrationTests |
||||
extends AbstractCouchbaseAutoConfigurationTests { |
||||
|
||||
@Rule |
||||
public final CouchbaseTestServer couchbase = new CouchbaseTestServer(); |
||||
|
||||
@Test |
||||
public void defaultConfiguration() { |
||||
load(null, "spring.couchbase.bootstrapHosts=localhost"); |
||||
assertThat(this.context.getBeansOfType(Cluster.class)).hasSize(1); |
||||
assertThat(this.context.getBeansOfType(ClusterInfo.class)).hasSize(1); |
||||
assertThat(this.context.getBeansOfType(CouchbaseEnvironment.class)).hasSize(1); |
||||
assertThat(this.context.getBeansOfType(Bucket.class)).hasSize(1); |
||||
} |
||||
|
||||
@Test |
||||
public void customConfiguration() { |
||||
load(CustomConfiguration.class, "spring.couchbase.bootstrapHosts=localhost"); |
||||
assertThat(this.context.getBeansOfType(Cluster.class)).hasSize(2); |
||||
assertThat(this.context.getBeansOfType(ClusterInfo.class)).hasSize(1); |
||||
assertThat(this.context.getBeansOfType(CouchbaseEnvironment.class)).hasSize(1); |
||||
assertThat(this.context.getBeansOfType(Bucket.class)).hasSize(2); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class CustomConfiguration { |
||||
|
||||
@Bean |
||||
public Cluster myCustomCouchbaseCluster() throws Exception { |
||||
return mock(Cluster.class); |
||||
} |
||||
|
||||
@Bean |
||||
public Bucket myCustomCouchbaseClient() { |
||||
return mock(CouchbaseBucket.class); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,120 @@
@@ -0,0 +1,120 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.couchbase; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import com.couchbase.client.java.Bucket; |
||||
import com.couchbase.client.java.Cluster; |
||||
import com.couchbase.client.java.CouchbaseCluster; |
||||
import com.couchbase.client.java.env.CouchbaseEnvironment; |
||||
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment; |
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.junit.Assume; |
||||
import org.junit.rules.TestRule; |
||||
import org.junit.runner.Description; |
||||
import org.junit.runners.model.Statement; |
||||
|
||||
/** |
||||
* {@link TestRule} for working with an optional Couchbase server. Expects |
||||
* a default {@link Bucket} with no password to be available on localhost. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
public class CouchbaseTestServer implements TestRule { |
||||
|
||||
private static final Log logger = LogFactory.getLog(CouchbaseTestServer.class); |
||||
|
||||
private CouchbaseEnvironment env; |
||||
|
||||
private Cluster cluster; |
||||
|
||||
@Override |
||||
public Statement apply(Statement base, Description description) { |
||||
try { |
||||
this.env = DefaultCouchbaseEnvironment.create(); |
||||
this.cluster = CouchbaseCluster.create(this.env, "localhost"); |
||||
testConnection(this.cluster); |
||||
return new CouchbaseStatement(base, this.env, this.cluster); |
||||
} |
||||
catch (Exception e) { |
||||
logger.info("No couchbase server available"); |
||||
return new SkipStatement(); |
||||
} |
||||
} |
||||
|
||||
private static void testConnection(Cluster cluster) { |
||||
Bucket bucket = cluster.openBucket(2, TimeUnit.SECONDS); |
||||
bucket.close(); |
||||
} |
||||
|
||||
/** |
||||
* @return the couchbase env if any |
||||
*/ |
||||
public CouchbaseEnvironment getEnv() { |
||||
return this.env; |
||||
} |
||||
|
||||
/** |
||||
* @return the cluster if any |
||||
*/ |
||||
public Cluster getCluster() { |
||||
return this.cluster; |
||||
} |
||||
|
||||
|
||||
private static class CouchbaseStatement extends Statement { |
||||
private final Statement base; |
||||
private final CouchbaseEnvironment env; |
||||
|
||||
private final Cluster cluster; |
||||
|
||||
CouchbaseStatement(Statement base, CouchbaseEnvironment env, Cluster cluster) { |
||||
this.base = base; |
||||
this.env = env; |
||||
this.cluster = cluster; |
||||
} |
||||
|
||||
@Override |
||||
public void evaluate() throws Throwable { |
||||
try { |
||||
this.base.evaluate(); |
||||
} |
||||
finally { |
||||
try { |
||||
this.cluster.disconnect(); |
||||
this.env.shutdownAsync(); |
||||
} |
||||
catch (Exception ex) { |
||||
logger.warn("Exception while trying to cleanup couchbase resource", ex); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static class SkipStatement extends Statement { |
||||
|
||||
@Override |
||||
public void evaluate() throws Throwable { |
||||
Assume.assumeTrue("Skipping test due to Couchbase " |
||||
+ "not being available", false); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,150 @@
@@ -0,0 +1,150 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.data.couchbase; |
||||
|
||||
import javax.validation.Validator; |
||||
|
||||
import org.junit.After; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.beans.DirectFieldAccessor; |
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfigurer; |
||||
import org.springframework.boot.test.EnvironmentTestUtils; |
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseDataConfiguration; |
||||
import org.springframework.data.couchbase.config.CouchbaseConfigurer; |
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate; |
||||
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener; |
||||
import org.springframework.data.couchbase.core.query.Consistency; |
||||
import org.springframework.data.couchbase.repository.support.IndexManager; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link CouchbaseDataAutoConfiguration}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
public class CouchbaseDataAutoConfigurationTests { |
||||
|
||||
private AnnotationConfigApplicationContext context; |
||||
|
||||
@After |
||||
public void close() { |
||||
if (this.context != null) { |
||||
this.context.close(); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void disabledIfCouchbaseIsNotConfigured() { |
||||
load(null); |
||||
assertThat(this.context.getBeansOfType(IndexManager.class)).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void customConfiguration() { |
||||
load(CustomCouchbaseConfiguration.class); |
||||
CouchbaseTemplate couchbaseTemplate = this.context.getBean(CouchbaseTemplate.class); |
||||
assertThat(couchbaseTemplate.getDefaultConsistency()).isEqualTo(Consistency.STRONGLY_CONSISTENT); |
||||
} |
||||
|
||||
@Test |
||||
public void validatorIsPresent() { |
||||
load(ValidatorConfiguration.class); |
||||
|
||||
ValidatingCouchbaseEventListener listener = this.context |
||||
.getBean(ValidatingCouchbaseEventListener.class); |
||||
assertThat(new DirectFieldAccessor(listener).getPropertyValue("validator")) |
||||
.isEqualTo(this.context.getBean(Validator.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void autoIndexIsDisabledByDefault() { |
||||
load(CouchbaseTestConfigurer.class); |
||||
IndexManager indexManager = this.context.getBean(IndexManager.class); |
||||
assertThat(indexManager.isIgnoreViews()).isTrue(); |
||||
assertThat(indexManager.isIgnoreN1qlPrimary()).isTrue(); |
||||
assertThat(indexManager.isIgnoreN1qlSecondary()).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void enableAutoIndex() { |
||||
load(CouchbaseTestConfigurer.class, |
||||
"spring.data.couchbase.auto-index=true"); |
||||
IndexManager indexManager = this.context.getBean(IndexManager.class); |
||||
assertThat(indexManager.isIgnoreViews()).isFalse(); |
||||
assertThat(indexManager.isIgnoreN1qlPrimary()).isFalse(); |
||||
assertThat(indexManager.isIgnoreN1qlSecondary()).isFalse(); |
||||
} |
||||
|
||||
@Test |
||||
public void changeConsistency() { |
||||
load(CouchbaseTestConfigurer.class, |
||||
"spring.data.couchbase.consistency=eventually-consistent"); |
||||
SpringBootCouchbaseDataConfiguration configuration = this.context |
||||
.getBean(SpringBootCouchbaseDataConfiguration.class); |
||||
assertThat(configuration.getDefaultConsistency()) |
||||
.isEqualTo(Consistency.EVENTUALLY_CONSISTENT); |
||||
} |
||||
|
||||
private void load(Class<?> config, String... environment) { |
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
||||
EnvironmentTestUtils.addEnvironment(context, environment); |
||||
if (config != null) { |
||||
context.register(config); |
||||
} |
||||
context.register(PropertyPlaceholderAutoConfiguration.class, |
||||
CouchbaseAutoConfiguration.class, |
||||
CouchbaseDataAutoConfiguration.class); |
||||
context.refresh(); |
||||
this.context = context; |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
@Import(CouchbaseTestConfigurer.class) |
||||
static class ValidatorConfiguration { |
||||
|
||||
@Bean |
||||
public Validator myValidator() { |
||||
return mock(Validator.class); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
static class CustomCouchbaseConfiguration extends AbstractCouchbaseDataConfiguration { |
||||
|
||||
@Override |
||||
protected CouchbaseConfigurer couchbaseConfigurer() { |
||||
return new CouchbaseTestConfigurer(); |
||||
} |
||||
|
||||
@Override |
||||
protected Consistency getDefaultConsistency() { |
||||
return Consistency.STRONGLY_CONSISTENT; |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue