Skip to content

Commit

Permalink
Merge pull request spring-projects#10812 from alexsderkach:issue/8925
Browse files Browse the repository at this point in the history
* pr/10812:
  Polish "Add support for reactive Spring Data Couchbase"
  Add support for reactive Spring Data Couchbase
  • Loading branch information
snicoll committed Nov 14, 2017
2 parents 1370e9d + bbdff1a commit 1e2220b
Show file tree
Hide file tree
Showing 17 changed files with 732 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2012-2017 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 reactor.core.publisher.Flux;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository;

/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Reactive Couchbase
* support.
*
* @author Alex Derkach
* @since 2.0.0
*/
@Configuration
@ConditionalOnClass({Bucket.class, ReactiveCouchbaseRepository.class, Flux.class})
@AutoConfigureAfter(CouchbaseDataAutoConfiguration.class)
@Import(SpringBootCouchbaseReactiveDataConfiguration.class)
public class CouchbaseReactiveDataAutoConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2012-2017 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.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.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.ReactiveCouchbaseRepository;
import org.springframework.data.couchbase.repository.config.ReactiveRepositoryOperationsMapping;
import org.springframework.data.couchbase.repository.support.ReactiveCouchbaseRepositoryFactoryBean;

/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Couchbase
* Reactive Repositories.
*
* @author Alex Derkach
* @since 2.0.0
*/
@Configuration
@ConditionalOnClass({Bucket.class, ReactiveCouchbaseRepository.class})
@ConditionalOnProperty(prefix = "spring.data.couchbase.reactive-repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnBean(ReactiveRepositoryOperationsMapping.class)
@ConditionalOnMissingBean(ReactiveCouchbaseRepositoryFactoryBean.class)
@Import(CouchbaseReactiveRepositoriesAutoConfigureRegistrar.class)
@AutoConfigureAfter(CouchbaseReactiveDataAutoConfiguration.class)
public class CouchbaseReactiveRepositoriesAutoConfiguration {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2012-2017 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.EnableReactiveCouchbaseRepositories;
import org.springframework.data.couchbase.repository.config.ReactiveCouchbaseRepositoryConfigurationExtension;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;

/**
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Couchbase
* Reactive Repositories.
*
* @author Alex Derkach
* @since 2.0.0
*/
public class CouchbaseReactiveRepositoriesAutoConfigureRegistrar
extends AbstractRepositoryConfigurationSourceSupport {

@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableReactiveCouchbaseRepositories.class;
}

@Override
protected Class<?> getConfiguration() {
return EnableReactiveCouchbaseRepositoriesConfiguration.class;
}

@Override
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() {
return new ReactiveCouchbaseRepositoryConfigurationExtension();
}

@EnableReactiveCouchbaseRepositories
private static class EnableReactiveCouchbaseRepositoriesConfiguration {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2012-2017 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.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractReactiveCouchbaseDataConfiguration;
import org.springframework.data.couchbase.config.BeanNames;
import org.springframework.data.couchbase.config.CouchbaseConfigurer;
import org.springframework.data.couchbase.core.RxJavaCouchbaseTemplate;
import org.springframework.data.couchbase.core.query.Consistency;
import org.springframework.data.couchbase.repository.config.ReactiveRepositoryOperationsMapping;

/**
* Configure Spring Data's reactive couchbase support.
*
* @author Alex Derkach
* @since 2.0.0
*/
@Configuration
@ConditionalOnMissingBean(AbstractReactiveCouchbaseDataConfiguration.class)
@ConditionalOnBean(CouchbaseConfigurer.class)
class SpringBootCouchbaseReactiveDataConfiguration
extends AbstractReactiveCouchbaseDataConfiguration {

private final CouchbaseDataProperties properties;

private final CouchbaseConfigurer couchbaseConfigurer;

SpringBootCouchbaseReactiveDataConfiguration(CouchbaseDataProperties properties,
CouchbaseConfigurer couchbaseConfigurer) {
this.properties = properties;
this.couchbaseConfigurer = couchbaseConfigurer;
}

@Override
protected CouchbaseConfigurer couchbaseConfigurer() {
return this.couchbaseConfigurer;
}

@Override
protected Consistency getDefaultConsistency() {
return this.properties.getConsistency();
}

@Override
@ConditionalOnMissingBean(name = BeanNames.RXJAVA1_COUCHBASE_TEMPLATE)
@Bean(name = BeanNames.RXJAVA1_COUCHBASE_TEMPLATE)
public RxJavaCouchbaseTemplate reactiveCouchbaseTemplate() throws Exception {
return super.reactiveCouchbaseTemplate();
}

@Override
@ConditionalOnMissingBean(name = BeanNames.REACTIVE_COUCHBASE_OPERATIONS_MAPPING)
@Bean(name = BeanNames.REACTIVE_COUCHBASE_OPERATIONS_MAPPING)
public ReactiveRepositoryOperationsMapping reactiveRepositoryOperationsMapping(RxJavaCouchbaseTemplate reactiveCouchbaseTemplate) throws Exception {
return super.reactiveRepositoryOperationsMapping(reactiveCouchbaseTemplate);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,20 @@
"description": "Enable Cassandra reactive repositories.",
"defaultValue": true
},
{
"name": "spring.data.cassandra.repositories.enabled",
"type": "java.lang.Boolean",
"description": "Enable Cassandra repositories.",
"defaultValue": true
},
{
"name": "spring.data.couchbase.consistency",
"defaultValue": "read-your-own-writes"
},
{
"name": "spring.data.cassandra.repositories.enabled",
"name": "spring.data.couchbase.reactive-repositories.enabled",
"type": "java.lang.Boolean",
"description": "Enable Cassandra repositories.",
"description": "Enable Couchbase reactive repositories.",
"defaultValue": true
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoC
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2012-2017 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 reactor.core.publisher.Mono;

import org.springframework.boot.autoconfigure.data.couchbase.city.City;
import org.springframework.data.repository.Repository;

public interface ReactiveCityCouchbaseRepository extends Repository<City, Long> {
Mono<City> save(City city);

Mono<City> findById(Long id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2012-2017 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.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Test;

import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfigurationTests;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfigurer;
import org.springframework.boot.autoconfigure.data.couchbase.city.CityRepository;
import org.springframework.boot.autoconfigure.data.couchbase.city.ReactiveCityRepository;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.couchbase.repository.config.EnableReactiveCouchbaseRepositories;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link CouchbaseRepositoriesAutoConfiguration} and
* {@link CouchbaseReactiveRepositoriesAutoConfiguration}.
*
* @author Stephane Nicoll
*/
public class CouchbaseReactiveAndBlockingRepositoriesAutoConfigurationTests {

private AnnotationConfigApplicationContext context;

@After
public void close() {
this.context.close();
}

@Test
public void shouldCreateInstancesForReactiveAndBlockingRepositories()
throws Exception {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.datasource.initialization-mode:never")
.applyTo(this.context);
this.context.register(BlockingAndReactiveConfiguration.class,
BaseConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(CityRepository.class)).isNotNull();
assertThat(this.context.getBean(ReactiveCityRepository.class)).isNotNull();
}

@Configuration
@TestAutoConfigurationPackage(CouchbaseAutoConfigurationTests.class)
@EnableCouchbaseRepositories(basePackageClasses = CityRepository.class)
@EnableReactiveCouchbaseRepositories(basePackageClasses = ReactiveCityRepository.class)
protected static class BlockingAndReactiveConfiguration {

}

@Configuration
@Import({ CouchbaseTestConfigurer.class, Registrar.class })
protected static class BaseConfiguration {

}

protected static class Registrar implements ImportSelector {

@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
List<String> names = new ArrayList<>();
for (Class<?> type : new Class<?>[] { CouchbaseAutoConfiguration.class,
CouchbaseDataAutoConfiguration.class,
CouchbaseRepositoriesAutoConfiguration.class,
CouchbaseReactiveDataAutoConfiguration.class,
CouchbaseReactiveRepositoriesAutoConfiguration.class }) {
names.add(type.getName());
}
return names.toArray(new String[0]);
}

}

}
Loading

0 comments on commit 1e2220b

Please sign in to comment.