|
1 | 1 | package org.baeldung;
|
2 | 2 |
|
| 3 | +import java.io.IOException; |
| 4 | +import java.util.HashMap; |
| 5 | + |
| 6 | +import org.apache.cassandra.exceptions.ConfigurationException; |
| 7 | +import org.apache.thrift.transport.TTransportException; |
3 | 8 | import org.baeldung.spring.data.cassandra.config.CassandraConfig;
|
| 9 | +import org.baeldung.spring.data.cassandra.model.Book; |
| 10 | +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.AfterClass; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.BeforeClass; |
4 | 15 | import org.junit.Test;
|
5 | 16 | import org.junit.runner.RunWith;
|
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.cassandra.core.cql.CqlIdentifier; |
| 19 | +import org.springframework.data.cassandra.core.CassandraAdminOperations; |
6 | 20 | import org.springframework.test.context.ContextConfiguration;
|
7 | 21 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
8 | 22 |
|
| 23 | +import com.datastax.driver.core.Cluster; |
| 24 | +import com.datastax.driver.core.Session; |
| 25 | + |
9 | 26 | @RunWith(SpringJUnit4ClassRunner.class)
|
10 | 27 | @ContextConfiguration(classes = CassandraConfig.class)
|
11 | 28 | public class SpringContextIntegrationTest {
|
12 | 29 |
|
13 |
| - @Test |
| 30 | + public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"; |
| 31 | + |
| 32 | + public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;"; |
| 33 | + |
| 34 | + public static final String DATA_TABLE_NAME = "book"; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private CassandraAdminOperations adminTemplate; |
| 38 | + |
| 39 | + @BeforeClass |
| 40 | + public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { |
| 41 | + EmbeddedCassandraServerHelper.startEmbeddedCassandra(); |
| 42 | + final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build(); |
| 43 | + final Session session = cluster.connect(); |
| 44 | + session.execute(KEYSPACE_CREATION_QUERY); |
| 45 | + session.execute(KEYSPACE_ACTIVATE_QUERY); |
| 46 | + Thread.sleep(5000); |
| 47 | + } |
| 48 | + |
| 49 | + @Before |
| 50 | + public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException { |
| 51 | + adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
14 | 55 | public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
15 | 56 | }
|
| 57 | + |
| 58 | + @After |
| 59 | + public void dropTable() { |
| 60 | + adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME)); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterClass |
| 64 | + public static void stopCassandraEmbedded() { |
| 65 | + EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); |
| 66 | + } |
16 | 67 | }
|
0 commit comments