Addressing checkstyle issues
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -34,59 +34,55 @@ import org.springframework.util.Assert;
|
||||
* @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);
|
||||
@@ -95,24 +91,24 @@ public final class StandardBatchUtils {
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
@@ -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;
|
||||
@@ -25,13 +24,15 @@ import java.util.Map;
|
||||
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 {
|
||||
|
||||
@@ -43,24 +44,24 @@ public class StandardBatchUtilsTests {
|
||||
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();
|
||||
@@ -68,9 +69,9 @@ public class StandardBatchUtilsTests {
|
||||
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);
|
||||
@@ -78,10 +79,10 @@ public class StandardBatchUtilsTests {
|
||||
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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user