Skip to content

Commit

Permalink
happy checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainkarafallah committed Aug 6, 2024
1 parent e6d5a6d commit 3684259
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ protected void checkActiveTransaction(int shard, boolean transactionActive, bool

@Override
public SendMessagesResult sendMessages(SendMessagesRequest request) {
var transactionActive = TransactionSynchronizationManager.isActualTransactionActive();
request.getTkmsMessages().forEach(message -> messageDecorators.forEach(message::accept));
validateMessages(request);

var transactionActive = TransactionSynchronizationManager.isActualTransactionActive();
var validatedTopics = new HashSet<String>();

int seq = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.transferwise.kafka.tkms.api.TkmsMessage.Header;
import java.util.List;
import java.util.Map;

public interface ITkmsMessageDecorator {
default List<Header> getHeaders(TkmsMessage message){

default List<Header> getHeaders(TkmsMessage message) {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public TkmsMessage addHeader(Header header) {
return this;
}

public TkmsMessage accept(ITkmsMessageDecorator decorator){
public TkmsMessage accept(ITkmsMessageDecorator decorator) {
var headers = decorator.getHeaders(this);
if(headers != null){
if (headers != null) {
headers.forEach(this::addHeader);
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.transferwise.common.baseutils.transactionsmanagement.TransactionsHelper;
import com.transferwise.kafka.tkms.api.ITkmsMessageDecorator;
import io.micrometer.core.instrument.MeterRegistry;
import java.util.Collections;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -16,8 +18,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import java.util.Collections;
import java.util.List;

@Configuration
@AutoConfigureAfter({FlywayAutoConfiguration.class, ValidationAutoConfiguration.class})
Expand All @@ -42,7 +42,7 @@ public IMeterCache twDefaultMeterCache(MeterRegistry meterRegistry) {
public TransactionsHelper twTransactionsHelper() {
return new TransactionsHelper();
}

@Bean
@ConditionalOnMissingBean(ITkmsMessageDecorator.class)
public List<ITkmsMessageDecorator> messageDecorators() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
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.ITkmsMessageInterceptor.MessageInterceptionDecision;
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;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Slf4j
class MessageDecorationTest extends BaseIntTest {
Expand All @@ -49,22 +43,22 @@ void messagesAreDecorateWithJambi() {

transactionsHelper.withTransaction().run(() ->
transactionalKafkaMessageSender.sendMessages(new SendMessagesRequest()
.addTkmsMessage(new TkmsMessage().setTopic(topic).setKey("A").setValue(someValue))
.addTkmsMessage(new TkmsMessage().setTopic(topic).setKey("B").setValue(someValue))
.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);
assertThat(messages.size()).isEqualTo(2);
checkForHeader(messages.get(0) , "adam-jones" , "jambi");
checkForHeader(messages.get(1) , "danny-carey" , "the-grudge");

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())))
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

View workflow job for this annotation

GitHub Actions / Spotbugs Report-(3.2.2)

DM_DEFAULT_ENCODING

Found reliance on default encoding in com.transferwise.kafka.tkms.MessageDecorationTest.lambda$checkForHeader$2(String, String, Header): new String(byte[])
Raw output
Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behavior to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.

Check warning on line 61 in tw-tkms-starter/src/test/java/com/transferwise/kafka/tkms/MessageDecorationTest.java

View workflow job for this annotation

GitHub Actions / Spotbugs Report-(3.3.1)

DM_DEFAULT_ENCODING

Found reliance on default encoding in com.transferwise.kafka.tkms.MessageDecorationTest.lambda$checkForHeader$2(String, String, Header): new String(byte[])
Raw output
Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behavior to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import com.transferwise.kafka.tkms.api.ITkmsMessageDecorator;
import com.transferwise.kafka.tkms.api.TkmsMessage;
import com.transferwise.kafka.tkms.api.TkmsMessage.Header;
import org.springframework.stereotype.Component;
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("adam-jones").setValue("jambi".getBytes());
if (message.getValue() != null && new String(message.getValue()).startsWith("Here from the king")) {
return List.of(h1);
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

View workflow job for this annotation

GitHub Actions / Spotbugs Report-(3.2.2)

DM_DEFAULT_ENCODING

Found reliance on default encoding in com.transferwise.kafka.tkms.test.TestMessageDecorator.getHeaders(TkmsMessage): String.getBytes()
Raw output
Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behavior to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.

Check warning on line 14 in tw-tkms-starter/src/test/java/com/transferwise/kafka/tkms/test/TestMessageDecorator.java

View workflow job for this annotation

GitHub Actions / Spotbugs Report-(3.3.1)

DM_DEFAULT_ENCODING

Found reliance on default encoding in com.transferwise.kafka.tkms.test.TestMessageDecorator.getHeaders(TkmsMessage): String.getBytes()
Raw output
Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behavior to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.
if (message.getKey() == null) {
return List.of();
}
return List.of();
return List.of(h1);
}

}

0 comments on commit 3684259

Please sign in to comment.