com.github.fridujo.rabbitmq.mock.MockHeadersExchange Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rabbitmq-mock Show documentation
Show all versions of rabbitmq-mock Show documentation
Mock for RabbitMQ Java amqp-client
package com.github.fridujo.rabbitmq.mock;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class MockHeadersExchange extends BindableMockExchange {
private static final String MATCH_ALL = "all";
private static final Set X_MATCH_VALID_VALUES = new HashSet<>(Arrays.asList("any", MATCH_ALL));
public MockHeadersExchange(String exchangeName, Map arguments, ReceiverRegistry receiverRegistry) {
super(exchangeName, arguments, receiverRegistry);
}
@Override
protected boolean match(String bindingKey, Map bindArguments, String routingKey, Map headers) {
String xMatch = Optional.ofNullable(bindArguments.get(X_MATCH_KEY))
.filter(xMatchObject -> xMatchObject instanceof String)
.map(String.class::cast)
.filter(X_MATCH_VALID_VALUES::contains)
.orElse(MATCH_ALL);
Predicate> argumentPredicate = e -> Objects.equals(e.getValue(), headers.get(e.getKey()));
Stream> argumentsToMatch = bindArguments.entrySet().stream()
.filter(e -> !X_MATCH_KEY.equals(e.getKey()));
final boolean match;
if (MATCH_ALL.equals(xMatch)) {
match = argumentsToMatch.allMatch(argumentPredicate);
} else {
match = argumentsToMatch.anyMatch(argumentPredicate);
}
return match;
}
}