first commit for new repo
[sdc/sdc-titan-cassandra.git] / src / test / java / com / thinkaurelius / titan / diskstorage / cassandra / AbstractCassandraStoreTest.java
1 package com.thinkaurelius.titan.diskstorage.cassandra;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.Collections;
8 import java.util.Map;
9
10 import com.thinkaurelius.titan.diskstorage.BackendException;
11 import com.thinkaurelius.titan.diskstorage.configuration.Configuration;
12 import com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration;
13 import org.junit.Test;
14 import org.junit.experimental.categories.Category;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import com.google.common.collect.ImmutableMap;
19 import com.thinkaurelius.titan.diskstorage.KeyColumnValueStoreTest;
20 import com.thinkaurelius.titan.diskstorage.keycolumnvalue.StoreFeatures;
21 import com.thinkaurelius.titan.testcategory.OrderedKeyStoreTests;
22 import com.thinkaurelius.titan.testcategory.UnorderedKeyStoreTests;
23
24 public abstract class AbstractCassandraStoreTest extends KeyColumnValueStoreTest {
25
26     private static final Logger log =
27             LoggerFactory.getLogger(AbstractCassandraStoreTest.class);
28     private static final String TEST_CF_NAME = "testcf";
29     private static final String DEFAULT_COMPRESSOR_PACKAGE = "org.apache.cassandra.io.compress";
30
31     public abstract ModifiableConfiguration getBaseStorageConfiguration();
32
33     public abstract AbstractCassandraStoreManager openStorageManager(Configuration c) throws BackendException;
34
35     @Test
36     @Category({ UnorderedKeyStoreTests.class })
37     public void testUnorderedConfiguration() {
38         if (!manager.getFeatures().hasUnorderedScan()) {
39             log.warn(
40                 "Can't test key-unordered features on incompatible store.  "
41                 + "This warning could indicate reduced test coverage and "
42                 + "a broken JUnit configuration.  Skipping test {}.",
43                 name.getMethodName());
44             return;
45         }
46
47         StoreFeatures features = manager.getFeatures();
48         assertFalse(features.isKeyOrdered());
49         assertFalse(features.hasLocalKeyPartition());
50     }
51
52     @Test
53     @Category({ OrderedKeyStoreTests.class })
54     public void testOrderedConfiguration() {
55         if (!manager.getFeatures().hasOrderedScan()) {
56             log.warn(
57                 "Can't test key-ordered features on incompatible store.  "
58                 + "This warning could indicate reduced test coverage and "
59                 + "a broken JUnit configuration.  Skipping test {}.",
60                 name.getMethodName());
61             return;
62         }
63
64         StoreFeatures features = manager.getFeatures();
65         assertTrue(features.isKeyOrdered());
66     }
67
68     @Test
69     public void testDefaultCFCompressor() throws BackendException {
70
71         final String cf = TEST_CF_NAME + "_snappy";
72
73         AbstractCassandraStoreManager mgr = openStorageManager();
74
75         mgr.openDatabase(cf);
76
77         Map<String, String> defaultCfCompressionOps =
78                 new ImmutableMap.Builder<String, String>()
79                 .put("sstable_compression", DEFAULT_COMPRESSOR_PACKAGE + "." + AbstractCassandraStoreManager.CF_COMPRESSION_TYPE.getDefaultValue())
80                 .put("chunk_length_kb", "64")
81                 .build();
82
83         assertEquals(defaultCfCompressionOps, mgr.getCompressionOptions(cf));
84     }
85
86     @Test
87     public void testCustomCFCompressor() throws BackendException {
88
89         final String cname = "DeflateCompressor";
90         final int ckb = 128;
91         final String cf = TEST_CF_NAME + "_gzip";
92
93         ModifiableConfiguration config = getBaseStorageConfiguration();
94         config.set(AbstractCassandraStoreManager.CF_COMPRESSION_TYPE,cname);
95         config.set(AbstractCassandraStoreManager.CF_COMPRESSION_BLOCK_SIZE,ckb);
96
97         AbstractCassandraStoreManager mgr = openStorageManager(config);
98
99         // N.B.: clearStorage() truncates CFs but does not delete them
100         mgr.openDatabase(cf);
101
102         final Map<String, String> expected = ImmutableMap
103                 .<String, String> builder()
104                 .put("sstable_compression",
105                         DEFAULT_COMPRESSOR_PACKAGE + "." + cname)
106                 .put("chunk_length_kb", String.valueOf(ckb)).build();
107
108         assertEquals(expected, mgr.getCompressionOptions(cf));
109     }
110
111     @Test
112     public void testDisableCFCompressor() throws BackendException {
113
114         final String cf = TEST_CF_NAME + "_nocompress";
115
116         ModifiableConfiguration config = getBaseStorageConfiguration();
117         config.set(AbstractCassandraStoreManager.CF_COMPRESSION,false);
118         AbstractCassandraStoreManager mgr = openStorageManager(config);
119
120         // N.B.: clearStorage() truncates CFs but does not delete them
121         mgr.openDatabase(cf);
122
123         assertEquals(Collections.emptyMap(), mgr.getCompressionOptions(cf));
124     }
125
126     @Test
127     public void testTTLSupported() throws Exception {
128         StoreFeatures features = manager.getFeatures();
129         assertTrue(features.hasCellTTL());
130     }
131
132     @Override
133     public AbstractCassandraStoreManager openStorageManager() throws BackendException {
134         return openStorageManager(getBaseStorageConfiguration());
135     }
136 }