-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Issue #45] [pulsar-client-kafka-compat] Handled Kafka record headers… #46
Open
swamymavuri
wants to merge
16
commits into
apache:master
Choose a base branch
from
swamymavuri:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ea9c294
[Issue #45] [pulsar-client-kafka-compat] Handled Kafka record headers…
swamymavuri e62004b
Added long type to the checksumto the
swamymavuri f777f41
[Issue #45] [pulsar-client-kafka-compat] Added KafkaConsumerTest cases
swamymavuri 18d09c4
Removed Hex encoding and decoding for headers
swamymavuri 0ee92ca
Added Hex encoding and decoding for headers
swamymavuri 499f6d9
Added licence header to the newly added file
swamymavuri 9f6be3d
Verified added header properties with mockTypedMessageBuilder
swamymavuri 93198bb
Added test cases with Mockito and testng with assertions
swamymavuri 4673dff
Formattedlicence headers to pass the build
swamymavuri 3d69fa1
Asserted Header Value
swamymavuri b5419f1
Merge branch 'apache:master' into master
swamymavuri 9679855
Added Comments
swamymavuri de88cf2
Added Comments
swamymavuri 050b0fb
Removed redundant mock
swamymavuri af7774d
Added Integration tests and changed to PularKafkaConsumer
swamymavuri fcdebaf
excluded kafka client dependency
swamymavuri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...ulsar-client-kafka/src/main/java/org/apache/kafka/clients/constants/MessageConstants.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,28 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
package org.apache.kafka.clients.constants; | ||
|
||
public final class MessageConstants { | ||
private MessageConstants() { | ||
|
||
} | ||
|
||
public static final String KAFKA_MESSAGE_HEADER_PREFIX = "kafka.header."; | ||
} |
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
109 changes: 109 additions & 0 deletions
109
...client-kafka/src/test/java/org/apache/kafka/clients/consumer/PulsarKafkaConsumerTest.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,109 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
package org.apache.kafka.clients.consumer; | ||
|
||
import static org.mockito.ArgumentMatchers.anyCollection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.apache.commons.codec.binary.Hex; | ||
import org.apache.kafka.clients.constants.MessageConstants; | ||
import org.apache.kafka.common.serialization.IntegerDeserializer; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.apache.pulsar.client.api.ClientBuilder; | ||
import org.apache.pulsar.client.api.Consumer; | ||
import org.apache.pulsar.client.api.Message; | ||
import org.apache.pulsar.client.api.MessageId; | ||
import org.apache.pulsar.client.api.PulsarClient; | ||
import org.apache.pulsar.client.api.Schema; | ||
import org.apache.pulsar.client.impl.MessageImpl; | ||
import org.apache.pulsar.client.impl.PulsarClientImpl; | ||
import org.apache.pulsar.client.kafka.compat.KafkaMessageRouter; | ||
import org.apache.pulsar.common.api.proto.MessageMetadata; | ||
import org.apache.pulsar.common.naming.TopicName; | ||
import org.mockito.Mockito; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class PulsarKafkaConsumerTest { | ||
|
||
@Test | ||
public void testPulsarKafkaConsumerWithHeaders_noAck() throws Exception { | ||
Consumer consumer = Mockito.mock(Consumer.class); | ||
String topic = "topic"; | ||
|
||
Mockito.when(Mockito.mock(TopicName.class).getPartitionedTopicName()).thenReturn(topic); | ||
Mockito.doReturn("topic").when(consumer).getTopic(); | ||
|
||
MessageMetadata messageMetadata = new MessageMetadata(); | ||
messageMetadata.setPublishTime(System.currentTimeMillis()); | ||
|
||
Map<String, String> headerMap = new HashMap<>(); | ||
String kafkaHeaderKey = MessageConstants.KAFKA_MESSAGE_HEADER_PREFIX + "header1"; | ||
String kafkaHeaderValue = Hex.encodeHexString(kafkaHeaderKey.getBytes()); | ||
headerMap.put(kafkaHeaderKey, kafkaHeaderValue); | ||
headerMap.put(KafkaMessageRouter.PARTITION_ID, "0"); | ||
Message<byte[]> msg = new MessageImpl<>( | ||
topic, | ||
"1:1", | ||
headerMap, | ||
"string".getBytes(), | ||
Schema.BYTES, | ||
messageMetadata | ||
); | ||
|
||
PulsarClient mockClient = Mockito.mock(PulsarClient.class); | ||
PulsarClientImpl mockClientImpl = Mockito.mock(PulsarClientImpl.class); | ||
|
||
CompletableFuture<Integer> mockNoOfPartitionFuture = CompletableFuture.completedFuture(1); | ||
|
||
ClientBuilder mockClientBuilder = Mockito.mock(ClientBuilder.class); | ||
Mockito.doReturn(mockClientBuilder).when(mockClientBuilder).serviceUrl(Mockito.anyString()); | ||
Mockito.doReturn(mockClient).when(mockClientBuilder).build(); | ||
|
||
Mockito.when(mockClientImpl.getNumberOfPartitions(Mockito.anyString())).thenReturn(mockNoOfPartitionFuture); | ||
|
||
Properties properties = new Properties(); | ||
|
||
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class.getName()); | ||
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); | ||
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, Collections.singletonList("pulsar://localhost:6650")); | ||
properties.put(ConsumerConfig.GROUP_ID_CONFIG, "my-subscription-name"); | ||
|
||
PulsarKafkaConsumer<Integer, String> pulsarKafkaConsumer = | ||
new PulsarKafkaConsumer<>(properties, new IntegerDeserializer(), new StringDeserializer()); | ||
|
||
PulsarKafkaConsumer<Integer, String> pulsarKafkaConsumerSpy = Mockito.spy(pulsarKafkaConsumer); | ||
|
||
Mockito.doNothing().when(pulsarKafkaConsumerSpy).seekToEnd(anyCollection()); | ||
|
||
pulsarKafkaConsumerSpy.received(consumer, msg); | ||
pulsarKafkaConsumerSpy.poll(100); | ||
pulsarKafkaConsumerSpy.close(); | ||
|
||
Assert.assertEquals(kafkaHeaderValue, msg.getProperty(kafkaHeaderKey)); | ||
Mockito.verify(pulsarKafkaConsumerSpy).seekToEnd(anyCollection()); | ||
Mockito.verify(consumer, Mockito.times(0)).acknowledgeCumulativeAsync(Mockito.any(MessageId.class)); | ||
} | ||
|
||
|
||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this import used at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, while encoding the kafka record headers