This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
forked from hector-client/hector
-
Notifications
You must be signed in to change notification settings - Fork 31
/
CassandraClusterTest.java
200 lines (154 loc) · 8.09 KB
/
CassandraClusterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package me.prettyprint.cassandra.service;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
import me.prettyprint.cassandra.BaseEmbededServerSetupTest;
import me.prettyprint.cassandra.model.BasicColumnDefinition;
import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ColumnIndexType;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.TokenRange;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class CassandraClusterTest extends BaseEmbededServerSetupTest {
private ThriftCluster cassandraCluster;
private CassandraHostConfigurator cassandraHostConfigurator;
@Before
public void setupCase() throws TTransportException, TException, IllegalArgumentException,
NotFoundException, UnknownHostException, Exception {
cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170");
cassandraCluster = new ThriftCluster("Test Cluster", cassandraHostConfigurator);
}
@Test
public void testDescribeKeyspaces() throws Exception {
List<KeyspaceDefinition> keyspaces = cassandraCluster.describeKeyspaces();
assertEquals(2,keyspaces.size());
}
@Test
public void testDescribeClusterName() throws Exception {
assertEquals("Test Cluster",cassandraCluster.describeClusterName());
}
/**
* This will need to be updated as we update the Thrift API, but probably a good sanity check
*
*/
@Test
public void testDescribeThriftVersion() throws Exception {
assertEquals("19.10.0",cassandraCluster.describeThriftVersion());
}
@Test
public void testDescribeRing() throws Exception {
List<TokenRange> ring = cassandraCluster.describeRing("Keyspace1");
assertEquals(1, ring.size());
}
@Test
public void testDescribeKeyspace() throws Exception {
KeyspaceDefinition keyspaceDetail = cassandraCluster.describeKeyspace("Keyspace1");
assertNotNull(keyspaceDetail);
assertEquals(22, keyspaceDetail.getCfDefs().size());
}
@Test
public void testDescribePartitioner() throws Exception {
String partitioner = cassandraCluster.describePartitioner();
assertEquals("org.apache.cassandra.dht.OrderPreservingPartitioner",partitioner);
}
@Test
public void testAddDropColumnFamily() throws Exception {
ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("Keyspace1", "DynCf");
cassandraCluster.addColumnFamily(cfDef);
String cfid2 = cassandraCluster.dropColumnFamily("Keyspace1", "DynCf");
assertNotNull(cfid2);
// Let's wait for agreement
cassandraCluster.addColumnFamily(cfDef, true);
cfid2 = cassandraCluster.dropColumnFamily("Keyspace1", "DynCf", true);
assertNotNull(cfid2);
}
@Test
public void testTruncateColumnFamily() throws Exception {
ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("Keyspace1", "TruncateableCf");
cassandraCluster.addColumnFamily(cfDef);
Keyspace workingKeyspace = HFactory.createKeyspace("Keyspace1", cassandraCluster);
Mutator<String> mutator = HFactory.createMutator(workingKeyspace, StringSerializer.get());
mutator.insert("mykey", "TruncateableCf", HFactory.createStringColumn("mycolname", "myval"));
ColumnQuery<String,String,String> q = HFactory.createColumnQuery(workingKeyspace, StringSerializer.get(), StringSerializer.get(), StringSerializer.get());
q.setKey("mykey").setName("mycolname").setColumnFamily("TruncateableCf");
assertEquals("myval",q.execute().get().getValue());
cassandraCluster.truncate("Keyspace1", "TruncateableCf");
assertNull(q.execute().get());
}
@Test
public void testAddDropKeyspace() throws Exception {
ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("DynKeyspace", "DynCf");
cassandraCluster.addKeyspace(
new ThriftKsDef("DynKeyspace", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef)));
String ksid2 = cassandraCluster.dropKeyspace("DynKeyspace");
assertNotNull(ksid2);
// Now let's wait for schema agreement.
cassandraCluster.addKeyspace(new ThriftKsDef("DynKeyspace", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef)), true);
ksid2 = cassandraCluster.dropKeyspace("DynKeyspace", true);
assertNotNull(ksid2);
}
@Test
public void testEditKeyspace() throws Exception {
BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition();
columnFamilyDefinition.setKeyspaceName("DynKeyspace2");
columnFamilyDefinition.setName("DynamicCF");
ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition);
KeyspaceDefinition keyspaceDefinition =
HFactory.createKeyspaceDefinition("DynKeyspace2", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef));
cassandraCluster.addKeyspace(keyspaceDefinition);
keyspaceDefinition =
HFactory.createKeyspaceDefinition("DynKeyspace2", "org.apache.cassandra.locator.SimpleStrategy", 2, null);
cassandraCluster.updateKeyspace(keyspaceDefinition);
KeyspaceDefinition fromCluster = cassandraCluster.describeKeyspace("DynKeyspace2");
assertEquals(2,fromCluster.getReplicationFactor());
cassandraCluster.dropKeyspace("DynKeyspace2");
}
@Test
public void testEditColumnFamily() throws Exception {
BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition();
columnFamilyDefinition.setKeyspaceName("DynKeyspace3");
columnFamilyDefinition.setName("DynamicCF");
ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition);
KeyspaceDefinition keyspaceDefinition =
HFactory.createKeyspaceDefinition("DynKeyspace3", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef));
cassandraCluster.addKeyspace(keyspaceDefinition);
KeyspaceDefinition fromCluster = cassandraCluster.describeKeyspace("DynKeyspace3");
cfDef = fromCluster.getCfDefs().get(0);
columnFamilyDefinition = new BasicColumnFamilyDefinition(cfDef);
BasicColumnDefinition columnDefinition = new BasicColumnDefinition();
columnDefinition.setName(StringSerializer.get().toByteBuffer("birthdate"));
columnDefinition.setIndexType(ColumnIndexType.KEYS);
columnDefinition.setValidationClass(ComparatorType.LONGTYPE.getClassName());
columnFamilyDefinition.addColumnDefinition(columnDefinition);
columnDefinition = new BasicColumnDefinition();
columnDefinition.setName(StringSerializer.get().toByteBuffer("nonindexed_field"));
columnDefinition.setValidationClass(ComparatorType.LONGTYPE.getClassName());
columnFamilyDefinition.addColumnDefinition(columnDefinition);
cassandraCluster.updateColumnFamily(new ThriftCfDef(columnFamilyDefinition));
fromCluster = cassandraCluster.describeKeyspace("DynKeyspace3");
assertEquals("birthdate",StringSerializer.get().fromByteBuffer(fromCluster.getCfDefs().get(0).getColumnMetadata().get(0).getName()));
assertEquals("nonindexed_field",StringSerializer.get().fromByteBuffer(fromCluster.getCfDefs().get(0).getColumnMetadata().get(1).getName()));
}
@Test
public void testAddEmptyKeyspace() throws Exception {
cassandraCluster.addKeyspace(new ThriftKsDef("DynKeyspaceEmpty"));
assertNotNull(cassandraCluster.describeKeyspace("DynKeyspaceEmpty"));
String ksid2 = cassandraCluster.dropKeyspace("DynKeyspaceEmpty");
assertNotNull(ksid2);
}
}