Polish "Add reactive support for Spring Data Cassandra"

Closes gh-8568
pull/8819/merge
Stephane Nicoll 8 years ago
parent abd35f041b
commit 47037d5227

@ -18,11 +18,11 @@ package org.springframework.boot.autoconfigure.data.cassandra;
import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session; import com.datastax.driver.core.Session;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers; import reactor.core.scheduler.Schedulers;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -43,29 +43,29 @@ import org.springframework.data.cassandra.core.ReactiveCassandraTemplate;
* @since 2.0.0 * @since 2.0.0
*/ */
@Configuration @Configuration
@ConditionalOnClass({ Cluster.class, ReactiveCassandraTemplate.class }) @ConditionalOnClass({ Cluster.class, ReactiveCassandraTemplate.class, Flux.class })
@ConditionalOnBean(Session.class) @ConditionalOnBean(Session.class)
@AutoConfigureAfter(CassandraAutoConfiguration.class) @AutoConfigureAfter(CassandraDataAutoConfiguration.class)
public class ReactiveCassandraDataAutoConfiguration { public class ReactiveCassandraDataAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(ReactiveSession.class) @ConditionalOnMissingBean(ReactiveSession.class)
public ReactiveSession rectiveSession(Session session) public ReactiveSession reactiveCassandraSession(Session session) {
throws Exception {
return new DefaultBridgedReactiveSession(session, Schedulers.elastic()); return new DefaultBridgedReactiveSession(session, Schedulers.elastic());
} }
@Bean @Bean
public ReactiveSessionFactory reactiveSessionFactory(ReactiveSession reactiveSession) public ReactiveSessionFactory reactiveCassandraSessionFactory(
throws Exception { ReactiveSession reactiveCassandraSession) {
return new DefaultReactiveSessionFactory(reactiveSession); return new DefaultReactiveSessionFactory(reactiveCassandraSession);
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ReactiveCassandraTemplate reactiveCassandraTemplate(ReactiveSession session, public ReactiveCassandraTemplate reactiveCassandraTemplate(
CassandraConverter converter) throws Exception { ReactiveSession reactiveCassandraSession,
return new ReactiveCassandraTemplate(session, converter); CassandraConverter converter) {
return new ReactiveCassandraTemplate(reactiveCassandraSession, converter);
} }
} }

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.data.cassandra; package org.springframework.boot.autoconfigure.data.cassandra;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -32,14 +33,15 @@ import org.springframework.data.cassandra.repository.support.ReactiveCassandraRe
* Reactive Repositories. * Reactive Repositories.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @see EnableReactiveCassandraRepositories
* @since 2.0.0 * @since 2.0.0
* @see EnableReactiveCassandraRepositories
*/ */
@Configuration @Configuration
@ConditionalOnClass({ ReactiveSession.class, ReactiveCassandraRepository.class }) @ConditionalOnClass({ ReactiveSession.class, ReactiveCassandraRepository.class })
@ConditionalOnProperty(prefix = "spring.data.cassandra.reactive-repositories", name = "enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = "spring.data.cassandra.reactive-repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnMissingBean(ReactiveCassandraRepositoryFactoryBean.class) @ConditionalOnMissingBean(ReactiveCassandraRepositoryFactoryBean.class)
@Import(ReactiveCassandraRepositoriesAutoConfigureRegistrar.class) @Import(ReactiveCassandraRepositoriesAutoConfigureRegistrar.class)
@AutoConfigureAfter(ReactiveCassandraDataAutoConfiguration.class)
public class ReactiveCassandraRepositoriesAutoConfiguration { public class ReactiveCassandraRepositoriesAutoConfiguration {
} }

@ -31,7 +31,7 @@ import org.springframework.data.repository.config.RepositoryConfigurationExtensi
* @author Eddú Meléndez * @author Eddú Meléndez
* @since 2.0.0 * @since 2.0.0
*/ */
public class ReactiveCassandraRepositoriesAutoConfigureRegistrar class ReactiveCassandraRepositoriesAutoConfigureRegistrar
extends AbstractRepositoryConfigurationSourceSupport { extends AbstractRepositoryConfigurationSourceSupport {
@Override @Override

@ -20,21 +20,15 @@ import java.util.Set;
import com.datastax.driver.core.Session; import com.datastax.driver.core.Session;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration; import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.cassandra.city.City; import org.springframework.boot.autoconfigure.data.cassandra.city.City;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.cassandra.core.session.ReactiveSession;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.cassandra.core.ReactiveCassandraTemplate; import org.springframework.data.cassandra.core.ReactiveCassandraTemplate;
import org.springframework.data.cassandra.mapping.CassandraMappingContext; import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.mapping.SimpleUserTypeResolver; import org.springframework.data.cassandra.mapping.SimpleUserTypeResolver;
@ -44,19 +38,15 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link ReactiveCassandraDataAutoConfiguration} that require a Cassandra instance. * Tests for {@link ReactiveCassandraDataAutoConfiguration}.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
*/ */
public class ReactiveCassandraDataAutoConfigurationTests { public class ReactiveCassandraDataAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private AnnotationConfigApplicationContext context;
@Before
public void setup() {
this.context = new AnnotationConfigApplicationContext();
}
@After @After
public void close() { public void close() {
if (this.context != null) { if (this.context != null) {
@ -66,27 +56,15 @@ public class ReactiveCassandraDataAutoConfigurationTests {
@Test @Test
public void templateExists() { public void templateExists() {
EnvironmentTestUtils.addEnvironment(this.context, load("spring.data.cassandra.keyspaceName:boot_test");
"spring.data.cassandra.keyspaceName:boot_test"); assertThat(this.context.getBeanNamesForType(ReactiveCassandraTemplate.class))
this.context.register(TestExcludeConfiguration.class, TestConfiguration.class, .hasSize(1);
PropertyPlaceholderAutoConfiguration.class,
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
ReactiveCassandraDataAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(ReactiveCassandraTemplate.class).length)
.isEqualTo(1);
} }
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void entityScanShouldSetInitialEntitySet() throws Exception { public void entityScanShouldSetInitialEntitySet() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, load(EntityScanConfig.class, "spring.data.cassandra.keyspaceName:boot_test");
"spring.data.cassandra.keyspaceName:boot_test");
this.context.register(TestConfiguration.class, EntityScanConfig.class,
PropertyPlaceholderAutoConfiguration.class,
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
ReactiveCassandraDataAutoConfiguration.class);
this.context.refresh();
CassandraMappingContext mappingContext = this.context CassandraMappingContext mappingContext = this.context
.getBean(CassandraMappingContext.class); .getBean(CassandraMappingContext.class);
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
@ -96,24 +74,28 @@ public class ReactiveCassandraDataAutoConfigurationTests {
@Test @Test
public void userTypeResolverShouldBeSet() throws Exception { public void userTypeResolverShouldBeSet() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, load("spring.data.cassandra.keyspaceName:boot_test");
"spring.data.cassandra.keyspaceName:boot_test");
this.context.register(TestConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
ReactiveCassandraDataAutoConfiguration.class);
this.context.refresh();
CassandraMappingContext mappingContext = this.context CassandraMappingContext mappingContext = this.context
.getBean(CassandraMappingContext.class); .getBean(CassandraMappingContext.class);
assertThat(ReflectionTestUtils.getField(mappingContext, "userTypeResolver")) assertThat(ReflectionTestUtils.getField(mappingContext, "userTypeResolver"))
.isInstanceOf(SimpleUserTypeResolver.class); .isInstanceOf(SimpleUserTypeResolver.class);
} }
@Configuration private void load(String... environment) {
@ComponentScan(excludeFilters = @Filter(classes = { load(null, environment);
ReactiveSession.class }, type = FilterType.ASSIGNABLE_TYPE)) }
static class TestExcludeConfiguration {
private void load(Class<?> config, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(ctx, environment);
if (config != null) {
ctx.register(config);
}
ctx.register(TestConfiguration.class, CassandraAutoConfiguration.class,
CassandraDataAutoConfiguration.class,
ReactiveCassandraDataAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
} }
@Configuration @Configuration

@ -21,7 +21,6 @@ import java.util.Set;
import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session; import com.datastax.driver.core.Session;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
@ -49,16 +48,12 @@ import static org.mockito.Mockito.mock;
* Tests for {@link ReactiveCassandraRepositoriesAutoConfiguration}. * Tests for {@link ReactiveCassandraRepositoriesAutoConfiguration}.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
*/ */
public class ReactiveCassandraRepositoriesAutoConfigurationTests { public class ReactiveCassandraRepositoriesAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private AnnotationConfigApplicationContext context;
@Before
public void setUp() {
this.context = new AnnotationConfigApplicationContext();
}
@After @After
public void close() { public void close() {
if (this.context != null) { if (this.context != null) {
@ -68,7 +63,7 @@ public class ReactiveCassandraRepositoriesAutoConfigurationTests {
@Test @Test
public void testDefaultRepositoryConfiguration() { public void testDefaultRepositoryConfiguration() {
addConfigurations(TestConfiguration.class); load(TestConfiguration.class);
assertThat(this.context.getBean(ReactiveCityRepository.class)).isNotNull(); assertThat(this.context.getBean(ReactiveCityRepository.class)).isNotNull();
assertThat(this.context.getBean(Cluster.class)).isNotNull(); assertThat(this.context.getBean(Cluster.class)).isNotNull();
assertThat(getInitialEntitySet()).hasSize(1); assertThat(getInitialEntitySet()).hasSize(1);
@ -76,14 +71,14 @@ public class ReactiveCassandraRepositoriesAutoConfigurationTests {
@Test @Test
public void testNoRepositoryConfiguration() { public void testNoRepositoryConfiguration() {
addConfigurations(TestExcludeConfiguration.class, EmptyConfiguration.class); load(TestExcludeConfiguration.class, EmptyConfiguration.class);
assertThat(this.context.getBean(Cluster.class)).isNotNull(); assertThat(this.context.getBean(Cluster.class)).isNotNull();
assertThat(getInitialEntitySet()).hasSize(1).containsOnly(City.class); assertThat(getInitialEntitySet()).hasSize(1).containsOnly(City.class);
} }
@Test @Test
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() { public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
addConfigurations(TestExcludeConfiguration.class, CustomizedConfiguration.class); load(TestExcludeConfiguration.class, CustomizedConfiguration.class);
assertThat(this.context.getBean(ReactiveCityCassandraRepository.class)).isNotNull(); assertThat(this.context.getBean(ReactiveCityCassandraRepository.class)).isNotNull();
assertThat(getInitialEntitySet()).hasSize(1).containsOnly(City.class); assertThat(getInitialEntitySet()).hasSize(1).containsOnly(City.class);
} }
@ -96,15 +91,17 @@ public class ReactiveCassandraRepositoriesAutoConfigurationTests {
"initialEntitySet"); "initialEntitySet");
} }
private void addConfigurations(Class<?>... configurations) { private void load(Class<?>... configurations) {
this.context.register(configurations); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
this.context.register(CassandraAutoConfiguration.class, ctx.register(configurations);
ctx.register(CassandraAutoConfiguration.class,
CassandraRepositoriesAutoConfiguration.class, CassandraRepositoriesAutoConfiguration.class,
CassandraDataAutoConfiguration.class, CassandraDataAutoConfiguration.class,
ReactiveCassandraDataAutoConfiguration.class, ReactiveCassandraDataAutoConfiguration.class,
ReactiveCassandraRepositoriesAutoConfiguration.class, ReactiveCassandraRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); ctx.refresh();
this.context = ctx;
} }
@Configuration @Configuration

@ -16,8 +16,8 @@
package org.springframework.boot.autoconfigure.data.cassandra.city; package org.springframework.boot.autoconfigure.data.cassandra.city;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.reactive.ReactiveCrudRepository;
public interface ReactiveCityRepository extends Repository<City, Long> { public interface ReactiveCityRepository extends ReactiveCrudRepository<City, Long> {
} }

@ -573,7 +573,7 @@ content into your application; rather pick only the properties that you need.
spring.data.cassandra.load-balancing-policy= # Class name of the load balancing policy. spring.data.cassandra.load-balancing-policy= # Class name of the load balancing policy.
spring.data.cassandra.port= # Port of the Cassandra server. spring.data.cassandra.port= # Port of the Cassandra server.
spring.data.cassandra.password= # Login password of the server. spring.data.cassandra.password= # Login password of the server.
spring.data.cassandra.reactive-repositories.enabled= # Enable Cassandra reactive repositories. spring.data.cassandra.reactive-repositories.enabled=true # Enable Cassandra reactive repositories.
spring.data.cassandra.read-timeout-millis= # Socket option: read time out. spring.data.cassandra.read-timeout-millis= # Socket option: read time out.
spring.data.cassandra.reconnection-policy= # Reconnection policy class. spring.data.cassandra.reconnection-policy= # Reconnection policy class.
spring.data.cassandra.repositories.enabled= # Enable Cassandra repositories. spring.data.cassandra.repositories.enabled= # Enable Cassandra repositories.

@ -1,21 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <project xmlns="http://maven.apache.org/POM/4.0.0"
~ Copyright 2012-2017 the original author or authors. 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">
~ 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.
-->
<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> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -25,7 +11,8 @@
<artifactId>spring-boot-starter-data-cassandra-reactive</artifactId> <artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
<name>Spring Boot Data Cassandra Reactive Starter</name> <name>Spring Boot Data Cassandra Reactive Starter</name>
<description>Starter for using Cassandra distributed database and Spring Data <description>Starter for using Cassandra distributed database and Spring Data
Cassandra Reactive</description> Cassandra Reactive
</description>
<url>http://projects.spring.io/spring-boot/</url> <url>http://projects.spring.io/spring-boot/</url>
<organization> <organization>
<name>Pivotal Software, Inc.</name> <name>Pivotal Software, Inc.</name>
@ -46,6 +33,12 @@
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId> <artifactId>spring-data-cassandra</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>

Loading…
Cancel
Save