-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide
BytesKvSourceRecordMapper
to avoid both the envelope wrappi…
…ng key and value and the escaped encoding of the json document
- Loading branch information
Showing
7 changed files
with
318 additions
and
5 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
76 changes: 76 additions & 0 deletions
76
src/main/java/com/github/castorm/kafka/connect/http/record/BytesKvSourceRecordMapper.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,76 @@ | ||
package com.github.castorm.kafka.connect.http.record; | ||
|
||
/*- | ||
* #%L | ||
* kafka-connect-http | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import com.github.castorm.kafka.connect.http.model.Offset; | ||
import com.github.castorm.kafka.connect.http.record.model.KvRecord; | ||
import com.github.castorm.kafka.connect.http.record.spi.KvSourceRecordMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.kafka.connect.data.Schema; | ||
import org.apache.kafka.connect.source.SourceRecord; | ||
|
||
import java.nio.charset.Charset; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import static java.util.Collections.emptyMap; | ||
import static org.apache.kafka.connect.data.SchemaBuilder.bytes; | ||
|
||
@RequiredArgsConstructor | ||
public class BytesKvSourceRecordMapper implements KvSourceRecordMapper { | ||
|
||
private static Map<String, ?> sourcePartition = emptyMap(); | ||
|
||
private static final Schema keySchema = bytes().build(); | ||
|
||
private static final Schema valueSchema = bytes().build(); | ||
|
||
private final Function<Map<String, ?>, BytesKvSourceRecordMapperConfig> configFactory; | ||
|
||
private BytesKvSourceRecordMapperConfig config; | ||
|
||
private Charset charset; | ||
|
||
public BytesKvSourceRecordMapper() { | ||
this(BytesKvSourceRecordMapperConfig::new); | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> settings) { | ||
config = configFactory.apply(settings); | ||
} | ||
|
||
@Override | ||
public SourceRecord map(KvRecord record) { | ||
|
||
Offset offset = record.getOffset(); | ||
return new SourceRecord( | ||
sourcePartition, | ||
offset.toMap(), | ||
config.getTopic(), | ||
null, | ||
keySchema, | ||
record.getKey().getBytes(config.getCharset()), | ||
valueSchema, | ||
record.getValue().getBytes(config.getCharset()), | ||
offset.getTimestamp().toEpochMilli()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...in/java/com/github/castorm/kafka/connect/http/record/BytesKvSourceRecordMapperConfig.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,54 @@ | ||
package com.github.castorm.kafka.connect.http.record; | ||
|
||
/*- | ||
* #%L | ||
* kafka-connect-http | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import lombok.Getter; | ||
import org.apache.kafka.common.config.AbstractConfig; | ||
import org.apache.kafka.common.config.ConfigDef; | ||
|
||
import java.nio.charset.Charset; | ||
import java.util.Map; | ||
|
||
import static org.apache.kafka.common.config.ConfigDef.Importance.HIGH; | ||
import static org.apache.kafka.common.config.ConfigDef.Importance.LOW; | ||
import static org.apache.kafka.common.config.ConfigDef.Type.STRING; | ||
|
||
@Getter | ||
public class BytesKvSourceRecordMapperConfig extends AbstractConfig { | ||
|
||
private static final String TOPIC = "kafka.topic"; | ||
private static final String CHARSET = "http.response.record.mapper.charset"; | ||
|
||
private final String topic; | ||
private final Charset charset; | ||
|
||
BytesKvSourceRecordMapperConfig(Map<String, ?> originals) { | ||
super(config(), originals); | ||
topic = getString(TOPIC); | ||
charset = Charset.forName(getString(CHARSET)); | ||
} | ||
|
||
public static ConfigDef config() { | ||
return new ConfigDef() | ||
.define(TOPIC, STRING, HIGH, "Kafka Topic") | ||
.define(CHARSET, STRING, "UTF-8", LOW, "Charset"); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ava/com/github/castorm/kafka/connect/http/record/BytesKvSourceRecordMapperConfigTest.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,67 @@ | ||
package com.github.castorm.kafka.connect.http.record; | ||
|
||
/*- | ||
* #%L | ||
* kafka-connect-http | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.kafka.common.config.ConfigException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.charset.Charset; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.github.castorm.kafka.connect.http.record.BytesKvSourceRecordMapperConfigTest.Fixture.minimumConfig; | ||
import static java.util.Collections.emptyMap; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.catchThrowable; | ||
|
||
class BytesKvSourceRecordMapperConfigTest { | ||
|
||
@Test | ||
void whenMissingKafkaTopic_thenException() { | ||
assertThat(catchThrowable(() -> new BytesKvSourceRecordMapperConfig(emptyMap()))).isInstanceOf(ConfigException.class); | ||
} | ||
|
||
@Test | ||
void whenKafkaTopic_thenInitialized() { | ||
assertThat(minimumConfig(emptyMap()).getTopic()).isEqualTo("test-topic"); | ||
} | ||
|
||
@Test | ||
void whenMissingCharsetProperty_thenDefault() { | ||
assertThat(minimumConfig(emptyMap()).getCharset()).isEqualTo(Charset.forName("UTF-8")); | ||
} | ||
|
||
@Test | ||
void whenCharsetProperty_thenInitialized() { | ||
assertThat(minimumConfig(ImmutableMap.of("http.response.record.mapper.charset", "US-ASCII")).getCharset()).isEqualTo(Charset.forName("US-ASCII")); | ||
} | ||
|
||
interface Fixture { | ||
|
||
static BytesKvSourceRecordMapperConfig minimumConfig(Map<String, String> customConfig) { | ||
HashMap<String, String> finalConfig = new HashMap<>(); | ||
finalConfig.put("kafka.topic", "test-topic"); | ||
finalConfig.putAll(customConfig); | ||
return new BytesKvSourceRecordMapperConfig(finalConfig); | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...test/java/com/github/castorm/kafka/connect/http/record/BytesKvSourceRecordMapperTest.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,106 @@ | ||
package com.github.castorm.kafka.connect.http.record; | ||
|
||
/*- | ||
* #%L | ||
* kafka-connect-http | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import com.github.castorm.kafka.connect.http.model.Offset; | ||
import com.github.castorm.kafka.connect.http.record.model.KvRecord; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.nio.charset.Charset; | ||
import java.time.Instant; | ||
|
||
import static com.github.castorm.kafka.connect.http.record.BytesKvSourceRecordMapperTest.Fixture.charset; | ||
import static com.github.castorm.kafka.connect.http.record.BytesKvSourceRecordMapperTest.Fixture.now; | ||
import static com.github.castorm.kafka.connect.http.record.BytesKvSourceRecordMapperTest.Fixture.offset; | ||
import static com.github.castorm.kafka.connect.http.record.BytesKvSourceRecordMapperTest.Fixture.record; | ||
import static java.time.Instant.now; | ||
import static java.util.Collections.emptyMap; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class BytesKvSourceRecordMapperTest { | ||
|
||
BytesKvSourceRecordMapper mapper; | ||
|
||
@Mock | ||
BytesKvSourceRecordMapperConfig config; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
given(config.getTopic()).willReturn("topic"); | ||
given(config.getCharset()).willReturn(charset); | ||
mapper = new BytesKvSourceRecordMapper(__ -> config); | ||
mapper.configure(emptyMap()); | ||
} | ||
|
||
@Test | ||
void givenTopic_whenMap_thenTopicMapped() { | ||
assertThat(mapper.map(record).topic()).isEqualTo("topic"); | ||
} | ||
|
||
@Test | ||
void givenKey_whenMap_thenIdMapped() { | ||
assertThat(mapper.map(record.withKey("value")).key()).isEqualTo("value".getBytes(charset)); | ||
} | ||
|
||
@Test | ||
void givenValue_whenMap_thenBodyMapped() { | ||
assertThat(mapper.map(record.withValue("value")).value()).isEqualTo("value".getBytes(charset)); | ||
} | ||
|
||
@Test | ||
void givenOffset_whenMap_thenOffsetMapped() { | ||
assertThat(mapper.map(record.withOffset(offset)).sourceOffset()).isEqualTo(offset.toMap()); | ||
} | ||
|
||
@Test | ||
void givenTimestamp_whenMap_thenTimestampMapped() { | ||
assertThat(mapper.map(record.withOffset(offset)).timestamp()).isEqualTo(now.toEpochMilli()); | ||
} | ||
|
||
@Test | ||
void whenMap_thenNoPartitionMapped() { | ||
assertThat(mapper.map(record).kafkaPartition()).isNull(); | ||
} | ||
|
||
@Test | ||
void whenMap_thenKeySchemaMapped() { | ||
assertThat(mapper.map(record).keySchema()).isNotNull(); | ||
} | ||
|
||
@Test | ||
void whenMap_thenValueSchemaMapped() { | ||
assertThat(mapper.map(record).valueSchema()).isNotNull(); | ||
} | ||
|
||
interface Fixture { | ||
Instant now = now(); | ||
Offset offset = Offset.of(ImmutableMap.of("k", "v"), "key", now); | ||
KvRecord record = KvRecord.builder().key("not-null").value("not-null").offset(offset).build(); | ||
Charset charset = Charset.forName("UTF-8"); | ||
} | ||
} |
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