com.github.fridujo.rabbitmq.mock.AmqArguments 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 static java.util.Collections.emptyMap;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
public class AmqArguments {
public static final String DEAD_LETTER_EXCHANGE_KEY = "x-dead-letter-exchange";
public static final String DEAD_LETTER_ROUTING_KEY_KEY = "x-dead-letter-routing-key";
public static final String MESSAGE_TTL_KEY = "x-message-ttl";
public static final String QUEUE_MAX_LENGTH_KEY = "x-max-length";
public static final String QUEUE_MAX_LENGTH_BYTES_KEY = "x-max-length-bytes";
public static final String OVERFLOW_KEY = "x-overflow";
public static final String MAX_PRIORITY_KEY = "x-max-priority";
private final String ALTERNATE_EXCHANGE_KEY = "alternate-exchange";
private final Map arguments;
public static AmqArguments empty() {
return new AmqArguments(emptyMap());
}
public AmqArguments(Map arguments) {
this.arguments = arguments;
}
public Optional getAlternateExchange() {
return string(ALTERNATE_EXCHANGE_KEY)
.map(aeName -> new ReceiverPointer(ReceiverPointer.Type.EXCHANGE, aeName));
}
public Optional getDeadLetterExchange() {
return string(DEAD_LETTER_EXCHANGE_KEY)
.map(aeName -> new ReceiverPointer(ReceiverPointer.Type.EXCHANGE, aeName));
}
public Optional getDeadLetterRoutingKey() {
return string(DEAD_LETTER_ROUTING_KEY_KEY);
}
public Optional queueLengthLimit() {
return positiveInteger(QUEUE_MAX_LENGTH_KEY);
}
public Optional queueLengthBytesLimit() {
return positiveInteger(QUEUE_MAX_LENGTH_BYTES_KEY);
}
public Overflow overflow() {
return string(OVERFLOW_KEY)
.flatMap(Overflow::parse)
.orElse(Overflow.DROP_HEAD);
}
public Optional getMessageTtlOfQueue() {
return Optional.ofNullable(arguments.get(MESSAGE_TTL_KEY))
.filter(aeObject -> aeObject instanceof Number)
.map(Number.class::cast)
.map(number -> number.longValue());
}
public Optional queueMaxPriority() {
return positiveInteger(MAX_PRIORITY_KEY)
.filter(i -> i < 256)
.map(Integer::shortValue);
}
private Optional positiveInteger(String key) {
return Optional.ofNullable(arguments.get(key))
.filter(aeObject -> aeObject instanceof Number)
.map(Number.class::cast)
.map(num -> num.intValue())
.filter(i -> i > 0);
}
private Optional string(String key) {
return Optional.ofNullable(arguments.get(key))
.filter(aeObject -> aeObject instanceof String)
.map(String.class::cast);
}
public enum Overflow {
DROP_HEAD("drop-head"), REJECT_PUBLISH("reject-publish");
private final String stringValue;
Overflow(String stringValue) {
this.stringValue = stringValue;
}
private static Optional parse(String value) {
return Arrays.stream(values()).filter(v -> value.equals(v.stringValue)).findFirst();
}
@Override
public String toString() {
return stringValue;
}
}
}