/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.camel.processor.enricher;

import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;

public class PollEnricherTest extends ContextTestSupport {

    private static SampleAggregator aggregationStrategy = new SampleAggregator();

    protected MockEndpoint mock;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mock = getMockEndpoint("mock:mock");
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    // -------------------------------------------------------------
    //  InOnly routes
    // -------------------------------------------------------------

    public void testPollEnrichInOnly() throws InterruptedException {
        template.sendBody("seda:foo1", "blah");

        mock.expectedBodiesReceived("test:blah");
        template.sendBody("direct:enricher-test-1", "test");
        mock.assertIsSatisfied();
    }

    public void testPollEnrichInOnlyWaitWithTimeout() throws InterruptedException {
        // this first try there is no data so we timeout
        mock.expectedBodiesReceived("test:blah");
        template.sendBody("direct:enricher-test-2", "test");
        // not expected data so we are not happy
        mock.assertIsNotSatisfied();

        // now send it and try again
        mock.reset();
        template.sendBody("seda:foo2", "blah");
        template.sendBody("direct:enricher-test-2", "test");
        mock.assertIsSatisfied();
    }

    public void testPollEnrichInOnlyWaitNoTimeout() throws InterruptedException {
        // use another thread to send it after 2 seconds
        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // ignore
                }
                template.sendBody("seda:foo3", "blah");
            }
        });

        long start = System.currentTimeMillis();
        mock.expectedBodiesReceived("test:blah");
        t.start();
        template.sendBody("direct:enricher-test-3", "test");
        // should take approx 1 sec to complete as the other thread is sending a bit later and we wait
        mock.assertIsSatisfied();
        long delta = System.currentTimeMillis() - start;
        assertTrue("Should take approx 1 sec: was " + delta, delta > 900);
    }

    // -------------------------------------------------------------
    //  InOut routes
    // -------------------------------------------------------------

    public void testPollEnrichInOut() throws InterruptedException {
        template.sendBody("seda:foo4", "blah");

        String result = (String) template.sendBody("direct:enricher-test-4", ExchangePattern.InOut, "test");
        assertEquals("test:blah", result);
    }

    public void testPollEnrichInOutPlusHeader() throws InterruptedException {
        template.sendBody("seda:foo4", "blah");

        Exchange exchange = template.request("direct:enricher-test-4", new Processor() {
            public void process(Exchange exchange) {
                exchange.getIn().setHeader("foo", "bar");
                exchange.getIn().setBody("test");
            }
        });
        assertEquals("bar", exchange.getIn().getHeader("foo"));
        assertEquals("test:blah", exchange.getIn().getBody());
        assertFalse(exchange.hasFault());
        assertNull(exchange.getException());
    }

    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                // -------------------------------------------------------------
                //  InOnly routes
                // -------------------------------------------------------------

                from("direct:enricher-test-1")
                    .pollEnrich("seda:foo1", aggregationStrategy)
                    .to("mock:mock");

                from("direct:enricher-test-2")
                    .pollEnrich("seda:foo2", 1000, aggregationStrategy)
                    .to("mock:mock");

                from("direct:enricher-test-3")
                    .pollEnrich("seda:foo3", -1, aggregationStrategy)
                    .to("mock:mock");

                // -------------------------------------------------------------
                //  InOut routes
                // -------------------------------------------------------------

                from("direct:enricher-test-4")
                    .pollEnrich("seda:foo4", aggregationStrategy);
            }
        };
    }

}
@@ -1758,28 +1758,30 @@
 e.sendBody(%22
-seda
+direct
 :foo1%22, %22bla
@@ -2359,28 +2359,30 @@
 e.sendBody(%22
-seda
+direct
 :foo2%22, %22bla
@@ -2898,28 +2898,30 @@
 e.sendBody(%22
-seda
+direct
 :foo3%22, %22bla
@@ -3654,36 +3654,38 @@
 plate.sendBody(%22
-seda
+direct
 :foo4%22, %22blah%22);
@@ -3950,20 +3950,22 @@
 ndBody(%22
-seda
+direct
 :foo4%22, 
@@ -4868,28 +4868,30 @@
 pollEnrich(%22
-seda
+direct
 :foo1%22, aggr
@@ -5022,28 +5022,30 @@
 pollEnrich(%22
-seda
+direct
 :foo2%22, 1000
@@ -5182,28 +5182,30 @@
 pollEnrich(%22
-seda
+direct
 :foo3%22, -1, 
@@ -5544,12 +5544,14 @@
 ch(%22
-seda
+direct
 :foo

Use direct endpoints to avoid a race condition that fails these tests sometimes