Merge pull request #20662 from mp911de

* pr/20662:
  Polish "Migrate off deprecated Spring Data Cassandra API"
  Migrate off deprecated Spring Data Cassandra API

Closes gh-20662
pull/20667/head
Stephane Nicoll 5 years ago
commit a03a8ebaff

@ -80,8 +80,7 @@ public class CassandraDataAutoConfiguration {
if (!packages.isEmpty()) {
context.setInitialEntitySet(CassandraEntityClassScanner.scan(packages));
}
context.setUserTypeResolver(new SimpleUserTypeResolver(this.session));
context.setCustomConversions(conversions);
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
return context;
}
@ -90,7 +89,9 @@ public class CassandraDataAutoConfiguration {
public CassandraConverter cassandraConverter(CassandraMappingContext mapping,
CassandraCustomConversions conversions) {
MappingCassandraConverter converter = new MappingCassandraConverter(mapping);
converter.setCodecRegistry(this.session.getContext().getCodecRegistry());
converter.setCustomConversions(conversions);
converter.setUserTypeResolver(new SimpleUserTypeResolver(this.session));
return converter;
}

@ -32,6 +32,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.convert.CassandraCustomConversions;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
@ -39,7 +40,6 @@ import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CassandraDataAutoConfiguration}.
@ -61,7 +61,7 @@ class CassandraDataAutoConfigurationTests {
@Test
void templateExists() {
load(TestConfiguration.class);
load(CassandraMockConfiguration.class);
assertThat(this.context.getBeanNamesForType(CassandraTemplate.class)).hasSize(1);
}
@ -78,8 +78,16 @@ class CassandraDataAutoConfigurationTests {
@Test
void userTypeResolverShouldBeSet() {
load();
CassandraMappingContext mappingContext = this.context.getBean(CassandraMappingContext.class);
assertThat(mappingContext).extracting("userTypeResolver").isInstanceOf(SimpleUserTypeResolver.class);
CassandraConverter cassandraConverter = this.context.getBean(CassandraConverter.class);
assertThat(cassandraConverter).extracting("userTypeResolver").isInstanceOf(SimpleUserTypeResolver.class);
}
@Test
void codecRegistryShouldBeSet() {
load();
CassandraConverter cassandraConverter = this.context.getBean(CassandraConverter.class);
assertThat(cassandraConverter.getCodecRegistry())
.isSameAs(this.context.getBean(CassandraMockConfiguration.class).codecRegistry);
}
@Test
@ -94,7 +102,6 @@ class CassandraDataAutoConfigurationTests {
load(CustomConversionConfig.class);
CassandraTemplate template = this.context.getBean(CassandraTemplate.class);
assertThat(template.getConverter().getConversionService().canConvert(Person.class, String.class)).isTrue();
}
@Test
@ -109,21 +116,12 @@ class CassandraDataAutoConfigurationTests {
if (!ObjectUtils.isEmpty(config)) {
ctx.register(config);
}
ctx.register(TestConfiguration.class, CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class);
ctx.register(CassandraMockConfiguration.class, CassandraAutoConfiguration.class,
CassandraDataAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
@Bean
CqlSession cqlSession() {
return mock(CqlSession.class);
}
}
@Configuration(proxyBeanMethods = false)
@EntityScan("org.springframework.boot.autoconfigure.data.cassandra.city")
static class EntityScanConfig {

@ -0,0 +1,48 @@
/*
* Copyright 2012-2020 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
*
* https://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.cassandra;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.context.DriverContext;
import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test configuration that mocks access to Cassandra.
*
* @author Stephane Nicoll
*/
@Configuration(proxyBeanMethods = false)
class CassandraMockConfiguration {
final CodecRegistry codecRegistry = mock(CodecRegistry.class);
@Bean
CqlSession cqlSession() {
DriverContext context = mock(DriverContext.class);
given(context.getCodecRegistry()).willReturn(this.codecRegistry);
CqlSession cqlSession = mock(CqlSession.class);
given(cqlSession.getContext()).willReturn(context);
return cqlSession;
}
}

@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.data.cassandra;
import java.util.Set;
import com.datastax.oss.driver.api.core.CqlSession;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
@ -27,15 +26,14 @@ import org.springframework.boot.autoconfigure.data.cassandra.city.City;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.ReactiveCassandraTemplate;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CassandraReactiveDataAutoConfiguration}.
@ -74,8 +72,8 @@ class CassandraReactiveDataAutoConfigurationTests {
@Test
void userTypeResolverShouldBeSet() {
load("spring.data.cassandra.keyspaceName:boot_test");
CassandraMappingContext mappingContext = this.context.getBean(CassandraMappingContext.class);
assertThat(mappingContext).extracting("userTypeResolver").isInstanceOf(SimpleUserTypeResolver.class);
CassandraConverter cassandraConverter = this.context.getBean(CassandraConverter.class);
assertThat(cassandraConverter).extracting("userTypeResolver").isInstanceOf(SimpleUserTypeResolver.class);
}
private void load(String... environment) {
@ -88,22 +86,12 @@ class CassandraReactiveDataAutoConfigurationTests {
if (config != null) {
ctx.register(config);
}
ctx.register(TestConfiguration.class, CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
CassandraReactiveDataAutoConfiguration.class);
ctx.register(CassandraMockConfiguration.class, CassandraAutoConfiguration.class,
CassandraDataAutoConfiguration.class, CassandraReactiveDataAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
@Bean
CqlSession cqlSession() {
return mock(CqlSession.class);
}
}
@Configuration(proxyBeanMethods = false)
@EntityScan("org.springframework.boot.autoconfigure.data.cassandra.city")
static class EntityScanConfig {

@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.data.cassandra;
import java.util.Set;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import org.junit.jupiter.api.Test;
@ -32,7 +31,6 @@ import org.springframework.boot.autoconfigure.data.cassandra.city.ReactiveCityRe
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
@ -40,7 +38,6 @@ import org.springframework.data.cassandra.repository.config.EnableReactiveCassan
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CassandraReactiveRepositoriesAutoConfiguration}.
@ -102,26 +99,16 @@ class CassandraReactiveRepositoriesAutoConfigurationTests {
return (Set<Class<?>>) ReflectionTestUtils.getField(mappingContext, "initialEntitySet");
}
@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
@Bean
CqlSession cqlSession() {
return mock(CqlSession.class);
}
}
@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(EmptyDataPackage.class)
@Import(TestConfiguration.class)
@Import(CassandraMockConfiguration.class)
static class EmptyConfiguration {
}
@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(City.class)
@Import(TestConfiguration.class)
@Import(CassandraMockConfiguration.class)
static class DefaultConfiguration {
}
@ -129,7 +116,7 @@ class CassandraReactiveRepositoriesAutoConfigurationTests {
@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(CassandraReactiveRepositoriesAutoConfigurationTests.class)
@EnableReactiveCassandraRepositories(basePackageClasses = ReactiveCityCassandraRepository.class)
@Import(TestConfiguration.class)
@Import(CassandraMockConfiguration.class)
static class CustomizedConfiguration {
}

@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.data.cassandra;
import java.util.Set;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import org.junit.jupiter.api.Test;
@ -32,7 +31,6 @@ import org.springframework.boot.autoconfigure.data.cassandra.city.CityRepository
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
@ -40,7 +38,6 @@ import org.springframework.data.cassandra.repository.config.EnableCassandraRepos
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CassandraRepositoriesAutoConfiguration}.
@ -100,26 +97,16 @@ class CassandraRepositoriesAutoConfigurationTests {
return (Set<Class<?>>) ReflectionTestUtils.getField(mappingContext, "initialEntitySet");
}
@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
@Bean
CqlSession cqlSession() {
return mock(CqlSession.class);
}
}
@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(EmptyDataPackage.class)
@Import(TestConfiguration.class)
@Import(CassandraMockConfiguration.class)
static class EmptyConfiguration {
}
@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(City.class)
@Import(TestConfiguration.class)
@Import(CassandraMockConfiguration.class)
static class DefaultConfiguration {
}
@ -127,7 +114,7 @@ class CassandraRepositoriesAutoConfigurationTests {
@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(CassandraRepositoriesAutoConfigurationTests.class)
@EnableCassandraRepositories(basePackageClasses = CityCassandraRepository.class)
@Import(TestConfiguration.class)
@Import(CassandraMockConfiguration.class)
static class CustomizedConfiguration {
}

Loading…
Cancel
Save