All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fridujo.rabbitmq.mock.MockHeadersExchange Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy