Wrapped the describeRing() in CassandraTemplate with the

SessionCallback.
Added Unit Test for CassandraOperations, which uses the cassandra-unit
embedded Cassandra DB.
This commit is contained in:
dwebb
2013-11-11 14:18:54 -05:00
parent eba84d0423
commit d881a17af0
5 changed files with 829 additions and 3 deletions

View File

@@ -0,0 +1,45 @@
package org.springframework.data.cassandra.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraKeyspaceFactoryBean;
import org.springframework.data.cassandra.core.CassandraTemplate;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Cluster.Builder;
/**
* Setup any spring configuration for unit tests
*
* @author David Webb
*
*/
@Configuration
public class TestConfig extends AbstractCassandraConfiguration {
/* (non-Javadoc)
* @see org.springframework.data.cassandra.config.AbstractCassandraConfiguration#getKeyspaceName()
*/
@Override
protected String getKeyspaceName() {
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.config.AbstractCassandraConfiguration#cluster()
*/
@Override
@Bean
public Cluster cluster() throws Exception {
Builder builder = Cluster.builder();
builder.addContactPoint("127.0.0.1");
return builder.build();
}
}

View File

@@ -0,0 +1,81 @@
/**
* All BrightMove Code is Copyright 2004-2013 BrightMove Inc.
* Modification of code without the express written consent of
* BrightMove, Inc. is strictly forbidden.
*
* Author: David Webb (dwebb@brightmove.com)
* Created On: Nov 11, 2013
*/
package org.springframework.data.cassandra.template;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.util.List;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.config.TestConfig;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.vo.RingMember;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.datastax.driver.core.Session;
/**
* @author David Webb (dwebb@brightmove.com)
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration (classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class)
public class CassandraOperationsTest {
@Autowired
private CassandraTemplate cassandraTemplate;
private static Logger log = LoggerFactory.getLogger(CassandraOperationsTest.class);
protected Session session;
@BeforeClass
public static void startCassandra()
throws IOException, TTransportException, ConfigurationException, InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml");
}
@Test
public void ringTest() {
List<RingMember> ring = cassandraTemplate.describeRing();
/*
* There must be 1 node in the cluster if the embedded server is running.
*/
assertNotNull(ring);
for (RingMember h: ring) {
log.info(h.address);
}
}
@After
public void clearCassandra() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
@AfterClass
public static void stopCassandra() {
EmbeddedCassandraServerHelper.stopEmbeddedCassandra();
}
}