Skip to content

Commit baaf8d5

Browse files
committed
GH-2969 Initial commit to add StandardBatchUtils
1 parent a8fb34d commit baaf8d5

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.stream.function;
18+
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.springframework.messaging.Message;
25+
import org.springframework.messaging.MessageHeaders;
26+
import org.springframework.messaging.support.MessageBuilder;
27+
28+
/**
29+
* @author Oleg Zhurakousky
30+
* @since 4.2
31+
*/
32+
public class StandardBatchUtils {
33+
34+
public static String BATCH_HEADERS = "scst_batchHeaders";
35+
36+
37+
public static class BatchMessageBuilder {
38+
39+
private final List<Object> payloads = new ArrayList<>();
40+
41+
private final List<Map<String, Object>> batchHeaders = new ArrayList<>();
42+
43+
private final Map<String, Object> headers = new HashMap<>();
44+
45+
public BatchMessageBuilder addMessage(Object payload, Map<String, Object> batchHeaders) {
46+
this.payloads.add(payload);
47+
this.batchHeaders.add(batchHeaders);
48+
return this;
49+
}
50+
51+
public BatchMessageBuilder addHeader(String key, Object value) {
52+
this.headers.put(key, value);
53+
return this;
54+
}
55+
56+
public Message<List<Object>> build() {
57+
this.headers.put(BATCH_HEADERS, this.batchHeaders);
58+
return MessageBuilder.createMessage(payloads, new MessageHeaders(headers));
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.stream.function;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.junit.jupiter.api.Test;
26+
import org.springframework.cloud.stream.function.StandardBatchUtils.BatchMessageBuilder;
27+
import org.springframework.messaging.Message;
28+
29+
/**
30+
*
31+
*/
32+
public class StandardBatchUtilsTests {
33+
34+
@SuppressWarnings("unchecked")
35+
@Test
36+
public void testBatchMessageBuilder() {
37+
BatchMessageBuilder builder = new BatchMessageBuilder();
38+
builder.addMessage("foo", Collections.singletonMap("fooKey", "fooValue"));
39+
builder.addHeader("a", "a");
40+
builder.addMessage("bar", Collections.singletonMap("barKey", "barValue"));
41+
builder.addMessage("baz", Collections.singletonMap("bazKey", "bazValue"));
42+
43+
Message<List<Object>> batchMessage = builder.build();
44+
45+
List<Object> payloads = batchMessage.getPayload();
46+
assertThat(payloads.size()).isEqualTo(3);
47+
48+
List<Map<String, Object>> batchHeaders = (List<Map<String, Object>>) batchMessage.getHeaders().get(StandardBatchUtils.BATCH_HEADERS);
49+
assertThat(batchHeaders.size()).isEqualTo(3);
50+
51+
assertThat(payloads.get(0)).isEqualTo("foo");
52+
assertThat(batchHeaders.get(0).get("fooKey")).isEqualTo("fooValue");
53+
54+
assertThat(payloads.get(1)).isEqualTo("bar");
55+
assertThat(batchHeaders.get(1).get("barKey")).isEqualTo("barValue");
56+
57+
assertThat(batchMessage.getHeaders().get("a")).isEqualTo("a");
58+
}
59+
}

0 commit comments

Comments
 (0)