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

com.consol.citrus.message.selector.DelegatingMessageSelector Maven / Gradle / Ivy

There is a newer version: 3.4.1
Show newest version
/*
 * 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.ArrayList;
import java.util.List;
import java.util.Map;

import com.consol.citrus.context.TestContext;
import com.consol.citrus.message.Message;
import com.consol.citrus.message.MessageSelector;
import com.consol.citrus.message.MessageSelectorBuilder;
import org.springframework.util.Assert;

/**
 * Message selector delegates incoming messages to several other selector implementations
 * according to selector names.
 *
 * @author Christoph Deppisch
 * @since 3.0
 */
public class DelegatingMessageSelector implements MessageSelector {

    /** List of header elements to match */
    private final Map matchingHeaders;

    /** List of available message selector factories */
    private final List factories;

    /** Test context */
    private final TestContext context;

    /**
     * Default constructor using a selector string.
     */
    public DelegatingMessageSelector(String selector, TestContext context) {
        this.context = context;
        this.matchingHeaders = MessageSelectorBuilder.withString(selector).toKeyValueMap();

        Assert.isTrue(matchingHeaders.size() > 0, "Invalid empty message selector");

        factories = new ArrayList<>();

        if (context.getReferenceResolver() != null) {
            factories.addAll(context.getReferenceResolver().resolveAll(MessageSelectorFactory.class).values());
        }

        factories.addAll(MessageSelector.lookup().values());
    }

    @Override
    public boolean accept(Message message) {
        return matchingHeaders.entrySet()
                              .stream()
                              .allMatch(entry -> factories.stream()
                                                     .filter(factory -> factory.supports(entry.getKey()))
                                                     .findAny()
                                                     .orElse(new HeaderMatchingMessageSelector.Factory())
                                                     .create(entry.getKey(), entry.getValue(), context)
                                                     .accept(message));
    }

    /**
     * Add message selector factory to list of delegates.
     * @param factory
     */
    public void addMessageSelectorFactory(MessageSelectorFactory factory) {
        this.factories.add(factory);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy