-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PPP-502] TKMS - Add decorator for message headers (#87)
- Loading branch information
1 parent
0e34b7d
commit 7067b2c
Showing
9 changed files
with
153 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
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; | ||
} | ||
|
||
} |