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