@ -68,8 +68,11 @@ public class ConnectorsResourceTest {
@@ -68,8 +68,11 @@ public class ConnectorsResourceTest {
// URL construction properly, avoiding //, which will mess up routing in the REST server
private static final String LEADER_URL = "http://leader:8083/" ;
private static final String CONNECTOR_NAME = "test" ;
private static final String CONNECTOR_NAME_SPECIAL_CHARS = "t?a=b&c=d\rx=1.1\n>< \t`'\" x%y+z!#$&'()*+,:;=?@[]" ;
private static final String CONNECTOR_NAME_SPECIAL_CHARS = "ta/b&c=d//\\rx=1þ.1>< `'\" x%y+z!ሴ#$&'(æ)*+,:;=?ñ@[]ÿ" ;
private static final String CONNECTOR_NAME_CONTROL_SEQUENCES1 = "ta/b&c=drx=1\n.1>< `'\" x%y+z!#$&'()*+,:;=?@[]" ;
private static final String CONNECTOR2_NAME = "test2" ;
private static final String CONNECTOR_NAME_ALL_WHITESPACES = " \t\n \b" ;
private static final String CONNECTOR_NAME_PADDING_WHITESPACES = " " + CONNECTOR_NAME + " \n " ;
private static final Boolean FORWARD = true ;
private static final Map < String , String > CONNECTOR_CONFIG_SPECIAL_CHARS = new HashMap < > ( ) ;
static {
@ -82,6 +85,24 @@ public class ConnectorsResourceTest {
@@ -82,6 +85,24 @@ public class ConnectorsResourceTest {
CONNECTOR_CONFIG . put ( "name" , CONNECTOR_NAME ) ;
CONNECTOR_CONFIG . put ( "sample_config" , "test_config" ) ;
}
private static final Map < String , String > CONNECTOR_CONFIG_CONTROL_SEQUENCES = new HashMap < > ( ) ;
static {
CONNECTOR_CONFIG_CONTROL_SEQUENCES . put ( "name" , CONNECTOR_NAME_CONTROL_SEQUENCES1 ) ;
CONNECTOR_CONFIG_CONTROL_SEQUENCES . put ( "sample_config" , "test_config" ) ;
}
private static final Map < String , String > CONNECTOR_CONFIG_WITHOUT_NAME = new HashMap < > ( ) ;
static {
CONNECTOR_CONFIG_WITHOUT_NAME . put ( "sample_config" , "test_config" ) ;
}
private static final Map < String , String > CONNECTOR_CONFIG_WITH_EMPTY_NAME = new HashMap < > ( ) ;
static {
CONNECTOR_CONFIG_WITH_EMPTY_NAME . put ( ConnectorConfig . NAME_CONFIG , "" ) ;
CONNECTOR_CONFIG_WITH_EMPTY_NAME . put ( "sample_config" , "test_config" ) ;
}
private static final List < ConnectorTaskId > CONNECTOR_TASK_NAMES = Arrays . asList (
new ConnectorTaskId ( CONNECTOR_NAME , 0 ) ,
new ConnectorTaskId ( CONNECTOR_NAME , 1 )
@ -108,6 +129,12 @@ public class ConnectorsResourceTest {
@@ -108,6 +129,12 @@ public class ConnectorsResourceTest {
connectorsResource = new ConnectorsResource ( herder , null ) ;
}
private static final Map < String , String > getConnectorConfig ( Map < String , String > mapToClone ) {
Map < String , String > result = new HashMap < > ( ) ;
result . putAll ( mapToClone ) ;
return result ;
}
@Test
public void testListConnectors ( ) throws Throwable {
final Capture < Callback < Collection < String > > > cb = Capture . newInstance ( ) ;
@ -206,20 +233,59 @@ public class ConnectorsResourceTest {
@@ -206,20 +233,59 @@ public class ConnectorsResourceTest {
PowerMock . verifyAll ( ) ;
}
@Test ( expected = BadRequestException . class )
public void testCreateConnectorWithASlashInItsName ( ) throws Throwable {
String badConnectorName = CONNECTOR_NAME + "/" + "test" ;
@Test
public void testCreateConnectorNameTrimWhitespaces ( ) throws Throwable {
// Clone CONNECTOR_CONFIG_WITHOUT_NAME Map, as createConnector changes it (puts the name in it) and this
// will affect later tests
Map < String , String > inputConfig = getConnectorConfig ( CONNECTOR_CONFIG_WITHOUT_NAME ) ;
final CreateConnectorRequest bodyIn = new CreateConnectorRequest ( CONNECTOR_NAME_PADDING_WHITESPACES , inputConfig ) ;
final CreateConnectorRequest bodyOut = new CreateConnectorRequest ( CONNECTOR_NAME , CONNECTOR_CONFIG ) ;
final Capture < Callback < Herder . Created < ConnectorInfo > > > cb = Capture . newInstance ( ) ;
herder . putConnectorConfig ( EasyMock . eq ( bodyOut . name ( ) ) , EasyMock . eq ( bodyOut . config ( ) ) , EasyMock . eq ( false ) , EasyMock . capture ( cb ) ) ;
expectAndCallbackResult ( cb , new Herder . Created < > ( true , new ConnectorInfo ( bodyOut . name ( ) , bodyOut . config ( ) , CONNECTOR_TASK_NAMES , ConnectorType . SOURCE ) ) ) ;
PowerMock . replayAll ( ) ;
connectorsResource . createConnector ( FORWARD , bodyIn ) ;
CreateConnectorRequest body = new CreateConnectorRequest ( badConnectorName , Collections . singletonMap ( ConnectorConfig . NAME_CONFIG , badConnectorName ) ) ;
PowerMock . verifyAll ( ) ;
}
@Test
public void testCreateConnectorNameAllWhitespaces ( ) throws Throwable {
// Clone CONNECTOR_CONFIG_WITHOUT_NAME Map, as createConnector changes it (puts the name in it) and this
// will affect later tests
Map < String , String > inputConfig = getConnectorConfig ( CONNECTOR_CONFIG_WITHOUT_NAME ) ;
final CreateConnectorRequest bodyIn = new CreateConnectorRequest ( CONNECTOR_NAME_ALL_WHITESPACES , inputConfig ) ;
final CreateConnectorRequest bodyOut = new CreateConnectorRequest ( "" , CONNECTOR_CONFIG_WITH_EMPTY_NAME ) ;
final Capture < Callback < Herder . Created < ConnectorInfo > > > cb = Capture . newInstance ( ) ;
herder . putConnectorConfig ( EasyMock . eq ( CONNECTOR_NAME ) , EasyMock . eq ( body . config ( ) ) , EasyMock . eq ( false ) , EasyMock . capture ( cb ) ) ;
expectAndCallbackResult ( cb , new Herder . Created < > ( true , new ConnectorInfo ( CONNECTOR_NAME , CONNECTOR_CONFIG , CONNECTOR_TASK_NAMES ,
ConnectorType . SOURCE ) ) ) ;
herder . putConnectorConfig ( EasyMock . eq ( bodyOut . name ( ) ) , EasyMock . eq ( bodyOut . config ( ) ) , EasyMock . eq ( false ) , EasyMock . capture ( cb ) ) ;
expectAndCallbackResult ( cb , new Herder . Created < > ( true , new ConnectorInfo ( bodyOut . name ( ) , bodyOut . config ( ) , CONNECTOR_TASK_NAMES , ConnectorType . SOURCE ) ) ) ;
PowerMock . replayAll ( ) ;
connectorsResource . createConnector ( FORWARD , body ) ;
connectorsResource . createConnector ( FORWARD , bodyIn ) ;
PowerMock . verifyAll ( ) ;
}
@Test
public void testCreateConnectorNoName ( ) throws Throwable {
// Clone CONNECTOR_CONFIG_WITHOUT_NAME Map, as createConnector changes it (puts the name in it) and this
// will affect later tests
Map < String , String > inputConfig = getConnectorConfig ( CONNECTOR_CONFIG_WITHOUT_NAME ) ;
final CreateConnectorRequest bodyIn = new CreateConnectorRequest ( null , inputConfig ) ;
final CreateConnectorRequest bodyOut = new CreateConnectorRequest ( "" , CONNECTOR_CONFIG_WITH_EMPTY_NAME ) ;
final Capture < Callback < Herder . Created < ConnectorInfo > > > cb = Capture . newInstance ( ) ;
herder . putConnectorConfig ( EasyMock . eq ( bodyOut . name ( ) ) , EasyMock . eq ( bodyOut . config ( ) ) , EasyMock . eq ( false ) , EasyMock . capture ( cb ) ) ;
expectAndCallbackResult ( cb , new Herder . Created < > ( true , new ConnectorInfo ( bodyOut . name ( ) , bodyOut . config ( ) , CONNECTOR_TASK_NAMES , ConnectorType . SOURCE ) ) ) ;
PowerMock . replayAll ( ) ;
connectorsResource . createConnector ( FORWARD , bodyIn ) ;
PowerMock . verifyAll ( ) ;
}
@ -342,6 +408,24 @@ public class ConnectorsResourceTest {
@@ -342,6 +408,24 @@ public class ConnectorsResourceTest {
PowerMock . verifyAll ( ) ;
}
@Test
public void testCreateConnectorWithControlSequenceInName ( ) throws Throwable {
CreateConnectorRequest body = new CreateConnectorRequest ( CONNECTOR_NAME_CONTROL_SEQUENCES1 , Collections . singletonMap ( ConnectorConfig . NAME_CONFIG , CONNECTOR_NAME_CONTROL_SEQUENCES1 ) ) ;
final Capture < Callback < Herder . Created < ConnectorInfo > > > cb = Capture . newInstance ( ) ;
herder . putConnectorConfig ( EasyMock . eq ( CONNECTOR_NAME_CONTROL_SEQUENCES1 ) , EasyMock . eq ( body . config ( ) ) , EasyMock . eq ( false ) , EasyMock . capture ( cb ) ) ;
expectAndCallbackResult ( cb , new Herder . Created < > ( true , new ConnectorInfo ( CONNECTOR_NAME_CONTROL_SEQUENCES1 , CONNECTOR_CONFIG ,
CONNECTOR_TASK_NAMES , ConnectorType . SOURCE ) ) ) ;
PowerMock . replayAll ( ) ;
String rspLocation = connectorsResource . createConnector ( FORWARD , body ) . getLocation ( ) . toString ( ) ;
String decoded = new URI ( rspLocation ) . getPath ( ) ;
Assert . assertEquals ( "/connectors/" + CONNECTOR_NAME_CONTROL_SEQUENCES1 , decoded ) ;
PowerMock . verifyAll ( ) ;
}
@Test
public void testPutConnectorConfigWithSpecialCharsInName ( ) throws Throwable {
final Capture < Callback < Herder . Created < ConnectorInfo > > > cb = Capture . newInstance ( ) ;
@ -359,6 +443,23 @@ public class ConnectorsResourceTest {
@@ -359,6 +443,23 @@ public class ConnectorsResourceTest {
PowerMock . verifyAll ( ) ;
}
@Test
public void testPutConnectorConfigWithControlSequenceInName ( ) throws Throwable {
final Capture < Callback < Herder . Created < ConnectorInfo > > > cb = Capture . newInstance ( ) ;
herder . putConnectorConfig ( EasyMock . eq ( CONNECTOR_NAME_CONTROL_SEQUENCES1 ) , EasyMock . eq ( CONNECTOR_CONFIG_CONTROL_SEQUENCES ) , EasyMock . eq ( true ) , EasyMock . capture ( cb ) ) ;
expectAndCallbackResult ( cb , new Herder . Created < > ( true , new ConnectorInfo ( CONNECTOR_NAME_CONTROL_SEQUENCES1 , CONNECTOR_CONFIG_CONTROL_SEQUENCES , CONNECTOR_TASK_NAMES ,
ConnectorType . SINK ) ) ) ;
PowerMock . replayAll ( ) ;
String rspLocation = connectorsResource . putConnectorConfig ( CONNECTOR_NAME_CONTROL_SEQUENCES1 , FORWARD , CONNECTOR_CONFIG_CONTROL_SEQUENCES ) . getLocation ( ) . toString ( ) ;
String decoded = new URI ( rspLocation ) . getPath ( ) ;
Assert . assertEquals ( "/connectors/" + CONNECTOR_NAME_CONTROL_SEQUENCES1 , decoded ) ;
PowerMock . verifyAll ( ) ;
}
@Test ( expected = BadRequestException . class )
public void testPutConnectorConfigNameMismatch ( ) throws Throwable {
Map < String , String > connConfig = new HashMap < > ( CONNECTOR_CONFIG ) ;