33 changed files with 1168 additions and 6 deletions
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
/* |
||||
* 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.actuate.health; |
||||
|
||||
import java.util.List; |
||||
|
||||
import com.couchbase.client.java.util.features.Version; |
||||
|
||||
import org.springframework.data.couchbase.core.CouchbaseOperations; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* {@link HealthIndicator} for Couchbase. |
||||
* |
||||
* @author Eddú Meléndez |
||||
* @since 1.4.0 |
||||
*/ |
||||
public class CouchbaseHealthIndicator extends AbstractHealthIndicator { |
||||
|
||||
private CouchbaseOperations couchbaseOperations; |
||||
|
||||
public CouchbaseHealthIndicator(CouchbaseOperations couchbaseOperations) { |
||||
Assert.notNull(couchbaseOperations, "CouchbaseOperations must not be null"); |
||||
this.couchbaseOperations = couchbaseOperations; |
||||
} |
||||
|
||||
@Override |
||||
protected void doHealthCheck(Health.Builder builder) throws Exception { |
||||
List<Version> versions = this.couchbaseOperations.getCouchbaseClusterInfo().getAllVersions(); |
||||
builder.up().withDetail("versions", StringUtils.collectionToCommaDelimitedString(versions)); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,102 @@
@@ -0,0 +1,102 @@
|
||||
/* |
||||
* 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.List; |
||||
|
||||
import javax.validation.Validator; |
||||
|
||||
import com.couchbase.client.java.CouchbaseBucket; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; |
||||
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.condition.ConditionalOnProperty; |
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Conditional; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; |
||||
import org.springframework.data.couchbase.config.CouchbaseBucketFactoryBean; |
||||
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener; |
||||
|
||||
/** |
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration |
||||
* Auto-Configuration} for Couchbase. |
||||
* |
||||
* @author Eddú Meléndez |
||||
* @author Stephane Nicoll |
||||
* @since 1.4.0 |
||||
*/ |
||||
@Configuration |
||||
@ConditionalOnClass({CouchbaseBucket.class, CouchbaseBucketFactoryBean.class}) |
||||
@Conditional(CouchbaseAutoConfiguration.CouchbaseCondition.class) |
||||
@EnableConfigurationProperties(CouchbaseProperties.class) |
||||
public class CouchbaseAutoConfiguration { |
||||
|
||||
@Bean |
||||
@ConditionalOnBean(Validator.class) |
||||
public ValidatingCouchbaseEventListener validationEventListener(Validator validator) { |
||||
return new ValidatingCouchbaseEventListener(validator); |
||||
} |
||||
|
||||
@Configuration |
||||
@ConditionalOnMissingBean(AbstractCouchbaseConfiguration.class) |
||||
public static class CouchbaseConfiguration extends AbstractCouchbaseConfiguration { |
||||
|
||||
@Autowired |
||||
private CouchbaseProperties properties; |
||||
|
||||
@Override |
||||
protected List<String> getBootstrapHosts() { |
||||
return this.properties.getBootstrapHosts(); |
||||
} |
||||
|
||||
@Override |
||||
protected String getBucketName() { |
||||
return this.properties.getBucket().getName(); |
||||
} |
||||
|
||||
@Override |
||||
protected String getBucketPassword() { |
||||
return this.properties.getBucket().getPassword(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Determine if Couchbase should be configured. This happens if either the user-configuration |
||||
* defines a couchbase configuration or if at least the bucket name is specified. |
||||
*/ |
||||
static class CouchbaseCondition extends AnyNestedCondition { |
||||
|
||||
CouchbaseCondition() { |
||||
super(ConfigurationPhase.REGISTER_BEAN); |
||||
} |
||||
|
||||
@ConditionalOnProperty(prefix = "spring.data.couchbase.bucket", name = "name") |
||||
static class BucketNameProperty { |
||||
} |
||||
|
||||
@ConditionalOnBean(AbstractCouchbaseConfiguration.class) |
||||
static class CouchbaseConfiguration { |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,83 @@
@@ -0,0 +1,83 @@
|
||||
/* |
||||
* 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.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
|
||||
/** |
||||
* Configuration properties for Couchbase. |
||||
* |
||||
* @author Eddú Meléndez |
||||
* @author Stephane Nicoll |
||||
* @since 1.4.0 |
||||
*/ |
||||
@ConfigurationProperties(prefix = "spring.data.couchbase") |
||||
public class CouchbaseProperties { |
||||
|
||||
/** |
||||
* Couchbase nodes (host or IP address) to bootstrap from. |
||||
*/ |
||||
private List<String> bootstrapHosts = new ArrayList<String>(Collections.singletonList("localhost")); |
||||
|
||||
private final Bucket bucket = new Bucket(); |
||||
|
||||
public List<String> getBootstrapHosts() { |
||||
return this.bootstrapHosts; |
||||
} |
||||
|
||||
public void setBootstrapHosts(List<String> bootstrapHosts) { |
||||
this.bootstrapHosts = bootstrapHosts; |
||||
} |
||||
|
||||
public Bucket getBucket() { |
||||
return this.bucket; |
||||
} |
||||
|
||||
static class Bucket { |
||||
|
||||
/** |
||||
* Name of the bucket to connect to. |
||||
*/ |
||||
private String name; |
||||
|
||||
/** |
||||
* Password of the bucket. |
||||
*/ |
||||
private String password = ""; |
||||
|
||||
public String getName() { |
||||
return this.name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public String getPassword() { |
||||
return this.password; |
||||
} |
||||
|
||||
public void setPassword(String password) { |
||||
this.password = password; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
/* |
||||
* 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. |
||||
*/ |
||||
|
||||
/** |
||||
* Auto-configuration for Couchbase. |
||||
*/ |
||||
package org.springframework.boot.autoconfigure.couchbase; |
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
/* |
||||
* 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 org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.data.couchbase.repository.CouchbaseRepository; |
||||
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactoryBean; |
||||
|
||||
/** |
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Couchbase |
||||
* Repositories. |
||||
* |
||||
* @author Eddú Meléndez |
||||
* @since 1.4.0 |
||||
*/ |
||||
@Configuration |
||||
@ConditionalOnClass({ Bucket.class, CouchbaseRepository.class }) |
||||
@ConditionalOnProperty(prefix = "spring.data.couchbase.repositories", name = "enabled", havingValue = "true", matchIfMissing = true) |
||||
@ConditionalOnMissingBean(CouchbaseRepositoryFactoryBean.class) |
||||
@Import(CouchbaseRepositoriesRegistrar.class) |
||||
public class CouchbaseRepositoriesAutoConfiguration { |
||||
|
||||
} |
||||
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
/* |
||||
* 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 java.lang.annotation.Annotation; |
||||
|
||||
import org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport; |
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
||||
import org.springframework.data.couchbase.repository.config.CouchbaseRepositoryConfigurationExtension; |
||||
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; |
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtension; |
||||
|
||||
/** |
||||
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Couchbase |
||||
* Repositories. |
||||
* |
||||
* @author Eddú Meléndez |
||||
* @since 1.4.0 |
||||
*/ |
||||
public class CouchbaseRepositoriesRegistrar extends AbstractRepositoryConfigurationSourceSupport { |
||||
|
||||
@Override |
||||
protected Class<? extends Annotation> getAnnotation() { |
||||
return EnableCouchbaseRepositories.class; |
||||
} |
||||
|
||||
@Override |
||||
protected Class<?> getConfiguration() { |
||||
return EnableCouchbaseRepositoriesConfiguration.class; |
||||
} |
||||
|
||||
@Override |
||||
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() { |
||||
return new CouchbaseRepositoryConfigurationExtension(); |
||||
} |
||||
|
||||
@EnableCouchbaseRepositories |
||||
private static class EnableCouchbaseRepositoriesConfiguration { |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
/* |
||||
* 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. |
||||
*/ |
||||
|
||||
/** |
||||
* Auto-configuration for Spring Data Couchbase. |
||||
*/ |
||||
package org.springframework.boot.autoconfigure.data.couchbase; |
||||
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
/* |
||||
* 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 javax.validation.Validator; |
||||
|
||||
import com.couchbase.client.java.Bucket; |
||||
import org.junit.After; |
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.ExpectedException; |
||||
|
||||
import org.springframework.beans.DirectFieldAccessor; |
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; |
||||
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.AbstractCouchbaseConfiguration; |
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate; |
||||
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link CouchbaseAutoConfiguration} |
||||
* |
||||
* @author Eddú Meléndez |
||||
*/ |
||||
public class CouchbaseAutoConfigurationTests { |
||||
|
||||
@Rule |
||||
public ExpectedException thrown = ExpectedException.none(); |
||||
|
||||
private AnnotationConfigApplicationContext context; |
||||
|
||||
@After |
||||
public void close() { |
||||
if (this.context != null) { |
||||
this.context.close(); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void bucketNameIsRequired() { |
||||
load(null); |
||||
assertThat(this.context.getBeansOfType(CouchbaseTemplate.class)).isEmpty(); |
||||
assertThat(this.context.getBeansOfType(Bucket.class)).isEmpty(); |
||||
assertThat(this.context.getBeansOfType(ValidatingCouchbaseEventListener.class)).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void bucketNameIsNotRequiredIfCustomConfigurationIsSpecified() throws Exception { |
||||
load(CouchbaseTestConfiguration.class); |
||||
|
||||
assertThat(this.context.getBeansOfType(AbstractCouchbaseConfiguration.class)).hasSize(1); |
||||
CouchbaseTestConfiguration configuration = this.context.getBean(CouchbaseTestConfiguration.class); |
||||
assertThat(this.context.getBean(CouchbaseTemplate.class)).isSameAs(configuration.couchbaseTemplate()); |
||||
assertThat(this.context.getBean(Bucket.class)).isSameAs(configuration.couchbaseClient()); |
||||
assertThat(this.context.getBeansOfType(ValidatingCouchbaseEventListener.class)).isEmpty(); |
||||
} |
||||
|
||||
@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)); |
||||
} |
||||
|
||||
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); |
||||
context.refresh(); |
||||
this.context = context; |
||||
} |
||||
|
||||
@Configuration |
||||
@Import(CouchbaseTestConfiguration.class) |
||||
static class ValidatorConfiguration { |
||||
|
||||
@Bean |
||||
public Validator myValidator() { |
||||
return mock(Validator.class); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
/* |
||||
* 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.Collections; |
||||
import java.util.List; |
||||
|
||||
import com.couchbase.client.java.Bucket; |
||||
import com.couchbase.client.java.Cluster; |
||||
import com.couchbase.client.java.CouchbaseBucket; |
||||
import com.couchbase.client.java.CouchbaseCluster; |
||||
import com.couchbase.client.java.cluster.ClusterInfo; |
||||
|
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; |
||||
|
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Test configuration for couchbase that mocks access. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
@Configuration |
||||
public class CouchbaseTestConfiguration extends AbstractCouchbaseConfiguration { |
||||
|
||||
@Override |
||||
protected List<String> getBootstrapHosts() { |
||||
return Collections.singletonList("localhost"); |
||||
} |
||||
|
||||
@Override |
||||
protected String getBucketName() { |
||||
return "my-bucket"; |
||||
} |
||||
|
||||
@Override |
||||
protected String getBucketPassword() { |
||||
return "my-password"; |
||||
} |
||||
|
||||
@Override |
||||
public Cluster couchbaseCluster() throws Exception { |
||||
return mock(CouchbaseCluster.class); |
||||
} |
||||
|
||||
@Bean |
||||
public ClusterInfo couchbaseClusterInfo() { |
||||
return mock(ClusterInfo.class); |
||||
} |
||||
|
||||
@Override |
||||
public Bucket couchbaseClient() throws Exception { |
||||
return mock(CouchbaseBucket.class); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
/* |
||||
* 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.alt.couchbase; |
||||
|
||||
import org.springframework.boot.autoconfigure.data.couchbase.city.City; |
||||
import org.springframework.data.repository.Repository; |
||||
|
||||
/** |
||||
* @author Eddú Meléndez |
||||
*/ |
||||
public interface CityCouchbaseRepository extends Repository<City, Long> { |
||||
} |
||||
@ -0,0 +1,79 @@
@@ -0,0 +1,79 @@
|
||||
/* |
||||
* 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 org.junit.After; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; |
||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfiguration; |
||||
import org.springframework.boot.autoconfigure.data.couchbase.city.City; |
||||
import org.springframework.boot.autoconfigure.data.couchbase.city.CityRepository; |
||||
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage; |
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Import; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* @author Eddú Meléndez |
||||
*/ |
||||
public class CouchbaseRepositoriesAutoConfigurationTests { |
||||
|
||||
private AnnotationConfigApplicationContext context; |
||||
|
||||
@After |
||||
public void close() { |
||||
this.context.close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testDefaultRepositoryConfiguration() throws Exception { |
||||
this.context = new AnnotationConfigApplicationContext(); |
||||
this.context.register(TestConfiguration.class, |
||||
PropertyPlaceholderAutoConfiguration.class); |
||||
this.context.refresh(); |
||||
assertThat(this.context.getBean(CityRepository.class)).isNotNull(); |
||||
assertThat(this.context.getBean(Bucket.class)).isNotNull(); |
||||
} |
||||
|
||||
@Test |
||||
public void testNoRepositoryConfiguration() throws Exception { |
||||
this.context = new AnnotationConfigApplicationContext(); |
||||
this.context.register(EmptyConfiguration.class, TestConfiguration.class, |
||||
PropertyPlaceholderAutoConfiguration.class); |
||||
this.context.refresh(); |
||||
assertThat(this.context.getBean(Bucket.class)).isNotNull(); |
||||
} |
||||
|
||||
@Configuration |
||||
@TestAutoConfigurationPackage(City.class) |
||||
@Import({ CouchbaseRepositoriesRegistrar.class, CouchbaseTestConfiguration.class }) |
||||
static class TestConfiguration { |
||||
|
||||
} |
||||
|
||||
@Configuration |
||||
@TestAutoConfigurationPackage(EmptyDataPackage.class) |
||||
protected static class EmptyConfiguration { |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
/* |
||||
* 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.city; |
||||
|
||||
import com.couchbase.client.java.repository.annotation.Field; |
||||
import com.couchbase.client.java.repository.annotation.Id; |
||||
|
||||
import org.springframework.data.couchbase.core.mapping.Document; |
||||
|
||||
@Document |
||||
public class City { |
||||
|
||||
@Id |
||||
private String id; |
||||
|
||||
@Field |
||||
private String name; |
||||
|
||||
} |
||||
@ -0,0 +1,23 @@
@@ -0,0 +1,23 @@
|
||||
/* |
||||
* 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.city; |
||||
|
||||
import org.springframework.data.repository.Repository; |
||||
|
||||
public interface CityRepository extends Repository<City, Long> { |
||||
|
||||
} |
||||
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
= Spring Boot Couchbase Sample |
||||
|
||||
This sample demonstrates how you can store a simple document using Spring Data Couchbase. |
||||
|
||||
The sample expects couchbase to run on your machine with a bucket named `mybucket` and |
||||
`couchbase` for the password. You can customize these settings in `application.properties`. |
||||
|
||||
Before you use the sample, you need _at least_ to create an `all` view for the `User`: go |
||||
to the Couchbase server’s admin console and visit the Views screen, then click `Create |
||||
Development View` and name it `all` with `user` as document name. On that view, you need |
||||
to change the code to: |
||||
|
||||
```java |
||||
function (doc, meta) { |
||||
if (doc._class == "sample.data.couchbase.User") { |
||||
emit(meta.id, null); |
||||
} |
||||
} |
||||
``` |
||||
|
||||
and the _reduce_ function to `_count`. After you've saved your changes go back to `Views` |
||||
and click on `Publish` so that the `all` view move to the `Production Views` tab. |
||||
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<artifactId>spring-boot-samples</artifactId> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<version>1.4.0.BUILD-SNAPSHOT</version> |
||||
</parent> |
||||
<artifactId>spring-boot-sample-data-couchbase</artifactId> |
||||
<name>Spring Boot Data Couchbase Sample</name> |
||||
<description>Spring Boot Data Couchbase Sample</description> |
||||
<url>http://projects.spring.io/spring-boot/</url> |
||||
<organization> |
||||
<name>Pivotal Software, Inc.</name> |
||||
<url>http://www.spring.io</url> |
||||
</organization> |
||||
<properties> |
||||
<main.basedir>${basedir}/../..</main.basedir> |
||||
</properties> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-data-couchbase</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-web</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-actuator</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-test</artifactId> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
</dependencies> |
||||
<build> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
|
||||
</project> |
||||
@ -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 sample.data.couchbase; |
||||
|
||||
import java.util.UUID; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.CommandLineRunner; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
||||
@SpringBootApplication |
||||
public class SampleCouchbaseApplication implements CommandLineRunner { |
||||
|
||||
@Autowired |
||||
private UserRepository userRepository; |
||||
|
||||
public static void main(String[] args) { |
||||
SpringApplication.run(SampleCouchbaseApplication.class); |
||||
} |
||||
|
||||
@Override |
||||
public void run(String... args) throws Exception { |
||||
saveUsers(); |
||||
|
||||
System.out.println(this.userRepository.findAll()); |
||||
} |
||||
|
||||
private void saveUsers() { |
||||
User user = new User(); |
||||
user.setId(UUID.randomUUID().toString()); |
||||
user.setFirstName("Alice"); |
||||
user.setLastName("Smith"); |
||||
|
||||
this.userRepository.save(user); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/* |
||||
* 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 sample.data.couchbase; |
||||
|
||||
|
||||
import com.couchbase.client.java.repository.annotation.Field; |
||||
import com.couchbase.client.java.repository.annotation.Id; |
||||
import org.springframework.data.couchbase.core.mapping.Document; |
||||
|
||||
@Document |
||||
public class User { |
||||
|
||||
@Id |
||||
private String id; |
||||
|
||||
@Field |
||||
private String firstName; |
||||
|
||||
@Field |
||||
private String lastName; |
||||
|
||||
public String getId() { |
||||
return this.id; |
||||
} |
||||
|
||||
public void setId(String id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public String getFirstName() { |
||||
return this.firstName; |
||||
} |
||||
|
||||
public void setFirstName(String firstName) { |
||||
this.firstName = firstName; |
||||
} |
||||
|
||||
public String getLastName() { |
||||
return this.lastName; |
||||
} |
||||
|
||||
public void setLastName(String lastName) { |
||||
this.lastName = lastName; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "User{" + |
||||
"id='" + this.id + '\'' + |
||||
", firstName='" + this.firstName + '\'' + |
||||
", lastName='" + this.lastName + '\'' + |
||||
'}'; |
||||
} |
||||
} |
||||
@ -0,0 +1,23 @@
@@ -0,0 +1,23 @@
|
||||
/* |
||||
* 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 sample.data.couchbase; |
||||
|
||||
import org.springframework.data.couchbase.repository.CouchbaseRepository; |
||||
|
||||
public interface UserRepository extends CouchbaseRepository<User, String> { |
||||
|
||||
} |
||||
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
package sample.data.couchbase; |
||||
|
||||
import java.net.ConnectException; |
||||
|
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder; |
||||
import org.springframework.boot.test.OutputCapture; |
||||
import org.springframework.core.NestedCheckedException; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
public class SampleCouchbaseApplicationTests { |
||||
|
||||
@Rule |
||||
public OutputCapture outputCapture = new OutputCapture(); |
||||
|
||||
@Test |
||||
public void testDefaultSettings() throws Exception { |
||||
try { |
||||
new SpringApplicationBuilder(SampleCouchbaseApplication.class) |
||||
.run(); |
||||
} |
||||
catch (RuntimeException ex) { |
||||
if (serverNotRunning(ex)) { |
||||
return; |
||||
} |
||||
} |
||||
String output = this.outputCapture.toString(); |
||||
assertThat(output).contains("firstName='Alice', lastName='Smith'"); |
||||
} |
||||
|
||||
private boolean serverNotRunning(RuntimeException ex) { |
||||
@SuppressWarnings("serial") |
||||
NestedCheckedException nested = new NestedCheckedException("failed", ex) { |
||||
}; |
||||
if (nested.contains(ConnectException.class)) { |
||||
Throwable root = nested.getRootCause(); |
||||
if (root.getMessage().contains("Connection refused")) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,2 @@
@@ -0,0 +1,2 @@
|
||||
spring.data.couchbase.bucket.name=mybucket |
||||
spring.data.couchbase.bucket.password=couchbase |
||||
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<artifactId>spring-boot-starters</artifactId> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<version>1.4.0.BUILD-SNAPSHOT</version> |
||||
</parent> |
||||
<artifactId>spring-boot-starter-data-couchbase</artifactId> |
||||
<name>Spring Boot Data Couchbase Starter</name> |
||||
<description>Spring Boot Data Couchbase Starter</description> |
||||
<url>http://projects.spring.io/spring-boot/</url> |
||||
<organization> |
||||
<name>Pivotal Software, Inc.</name> |
||||
<url>http://www.spring.io</url> |
||||
</organization> |
||||
<properties> |
||||
<main.basedir>${basedir}/../..</main.basedir> |
||||
</properties> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.data</groupId> |
||||
<artifactId>spring-data-couchbase</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
</project> |
||||
Loading…
Reference in new issue