-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7671439
commit 4756a11
Showing
7 changed files
with
256 additions
and
36 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
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
189 changes: 189 additions & 0 deletions
189
...va/io/confluent/connect/elasticsearch/integration/ElasticsearchConnectorDataFormatIT.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,189 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.connect.elasticsearch.integration; | ||
|
||
import io.confluent.connect.avro.AvroConverter; | ||
import io.confluent.connect.json.JsonSchemaConverter; | ||
import io.confluent.connect.protobuf.ProtobufConverter; | ||
import io.confluent.kafka.schemaregistry.CompatibilityLevel; | ||
import io.confluent.kafka.schemaregistry.RestApp; | ||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.common.KafkaException; | ||
import org.apache.kafka.common.serialization.ByteArraySerializer; | ||
import org.apache.kafka.connect.data.Schema; | ||
import org.apache.kafka.connect.data.SchemaAndValue; | ||
import org.apache.kafka.connect.data.SchemaBuilder; | ||
import org.apache.kafka.connect.data.Struct; | ||
import org.apache.kafka.connect.storage.Converter; | ||
import org.apache.kafka.test.TestUtils; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static io.confluent.kafka.schemaregistry.ClusterTestHarness.KAFKASTORE_TOPIC; | ||
|
||
@RunWith(Parameterized.class) | ||
public class ElasticsearchConnectorDataFormatIT extends ElasticsearchConnectorBaseIT { | ||
|
||
protected void startSchemaRegistry() throws Exception { | ||
int port = findAvailableOpenPort(); | ||
restApp = new RestApp(port, null, connect.kafka().bootstrapServers(), | ||
KAFKASTORE_TOPIC, CompatibilityLevel.NONE.name, true, new Properties()); | ||
restApp.start(); | ||
waitForSchemaRegistryToStart(); | ||
} | ||
|
||
protected void stopSchemaRegistry() throws Exception { | ||
restApp.stop(); | ||
} | ||
|
||
protected void waitForSchemaRegistryToStart() throws InterruptedException { | ||
TestUtils.waitForCondition( | ||
() -> restApp.restServer.isRunning(), | ||
CONNECTOR_STARTUP_DURATION_MS, | ||
"Schema-registry server did not start in time." | ||
); | ||
} | ||
|
||
private Converter converter; | ||
private Class<? extends Converter> converterClass; | ||
|
||
@Override | ||
public void setup() throws Exception { | ||
startConnect(); | ||
startSchemaRegistry(); | ||
connect.kafka().createTopic(TOPIC); | ||
|
||
props = createProps(); | ||
client = createClient(); | ||
} | ||
|
||
@Override | ||
public void cleanup() throws Exception { | ||
stopConnect(); | ||
stopSchemaRegistry(); | ||
client.deleteAll(); | ||
client.close(); | ||
} | ||
|
||
@Parameters | ||
public static List<Class<? extends Converter>> data() { | ||
return Arrays.asList(JsonSchemaConverter.class, ProtobufConverter.class, AvroConverter.class); | ||
} | ||
|
||
|
||
public ElasticsearchConnectorDataFormatIT(Class<? extends Converter> converter) throws Exception { | ||
this.converterClass = converter; | ||
this.converter = converterClass.getConstructor().newInstance(); | ||
} | ||
|
||
@Test | ||
public void testHappyPathDataFormat() throws Exception { | ||
// configure configs and converter with schema-registry addr | ||
props.put("value.converter", converterClass.getSimpleName()); | ||
props.put("value.converter.schema.registry.url", restApp.restServer.getURI().toString()); | ||
props.put("value.converter.scrub.invalid.names", "true"); | ||
converter.configure(Collections.singletonMap( | ||
"schema.registry.url", restApp.restServer.getURI().toString() | ||
), false | ||
); | ||
|
||
// wait for schema-registry to spin up | ||
waitForSchemaRegistryToStart(); | ||
|
||
// run test | ||
writeRecords(NUM_RECORDS); | ||
} | ||
|
||
@Override | ||
protected void writeRecords(int numRecords) { | ||
writeRecordsFromIndex(0, numRecords, converter); | ||
} | ||
|
||
protected void writeRecordsFromIndex(int start, int numRecords, Converter converter) { | ||
// get defined schema for the test | ||
Schema schema = getRecordSchema(); | ||
|
||
// configure producer with default properties | ||
KafkaProducer<byte[], byte[]> producer = configureProducer(); | ||
|
||
List<SchemaAndValue> recordsList = getRecords(schema, start, numRecords); | ||
|
||
// produce records into topic | ||
produceRecords(producer, converter, recordsList, TOPIC); | ||
} | ||
|
||
private Integer findAvailableOpenPort() throws IOException { | ||
try (ServerSocket socket = new ServerSocket(0)) { | ||
return socket.getLocalPort(); | ||
} | ||
} | ||
|
||
protected List<SchemaAndValue> getRecords(Schema schema, int start, int numRecords) { | ||
List<SchemaAndValue> recordList = new ArrayList<>(); | ||
for (int i = start; i < start + numRecords; i++) { | ||
Struct struct = new Struct(schema); | ||
struct.put("doc_num", i); | ||
SchemaAndValue schemaAndValue = new SchemaAndValue(schema, struct); | ||
recordList.add(schemaAndValue); | ||
} | ||
return recordList; | ||
} | ||
|
||
protected void produceRecords( | ||
KafkaProducer<byte[], byte[]> producer, | ||
Converter converter, | ||
List<SchemaAndValue> recordsList, | ||
String topic | ||
) { | ||
for (int i = 0; i < recordsList.size(); i++) { | ||
SchemaAndValue schemaAndValue = recordsList.get(i); | ||
byte[] convertedStruct = converter.fromConnectData(topic, schemaAndValue.schema(), schemaAndValue.value()); | ||
ProducerRecord<byte[], byte[]> msg = new ProducerRecord<>(topic, 0, String.valueOf(i).getBytes(), convertedStruct); | ||
try { | ||
producer.send(msg).get(TimeUnit.SECONDS.toMillis(120), TimeUnit.MILLISECONDS); | ||
} catch (Exception e) { | ||
throw new KafkaException("Could not produce message: " + msg, e); | ||
} | ||
} | ||
} | ||
|
||
protected KafkaProducer<byte[], byte[]> configureProducer() { | ||
Map<String, Object> producerProps = new HashMap<>(); | ||
producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, connect.kafka().bootstrapServers()); | ||
return new KafkaProducer<>(producerProps, new ByteArraySerializer(), new ByteArraySerializer()); | ||
} | ||
|
||
protected Schema getRecordSchema() { | ||
SchemaBuilder schemaBuilder = SchemaBuilder.struct(); | ||
schemaBuilder.field("doc_num", Schema.INT32_SCHEMA); | ||
return schemaBuilder.build(); | ||
} | ||
} |
Oops, something went wrong.