Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kafka multiple values header #35

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static java.util.Collections.emptyMap;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static org.apache.commons.lang3.StringUtils.defaultIfEmpty;
import static org.apache.commons.lang3.StringUtils.isBlank;
Expand All @@ -35,6 +34,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -227,7 +227,23 @@ private Map<String, Object> extractMessageFromRecord(ConsumerRecord<String, Stri
}

private Map<String, Object> extractHeaders(ConsumerRecord<String, String> record) {
return Stream.of(record.headers().toArray()).distinct().collect(toMap(Header::key, header -> new String(header.value(), UTF_8)));
var result = new HashMap<String, Object>();
Stream<Header> distinctHeaders = Stream.of(record.headers().toArray()).distinct();
distinctHeaders.forEach(header -> {
String headerKey = header.key();
if (result.containsKey(headerKey)) {
Object headerValue = result.get(headerKey);
if (headerValue instanceof String) {
var headerValueAsList = new ArrayList<>();
headerValueAsList.add(headerValue);
result.put(headerKey, headerValueAsList);
}
((Collection<String>) result.get(headerKey)).add(new String(header.value(), UTF_8));
} else {
result.put(headerKey, new String(header.value(), UTF_8));
}
});
return result;
}

private ConcurrentMessageListenerContainer<String, String> createMessageListenerContainer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static com.chutneytesting.action.kafka.KafkaBasicConsumeAction.OUTPUT_PAYLOADS;
import static com.chutneytesting.action.spi.ActionExecutionResult.Status.Failure;
import static com.chutneytesting.action.spi.ActionExecutionResult.Status.Success;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.Collections.emptyMap;
import static java.util.Collections.shuffle;
Expand Down Expand Up @@ -85,7 +86,7 @@ public void before() {

@Test
void should_set_inputs_default_values() {
KafkaBasicConsumeAction defaultAction = new KafkaBasicConsumeAction(null, null, null, null, null, null, null, null, null, null, null,null);
KafkaBasicConsumeAction defaultAction = new KafkaBasicConsumeAction(null, null, null, null, null, null, null, null, null, null, null, null);
assertThat(defaultAction)
.hasFieldOrPropertyWithValue("topic", null)
.hasFieldOrPropertyWithValue("group", null)
Expand Down Expand Up @@ -124,7 +125,7 @@ void should_validate_all_mandatory_inputs() {
@Test
void should_validate_timeout_input() {
String badTimeout = "twenty seconds";
KafkaBasicConsumeAction defaultAction = new KafkaBasicConsumeAction(TARGET_STUB, "topic", "group", null, null, null, null, null, badTimeout, null,null, null);
KafkaBasicConsumeAction defaultAction = new KafkaBasicConsumeAction(TARGET_STUB, "topic", "group", null, null, null, null, null, badTimeout, null, null, null);

List<String> errors = defaultAction.validateInputs();

Expand All @@ -135,7 +136,7 @@ void should_validate_timeout_input() {
@Test
void should_validate_ackMode_input() {
String badTackMode = "UNKNOWN_ACKMODE";
KafkaBasicConsumeAction defaultAction = new KafkaBasicConsumeAction(TARGET_STUB, "topic", "group", null, null, null, null, null, null, badTackMode, null,null);
KafkaBasicConsumeAction defaultAction = new KafkaBasicConsumeAction(TARGET_STUB, "topic", "group", null, null, null, null, null, null, badTackMode, null, null);

List<String> errors = defaultAction.validateInputs();

Expand Down Expand Up @@ -167,7 +168,7 @@ void should_merge_kafka_consumer_target_properties_with_input_properties() {
propertyToOverride, "a property value"
);

KafkaBasicConsumeAction defaultAction = new KafkaBasicConsumeAction(target, null, null, properties, null, null, null, null, null, null, null,null);
KafkaBasicConsumeAction defaultAction = new KafkaBasicConsumeAction(target, null, null, properties, null, null, null, null, null, null, null, null);
assertThat(defaultAction)
.hasFieldOrPropertyWithValue("properties", expectedConfig)
;
Expand Down Expand Up @@ -198,6 +199,31 @@ public void should_consume_simple_text_message() {
assertThat(logger.errors).isEmpty();
}

@Test
public void should_consume_simple_text_message_with_multiple_values_for_the_same_header() {
// Given
ImmutableList<Header> headers = ImmutableList.of(
new RecordHeader("key-with-multiple-values", "value 1".getBytes()),
new RecordHeader("key-with-multiple-values", "value 2".getBytes())
);
Action sut = givenKafkaConsumeAction(null, TEXT_PLAIN_VALUE, null);
givenActionReceiveMessages(sut,
buildRecord(FIRST_OFFSET, "KEY", "test message", headers)
);

// When
ActionExecutionResult actionExecutionResult = sut.execute();

// Then
assertThat(actionExecutionResult.status).isEqualTo(Success);
var result_headers = (List<Map<String, Object>>) actionExecutionResult.outputs.get(OUTPUT_BODY_HEADERS_KEY);
assertThat(result_headers).hasSize(1);
assertThat(result_headers.get(0))
.containsExactly(
Map.entry("key-with-multiple-values", asList("value 1", "value 2"))
);
}

@Test
public void should_not_find_any_message() {
// Given
Expand Down Expand Up @@ -358,9 +384,11 @@ public void should_select_message_whose_headers_match_given_payload_jsonpath_sel
// Given
ImmutableList<Header> headers = ImmutableList.of(
new RecordHeader("X-Custom-HeaderKey", "X-Custom-HeaderValue".getBytes()),
new RecordHeader("header", "123".getBytes())
new RecordHeader("header", "123".getBytes()),
new RecordHeader("key-with-multiple-values", "value 1".getBytes()),
new RecordHeader("key-with-multiple-values", "value 2".getBytes())
);
Action action = givenKafkaConsumeAction(3, null, "$..[?($.header=='123')]", null, null);
Action action = givenKafkaConsumeAction(3, null, "$..[?($.header=='123' && $.key-with-multiple-values contains 'value 1')]", null, null);
String textMessageToSelect = "first text message";
String xmlMessageToSelect = "<root>first xml message</root>";
String jsonMessageToSelect = "first json message";
Expand Down Expand Up @@ -537,7 +565,7 @@ private KafkaBasicConsumeAction givenKafkaConsumeAction(String selector, String
}

private KafkaBasicConsumeAction givenKafkaConsumeAction(int expectedMessageNb, String selector, String headerSelector, String mimeType, String timeout) {
return new KafkaBasicConsumeAction(TARGET_STUB, TOPIC, GROUP, emptyMap(), expectedMessageNb, selector, headerSelector, mimeType, timeout, null,null, logger);
return new KafkaBasicConsumeAction(TARGET_STUB, TOPIC, GROUP, emptyMap(), expectedMessageNb, selector, headerSelector, mimeType, timeout, null, null, logger);
}

private void givenActionReceiveMessages(Action action, ConsumerRecord<String, String>... messages) {
Expand Down
Loading