Skip to content

Commit

Permalink
Addressing checkstyle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sobychacko committed Nov 5, 2024
1 parent 505a33b commit 13874dc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public final class BinderHeaders {
IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, MessageHeaders.CONTENT_TYPE};

private static final String PREFIX = "scst_";



/**
* Name of the Message header identifying structure for batch Message headers.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,59 +34,55 @@
* @since 4.2
*/
public final class StandardBatchUtils {

private StandardBatchUtils() {

}

/**
* Iterates over batch message structure returning {@link Iterable} of individual messages.
*
*
* @param batchMessage instance of batch {@link Message}
* @return instance of {@link Iterable} representing individual Messages in a batch {@link Message} as {@link Entry}.
*/
public static Iterable<Entry<Object, Map<String, Object>>> iterate(Message<List<Object>> batchMessage) {
return new Iterable<Map.Entry<Object,Map<String, Object>>>() {
return () -> new Iterator<>() {
int index = 0;

@Override
public Iterator<Entry<Object, Map<String, Object>>> iterator() {
return new Iterator<Entry<Object, Map<String, Object>>>() {
int index = 0;
@Override
public Entry<Object, Map<String, Object>> next() {
return getMessageByIndex(batchMessage, index++);
}

@Override
public boolean hasNext() {
return index < batchMessage.getPayload().size();
}
};
public Entry<Object, Map<String, Object>> next() {
return getMessageByIndex(batchMessage, index++);
}

@Override
public boolean hasNext() {
return index < batchMessage.getPayload().size();
}
};
}

/**
* Extracts individual {@link Message} by index from batch {@link Message}
* Extracts individual {@link Message} by index from batch {@link Message}.
* @param batchMessage instance of batch {@link Message}
* @param index index of individual {@link Message} in a batch
* @return individual {@link Message} in a batch {@link Message}
*/
public static Entry<Object, Map<String, Object>> getMessageByIndex(Message<List<Object>> batchMessage, int index) {
Assert.isTrue(index < batchMessage.getPayload().size(), "Index " + index + " is out of bounds as there are only "
Assert.isTrue(index < batchMessage.getPayload().size(), "Index " + index + " is out of bounds as there are only "
+ batchMessage.getPayload().size() + " messages in a batch.");
return new Entry<Object, Map<String,Object>>() {
return new Entry<>() {

@Override
public Map<String, Object> setValue(Map<String, Object> value) {
throw new UnsupportedOperationException();
}

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> getValue() {
return ((List<Map<String, Object>>) batchMessage.getHeaders().get(BinderHeaders.BATCH_HEADERS)).get(index);
}

@Override
public Object getKey() {
return batchMessage.getPayload().get(index);
Expand All @@ -95,24 +91,24 @@ public Object getKey() {
}

public static class BatchMessageBuilder {

private final List<Object> payloads = new ArrayList<>();

private final List<Map<String, Object>> batchHeaders = new ArrayList<>();

private final Map<String, Object> headers = new HashMap<>();

public BatchMessageBuilder addMessage(Object payload, Map<String, Object> batchHeaders) {
this.payloads.add(payload);
this.batchHeaders.add(batchHeaders);
return this;
}

public BatchMessageBuilder addRootHeader(String key, Object value) {
this.headers.put(key, value);
return this;
}

public Message<List<Object>> build() {
this.headers.put(BinderHeaders.BATCH_HEADERS, this.batchHeaders);
return MessageBuilder.createMessage(payloads, new MessageHeaders(headers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.cloud.stream.function;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -25,13 +24,15 @@
import java.util.Map.Entry;

import org.junit.jupiter.api.Test;

import org.springframework.cloud.stream.binder.BinderHeaders;
import org.springframework.cloud.stream.function.StandardBatchUtils.BatchMessageBuilder;
import org.springframework.messaging.Message;

import static org.assertj.core.api.Assertions.assertThat;

/**
*
*
*/
public class StandardBatchUtilsTests {

Expand All @@ -43,45 +44,45 @@ public void testBatchMessageBuilder() {
builder.addRootHeader("a", "a");
builder.addMessage("bar", Collections.singletonMap("barKey", "barValue"));
builder.addMessage("baz", Collections.singletonMap("bazKey", "bazValue"));

Message<List<Object>> batchMessage = builder.build();

List<Object> payloads = batchMessage.getPayload();
assertThat(payloads.size()).isEqualTo(3);

List<Map<String, Object>> batchHeaders = (List<Map<String, Object>>) batchMessage.getHeaders().get(BinderHeaders.BATCH_HEADERS);
assertThat(batchHeaders.size()).isEqualTo(3);

assertThat(payloads.get(0)).isEqualTo("foo");
assertThat(batchHeaders.get(0).get("fooKey")).isEqualTo("fooValue");

assertThat(payloads.get(1)).isEqualTo("bar");
assertThat(batchHeaders.get(1).get("barKey")).isEqualTo("barValue");

assertThat(batchMessage.getHeaders().get("a")).isEqualTo("a");
}

@Test
public void testIterator() {
BatchMessageBuilder builder = new BatchMessageBuilder();
builder.addMessage("foo", Collections.singletonMap("fooKey", "fooValue"));
builder.addRootHeader("a", "a");
builder.addMessage("bar", Collections.singletonMap("barKey", "barValue"));
builder.addMessage("baz", Collections.singletonMap("bazKey", "bazValue"));

Message<List<Object>> batchMessage = builder.build();

List<Entry<Object, Map<String, Object>>> entries = new ArrayList<>();
StandardBatchUtils.iterate(batchMessage).forEach(entry -> {
entries.add(entry);
});
assertThat(entries.size()).isEqualTo(3);
assertThat(entries.get(0).getKey()).isEqualTo("foo");
assertThat(entries.get(0).getValue().get("fooKey")).isEqualTo("fooValue");

assertThat(entries.get(1).getKey()).isEqualTo("bar");
assertThat(entries.get(1).getValue().get("barKey")).isEqualTo("barValue");

assertThat(entries.get(2).getKey()).isEqualTo("baz");
assertThat(entries.get(2).getValue().get("bazKey")).isEqualTo("bazValue");
}
Expand Down

0 comments on commit 13874dc

Please sign in to comment.