-
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
Changes from 4 commits
cc9303a
2cf168a
e6d5a6d
3684259
e4dc338
e8846b8
7eab357
52a348d
f8cb522
86ced03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.transferwise.kafka.tkms.api; | ||
|
||
import com.transferwise.kafka.tkms.api.TkmsMessage.Header; | ||
import java.util.List; | ||
|
||
public interface ITkmsMessageDecorator { | ||
|
||
default List<Header> getHeaders(TkmsMessage message) { | ||
return List.of(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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").setValue(someValue)) | ||
.addTkmsMessage(new TkmsMessage().setTopic(topic).setKey("danny-carey").setValue(someValue)) | ||
)); | ||
|
||
await().until(() -> tkmsSentMessagesCollector.getSentMessages(topic).size() == 2); | ||
var messages = tkmsSentMessagesCollector.getSentMessages(topic); | ||
|
||
assertEquals(2, messages.size()); | ||
checkForHeader(messages.get(0), "tool", "jambi"); | ||
checkForHeader(messages.get(1), "tool", "jambi"); | ||
} | ||
|
||
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()))) | ||
Check warning on line 61 in tw-tkms-starter/src/test/java/com/transferwise/kafka/tkms/MessageDecorationTest.java GitHub Actions / Spotbugs Report-(3.2.2)DM_DEFAULT_ENCODING
Raw output
Check warning on line 61 in tw-tkms-starter/src/test/java/com/transferwise/kafka/tkms/MessageDecorationTest.java GitHub Actions / Spotbugs Report-(3.3.1)DM_DEFAULT_ENCODING
Raw output
|
||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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 java.util.List; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TestMessageDecorator implements ITkmsMessageDecorator { | ||
|
||
@Override | ||
public List<Header> getHeaders(TkmsMessage message) { | ||
var h1 = new Header().setKey("tool").setValue("jambi".getBytes()); | ||
Check warning on line 14 in tw-tkms-starter/src/test/java/com/transferwise/kafka/tkms/test/TestMessageDecorator.java GitHub Actions / Spotbugs Report-(3.2.2)DM_DEFAULT_ENCODING
Raw output
Check warning on line 14 in tw-tkms-starter/src/test/java/com/transferwise/kafka/tkms/test/TestMessageDecorator.java GitHub Actions / Spotbugs Report-(3.3.1)DM_DEFAULT_ENCODING
Raw output
|
||
if (message.getKey() == null) { | ||
return List.of(); | ||
} | ||
return List.of(h1); | ||
} | ||
|
||
} |
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).