forked from sky-uk/cqlmigrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sky-uk#31 from sky-uk/add-authentication
Add authentication
- Loading branch information
Showing
12 changed files
with
182 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/uk/sky/cqlmigrate/CassandraClusterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package uk.sky.cqlmigrate; | ||
|
||
import com.datastax.driver.core.Cluster; | ||
|
||
public class CassandraClusterFactory { | ||
|
||
/** | ||
* Creates an instance of cassandra {@link Cluster} based on the provided configuration | ||
* | ||
* @return a configured Cluster | ||
*/ | ||
public static Cluster createCluster(String[] hosts, int port, String username, String password) { | ||
Cluster.Builder builder = Cluster.builder() | ||
.addContactPoints(hosts) | ||
.withPort(port); | ||
|
||
if (username != null && password != null) { | ||
builder = builder.withCredentials(username, password); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/java/uk/sky/cqlmigrate/CassandraClusterFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package uk.sky.cqlmigrate; | ||
|
||
import com.datastax.driver.core.Cluster; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.anyInt; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.powermock.api.mockito.PowerMockito.mockStatic; | ||
import static org.powermock.api.mockito.PowerMockito.when; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({Cluster.class}) | ||
public class CassandraClusterFactoryTest { | ||
|
||
@Mock | ||
private Cluster.Builder clusterBuilderMock; | ||
|
||
@Mock | ||
private Cluster clusterMock; | ||
|
||
|
||
@Before | ||
public void setUp() throws Exception { | ||
mockStatic(Cluster.class); | ||
when(Cluster.builder()).thenReturn(clusterBuilderMock); | ||
when(clusterBuilderMock.addContactPoints(any(String[].class))).thenReturn(clusterBuilderMock); | ||
when(clusterBuilderMock.withPort(anyInt())).thenReturn(clusterBuilderMock); | ||
when(clusterBuilderMock.withCredentials(any(String.class), any(String.class))).thenReturn(clusterBuilderMock); | ||
} | ||
|
||
@Test | ||
public void createClusterWithCredentials() throws Exception { | ||
String expectedUser = "cassandra-user"; | ||
String expectedPassword = "cassandra-password"; | ||
|
||
//when | ||
CassandraClusterFactory.createCluster(new String[]{"host1"}, 0, expectedUser, expectedPassword); | ||
|
||
//then | ||
verify(clusterBuilderMock).withCredentials(expectedUser, expectedPassword); | ||
} | ||
|
||
@Test | ||
public void createClusterWithNullCredentials() throws Exception { | ||
String expectedUser = null; | ||
String expectedPassword = null; | ||
|
||
//when | ||
CassandraClusterFactory.createCluster(new String[]{"host1"}, 0, expectedUser, expectedPassword); | ||
|
||
//then | ||
verify(clusterBuilderMock, never()).withCredentials(expectedUser, expectedPassword); | ||
} | ||
|
||
|
||
@Test | ||
public void createClusterWithNullPassword() throws Exception { | ||
String expectedUser = "cassandra"; | ||
String expectedPassword = null; | ||
|
||
//when | ||
CassandraClusterFactory.createCluster(new String[]{"host1"}, 0, expectedUser, expectedPassword); | ||
|
||
//then | ||
verify(clusterBuilderMock, never()).withCredentials(expectedUser, expectedPassword); | ||
} | ||
|
||
@Test | ||
public void createClusterWithNullUser() throws Exception { | ||
String expectedUser = null; | ||
String expectedPassword = "cassandra"; | ||
|
||
//when | ||
CassandraClusterFactory.createCluster(new String[]{"host1"}, 0, expectedUser, expectedPassword); | ||
|
||
//then | ||
verify(clusterBuilderMock, never()).withCredentials(expectedUser, expectedPassword); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.