|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package smoketest.data.cassandra; |
| 18 | + |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 22 | +import com.datastax.oss.driver.api.core.CqlSessionBuilder; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.testcontainers.junit.jupiter.Container; |
| 25 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.boot.test.context.SpringBootTest; |
| 29 | +import org.springframework.boot.test.context.TestConfiguration; |
| 30 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 31 | +import org.springframework.context.annotation.Bean; |
| 32 | +import org.springframework.data.cassandra.core.CassandraTemplate; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * Smoke tests for Cassandra with SSL. |
| 38 | + * |
| 39 | + * @author Scott Frederick |
| 40 | + */ |
| 41 | +@Testcontainers(disabledWithoutDocker = true) |
| 42 | +@SpringBootTest(properties = { "spring.cassandra.schema-action=create-if-not-exists", |
| 43 | + "spring.cassandra.connection.connect-timeout=60s", "spring.cassandra.connection.init-query-timeout=60s", |
| 44 | + "spring.cassandra.request.timeout=60s", "spring.cassandra.ssl.bundle=client", |
| 45 | + "spring.ssl.bundle.jks.client.keystore.location=classpath:ssl/test-client.p12", |
| 46 | + "spring.ssl.bundle.jks.client.keystore.password=password", |
| 47 | + "spring.ssl.bundle.jks.client.truststore.location=classpath:ssl/test-ca.p12", |
| 48 | + "spring.ssl.bundle.jks.client.truststore.password=password" }) |
| 49 | +class SampleCassandraApplicationSslTests { |
| 50 | + |
| 51 | + @Container |
| 52 | + @ServiceConnection |
| 53 | + static final SecureCassandraContainer secureCassandra = new SecureCassandraContainer(); |
| 54 | + |
| 55 | + @Autowired |
| 56 | + private CassandraTemplate cassandraTemplate; |
| 57 | + |
| 58 | + @Autowired |
| 59 | + private SampleRepository repository; |
| 60 | + |
| 61 | + @Test |
| 62 | + void testRepository() { |
| 63 | + SampleEntity entity = new SampleEntity(); |
| 64 | + entity.setDescription("Look, new @DataCassandraTest!"); |
| 65 | + String id = UUID.randomUUID().toString(); |
| 66 | + entity.setId(id); |
| 67 | + SampleEntity savedEntity = this.repository.save(entity); |
| 68 | + SampleEntity getEntity = this.cassandraTemplate.selectOneById(id, SampleEntity.class); |
| 69 | + assertThat(getEntity).isNotNull(); |
| 70 | + assertThat(getEntity.getId()).isNotNull(); |
| 71 | + assertThat(getEntity.getId()).isEqualTo(savedEntity.getId()); |
| 72 | + this.repository.deleteAll(); |
| 73 | + } |
| 74 | + |
| 75 | + @TestConfiguration(proxyBeanMethods = false) |
| 76 | + static class KeyspaceTestConfiguration { |
| 77 | + |
| 78 | + @Bean |
| 79 | + CqlSession cqlSession(CqlSessionBuilder cqlSessionBuilder) { |
| 80 | + try (CqlSession session = cqlSessionBuilder.build()) { |
| 81 | + session.execute("CREATE KEYSPACE IF NOT EXISTS boot_test" |
| 82 | + + " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"); |
| 83 | + } |
| 84 | + return cqlSessionBuilder.withKeyspace("boot_test").build(); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments