1818import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .BATCH_SIZE_CONFIG ;
1919import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .BEHAVIOR_ON_MALFORMED_DOCS_CONFIG ;
2020import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .BEHAVIOR_ON_NULL_VALUES_CONFIG ;
21- import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .CONNECTION_PASSWORD_CONFIG ;
2221import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .CONNECTION_URL_CONFIG ;
23- import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .CONNECTION_USERNAME_CONFIG ;
2422import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .DATA_STREAM_DATASET_CONFIG ;
2523import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .DATA_STREAM_TYPE_CONFIG ;
2624import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .IGNORE_KEY_CONFIG ;
2927import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .MAX_IN_FLIGHT_REQUESTS_CONFIG ;
3028import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .MAX_RETRIES_CONFIG ;
3129import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .RETRY_BACKOFF_MS_CONFIG ;
32- import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .SECURITY_PROTOCOL_CONFIG ;
33- import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .SSL_CONFIG_PREFIX ;
3430import static io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .WRITE_METHOD_CONFIG ;
3531import static org .junit .Assert .assertEquals ;
3632import static org .junit .Assert .assertFalse ;
4642
4743import io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .BehaviorOnMalformedDoc ;
4844import io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .BehaviorOnNullValues ;
49- import io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .SecurityProtocol ;
5045import io .confluent .connect .elasticsearch .ElasticsearchSinkConnectorConfig .WriteMethod ;
5146import io .confluent .connect .elasticsearch .helper .ElasticsearchContainer ;
5247import io .confluent .connect .elasticsearch .helper .ElasticsearchHelperClient ;
5954import java .util .Set ;
6055import java .util .concurrent .CompletableFuture ;
6156import java .util .concurrent .TimeUnit ;
62- import org .apache .kafka .common .config .SslConfigs ;
63- import org .apache .kafka .common .record .TimestampType ;
6457import org .apache .kafka .connect .data .Schema ;
6558import org .apache .kafka .connect .data .SchemaBuilder ;
6659import org .apache .kafka .connect .data .Struct ;
6760import org .apache .kafka .connect .errors .ConnectException ;
6861import org .apache .kafka .connect .sink .ErrantRecordReporter ;
6962import org .apache .kafka .connect .sink .SinkRecord ;
70- import org .apache .kafka .test .TestUtils ;
71- import org .elasticsearch .ElasticsearchStatusException ;
7263import org .elasticsearch .action .DocWriteRequest ;
7364import org .elasticsearch .action .bulk .BulkItemResponse ;
7465import org .elasticsearch .index .VersionType ;
7566import org .elasticsearch .search .SearchHit ;
7667import org .junit .After ;
77- import org .junit .AfterClass ;
7868import org .junit .Before ;
7969import org .junit .BeforeClass ;
8070import org .junit .Test ;
8171
82- public class ElasticsearchClientTest {
83-
84- private static final String INDEX = "index" ;
85- private static final String ELASTIC_SUPERUSER_NAME = "elastic" ;
86- private static final String ELASTIC_SUPERUSER_PASSWORD = "elastic" ;
87- private static final String TOPIC = "index" ;
88- private static final String DATA_STREAM_TYPE = "logs" ;
89- private static final String DATA_STREAM_DATASET = "dataset" ;
90-
91- private static ElasticsearchContainer container ;
92-
93- private DataConverter converter ;
94- private ElasticsearchHelperClient helperClient ;
95- private ElasticsearchSinkConnectorConfig config ;
96- private Map <String , String > props ;
97- private String index ;
98- private OffsetTracker offsetTracker ;
72+ public class ElasticsearchClientTest extends ElasticsearchClientTestBase {
9973
10074 @ BeforeClass
10175 public static void setupBeforeAll () {
10276 container = ElasticsearchContainer .fromSystemProperties ();
10377 container .start ();
10478 }
10579
106- @ AfterClass
107- public static void cleanupAfterAll () {
108- container .close ();
109- }
110-
11180 @ Before
11281 public void setup () {
11382 index = TOPIC ;
@@ -748,43 +717,6 @@ public void testNoVersionConflict() throws Exception {
748717 client2 .close ();
749718 }
750719
751- @ Test
752- public void testSsl () throws Exception {
753- container .close ();
754- container = ElasticsearchContainer .fromSystemProperties ().withSslEnabled (true );
755- container .start ();
756-
757- String address = container .getConnectionUrl (false );
758- props .put (CONNECTION_URL_CONFIG , address );
759- props .put (CONNECTION_USERNAME_CONFIG , ELASTIC_SUPERUSER_NAME );
760- props .put (CONNECTION_PASSWORD_CONFIG , ELASTIC_SUPERUSER_PASSWORD );
761- props .put (SECURITY_PROTOCOL_CONFIG , SecurityProtocol .SSL .name ());
762- props .put (SSL_CONFIG_PREFIX + SslConfigs .SSL_KEYSTORE_LOCATION_CONFIG , container .getKeystorePath ());
763- props .put (SSL_CONFIG_PREFIX + SslConfigs .SSL_KEYSTORE_PASSWORD_CONFIG , container .getKeystorePassword ());
764- props .put (SSL_CONFIG_PREFIX + SslConfigs .SSL_TRUSTSTORE_LOCATION_CONFIG , container .getTruststorePath ());
765- props .put (SSL_CONFIG_PREFIX + SslConfigs .SSL_TRUSTSTORE_PASSWORD_CONFIG , container .getTruststorePassword ());
766- props .put (SSL_CONFIG_PREFIX + SslConfigs .SSL_KEY_PASSWORD_CONFIG , container .getKeyPassword ());
767- config = new ElasticsearchSinkConnectorConfig (props );
768- converter = new DataConverter (config );
769-
770- ElasticsearchClient client = new ElasticsearchClient (config , null , () -> offsetTracker .updateOffsets (), 1 , "elasticsearch-sink" );
771- helperClient = new ElasticsearchHelperClient (address , config ,
772- container .shouldStartClientInCompatibilityMode ());
773- client .createIndexOrDataStream (index );
774-
775- writeRecord (sinkRecord (0 ), client );
776- client .flush ();
777-
778- waitUntilRecordsInES (1 );
779- assertEquals (1 , helperClient .getDocCount (index ));
780- client .close ();
781- helperClient = null ;
782-
783- container .close ();
784- container = ElasticsearchContainer .fromSystemProperties ();
785- container .start ();
786- }
787-
788720 @ Test
789721 public void testWriteDataStreamInjectTimestamp () throws Exception {
790722 props .put (DATA_STREAM_TYPE_CONFIG , DATA_STREAM_TYPE );
@@ -806,12 +738,6 @@ public void testWriteDataStreamInjectTimestamp() throws Exception {
806738 client .close ();
807739 }
808740
809- private String createIndexName (String name ) {
810- return config .isDataStream ()
811- ? String .format ("%s-%s-%s" , DATA_STREAM_TYPE , DATA_STREAM_DATASET , name )
812- : name ;
813- }
814-
815741 @ Test
816742 public void testConnectionUrlExtraSlash () {
817743 props .put (CONNECTION_URL_CONFIG , container .getConnectionUrl () + "/" );
@@ -862,59 +788,4 @@ public void testThreadNamingWithConnectorNameAndTaskId() throws Exception {
862788
863789 client .close ();
864790 }
865-
866- private static Schema schema () {
867- return SchemaBuilder
868- .struct ()
869- .name ("record" )
870- .field ("offset" , SchemaBuilder .int32 ().defaultValue (0 ).build ())
871- .field ("another" , SchemaBuilder .int32 ().defaultValue (0 ).build ())
872- .build ();
873- }
874-
875- private static SinkRecord sinkRecord (int offset ) {
876- return sinkRecord ("key" , offset );
877- }
878-
879- private static SinkRecord sinkRecord (String key , int offset ) {
880- Struct value = new Struct (schema ()).put ("offset" , offset ).put ("another" , offset + 1 );
881- return sinkRecord (key , schema (), value , offset );
882- }
883-
884- private static SinkRecord sinkRecord (String key , Schema schema , Struct value , int offset ) {
885- return new SinkRecord (
886- TOPIC ,
887- 0 ,
888- Schema .STRING_SCHEMA ,
889- key ,
890- schema ,
891- value ,
892- offset ,
893- System .currentTimeMillis (),
894- TimestampType .CREATE_TIME
895- );
896- }
897-
898- private void waitUntilRecordsInES (int expectedRecords ) throws InterruptedException {
899- TestUtils .waitForCondition (
900- () -> {
901- try {
902- return helperClient .getDocCount (index ) == expectedRecords ;
903- } catch (ElasticsearchStatusException e ) {
904- if (e .getMessage ().contains ("index_not_found_exception" )) {
905- return false ;
906- }
907-
908- throw e ;
909- }
910- },
911- TimeUnit .MINUTES .toMillis (1 ),
912- String .format ("Could not find expected documents (%d) in time." , expectedRecords )
913- );
914- }
915-
916- private void writeRecord (SinkRecord record , ElasticsearchClient client ) {
917- client .index (record , converter .convertRecord (record , createIndexName (record .topic ())),
918- new AsyncOffsetTracker .AsyncOffsetState (record .kafkaOffset ()));
919- }
920791}
0 commit comments