Skip to content

Commit c65e929

Browse files
committed
Tests Stand Up Cassandra
This allows the tests to run and confirm that the guide still works.
1 parent 32d982e commit c65e929

File tree

11 files changed

+120
-1
lines changed

11 files changed

+120
-1
lines changed

complete/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies {
1919
implementation 'org.springframework.boot:spring-boot-starter-data-cassandra'
2020
//implementation 'com.datastax.astra:astra-spring-boot-starter:0.1.13'
2121
testImplementation 'org.springframework.boot:spring-boot-starter-test'
22+
runtimeOnly "org.springframework.boot:spring-boot-docker-compose"
2223
}
2324

2425
tasks.named('test') {

complete/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
<artifactId>spring-boot-starter-test</artifactId>
3636
<scope>test</scope>
3737
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-docker-compose</artifactId>
42+
<optional>true</optional>
43+
</dependency>
3844
</dependencies>
3945

4046
<build>

complete/src/main/java/com/example/accessingdatacassandra/AccessingDataCassandraApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
9+
910
import org.springframework.boot.CommandLineRunner;
1011
import org.springframework.boot.SpringApplication;
1112
import org.springframework.boot.autoconfigure.SpringBootApplication;
1213
import org.springframework.context.annotation.Bean;
13-
import org.springframework.data.cassandra.core.CassandraTemplate;
1414

1515
@SpringBootApplication
1616
public class AccessingDataCassandraApplication {

complete/src/main/resources/application.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ spring.cassandra.connection.init-query-timeout=10s
66
spring.cassandra.local-datacenter=datacenter1
77
spring.cassandra.keyspace-name=spring_cassandra
88

9+
### Properties for Docker Compose
10+
spring.docker.compose.file=classpath:docker/compose.yml
11+
spring.docker.compose.skip.in-tests=false
12+
13+
### Properties for Astra DB
914
# Credentials to Astra DB
1015
#astra.client-id=<CLIENT_ID>
1116
#astra.client-secret=<CLIENT_SECRET>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
cassandra:
3+
image: cassandra:4.0.7
4+
ports:
5+
- "9042:9042"
6+
healthcheck:
7+
test: ["CMD", "cqlsh", "-u", "cassandra", "-p", "cassandra"]
8+
interval: 10s
9+
timeout: 10s
10+
retries: 10

complete/src/test/java/com/example/accessingdatacassandra/AccessingDataCassandraApplicationTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package com.example.accessingdatacassandra;
22

3+
import java.util.List;
4+
35
import org.junit.jupiter.api.Test;
6+
7+
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties;
48
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.context.TestConfiguration;
10+
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
11+
import org.springframework.data.cassandra.config.SchemaAction;
12+
import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification;
13+
import org.springframework.data.cassandra.core.cql.keyspace.SpecificationBuilder;
14+
import org.springframework.lang.NonNull;
515

616
@SpringBootTest
717
class AccessingDataCassandraApplicationTests {
@@ -10,4 +20,34 @@ class AccessingDataCassandraApplicationTests {
1020
void contextLoads() {
1121
}
1222

23+
@TestConfiguration
24+
static class CreateKeyspaceConfiguration extends AbstractCassandraConfiguration {
25+
26+
private final CassandraProperties properties;
27+
28+
CreateKeyspaceConfiguration(CassandraProperties properties) {
29+
this.properties = properties;
30+
}
31+
32+
@NonNull
33+
@Override
34+
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
35+
CreateKeyspaceSpecification specification = SpecificationBuilder
36+
.createKeyspace(getKeyspaceName()).ifNotExists()
37+
.withSimpleReplication(1);
38+
39+
return List.of(specification);
40+
}
41+
42+
@Override
43+
public SchemaAction getSchemaAction() {
44+
return SchemaAction.RECREATE_DROP_UNUSED;
45+
}
46+
47+
@NonNull
48+
@Override
49+
protected String getKeyspaceName() {
50+
return this.properties.getKeyspaceName();
51+
}
52+
}
1353
}

initial/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
implementation 'org.springframework.boot:spring-boot-starter-data-cassandra'
1717
//implementation 'com.datastax.astra:astra-spring-boot-starter:0.1.13'
1818
testImplementation 'org.springframework.boot:spring-boot-starter-test'
19+
runtimeOnly "org.springframework.boot:spring-boot-docker-compose"
1920
}
2021

2122
test {

initial/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
<artifactId>spring-boot-starter-test</artifactId>
3939
<scope>test</scope>
4040
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-docker-compose</artifactId>
45+
<optional>true</optional>
46+
</dependency>
47+
4148
</dependencies>
4249

4350
<build>

initial/src/main/resources/application.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ spring.cassandra.connection.init-query-timeout=10s
66
spring.cassandra.local-datacenter=datacenter1
77
spring.cassandra.keyspace-name=spring_cassandra
88

9+
### Properties for Docker Compose
10+
spring.docker.compose.file=classpath:docker/compose.yml
11+
spring.docker.compose.skip.in-tests=false
12+
13+
### Properties for Astra DB
914
# Credentials to Astra DB
1015
#astra.client-id=<CLIENT_ID>
1116
#astra.client-secret=<CLIENT_SECRET>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
cassandra:
3+
image: cassandra:4.0.7
4+
ports:
5+
- "9042:9042"
6+
healthcheck:
7+
test: ["CMD", "cqlsh", "-u", "cassandra", "-p", "cassandra"]
8+
interval: 10s
9+
timeout: 10s
10+
retries: 10

0 commit comments

Comments
 (0)