@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2012 - 20 19 the original author or authors .
* Copyright 2012 - 20 20 the original author or authors .
*
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -16,23 +16,25 @@
package org.springframework.boot.test.autoconfigure.data.cassandra ;
package org.springframework.boot.test.autoconfigure.data.cassandra ;
import java.time.Duration ;
import java.util.UUID ;
import java.util.UUID ;
import com.datastax.driver.core.Cluster ;
import com.datastax.oss.driver.api.core.CqlSession ;
import com.datastax.oss.driver.api.core.CqlSessionBuilder ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.Test ;
import org.testcontainers.containers.CassandraContainer ;
import org.testcontainers.junit.jupiter.Container ;
import org.testcontainers.junit.jupiter.Container ;
import org.testcontainers.junit.jupiter.Testcontainers ;
import org.springframework.beans.factory.NoSuchBeanDefinitionException ;
import org.springframework.beans.factory.NoSuchBeanDefinitionException ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.boot.test.autoconfigure.data.redis.ExampleService ;
import org.springframework.boot.test.autoconfigure.data.redis.ExampleService ;
import org.springframework.boot.test.util.TestPropertyValues ;
import org.springframework.boot.test.context.TestConfiguration ;
import org.springframework.boot.testsupport.testcontainers.CassandraContainer ;
import org.springframework.boot.testsupport.testcontainers.DisabledWithoutDockerTestcontainers ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.ApplicationContextInitializer ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.ConfigurableApplicationContext ;
import org.springframework.data.cassandra.core.CassandraTemplate ;
import org.springframework.data.cassandra.core.CassandraTemplate ;
import org.springframework.test.context.ContextConfiguration ;
import org.springframework.test.context.DynamicPropertyRegistry ;
import org.springframework.test.context.DynamicPropertySource ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
@ -42,13 +44,21 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*
*
* @author Artsiom Yudovin
* @author Artsiom Yudovin
* /
* /
@DisabledWithoutDockerTestcontainers
@DataCassandraTest ( properties = { "spring.data.cassandra.local-datacenter=datacenter1" ,
@ContextConfiguration ( initializers = DataCassandraTestIntegrationTests . Initializer . class )
"spring.data.cassandra.schema-action=create-if-not-exists" ,
@DataCassandraTest
"spring.data.cassandra.connection.connect-timeout=10s" , "spring.data.cassandra.request.timeout=5s" } )
@Testcontainers ( disabledWithoutDocker = true )
class DataCassandraTestIntegrationTests {
class DataCassandraTestIntegrationTests {
@Container
@Container
public static CassandraContainer cassandra = new CassandraContainer ( ) ;
static final CassandraContainer < ? > cassandra = new CassandraContainer < > ( ) . withStartupAttempts ( 5 )
. withStartupTimeout ( Duration . ofMinutes ( 10 ) ) ;
@DynamicPropertySource
static void cassandraProperties ( DynamicPropertyRegistry registry ) {
registry . add ( "spring.data.cassandra.contact-points" ,
( ) - > cassandra . getHost ( ) + ":" + cassandra . getFirstMappedPort ( ) ) ;
}
@Autowired
@Autowired
private CassandraTemplate cassandraTemplate ;
private CassandraTemplate cassandraTemplate ;
@ -67,36 +77,28 @@ class DataCassandraTestIntegrationTests {
@Test
@Test
void testRepository ( ) {
void testRepository ( ) {
ExampleEntity entity = new ExampleEntity ( ) ;
Person person = new Person ( ) ;
entity . setDescription ( "Look, new @DataCassandraTest!" ) ;
person . setDescription ( "Look, new @DataCassandraTest!" ) ;
String id = UUID . randomUUID ( ) . toString ( ) ;
String id = UUID . randomUUID ( ) . toString ( ) ;
person . setId ( id ) ;
entity . setId ( id ) ;
ExampleEntity savedEntity = this . exampleRepository . save ( entity ) ;
Person savedEntity = this . exampleRepository . save ( person ) ;
ExampleEntity getEntity = this . cassandraTemplate . selectOneById ( id , ExampleEntity . class ) ;
Person getEntity = this . cassandraTemplate . selectOneById ( id , Person . class ) ;
assertThat ( getEntity ) . isNotNull ( ) ;
assertThat ( getEntity ) . isNotNull ( ) ;
assertThat ( getEntity . getId ( ) ) . isNotNull ( ) ;
assertThat ( getEntity . getId ( ) ) . isNotNull ( ) ;
assertThat ( getEntity . getId ( ) ) . isEqualTo ( savedEntity . getId ( ) ) ;
assertThat ( getEntity . getId ( ) ) . isEqualTo ( savedEntity . getId ( ) ) ;
this . exampleRepository . deleteAll ( ) ;
this . exampleRepository . deleteAll ( ) ;
}
}
static class Initializer implements ApplicationContextInitializer < ConfigurableApplicationContext > {
@TestConfiguration ( proxyBeanMethods = false )
static class KeyspaceTestConfiguration {
@Override
@Bean
public void initialize ( ConfigurableApplicationContext configurableApplicationContext ) {
CqlSession cqlSession ( CqlSessionBuilder cqlSessionBuilder ) {
TestPropertyValues . of ( "spring.data.cassandra.port=" + cassandra . getFirstMappedPort ( ) )
try ( CqlSession session = cqlSessionBuilder . build ( ) ) {
. and ( "spring.data.cassandra.keyspaceName=test" )
session . execute ( "CREATE KEYSPACE IF NOT EXISTS boot_test"
. and ( "spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS" )
+ " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };" ) ;
. applyTo ( configurableApplicationContext . getEnvironment ( ) ) ;
}
return cqlSessionBuilder . withKeyspace ( "boot_test" ) . build ( ) ;
Cluster cluster = Cluster . builder ( ) . withoutJMXReporting ( )
. addContactPoints ( cassandra . getContainerIpAddress ( ) ) . withPort ( cassandra . getFirstMappedPort ( ) )
. build ( ) ;
cluster . connect ( ) . execute ( "CREATE KEYSPACE test "
+ "WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};" ) ;
}
}
}
}