-
Notifications
You must be signed in to change notification settings - Fork 2
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
[PPP-502] TKMS - Add decorator for message headers #87
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cc9303a
good
hussainkarafallah 2cf168a
hey
hussainkarafallah e6d5a6d
hey
hussainkarafallah 3684259
happy checkstyle
hussainkarafallah e4dc338
spotbugs happy
hussainkarafallah e8846b8
test good
hussainkarafallah 7eab357
add extra stuff
hussainkarafallah 52a348d
ok test
hussainkarafallah f8cb522
test happy again
hussainkarafallah 86ced03
bump gradle props
hussainkarafallah 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=0.29.1 | ||
version=0.30.0 |
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
21 changes: 21 additions & 0 deletions
21
tw-tkms-starter/src/main/java/com/transferwise/kafka/tkms/api/ITkmsMessageDecorator.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,21 @@ | ||
package com.transferwise.kafka.tkms.api; | ||
|
||
import com.transferwise.kafka.tkms.api.TkmsMessage.Header; | ||
import java.util.List; | ||
import org.springframework.core.Ordered; | ||
|
||
public interface ITkmsMessageDecorator extends Ordered { | ||
|
||
default List<Header> getAdditionalHeaders(TkmsMessage message) { | ||
return List.of(); | ||
} | ||
|
||
default TkmsShardPartition getOverridedPartition(TkmsMessage message) { | ||
return null; | ||
} | ||
|
||
default int getOrder() { | ||
return LOWEST_PRECEDENCE; | ||
} | ||
|
||
} |
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
68 changes: 68 additions & 0 deletions
68
tw-tkms-starter/src/test/java/com/transferwise/kafka/tkms/MessageDecorationTest.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,68 @@ | ||
package com.transferwise.kafka.tkms; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.transferwise.common.baseutils.transactionsmanagement.ITransactionsHelper; | ||
import com.transferwise.kafka.tkms.api.ITransactionalKafkaMessageSender.SendMessagesRequest; | ||
import com.transferwise.kafka.tkms.api.TkmsMessage; | ||
import com.transferwise.kafka.tkms.test.BaseIntTest; | ||
import com.transferwise.kafka.tkms.test.ITkmsSentMessagesCollector.SentMessage; | ||
import com.transferwise.kafka.tkms.test.TestMessagesInterceptor; | ||
import com.transferwise.kafka.tkms.test.TestProperties; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.stream.StreamSupport; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
@Slf4j | ||
class MessageDecorationTest extends BaseIntTest { | ||
|
||
@Autowired | ||
private TestMessagesInterceptor testMessagesInterceptor; | ||
@Autowired | ||
private TransactionalKafkaMessageSender transactionalKafkaMessageSender; | ||
@Autowired | ||
private TestProperties testProperties; | ||
@Autowired | ||
private ITransactionsHelper transactionsHelper; | ||
|
||
@AfterEach | ||
void cleanupTest() { | ||
testMessagesInterceptor.setBeforeSendingToKafkaFunction(null); | ||
} | ||
|
||
@Test | ||
void messagesAreDecorateWithJambi() { | ||
byte[] someValue = "Here from the king's mountain view, Feast like a sultan I do!".getBytes(StandardCharsets.UTF_8); | ||
|
||
String topic = testProperties.getTestTopic(); | ||
|
||
transactionsHelper.withTransaction().run(() -> | ||
transactionalKafkaMessageSender.sendMessages(new SendMessagesRequest() | ||
.addTkmsMessage(new TkmsMessage().setTopic(topic).setKey("adam-jones").setShard(4).setValue(someValue)) | ||
.addTkmsMessage(new TkmsMessage().setTopic(topic).setKey("danny-carey").setPartition(5).setValue(someValue)) | ||
)); | ||
|
||
await().until(() -> tkmsSentMessagesCollector.getSentMessages(topic).size() == 2); | ||
var messages = tkmsSentMessagesCollector.getSentMessages(topic); | ||
|
||
assertEquals(2, messages.size()); | ||
checkForHeader(messages.get(0), "tool", "jambi"); | ||
assertEquals(0, messages.get(0).getShardPartition().getShard()); | ||
assertEquals(0, messages.get(0).getShardPartition().getPartition()); | ||
checkForHeader(messages.get(1), "tool", "jambi"); | ||
assertEquals(0, messages.get(1).getShardPartition().getShard()); | ||
assertEquals(0, messages.get(1).getShardPartition().getPartition()); | ||
} | ||
|
||
private void checkForHeader(SentMessage sentMessage, String key, String value) { | ||
assertTrue( | ||
StreamSupport.stream(sentMessage.getProducerRecord().headers().spliterator(), false) | ||
.anyMatch(h -> h.key().equals(key) && value.equals(new String(h.value(), StandardCharsets.UTF_8))) | ||
); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
tw-tkms-starter/src/test/java/com/transferwise/kafka/tkms/test/TestMessageDecorator.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,30 @@ | ||
package com.transferwise.kafka.tkms.test; | ||
|
||
import com.transferwise.kafka.tkms.api.ITkmsMessageDecorator; | ||
import com.transferwise.kafka.tkms.api.TkmsMessage; | ||
import com.transferwise.kafka.tkms.api.TkmsMessage.Header; | ||
import com.transferwise.kafka.tkms.api.TkmsShardPartition; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TestMessageDecorator implements ITkmsMessageDecorator { | ||
|
||
@Override | ||
public List<Header> getAdditionalHeaders(TkmsMessage message) { | ||
if (message.getValue() != null && new String(message.getValue(), StandardCharsets.UTF_8).startsWith("Here from")) { | ||
return List.of(new Header().setKey("tool").setValue("jambi".getBytes(StandardCharsets.UTF_8))); | ||
} | ||
return List.of(); | ||
} | ||
|
||
@Override | ||
public TkmsShardPartition getOverridedPartition(TkmsMessage message) { | ||
if (message.getValue() != null && new String(message.getValue(), StandardCharsets.UTF_8).startsWith("Here from")) { | ||
return new TkmsShardPartition(0, 0); | ||
} | ||
return null; | ||
} | ||
|
||
} |
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.
making checkstyle happy
Distance between variable 'transactionActive' declaration and its first usage is 4, but allowed 3. Consider making that variable final if you still need to store its value in advance (before method calls that might have side effects on the original value).