This repository has been archived by the owner on May 27, 2022. It is now read-only.
forked from hector-client/hector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NodeDiscoveryTest.java
98 lines (81 loc) · 3.87 KB
/
NodeDiscoveryTest.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
package me.prettyprint.cassandra.connection;
import java.util.LinkedList;
import java.util.List;
import junit.framework.Assert;
import me.prettyprint.cassandra.service.CassandraHost;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.cassandra.service.ThriftCluster;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;
import org.apache.cassandra.thrift.EndpointDetails;
import org.apache.cassandra.thrift.TokenRange;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import com.google.common.collect.Sets;
public class NodeDiscoveryTest {
public static final String TEST_CLUSTER_NAME = "TestCluster";
CassandraHostConfigurator cassandraHostConfigurator;
HConnectionManager connectionManager;
ThriftCluster cluster;
@Before
public void setup() {
cassandraHostConfigurator = new CassandraHostConfigurator("localhost");
connectionManager = Mockito.mock(HConnectionManager.class);
cluster = Mockito.mock(ThriftCluster.class);
Mockito.when(cluster.getConfigurator()).thenReturn(cassandraHostConfigurator);
final KeyspaceDefinition kdef = Mockito.mock(KeyspaceDefinition.class);
Mockito.when(kdef.getName()).thenReturn("TestKeyspace");
Mockito.when(cluster.describeKeyspaces()).thenReturn(new LinkedList<KeyspaceDefinition>() {{
add(kdef);
}});
}
@Test
public void testDoAdd() {
List<TokenRange> tokens = createRange("localhost", "google.com");
Mockito.when(cluster.describeRing("TestKeyspace")).thenReturn(tokens);
Mockito.when(cluster.getName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getClusterName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getHosts()).thenReturn(Sets.newHashSet(new CassandraHost("localhost",9160)));
HFactory.setClusterForTest(cluster);
NodeDiscovery q = new NodeDiscovery(cassandraHostConfigurator, connectionManager);
q.doAddNodes();
Mockito.verify(connectionManager).addCassandraHost(new CassandraHost("google.com",9160));
}
@Test
public void testMultipleAdded() {
List<TokenRange> tokens = createRange("localhost", "google.com", "yahoo.com", "datastax.com");
Mockito.when(cluster.describeRing("TestKeyspace")).thenReturn(tokens);
Mockito.when(cluster.getName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getClusterName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getHosts()).thenReturn(Sets.newHashSet(new CassandraHost("localhost",9160)));
HFactory.setClusterForTest(cluster);
NodeDiscovery q = new NodeDiscovery(cassandraHostConfigurator, connectionManager);
q.doAddNodes();
Mockito.verify(connectionManager).addCassandraHost(new CassandraHost("google.com",9160));
Mockito.verify(connectionManager).addCassandraHost(new CassandraHost("yahoo.com",9160));
Mockito.verify(connectionManager).addCassandraHost(new CassandraHost("datastax.com",9160));
}
@Test
public void testNoneAdded() {
List<TokenRange> tokens = createRange("localhost");
Mockito.when(cluster.describeRing("TestKeyspace")).thenReturn(tokens);
Mockito.when(cluster.getName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getClusterName()).thenReturn(TEST_CLUSTER_NAME);
Mockito.when(connectionManager.getHosts()).thenReturn(Sets.newHashSet(new CassandraHost("localhost",9160)));
HFactory.setClusterForTest(cluster);
NodeDiscovery q = new NodeDiscovery(cassandraHostConfigurator, connectionManager);
Assert.assertEquals(0, q.discoverNodes().size());
}
private static List<TokenRange> createRange(String ... hosts) {
List<TokenRange> ret = new LinkedList<TokenRange>();
for(final String h: hosts) {
TokenRange tr1 = Mockito.mock(TokenRange.class);
Mockito.when(tr1.getEndpoint_details()).thenReturn(new LinkedList<EndpointDetails>(){{
add(new EndpointDetails(h, "DC1"));
}});
ret.add(tr1);
}
return ret;
}
}