/**
 * 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.component.dataset;

import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

/**
 * @version 
 */
public class DataSetEndpointTest extends ContextTestSupport {

    @Override
    public boolean isUseRouteBuilder() {
        return false;
    }

    public void testDataSetEndpoint() throws Exception {
        final DataSetEndpoint endpoint = new DataSetEndpoint();
        endpoint.setCamelContext(context);
        endpoint.setEndpointUriIfNotSpecified("dataset://foo");
        endpoint.setDataSet(new SimpleDataSet(2));

        endpoint.reset();
        endpoint.setMinRate(1);
        assertEquals(1, endpoint.getMinRate());
        assertEquals(0, endpoint.getPreloadSize());
        assertEquals(0, endpoint.getConsumeDelay());
        assertEquals(3, endpoint.getProduceDelay());

        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from(endpoint).to("direct:foo");
                from("direct:foo").to(endpoint);
            }
        });
        context.start();
        endpoint.start();

        endpoint.assertIsSatisfied();
    }

    public void testDataSetEndpointCtr() throws Exception {
        final DataSetEndpoint endpoint = new DataSetEndpoint("dataset://foo", new SimpleDataSet(2));
        endpoint.setCamelContext(context);
        endpoint.setEndpointUriIfNotSpecified("dataset://foo");

        endpoint.setConsumeDelay(2);
        assertEquals(2, endpoint.getConsumeDelay());
        endpoint.setProduceDelay(5);
        assertEquals(5, endpoint.getProduceDelay());
        endpoint.setInitialDelay(50);
        assertEquals(50, endpoint.getInitialDelay());

        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from(endpoint).to("direct:foo");
                from("direct:foo").to(endpoint);
            }
        });
        context.start();
        endpoint.start();

        endpoint.assertIsSatisfied();
    }

    public void testDataSetReporter() throws Exception {
        final DataSetEndpoint endpoint = new DataSetEndpoint("dataset://foo", new SimpleDataSet(10));
        endpoint.setCamelContext(context);
        endpoint.setEndpointUriIfNotSpecified("dataset://foo");

        final AtomicBoolean reported = new AtomicBoolean(false);
        endpoint.setReporter(new Processor() {
            public void process(Exchange exchange) throws Exception {
                reported.set(true);
            }
        });

        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from(endpoint).to("direct:foo");
                from("direct:foo").to(endpoint);
            }
        });
        context.start();
        endpoint.start();

        endpoint.assertIsSatisfied();
        assertTrue(reported.get());
    }

    public void testSimpleDataSet() throws Exception {
        SimpleDataSet ds = new SimpleDataSet();
        ds.setSize(2);
        ds.setDefaultBody("Hi");
        assertEquals("Hi", ds.getDefaultBody());
    }

    public void testDataSetSupport() throws Exception {
        MyDataSet ds = new MyDataSet();
        ds.setSize(4);
        ds.setReportCount(0);
        ds.setOutputTransformer(new Processor() {
            public void process(Exchange exchange) throws Exception {
                String body = "Hi " + exchange.getIn().getBody(String.class);
                exchange.getIn().setBody(body);
            }
        });
        assertNotNull(ds.getOutputTransformer());

        final DataSetEndpoint endpoint = new DataSetEndpoint();
        endpoint.setCamelContext(context);
        endpoint.setEndpointUriIfNotSpecified("dataset://foo");
        endpoint.setDataSet(ds);
        // out transformer should have messaged with it
        endpoint.allMessages().body().startsWith("Hi ");

        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from(endpoint).to("direct:foo");
                from("direct:foo").to(endpoint);
            }
        });
        context.start();
        endpoint.start();

        endpoint.assertIsSatisfied();
    }

    private class MyDataSet extends DataSetSupport {

        @Override
        protected Object createMessageBody(long messageIndex) {
            return "Message " + messageIndex;
        }
    }

}
