com.consol.citrus.message.selector.HeaderMatchingMessageSelector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of citrus-base Show documentation
Show all versions of citrus-base Show documentation
Citrus base and default implementation
/*
* Copyright 2006-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.consol.citrus.message.selector;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.message.Message;
/**
* Message selector matches one or more header elements with the message header. Only in case all
* matching header elements are present in message header and its value matches the expected value
* the message is accepted.
*
* @author Christoph Deppisch
*/
public class HeaderMatchingMessageSelector extends AbstractMessageSelector {
/** Special selector key prefix identifying this message selector implementation */
public static final String SELECTOR_PREFIX = "header:";
/**
* Default constructor using fields.
*/
public HeaderMatchingMessageSelector(String selectKey, String matchingValue, TestContext context) {
super(selectKey, matchingValue, context);
}
@Override
public boolean accept(Message message) {
Map messageHeaders = message.getHeaders();
Map nestedMessageHeaders = new HashMap<>();
if (message.getPayload() instanceof Message) {
nestedMessageHeaders = ((Message) message.getPayload()).getHeaders();
}
if (nestedMessageHeaders.containsKey(selectKey)) {
return matchHeader(nestedMessageHeaders);
} else if (messageHeaders.containsKey(selectKey)) {
return matchHeader(messageHeaders);
} else {
return false;
}
}
private boolean matchHeader(Map messageHeaders) {
return Optional.ofNullable(messageHeaders.get(selectKey))
.map(Object::toString)
.map(this::evaluate)
.orElse(false);
}
/**
* Message selector factory for this implementation.
*/
public static class Factory implements MessageSelectorFactory {
@Override
public boolean supports(String key) {
return key.startsWith(SELECTOR_PREFIX);
}
@Override
public HeaderMatchingMessageSelector create(String key, String value, TestContext context) {
if (key.startsWith(SELECTOR_PREFIX)) {
return new HeaderMatchingMessageSelector(key.substring(SELECTOR_PREFIX.length()), value, context);
} else {
return new HeaderMatchingMessageSelector(key, value, context);
}
}
}
}